]> source.dussan.org Git - sonarqube.git/blob
22be30aa93e02c5da8a3ebbb16b85bf1075542e2
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2020 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 com.google.common.collect.ImmutableMap;
23 import com.google.common.collect.MapDifference.ValueDifference;
24 import org.assertj.core.data.MapEntry;
25 import org.junit.After;
26 import org.junit.Before;
27 import org.junit.Rule;
28 import org.junit.Test;
29 import org.sonar.api.rule.Severity;
30 import org.sonar.api.server.rule.RuleParamType;
31 import org.sonar.api.utils.System2;
32 import org.sonar.db.DbClient;
33 import org.sonar.db.DbSession;
34 import org.sonar.db.DbTester;
35 import org.sonar.db.qualityprofile.QProfileDto;
36 import org.sonar.db.rule.RuleDefinitionDto;
37 import org.sonar.db.rule.RuleParamDto;
38 import org.sonar.db.rule.RuleTesting;
39 import org.sonar.server.es.EsTester;
40 import org.sonar.server.qualityprofile.QProfileComparison.ActiveRuleDiff;
41 import org.sonar.server.qualityprofile.QProfileComparison.QProfileComparisonResult;
42 import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
43 import org.sonar.server.rule.index.RuleIndex;
44 import org.sonar.server.tester.UserSessionRule;
45 import org.sonar.server.util.IntegerTypeValidation;
46 import org.sonar.server.util.TypeValidations;
47
48 import static java.util.Collections.singleton;
49 import static java.util.Collections.singletonList;
50 import static org.assertj.core.api.Assertions.assertThat;
51
52 public class QProfileComparisonTest {
53
54   @Rule
55   public UserSessionRule userSession = UserSessionRule.standalone().anonymous();
56   @Rule
57   public DbTester dbTester = DbTester.create();
58   @Rule
59   public EsTester es = EsTester.create();
60
61   private DbSession dbSession;
62   private QProfileRules qProfileRules;
63   private QProfileComparison comparison;
64
65   private RuleDefinitionDto xooRule1;
66   private RuleDefinitionDto xooRule2;
67   private QProfileDto left;
68   private QProfileDto right;
69
70   @Before
71   public void before() {
72     DbClient db = dbTester.getDbClient();
73     dbSession = db.openSession(false);
74     RuleIndex ruleIndex = new RuleIndex(es.client(), System2.INSTANCE);
75     ActiveRuleIndexer activeRuleIndexer = new ActiveRuleIndexer(db, es.client());
76     RuleActivator ruleActivator = new RuleActivator(System2.INSTANCE, db, new TypeValidations(singletonList(new IntegerTypeValidation())), userSession);
77     qProfileRules = new QProfileRulesImpl(db, ruleActivator, ruleIndex, activeRuleIndexer);
78     comparison = new QProfileComparison(db);
79
80     xooRule1 = RuleTesting.newXooX1().setSeverity("MINOR").getDefinition();
81     xooRule2 = RuleTesting.newXooX2().setSeverity("MAJOR").getDefinition();
82     db.ruleDao().insert(dbSession, xooRule1);
83     db.ruleDao().insert(dbSession, xooRule2);
84     db.ruleDao().insertRuleParam(dbSession, xooRule1, RuleParamDto.createFor(xooRule1)
85       .setName("max").setType(RuleParamType.INTEGER.type()));
86     db.ruleDao().insertRuleParam(dbSession, xooRule1, RuleParamDto.createFor(xooRule1)
87       .setName("min").setType(RuleParamType.INTEGER.type()));
88
89     left = QProfileTesting.newXooP1();
90     right = QProfileTesting.newXooP2();
91     db.qualityProfileDao().insert(dbSession, left, right);
92
93     dbSession.commit();
94   }
95
96   @After
97   public void after() {
98     dbSession.close();
99   }
100
101   @Test
102   public void compare_empty_profiles() {
103     QProfileComparisonResult result = comparison.compare(dbSession, left, right);
104     assertThat(result.left().getKee()).isEqualTo(left.getKee());
105     assertThat(result.right().getKee()).isEqualTo(right.getKee());
106     assertThat(result.same()).isEmpty();
107     assertThat(result.inLeft()).isEmpty();
108     assertThat(result.inRight()).isEmpty();
109     assertThat(result.modified()).isEmpty();
110     assertThat(result.collectRuleKeys()).isEmpty();
111   }
112
113   @Test
114   public void compare_same() {
115     RuleActivation commonActivation = RuleActivation.create(xooRule1.getUuid(), Severity.CRITICAL,
116       ImmutableMap.of("min", "7", "max", "42"));
117     qProfileRules.activateAndCommit(dbSession, left, singleton(commonActivation));
118     qProfileRules.activateAndCommit(dbSession, right, singleton(commonActivation));
119
120     QProfileComparisonResult result = comparison.compare(dbSession, left, right);
121     assertThat(result.left().getKee()).isEqualTo(left.getKee());
122     assertThat(result.right().getKee()).isEqualTo(right.getKee());
123     assertThat(result.same()).isNotEmpty().containsOnlyKeys(xooRule1.getKey());
124     assertThat(result.inLeft()).isEmpty();
125     assertThat(result.inRight()).isEmpty();
126     assertThat(result.modified()).isEmpty();
127     assertThat(result.collectRuleKeys()).containsOnly(xooRule1.getKey());
128   }
129
130   @Test
131   public void compare_only_left() {
132     RuleActivation activation = RuleActivation.create(xooRule1.getUuid());
133     qProfileRules.activateAndCommit(dbSession, left, singleton(activation));
134
135     QProfileComparisonResult result = comparison.compare(dbSession, left, right);
136     assertThat(result.left().getKee()).isEqualTo(left.getKee());
137     assertThat(result.right().getKee()).isEqualTo(right.getKee());
138     assertThat(result.same()).isEmpty();
139     assertThat(result.inLeft()).isNotEmpty().containsOnlyKeys(xooRule1.getKey());
140     assertThat(result.inRight()).isEmpty();
141     assertThat(result.modified()).isEmpty();
142     assertThat(result.collectRuleKeys()).containsOnly(xooRule1.getKey());
143   }
144
145   @Test
146   public void compare_only_right() {
147     qProfileRules.activateAndCommit(dbSession, right, singleton(RuleActivation.create(xooRule1.getUuid())));
148
149     QProfileComparisonResult result = comparison.compare(dbSession, left, right);
150     assertThat(result.left().getKee()).isEqualTo(left.getKee());
151     assertThat(result.right().getKee()).isEqualTo(right.getKee());
152     assertThat(result.same()).isEmpty();
153     assertThat(result.inLeft()).isEmpty();
154     assertThat(result.inRight()).isNotEmpty().containsOnlyKeys(xooRule1.getKey());
155     assertThat(result.modified()).isEmpty();
156     assertThat(result.collectRuleKeys()).containsOnly(xooRule1.getKey());
157   }
158
159   @Test
160   public void compare_disjoint() {
161     qProfileRules.activateAndCommit(dbSession, left, singleton(RuleActivation.create(xooRule1.getUuid())));
162     qProfileRules.activateAndCommit(dbSession, right, singleton(RuleActivation.create(xooRule2.getUuid())));
163
164     QProfileComparisonResult result = comparison.compare(dbSession, left, right);
165     assertThat(result.left().getKee()).isEqualTo(left.getKee());
166     assertThat(result.right().getKee()).isEqualTo(right.getKee());
167     assertThat(result.same()).isEmpty();
168     assertThat(result.inLeft()).isNotEmpty().containsOnlyKeys(xooRule1.getKey());
169     assertThat(result.inRight()).isNotEmpty().containsOnlyKeys(xooRule2.getKey());
170     assertThat(result.modified()).isEmpty();
171     assertThat(result.collectRuleKeys()).containsOnly(xooRule1.getKey(), xooRule2.getKey());
172   }
173
174   @Test
175   public void compare_modified_severity() {
176     qProfileRules.activateAndCommit(dbSession, left, singleton(RuleActivation.create(xooRule1.getUuid(), Severity.CRITICAL, null)));
177     qProfileRules.activateAndCommit(dbSession, right, singleton(RuleActivation.create(xooRule1.getUuid(), Severity.BLOCKER, null)));
178
179     QProfileComparisonResult result = comparison.compare(dbSession, left, right);
180     assertThat(result.left().getKee()).isEqualTo(left.getKee());
181     assertThat(result.right().getKee()).isEqualTo(right.getKee());
182     assertThat(result.same()).isEmpty();
183     assertThat(result.inLeft()).isEmpty();
184     assertThat(result.inRight()).isEmpty();
185     assertThat(result.modified()).isNotEmpty().containsOnlyKeys(xooRule1.getKey());
186     assertThat(result.collectRuleKeys()).containsOnly(xooRule1.getKey());
187
188     ActiveRuleDiff activeRuleDiff = result.modified().get(xooRule1.getKey());
189     assertThat(activeRuleDiff.leftSeverity()).isEqualTo(Severity.CRITICAL);
190     assertThat(activeRuleDiff.rightSeverity()).isEqualTo(Severity.BLOCKER);
191     assertThat(activeRuleDiff.paramDifference().areEqual()).isTrue();
192   }
193
194   @Test
195   public void compare_modified_param() {
196     qProfileRules.activateAndCommit(dbSession, left, singleton(RuleActivation.create(xooRule1.getUuid(), null, ImmutableMap.of("max", "20"))));
197     qProfileRules.activateAndCommit(dbSession, right, singleton(RuleActivation.create(xooRule1.getUuid(), null, ImmutableMap.of("max", "30"))));
198
199     QProfileComparisonResult result = comparison.compare(dbSession, left, right);
200     assertThat(result.left().getKee()).isEqualTo(left.getKee());
201     assertThat(result.right().getKee()).isEqualTo(right.getKee());
202     assertThat(result.same()).isEmpty();
203     assertThat(result.inLeft()).isEmpty();
204     assertThat(result.inRight()).isEmpty();
205     assertThat(result.modified()).isNotEmpty().containsOnlyKeys(xooRule1.getKey());
206     assertThat(result.collectRuleKeys()).containsOnly(xooRule1.getKey());
207
208     ActiveRuleDiff activeRuleDiff = result.modified().get(xooRule1.getKey());
209     assertThat(activeRuleDiff.leftSeverity()).isEqualTo(activeRuleDiff.rightSeverity()).isEqualTo(xooRule1.getSeverityString());
210     assertThat(activeRuleDiff.paramDifference().areEqual()).isFalse();
211     assertThat(activeRuleDiff.paramDifference().entriesDiffering()).isNotEmpty();
212     ValueDifference<String> paramDiff = activeRuleDiff.paramDifference().entriesDiffering().get("max");
213     assertThat(paramDiff.leftValue()).isEqualTo("20");
214     assertThat(paramDiff.rightValue()).isEqualTo("30");
215   }
216
217   @Test
218   public void compare_different_params() {
219     qProfileRules.activateAndCommit(dbSession, left, singleton(RuleActivation.create(xooRule1.getUuid(), null, ImmutableMap.of("max", "20"))));
220     qProfileRules.activateAndCommit(dbSession, right, singleton(RuleActivation.create(xooRule1.getUuid(), null, ImmutableMap.of("min", "5"))));
221
222     QProfileComparisonResult result = comparison.compare(dbSession, left, right);
223     assertThat(result.left().getKee()).isEqualTo(left.getKee());
224     assertThat(result.right().getKee()).isEqualTo(right.getKee());
225     assertThat(result.same()).isEmpty();
226     assertThat(result.inLeft()).isEmpty();
227     assertThat(result.inRight()).isEmpty();
228     assertThat(result.modified()).isNotEmpty().containsOnlyKeys(xooRule1.getKey());
229     assertThat(result.collectRuleKeys()).containsOnly(xooRule1.getKey());
230
231     ActiveRuleDiff activeRuleDiff = result.modified().get(xooRule1.getKey());
232     assertThat(activeRuleDiff.leftSeverity()).isEqualTo(activeRuleDiff.rightSeverity()).isEqualTo(xooRule1.getSeverityString());
233     assertThat(activeRuleDiff.paramDifference().areEqual()).isFalse();
234     assertThat(activeRuleDiff.paramDifference().entriesDiffering()).isEmpty();
235     assertThat(activeRuleDiff.paramDifference().entriesOnlyOnLeft()).containsExactly(MapEntry.entry("max", "20"));
236     assertThat(activeRuleDiff.paramDifference().entriesOnlyOnRight()).containsExactly(MapEntry.entry("min", "5"));
237   }
238 }