]> source.dussan.org Git - sonarqube.git/blob
031316d8b07f7557a8c8ad5683a5ee979f4d73f9
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2022 SonarSource SA
4  * mailto:info AT sonarsource DOT com
5  *
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.
10  *
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.
15  *
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.
19  */
20 package org.sonar.server.qualityprofile;
21
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;
39
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;
53
54 public class QProfileResetImplTest {
55
56   private static final String LANGUAGE = "xoo";
57
58   @Rule
59   public DbTester db = DbTester.create();
60   @Rule
61   public UserSessionRule userSession = UserSessionRule.standalone();
62
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));
71
72   @Test
73   public void reset() {
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));
78
79     BulkChangeResult result = underTest.reset(db.getSession(), profile, singletonList(RuleActivation.create(newRule.getUuid())));
80
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()));
89   }
90
91   @Test
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));
100
101     underTest.reset(db.getSession(), childProfile, singletonList(RuleActivation.create(newRule.getUuid())));
102
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()));
107   }
108
109   @Test
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));
113
114     assertThatThrownBy(() -> {
115       underTest.reset(db.getSession(), profile, singletonList(RuleActivation.create(defaultRule.getUuid())));
116     })
117       .isInstanceOf(IllegalArgumentException.class)
118       .hasMessage(String.format("Operation forbidden for built-in Quality Profile '%s'", profile.getKee()));
119     verifyNoInteractions(qualityProfileChangeEventService);
120   }
121
122   @Test
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));
126
127     assertThatThrownBy(() -> {
128       underTest.reset(db.getSession(), profile, singletonList(RuleActivation.create(defaultRule.getUuid())));
129     })
130       .isInstanceOf(NullPointerException.class)
131       .hasMessage("Quality profile must be persisted");
132
133     verifyNoInteractions(qualityProfileChangeEventService);
134   }
135 }