diff options
author | Julien Lancelot <julien.lancelot@sonarsource.com> | 2017-09-05 13:56:34 +0200 |
---|---|---|
committer | Julien Lancelot <julien.lancelot@sonarsource.com> | 2017-09-05 14:24:05 +0200 |
commit | f513a71898b0ec20696140763ecffb37246212c0 (patch) | |
tree | d17861fd079b7a9ddce425891034edf43a286fdc | |
parent | 4c2a52d4b3b79c32753ab18f98e84d1a4f2037ba (diff) | |
download | sonarqube-f513a71898b0ec20696140763ecffb37246212c0.tar.gz sonarqube-f513a71898b0ec20696140763ecffb37246212c0.zip |
Improve coverage of QProfileResetImpl
-rw-r--r-- | server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileResetImplTest.java | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileResetImplTest.java b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileResetImplTest.java new file mode 100644 index 00000000000..fc27eb81be0 --- /dev/null +++ b/server/sonar-server/src/test/java/org/sonar/server/qualityprofile/QProfileResetImplTest.java @@ -0,0 +1,120 @@ +/* + * SonarQube + * Copyright (C) 2009-2017 SonarSource SA + * mailto:info AT sonarsource DOT com + * + * This program is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 3 of the License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public License + * along with this program; if not, write to the Free Software Foundation, + * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + */ +package org.sonar.server.qualityprofile; + +import org.junit.Rule; +import org.junit.Test; +import org.junit.rules.ExpectedException; +import org.sonar.api.utils.System2; +import org.sonar.api.utils.internal.AlwaysIncreasingSystem2; +import org.sonar.db.DbTester; +import org.sonar.db.qualityprofile.ActiveRuleKey; +import org.sonar.db.qualityprofile.OrgActiveRuleDto; +import org.sonar.db.qualityprofile.QProfileDto; +import org.sonar.db.qualityprofile.QualityProfileTesting; +import org.sonar.db.rule.RuleDefinitionDto; +import org.sonar.server.qualityprofile.index.ActiveRuleIndexer; +import org.sonar.server.tester.UserSessionRule; +import org.sonar.server.util.IntegerTypeValidation; +import org.sonar.server.util.StringTypeValidation; +import org.sonar.server.util.TypeValidations; + +import static java.util.Arrays.asList; +import static java.util.Collections.singletonList; +import static org.assertj.core.api.Java6Assertions.assertThat; +import static org.assertj.core.api.Java6Assertions.tuple; +import static org.mockito.Mockito.mock; +import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.ACTIVATED; + +public class QProfileResetImplTest { + + private static final String LANGUAGE = "xoo"; + + @Rule + public ExpectedException expectedException = ExpectedException.none(); + @Rule + public DbTester db = DbTester.create(); + @Rule + public UserSessionRule userSession = UserSessionRule.standalone(); + + private System2 system2 = new AlwaysIncreasingSystem2(); + private ActiveRuleIndexer activeRuleIndexer = mock(ActiveRuleIndexer.class); + private RuleActivatorContextFactory contextFactory = new RuleActivatorContextFactory(db.getDbClient()); + private TypeValidations typeValidations = new TypeValidations(asList(new StringTypeValidation(), new IntegerTypeValidation())); + private RuleActivator ruleActivator = new RuleActivator(system2, db.getDbClient(), null, contextFactory, typeValidations, activeRuleIndexer, userSession); + private QProfileResetImpl underTest = new QProfileResetImpl(db.getDbClient(), ruleActivator, activeRuleIndexer); + + @Test + public void reset() { + QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization(), p -> p.setLanguage(LANGUAGE)); + RuleDefinitionDto existingRule = db.rules().insert(r -> r.setLanguage(LANGUAGE)); + ruleActivator.activate(db.getSession(), RuleActivation.create(existingRule.getKey()), profile); + RuleDefinitionDto newRule = db.rules().insert(r -> r.setLanguage(LANGUAGE)); + + BulkChangeResult result = underTest.reset(db.getSession(), profile, singletonList(RuleActivation.create(newRule.getKey()))); + + assertThat(db.getDbClient().activeRuleDao().selectByProfile(db.getSession(), profile)) + .extracting(OrgActiveRuleDto::getRuleKey) + .containsExactlyInAnyOrder(newRule.getKey()); + // Only activated rules are returned in the result + assertThat(result.getChanges()) + .extracting(ActiveRuleChange::getKey, ActiveRuleChange::getType) + .containsExactlyInAnyOrder(tuple(ActiveRuleKey.of(profile, newRule.getKey()), ACTIVATED)); + } + + @Test + public void inherited_rules_are_not_disabled() { + QProfileDto parentProfile = db.qualityProfiles().insert(db.getDefaultOrganization(), p -> p.setLanguage(LANGUAGE)); + QProfileDto childProfile = db.qualityProfiles().insert(db.getDefaultOrganization(), p -> p.setLanguage(LANGUAGE)); + ruleActivator.setParentAndCommit(db.getSession(), childProfile, parentProfile); + RuleDefinitionDto existingRule = db.rules().insert(r -> r.setLanguage(LANGUAGE)); + ruleActivator.activate(db.getSession(), RuleActivation.create(existingRule.getKey()), parentProfile); + ruleActivator.activate(db.getSession(), RuleActivation.create(existingRule.getKey()), childProfile); + RuleDefinitionDto newRule = db.rules().insert(r -> r.setLanguage(LANGUAGE)); + + underTest.reset(db.getSession(), childProfile, singletonList(RuleActivation.create(newRule.getKey()))); + + assertThat(db.getDbClient().activeRuleDao().selectByProfile(db.getSession(), childProfile)) + .extracting(OrgActiveRuleDto::getRuleKey) + .containsExactlyInAnyOrder(newRule.getKey(), existingRule.getKey()); + } + + @Test + public void fail_when_profile_is_built_in() { + QProfileDto profile = db.qualityProfiles().insert(db.getDefaultOrganization(), p -> p.setLanguage(LANGUAGE).setIsBuiltIn(true)); + RuleDefinitionDto defaultRule = db.rules().insert(r -> r.setLanguage(LANGUAGE)); + + expectedException.expect(IllegalArgumentException.class); + expectedException.expectMessage(String.format("Operation forbidden for built-in Quality Profile '%s'", profile.getKee())); + + underTest.reset(db.getSession(), profile, singletonList(RuleActivation.create(defaultRule.getKey()))); + } + + @Test + public void fail_when_profile_is_not_persisted() { + QProfileDto profile = QualityProfileTesting.newQualityProfileDto().setLanguage(LANGUAGE); + RuleDefinitionDto defaultRule = db.rules().insert(r -> r.setLanguage(LANGUAGE)); + + expectedException.expect(NullPointerException.class); + expectedException.expectMessage(String.format("Quality profile must be persisted")); + + underTest.reset(db.getSession(), profile, singletonList(RuleActivation.create(defaultRule.getKey()))); + } +} |