]> source.dussan.org Git - sonarqube.git/blob
e5aefdcd36ab05e3df9e1212a8e8d28f2eccd6e2
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2024 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.telemetry;
21
22 import javax.annotation.Nullable;
23 import org.assertj.core.api.Assertions;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.sonar.api.utils.System2;
27 import org.sonar.db.DbClient;
28 import org.sonar.db.DbTester;
29 import org.sonar.db.component.ProjectData;
30 import org.sonar.db.qualityprofile.ActiveRuleDto;
31 import org.sonar.db.qualityprofile.ActiveRuleParamDto;
32 import org.sonar.db.qualityprofile.QProfileDto;
33 import org.sonar.db.rule.RuleDto;
34 import org.sonar.db.rule.RuleParamDto;
35 import org.sonar.server.qualityprofile.QProfileComparison;
36
37 import static org.assertj.core.groups.Tuple.tuple;
38 import static org.sonar.db.qualityprofile.ActiveRuleDto.OVERRIDES;
39
40 public class QualityProfileDataProviderIT {
41
42   @Rule
43   public DbTester dbTester = DbTester.create(System2.INSTANCE);
44
45   private DbClient dbClient = dbTester.getDbClient();
46
47   QualityProfileDataProvider underTest = new QualityProfileDataProvider(dbClient, new QProfileComparison(dbClient));
48
49   @Test
50   public void retrieveQualityProfilesData_whenDefaultRootProfile_shouldReturnRelevantInformation() {
51     QProfileDto qProfile1 = createQualityProfile(false, null);
52     dbTester.qualityProfiles().setAsDefault(qProfile1);
53     Assertions.assertThat(underTest.retrieveQualityProfilesData())
54       .extracting(p -> p.uuid(), p -> p.isDefault(), p -> p.isBuiltIn(), p -> p.builtInParent(),
55         p -> p.rulesActivatedCount(), p -> p.rulesDeactivatedCount(), p -> p.rulesOverriddenCount())
56       .containsExactlyInAnyOrder(tuple(qProfile1.getKee(), true, false, false, null, null, null));
57   }
58
59   @Test
60   public void retrieveQualityProfilesData_whenDefaultChildProfile_shouldReturnRelevantInformation() {
61     QProfileDto rootProfile = createQualityProfile(false, null);
62
63     QProfileDto childProfile = createQualityProfile(false, rootProfile.getKee());
64
65     dbTester.qualityProfiles().setAsDefault(childProfile);
66     Assertions.assertThat(underTest.retrieveQualityProfilesData())
67       .extracting(p -> p.uuid(), p -> p.isDefault(), p -> p.isBuiltIn(), p -> p.builtInParent(),
68         p -> p.rulesActivatedCount(), p -> p.rulesDeactivatedCount(), p -> p.rulesOverriddenCount())
69       .containsExactlyInAnyOrder(
70         tuple(rootProfile.getKee(), false, false, false, null, null, null),
71         tuple(childProfile.getKee(), true, false, false, null, null, null));
72   }
73
74   @Test
75   public void retrieveQualityProfilesData_whenProfileAssignedToProject_shouldReturnProfile() {
76     ProjectData projectData = dbTester.components().insertPublicProject();
77
78     QProfileDto associatedProfile = createQualityProfile(false, null);
79
80     QProfileDto unassociatedProfile = createQualityProfile(false, null);
81
82     dbTester.qualityProfiles().associateWithProject(projectData.getProjectDto(), associatedProfile);
83
84     Assertions.assertThat(underTest.retrieveQualityProfilesData())
85       .extracting(p -> p.uuid(), p -> p.isDefault())
86       .containsExactlyInAnyOrder(
87         tuple(associatedProfile.getKee(), false),
88         tuple(unassociatedProfile.getKee(), false)
89       );
90   }
91
92   @Test
93   public void retrieveQualityProfilesData_whenBuiltInParent_shouldReturnBuiltInParent() {
94
95     QProfileDto rootBuiltinProfile = createQualityProfile(true, null);
96
97     QProfileDto childProfile = createQualityProfile(false, rootBuiltinProfile.getKee());
98
99     QProfileDto grandChildProfile = createQualityProfile(false, childProfile.getKee());
100
101     dbTester.qualityProfiles().setAsDefault(rootBuiltinProfile, childProfile, grandChildProfile);
102
103     Assertions.assertThat(underTest.retrieveQualityProfilesData())
104       .extracting(p -> p.uuid(), p -> p.isBuiltIn(), p -> p.builtInParent())
105       .containsExactlyInAnyOrder(tuple(rootBuiltinProfile.getKee(), true, null),
106         tuple(childProfile.getKee(), false, true),
107         tuple(grandChildProfile.getKee(), false, true)
108       );
109   }
110
111   @Test
112   public void retrieveQualityProfilesData_whenBuiltInParent_shouldReturnActiveAndUnactiveRules() {
113
114     QProfileDto rootBuiltinProfile = createQualityProfile(true, null);
115
116     QProfileDto childProfile = createQualityProfile(false, rootBuiltinProfile.getKee());
117     RuleDto activatedRule = dbTester.rules().insert();
118     RuleDto deactivatedRule = dbTester.rules().insert();
119
120     dbTester.qualityProfiles().activateRule(rootBuiltinProfile, deactivatedRule);
121     dbTester.qualityProfiles().activateRule(childProfile, activatedRule);
122     dbTester.qualityProfiles().setAsDefault(childProfile);
123
124     Assertions.assertThat(underTest.retrieveQualityProfilesData())
125       .extracting(p -> p.uuid(), p -> p.rulesActivatedCount(), p -> p.rulesDeactivatedCount(), p -> p.rulesOverriddenCount())
126       .containsExactlyInAnyOrder(
127         tuple(rootBuiltinProfile.getKee(), null, null, null),
128         tuple(childProfile.getKee(), 1, 1, 0)
129       );
130   }
131
132   @Test
133   public void retrieveQualityProfilesData_whenBuiltInParent_shouldReturnOverriddenRules() {
134
135     QProfileDto rootBuiltinProfile = createQualityProfile(true, null);
136
137     QProfileDto childProfile = createQualityProfile(false, rootBuiltinProfile.getKee());
138     RuleDto rule = dbTester.rules().insert();
139     RuleParamDto initialRuleParam = dbTester.rules().insertRuleParam(rule, p -> p.setName("key").setDefaultValue("initial"));
140
141
142     ActiveRuleDto activeRuleDto = dbTester.qualityProfiles().activateRule(rootBuiltinProfile, rule);
143     dbTester.getDbClient().activeRuleDao().insertParam(dbTester.getSession(), activeRuleDto, newParam(activeRuleDto, initialRuleParam, "key", "value"));
144
145     ActiveRuleDto childActivateRule = dbTester.qualityProfiles().activateRule(childProfile, rule, ar -> {
146       ar.setInheritance(OVERRIDES);
147     });
148     dbTester.getDbClient().activeRuleDao().insertParam(dbTester.getSession(), activeRuleDto, newParam(childActivateRule, initialRuleParam, "key", "override"));
149
150     dbTester.qualityProfiles().setAsDefault(childProfile);
151
152     Assertions.assertThat(underTest.retrieveQualityProfilesData())
153       .extracting(p -> p.uuid(), p -> p.rulesActivatedCount(), p -> p.rulesDeactivatedCount(), p -> p.rulesOverriddenCount())
154       .containsExactlyInAnyOrder(
155         tuple(rootBuiltinProfile.getKee(), null, null, null),
156         tuple(childProfile.getKee(), 0, 0, 1));
157   }
158
159   private static ActiveRuleParamDto newParam(ActiveRuleDto activeRuleDto, RuleParamDto initial, String key, String value) {
160     return new ActiveRuleParamDto().setActiveRuleUuid(activeRuleDto.getRuleUuid()).setRulesParameterUuid(initial.getUuid()).setKey(key).setValue(value);
161   }
162
163   private QProfileDto createQualityProfile(boolean isBuiltIn, @Nullable String parentKee) {
164     return dbTester.qualityProfiles().insert(p -> {
165       p.setIsBuiltIn(isBuiltIn);
166       p.setParentKee(parentKee);
167     });
168   }
169 }