3 * Copyright (C) 2009-2022 SonarSource SA
4 * mailto:info AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.qualityprofile;
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.sonar.api.impl.utils.AlwaysIncreasingSystem2;
25 import org.sonar.api.utils.System2;
26 import org.sonar.db.DbTester;
27 import org.sonar.db.qualityprofile.ActiveRuleKey;
28 import org.sonar.db.qualityprofile.OrgActiveRuleDto;
29 import org.sonar.db.qualityprofile.QProfileDto;
30 import org.sonar.db.qualityprofile.QualityProfileTesting;
31 import org.sonar.db.rule.RuleDefinitionDto;
32 import org.sonar.server.pushapi.qualityprofile.QualityProfileChangeEventService;
33 import org.sonar.server.qualityprofile.builtin.RuleActivator;
34 import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
35 import org.sonar.server.tester.UserSessionRule;
36 import org.sonar.server.util.IntegerTypeValidation;
37 import org.sonar.server.util.StringTypeValidation;
38 import org.sonar.server.util.TypeValidations;
40 import static java.util.Arrays.asList;
41 import static java.util.Collections.singleton;
42 import static java.util.Collections.singletonList;
43 import static org.assertj.core.api.Assertions.assertThat;
44 import static org.assertj.core.api.Assertions.assertThatThrownBy;
45 import static org.assertj.core.api.Assertions.tuple;
46 import static org.mockito.ArgumentMatchers.any;
47 import static org.mockito.ArgumentMatchers.eq;
48 import static org.mockito.Mockito.mock;
49 import static org.mockito.Mockito.verify;
50 import static org.mockito.Mockito.verifyNoInteractions;
51 import static org.mockito.internal.verification.VerificationModeFactory.times;
52 import static org.sonar.server.qualityprofile.ActiveRuleChange.Type.ACTIVATED;
54 public class QProfileResetImplTest {
56 private static final String LANGUAGE = "xoo";
59 public DbTester db = DbTester.create();
61 public UserSessionRule userSession = UserSessionRule.standalone();
63 private System2 system2 = new AlwaysIncreasingSystem2();
64 private ActiveRuleIndexer activeRuleIndexer = mock(ActiveRuleIndexer.class);
65 private QualityProfileChangeEventService qualityProfileChangeEventService = mock(QualityProfileChangeEventService.class);
66 private TypeValidations typeValidations = new TypeValidations(asList(new StringTypeValidation(), new IntegerTypeValidation()));
67 private RuleActivator ruleActivator = new RuleActivator(system2, db.getDbClient(), typeValidations, userSession);
68 private QProfileTree qProfileTree = new QProfileTreeImpl(db.getDbClient(), ruleActivator, system2, activeRuleIndexer, mock(QualityProfileChangeEventService.class));
69 private QProfileRules qProfileRules = new QProfileRulesImpl(db.getDbClient(), ruleActivator, null, activeRuleIndexer, qualityProfileChangeEventService);
70 private QProfileResetImpl underTest = new QProfileResetImpl(db.getDbClient(), ruleActivator, activeRuleIndexer, mock(QualityProfileChangeEventService.class));
74 QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(LANGUAGE));
75 RuleDefinitionDto existingRule = db.rules().insert(r -> r.setLanguage(LANGUAGE));
76 qProfileRules.activateAndCommit(db.getSession(), profile, singleton(RuleActivation.create(existingRule.getUuid())));
77 RuleDefinitionDto newRule = db.rules().insert(r -> r.setLanguage(LANGUAGE));
79 BulkChangeResult result = underTest.reset(db.getSession(), profile, singletonList(RuleActivation.create(newRule.getUuid())));
81 assertThat(db.getDbClient().activeRuleDao().selectByProfile(db.getSession(), profile))
82 .extracting(OrgActiveRuleDto::getRuleKey)
83 .containsExactlyInAnyOrder(newRule.getKey());
84 // Only activated rules are returned in the result
85 assertThat(result.getChanges())
86 .extracting(ActiveRuleChange::getKey, ActiveRuleChange::getType)
87 .containsExactlyInAnyOrder(tuple(ActiveRuleKey.of(profile, newRule.getKey()), ACTIVATED));
88 verify(qualityProfileChangeEventService).distributeRuleChangeEvent(any(), any(), eq(profile.getLanguage()));
92 public void inherited_rules_are_not_disabled() {
93 QProfileDto parentProfile = db.qualityProfiles().insert(p -> p.setLanguage(LANGUAGE));
94 QProfileDto childProfile = db.qualityProfiles().insert(p -> p.setLanguage(LANGUAGE));
95 qProfileTree.setParentAndCommit(db.getSession(), childProfile, parentProfile);
96 RuleDefinitionDto existingRule = db.rules().insert(r -> r.setLanguage(LANGUAGE));
97 qProfileRules.activateAndCommit(db.getSession(), parentProfile, singleton(RuleActivation.create(existingRule.getUuid())));
98 qProfileRules.activateAndCommit(db.getSession(), childProfile, singleton(RuleActivation.create(existingRule.getUuid())));
99 RuleDefinitionDto newRule = db.rules().insert(r -> r.setLanguage(LANGUAGE));
101 underTest.reset(db.getSession(), childProfile, singletonList(RuleActivation.create(newRule.getUuid())));
103 assertThat(db.getDbClient().activeRuleDao().selectByProfile(db.getSession(), childProfile))
104 .extracting(OrgActiveRuleDto::getRuleKey)
105 .containsExactlyInAnyOrder(newRule.getKey(), existingRule.getKey());
106 verify(qualityProfileChangeEventService, times(2)).distributeRuleChangeEvent(any(), any(), eq(childProfile.getLanguage()));
110 public void fail_when_profile_is_built_in() {
111 QProfileDto profile = db.qualityProfiles().insert(p -> p.setLanguage(LANGUAGE).setIsBuiltIn(true));
112 RuleDefinitionDto defaultRule = db.rules().insert(r -> r.setLanguage(LANGUAGE));
114 assertThatThrownBy(() -> {
115 underTest.reset(db.getSession(), profile, singletonList(RuleActivation.create(defaultRule.getUuid())));
117 .isInstanceOf(IllegalArgumentException.class)
118 .hasMessage(String.format("Operation forbidden for built-in Quality Profile '%s'", profile.getKee()));
119 verifyNoInteractions(qualityProfileChangeEventService);
123 public void fail_when_profile_is_not_persisted() {
124 QProfileDto profile = QualityProfileTesting.newQualityProfileDto().setRulesProfileUuid(null).setLanguage(LANGUAGE);
125 RuleDefinitionDto defaultRule = db.rules().insert(r -> r.setLanguage(LANGUAGE));
127 assertThatThrownBy(() -> {
128 underTest.reset(db.getSession(), profile, singletonList(RuleActivation.create(defaultRule.getUuid())));
130 .isInstanceOf(NullPointerException.class)
131 .hasMessage("Quality profile must be persisted");
133 verifyNoInteractions(qualityProfileChangeEventService);