3 * Copyright (C) 2009-2024 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.telemetry;
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;
37 import static org.assertj.core.groups.Tuple.tuple;
38 import static org.sonar.db.qualityprofile.ActiveRuleDto.OVERRIDES;
40 public class QualityProfileDataProviderIT {
43 public DbTester dbTester = DbTester.create(System2.INSTANCE);
45 private DbClient dbClient = dbTester.getDbClient();
47 QualityProfileDataProvider underTest = new QualityProfileDataProvider(dbClient, new QProfileComparison(dbClient));
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));
60 public void retrieveQualityProfilesData_whenDefaultChildProfile_shouldReturnRelevantInformation() {
61 QProfileDto rootProfile = createQualityProfile(false, null);
63 QProfileDto childProfile = createQualityProfile(false, rootProfile.getKee());
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));
75 public void retrieveQualityProfilesData_whenProfileAssignedToProject_shouldReturnProfile() {
76 ProjectData projectData = dbTester.components().insertPublicProject();
78 QProfileDto associatedProfile = createQualityProfile(false, null);
80 QProfileDto unassociatedProfile = createQualityProfile(false, null);
82 dbTester.qualityProfiles().associateWithProject(projectData.getProjectDto(), associatedProfile);
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)
93 public void retrieveQualityProfilesData_whenBuiltInParent_shouldReturnBuiltInParent() {
95 QProfileDto rootBuiltinProfile = createQualityProfile(true, null);
97 QProfileDto childProfile = createQualityProfile(false, rootBuiltinProfile.getKee());
99 QProfileDto grandChildProfile = createQualityProfile(false, childProfile.getKee());
101 dbTester.qualityProfiles().setAsDefault(rootBuiltinProfile, childProfile, grandChildProfile);
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)
112 public void retrieveQualityProfilesData_whenBuiltInParent_shouldReturnActiveAndUnactiveRules() {
114 QProfileDto rootBuiltinProfile = createQualityProfile(true, null);
116 QProfileDto childProfile = createQualityProfile(false, rootBuiltinProfile.getKee());
117 RuleDto activatedRule = dbTester.rules().insert();
118 RuleDto deactivatedRule = dbTester.rules().insert();
120 dbTester.qualityProfiles().activateRule(rootBuiltinProfile, deactivatedRule);
121 dbTester.qualityProfiles().activateRule(childProfile, activatedRule);
122 dbTester.qualityProfiles().setAsDefault(childProfile);
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)
133 public void retrieveQualityProfilesData_whenBuiltInParent_shouldReturnOverriddenRules() {
135 QProfileDto rootBuiltinProfile = createQualityProfile(true, null);
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"));
142 ActiveRuleDto activeRuleDto = dbTester.qualityProfiles().activateRule(rootBuiltinProfile, rule);
143 dbTester.getDbClient().activeRuleDao().insertParam(dbTester.getSession(), activeRuleDto, newParam(activeRuleDto, initialRuleParam, "key", "value"));
145 ActiveRuleDto childActivateRule = dbTester.qualityProfiles().activateRule(childProfile, rule, ar -> {
146 ar.setInheritance(OVERRIDES);
148 dbTester.getDbClient().activeRuleDao().insertParam(dbTester.getSession(), activeRuleDto, newParam(childActivateRule, initialRuleParam, "key", "override"));
150 dbTester.qualityProfiles().setAsDefault(childProfile);
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));
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);
163 private QProfileDto createQualityProfile(boolean isBuiltIn, @Nullable String parentKee) {
164 return dbTester.qualityProfiles().insert(p -> {
165 p.setIsBuiltIn(isBuiltIn);
166 p.setParentKee(parentKee);