]> source.dussan.org Git - sonarqube.git/blob
3d85ade5b77ee59501dc735f972a6deabcb6f1ca
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2017 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 java.util.List;
23 import org.junit.After;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
27 import org.sonar.api.profiles.RulesProfile;
28 import org.sonar.api.rule.Severity;
29 import org.sonar.api.rules.RulePriority;
30 import org.sonar.api.utils.System2;
31 import org.sonar.api.utils.internal.AlwaysIncreasingSystem2;
32 import org.sonar.core.util.SequenceUuidFactory;
33 import org.sonar.core.util.UuidFactory;
34 import org.sonar.db.DbSession;
35 import org.sonar.db.DbTester;
36 import org.sonar.db.organization.OrganizationDto;
37 import org.sonar.db.qualityprofile.ActiveRuleDto;
38 import org.sonar.db.qualityprofile.ActiveRuleKey;
39 import org.sonar.db.qualityprofile.ActiveRuleParamDto;
40 import org.sonar.db.qualityprofile.QProfileChangeDto;
41 import org.sonar.db.qualityprofile.QProfileChangeQuery;
42 import org.sonar.db.qualityprofile.QProfileDto;
43 import org.sonar.db.rule.RuleDefinitionDto;
44 import org.sonar.server.language.LanguageTesting;
45 import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
46 import org.sonar.server.util.TypeValidations;
47
48 import static java.util.Collections.emptyList;
49 import static org.assertj.core.api.Assertions.assertThat;
50 import static org.mockito.Mockito.mock;
51 import static org.sonar.api.rules.RulePriority.CRITICAL;
52 import static org.sonar.api.rules.RulePriority.MAJOR;
53
54 public class BuiltInQProfileInsertImplTest {
55
56   @Rule
57   public BuiltInQProfileRepositoryRule builtInQProfileRepository = new BuiltInQProfileRepositoryRule();
58   @Rule
59   public DbTester db = DbTester.create().setDisableDefaultOrganization(true);
60   @Rule
61   public ExpectedException expectedException = ExpectedException.none();
62
63   private System2 system2 = new AlwaysIncreasingSystem2();
64   private UuidFactory uuidFactory = new SequenceUuidFactory();
65   private TypeValidations typeValidations = new TypeValidations(emptyList());
66   private DbSession dbSession = db.getSession();
67   private DbSession batchDbSession = db.getDbClient().openSession(true);
68   private ActiveRuleIndexer activeRuleIndexer = mock(ActiveRuleIndexer.class);
69   private BuiltInQProfileInsertImpl underTest = new BuiltInQProfileInsertImpl(db.getDbClient(), system2, uuidFactory, typeValidations, activeRuleIndexer);
70
71   @After
72   public void tearDown() {
73     batchDbSession.close();
74   }
75
76   @Test
77   public void insert_single_row_in_RULES_PROFILES_and_reference_it_in_ORG_QPROFILES() {
78     OrganizationDto org1 = db.organizations().insert();
79     OrganizationDto org2 = db.organizations().insert();
80     BuiltInQProfile builtIn = builtInQProfileRepository.create(LanguageTesting.newLanguage("xoo"), "the name", false);
81
82     call(builtIn);
83
84     verifyTableSize("org_qprofiles", 2);
85     verifyTableSize("rules_profiles", 1);
86     verifyTableSize("active_rules", 0);
87     verifyTableSize("active_rule_parameters", 0);
88     verifyTableSize("qprofile_changes", 0);
89     verifyTableSize("project_qprofiles", 0);
90
91     QProfileDto profileOnOrg1 = verifyProfileInDb(org1, builtIn);
92     QProfileDto profileOnOrg2 = verifyProfileInDb(org2, builtIn);
93
94     // same row in table rules_profiles is used
95     assertThat(profileOnOrg1.getKee()).isNotEqualTo(profileOnOrg2.getKee());
96     assertThat(profileOnOrg1.getRulesProfileUuid()).isEqualTo(profileOnOrg2.getRulesProfileUuid());
97     assertThat(profileOnOrg1.getId()).isEqualTo(profileOnOrg2.getId());
98   }
99
100   @Test
101   public void insert_active_rules_and_changelog() {
102     OrganizationDto org = db.organizations().insert();
103     RuleDefinitionDto rule1 = db.rules().insert(r -> r.setLanguage("xoo"));
104     RuleDefinitionDto rule2 = db.rules().insert(r -> r.setLanguage("xoo"));
105     RulesProfile apiProfile = RulesProfile.create("the name", "xoo");
106     activeRule(apiProfile, rule1, CRITICAL);
107     activeRule(apiProfile, rule2, MAJOR);
108
109     BuiltInQProfile builtIn = builtInQProfileRepository.create(apiProfile);
110     call(builtIn);
111
112     verifyTableSize("rules_profiles", 1);
113     verifyTableSize("active_rules", 2);
114     verifyTableSize("active_rule_parameters", 0);
115     verifyTableSize("qprofile_changes", 2);
116
117     QProfileDto profile = verifyProfileInDb(org, builtIn);
118     verifyActiveRuleInDb(profile, rule1, Severity.CRITICAL);
119     verifyActiveRuleInDb(profile, rule2, Severity.MAJOR);
120   }
121
122   @Test
123   public void flag_profile_as_default_on_organization_if_declared_as_default_by_api() {
124     OrganizationDto org = db.organizations().insert();
125     RulesProfile apiProfile = RulesProfile.create("the name", "xoo");
126     apiProfile.setDefaultProfile(true);
127     BuiltInQProfile builtIn = builtInQProfileRepository.create(apiProfile);
128
129     call(builtIn);
130
131     QProfileDto profile = verifyProfileInDb(org, builtIn);
132     QProfileDto defaultProfile = db.getDbClient().qualityProfileDao().selectDefaultProfile(dbSession, org, "xoo");
133     assertThat(defaultProfile.getKee()).isEqualTo(profile.getKee());
134   }
135
136   @Test
137   public void existing_default_profile_in_organization_must_not_be_changed() {
138     RulesProfile apiProfile = RulesProfile.create("the name", "xoo");
139     apiProfile.setDefaultProfile(true);
140     BuiltInQProfile builtIn = builtInQProfileRepository.create(apiProfile);
141
142     OrganizationDto org = db.organizations().insert();
143     QProfileDto currentDefault = db.qualityProfiles().insert(org, p -> p.setLanguage(apiProfile.getLanguage()));
144     db.qualityProfiles().setAsDefault(currentDefault);
145
146     call(builtIn);
147
148     QProfileDto defaultProfile = db.getDbClient().qualityProfileDao().selectDefaultProfile(dbSession, org, apiProfile.getLanguage());
149     assertThat(defaultProfile.getKee()).isEqualTo(currentDefault.getKee());
150   }
151
152   @Test
153   public void dont_flag_profile_as_default_on_organization_if_not_declared_as_default_by_api() {
154     OrganizationDto org = db.organizations().insert();
155     RulesProfile apiProfile = RulesProfile.create("the name", "xoo");
156     apiProfile.setDefaultProfile(false);
157     BuiltInQProfile builtIn = builtInQProfileRepository.create(apiProfile);
158
159     call(builtIn);
160
161     QProfileDto defaultProfile = db.getDbClient().qualityProfileDao().selectDefaultProfile(dbSession, org, "xoo");
162     assertThat(defaultProfile).isNull();
163   }
164
165   // TODO test params
166   // TODO test lot of active_rules, params, orgas
167
168   private void verifyActiveRuleInDb(QProfileDto profile, RuleDefinitionDto rule, String expectedSeverity) {
169     ActiveRuleDto activeRule = db.getDbClient().activeRuleDao().selectByKey(dbSession, ActiveRuleKey.of(profile, rule.getKey())).get();
170     assertThat(activeRule.getId()).isPositive();
171     assertThat(activeRule.getInheritance()).isNull();
172     assertThat(activeRule.doesOverride()).isFalse();
173     assertThat(activeRule.getRuleId()).isEqualTo(rule.getId());
174     assertThat(activeRule.getProfileId()).isEqualTo(profile.getId());
175     assertThat(activeRule.getSeverityString()).isEqualTo(expectedSeverity);
176     assertThat(activeRule.getCreatedAt()).isPositive();
177     assertThat(activeRule.getUpdatedAt()).isPositive();
178
179     List<ActiveRuleParamDto> params = db.getDbClient().activeRuleDao().selectParamsByActiveRuleId(dbSession, activeRule.getId());
180     assertThat(params).isEmpty();
181
182     QProfileChangeQuery changeQuery = new QProfileChangeQuery(profile.getKee());
183     QProfileChangeDto change = db.getDbClient().qProfileChangeDao().selectByQuery(dbSession, changeQuery).stream()
184       .filter(c -> c.getDataAsMap().get("ruleKey").equals(rule.getKey().toString()))
185       .findFirst()
186       .get();
187     assertThat(change.getChangeType()).isEqualTo(ActiveRuleChange.Type.ACTIVATED.name());
188     assertThat(change.getCreatedAt()).isPositive();
189     assertThat(change.getUuid()).isNotEmpty();
190     assertThat(change.getLogin()).isNull();
191     assertThat(change.getRulesProfileUuid()).isEqualTo(profile.getRulesProfileUuid());
192     assertThat(change.getDataAsMap().get("severity")).isEqualTo(expectedSeverity);
193   }
194
195   private static void activeRule(RulesProfile apiProfile, RuleDefinitionDto rule, RulePriority severity) {
196     apiProfile.activateRule(org.sonar.api.rules.Rule.create(rule.getRepositoryKey(), rule.getRuleKey()), severity);
197   }
198
199   private QProfileDto verifyProfileInDb(OrganizationDto organization, BuiltInQProfile builtIn) {
200     QProfileDto profileOnOrg1 = db.getDbClient().qualityProfileDao().selectByNameAndLanguage(dbSession, organization, builtIn.getName(), builtIn.getLanguage());
201     assertThat(profileOnOrg1.getLanguage()).isEqualTo(builtIn.getLanguage());
202     assertThat(profileOnOrg1.getName()).isEqualTo(builtIn.getName());
203     assertThat(profileOnOrg1.getOrganizationUuid()).isEqualTo(organization.getUuid());
204     assertThat(profileOnOrg1.getParentKee()).isNull();
205     assertThat(profileOnOrg1.getLastUsed()).isNull();
206     assertThat(profileOnOrg1.getUserUpdatedAt()).isNull();
207     assertThat(profileOnOrg1.getRulesUpdatedAt()).isNotEmpty();
208     assertThat(profileOnOrg1.getKee()).isNotEqualTo(profileOnOrg1.getRulesProfileUuid());
209     assertThat(profileOnOrg1.getId()).isNotNull();
210     return profileOnOrg1;
211   }
212
213   private void verifyTableSize(String table, int expectedSize) {
214     assertThat(db.countRowsOfTable(dbSession, table)).as("table " + table).isEqualTo(expectedSize);
215   }
216
217   private void call(BuiltInQProfile builtIn) {
218     underTest.create(dbSession, batchDbSession, builtIn);
219     dbSession.commit();
220     batchDbSession.commit();
221   }
222
223 }