3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact 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.qualityprofile.ws;
22 import java.util.Date;
23 import org.junit.After;
24 import org.junit.Before;
25 import org.junit.ClassRule;
26 import org.junit.Rule;
27 import org.junit.Test;
28 import org.sonar.api.rule.RuleKey;
29 import org.sonar.api.rule.RuleStatus;
30 import org.sonar.api.rule.Severity;
31 import org.sonar.db.DbClient;
32 import org.sonar.db.DbSession;
33 import org.sonar.db.qualityprofile.ActiveRuleDto;
34 import org.sonar.db.qualityprofile.QualityProfileDto;
35 import org.sonar.db.rule.RuleDto;
36 import org.sonar.server.exceptions.NotFoundException;
37 import org.sonar.server.qualityprofile.QProfileName;
38 import org.sonar.server.qualityprofile.QProfileTesting;
39 import org.sonar.server.qualityprofile.RuleActivation;
40 import org.sonar.server.qualityprofile.RuleActivator;
41 import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
42 import org.sonar.server.rule.index.RuleIndexer;
43 import org.sonar.server.tester.ServerTester;
44 import org.sonar.server.tester.UserSessionRule;
45 import org.sonar.server.ws.WsTester;
47 public class InheritanceActionMediumTest {
50 public static final ServerTester tester = new ServerTester().withEsIndexes();
53 public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester);
60 RuleIndexer ruleIndexer;
61 ActiveRuleIndexer activeRuleIndexer;
65 tester.clearDbAndIndexes();
66 db = tester.get(DbClient.class);
67 session = db.openSession(false);
68 ruleIndexer = tester.get(RuleIndexer.class);
69 ruleIndexer.setEnabled(true);
70 activeRuleIndexer = tester.get(ActiveRuleIndexer.class);
71 activeRuleIndexer.setEnabled(true);
73 wsTester = new WsTester(tester.get(QProfilesWs.class));
77 public void tearDown() {
82 public void inheritance_nominal() throws Exception {
83 RuleDto rule1 = createRule("xoo", "rule1");
84 RuleDto rule2 = createRule("xoo", "rule2");
85 RuleDto rule3 = createRule("xoo", "rule3");
88 * groupWide (2) <- companyWide (2) <- buWide (2, 1 overriding) <- (forProject1 (2), forProject2 (2))
90 QualityProfileDto groupWide = createProfile("xoo", "My Group Profile", "xoo-my-group-profile-01234");
91 createActiveRule(rule1, groupWide);
92 createActiveRule(rule2, groupWide);
96 activeRuleIndexer.index();
98 QualityProfileDto companyWide = createProfile("xoo", "My Company Profile", "xoo-my-company-profile-12345");
99 setParent(groupWide, companyWide);
101 QualityProfileDto buWide = createProfile("xoo", "My BU Profile", "xoo-my-bu-profile-23456");
102 setParent(companyWide, buWide);
103 overrideActiveRuleSeverity(rule1, buWide, Severity.CRITICAL);
105 QualityProfileDto forProject1 = createProfile("xoo", "For Project One", "xoo-for-project-one-34567");
106 setParent(buWide, forProject1);
107 createActiveRule(rule3, forProject1);
109 activeRuleIndexer.index();
111 QualityProfileDto forProject2 = createProfile("xoo", "For Project Two", "xoo-for-project-two-45678");
112 setParent(buWide, forProject2);
113 overrideActiveRuleSeverity(rule2, forProject2, Severity.CRITICAL);
115 wsTester.newGetRequest("api/qualityprofiles", "inheritance").setParam("profileKey", buWide.getKee())
116 .execute().assertJson(getClass(), "inheritance-buWide.json");
120 public void inheritance_no_family() throws Exception {
121 // Simple profile, no parent, no child
122 QualityProfileDto remi = createProfile("xoo", "Nobodys Boy", "xoo-nobody-s-boy-01234");
124 wsTester.newGetRequest("api/qualityprofiles", "inheritance").setParam("profileKey", remi.getKee()).execute().assertJson(getClass(), "inheritance-simple.json");
127 @Test(expected = NotFoundException.class)
128 public void fail_if_not_found() throws Exception {
129 wsTester.newGetRequest("api/qualityprofiles", "inheritance").setParam("profileKey", "polop").execute();
132 private QualityProfileDto createProfile(String lang, String name, String key) {
133 QualityProfileDto profile = QProfileTesting.newQProfileDto(new QProfileName(lang, name), key);
134 db.qualityProfileDao().insert(session, profile);
139 private void setParent(QualityProfileDto profile, QualityProfileDto parent) {
140 tester.get(RuleActivator.class).setParent(parent.getKey(), profile.getKey());
143 private RuleDto createRule(String lang, String id) {
144 long now = new Date().getTime();
145 RuleDto rule = RuleDto.createFor(RuleKey.of("blah", id))
147 .setSeverity(Severity.BLOCKER)
148 .setStatus(RuleStatus.READY)
149 .setUpdatedAtInMs(now)
150 .setCreatedAtInMs(now);
151 db.ruleDao().insert(session, rule);
155 private ActiveRuleDto createActiveRule(RuleDto rule, QualityProfileDto profile) {
156 long now = new Date().getTime();
157 ActiveRuleDto activeRule = ActiveRuleDto.createFor(profile, rule)
158 .setSeverity(rule.getSeverityString())
159 .setUpdatedAtInMs(now)
160 .setCreatedAtInMs(now);
161 db.activeRuleDao().insert(session, activeRule);
165 private void overrideActiveRuleSeverity(RuleDto rule, QualityProfileDto profile, String severity) {
166 tester.get(RuleActivator.class).activate(session, new RuleActivation(rule.getKey()).setSeverity(severity), profile.getKey());
168 activeRuleIndexer.index();