-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
InheritanceActionTest.java
206 lines (183 loc) · 7.91 KB
/
InheritanceActionTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
* SonarQube
* Copyright (C) 2009-2017 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.qualityprofile.ws;
import java.util.ArrayList;
import java.util.Date;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.sonar.api.config.MapSettings;
import org.sonar.api.resources.Languages;
import org.sonar.api.rule.RuleKey;
import org.sonar.api.rule.RuleStatus;
import org.sonar.api.rule.Severity;
import org.sonar.api.utils.System2;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import org.sonar.db.organization.OrganizationDto;
import org.sonar.db.qualityprofile.ActiveRuleDto;
import org.sonar.db.qualityprofile.QualityProfileDto;
import org.sonar.db.rule.RuleDefinitionDto;
import org.sonar.db.rule.RuleDto;
import org.sonar.db.rule.RuleTesting;
import org.sonar.server.es.EsClient;
import org.sonar.server.es.EsTester;
import org.sonar.server.exceptions.NotFoundException;
import org.sonar.server.organization.TestDefaultOrganizationProvider;
import org.sonar.server.qualityprofile.QProfileLookup;
import org.sonar.server.qualityprofile.QProfileName;
import org.sonar.server.qualityprofile.QProfileTesting;
import org.sonar.server.qualityprofile.RuleActivation;
import org.sonar.server.qualityprofile.RuleActivator;
import org.sonar.server.qualityprofile.RuleActivatorContextFactory;
import org.sonar.server.qualityprofile.index.ActiveRuleIndexer;
import org.sonar.server.rule.index.RuleIndex;
import org.sonar.server.rule.index.RuleIndexDefinition;
import org.sonar.server.rule.index.RuleIndexer;
import org.sonar.server.tester.UserSessionRule;
import org.sonar.server.util.TypeValidations;
import org.sonar.server.ws.WsActionTester;
import org.sonar.test.JsonAssert;
public class InheritanceActionTest {
@Rule
public DbTester dbTester = DbTester.create();
@Rule
public EsTester esTester = new EsTester(new RuleIndexDefinition(new MapSettings()));
@Rule
public UserSessionRule userSession = UserSessionRule.standalone();
private DbClient dbClient;
private DbSession dbSession;
private EsClient esClient;
private RuleIndexer ruleIndexer;
private ActiveRuleIndexer activeRuleIndexer;
private InheritanceAction underTest;
private WsActionTester wsActionTester;
private RuleActivator ruleActivator;
private OrganizationDto organization;
@Before
public void setUp() {
dbClient = dbTester.getDbClient();
dbSession = dbTester.getSession();
esClient = esTester.client();
ruleIndexer = new RuleIndexer(System2.INSTANCE, dbClient, esClient, TestDefaultOrganizationProvider.fromUuid("org-1"));
activeRuleIndexer = new ActiveRuleIndexer(System2.INSTANCE, dbClient, esClient);
TestDefaultOrganizationProvider defaultOrganizationProvider = TestDefaultOrganizationProvider.from(dbTester);
underTest = new InheritanceAction(
dbClient,
new QProfileLookup(dbClient),
new QProfileWsSupport(dbClient, userSession, defaultOrganizationProvider),
new Languages());
wsActionTester = new WsActionTester(underTest);
ruleActivator = new RuleActivator(
System2.INSTANCE,
dbClient,
new RuleIndex(esClient),
new RuleActivatorContextFactory(dbClient),
new TypeValidations(new ArrayList<>()),
activeRuleIndexer,
userSession);
organization = dbTester.organizations().insert();
}
@Test
public void inheritance_nominal() throws Exception {
RuleDefinitionDto rule1 = createRule("xoo", "rule1");
RuleDefinitionDto rule2 = createRule("xoo", "rule2");
RuleDefinitionDto rule3 = createRule("xoo", "rule3");
/*
* groupWide (2) <- companyWide (2) <- buWide (2, 1 overriding) <- (forProject1 (2), forProject2 (2))
*/
QualityProfileDto groupWide = createProfile("xoo", "My Group Profile", "xoo-my-group-profile-01234");
createActiveRule(rule1, groupWide);
createActiveRule(rule2, groupWide);
dbSession.commit();
ruleIndexer.index();
activeRuleIndexer.index();
QualityProfileDto companyWide = createProfile("xoo", "My Company Profile", "xoo-my-company-profile-12345");
setParent(groupWide, companyWide);
QualityProfileDto buWide = createProfile("xoo", "My BU Profile", "xoo-my-bu-profile-23456");
setParent(companyWide, buWide);
overrideActiveRuleSeverity(rule1, buWide, Severity.CRITICAL);
QualityProfileDto forProject1 = createProfile("xoo", "For Project One", "xoo-for-project-one-34567");
setParent(buWide, forProject1);
createActiveRule(rule3, forProject1);
dbSession.commit();
activeRuleIndexer.index();
QualityProfileDto forProject2 = createProfile("xoo", "For Project Two", "xoo-for-project-two-45678");
setParent(buWide, forProject2);
overrideActiveRuleSeverity(rule2, forProject2, Severity.CRITICAL);
String response = wsActionTester.newRequest()
.setMethod("GET")
.setParam("profileKey", buWide.getKee())
.execute()
.getInput();
JsonAssert.assertJson(response).isSimilarTo(getClass().getResource("InheritanceActionTest/inheritance-buWide.json"));
}
@Test
public void inheritance_no_family() throws Exception {
// Simple profile, no parent, no child
QualityProfileDto remi = createProfile("xoo", "Nobodys Boy", "xoo-nobody-s-boy-01234");
String response = wsActionTester.newRequest()
.setMethod("GET")
.setParam("profileKey", remi.getKee())
.execute()
.getInput();
JsonAssert.assertJson(response).isSimilarTo(getClass().getResource("InheritanceActionTest/inheritance-simple.json"));
}
@Test(expected = NotFoundException.class)
public void fail_if_not_found() throws Exception {
wsActionTester.newRequest()
.setMethod("GET").setParam("profileKey", "polop").execute();
}
private QualityProfileDto createProfile(String lang, String name, String key) {
QualityProfileDto profile = QProfileTesting.newQProfileDto(organization, new QProfileName(lang, name), key);
dbClient.qualityProfileDao().insert(dbSession, profile);
dbSession.commit();
return profile;
}
private void setParent(QualityProfileDto profile, QualityProfileDto parent) {
ruleActivator.setParent(dbSession, parent.getKey(), profile.getKey());
}
private RuleDefinitionDto createRule(String lang, String id) {
long now = new Date().getTime();
RuleDto rule = RuleTesting.newDto(RuleKey.of("blah", id))
.setLanguage(lang)
.setSeverity(Severity.BLOCKER)
.setStatus(RuleStatus.READY)
.setUpdatedAt(now)
.setCreatedAt(now);
dbClient.ruleDao().insert(dbSession, rule.getDefinition());
return rule.getDefinition();
}
private ActiveRuleDto createActiveRule(RuleDefinitionDto rule, QualityProfileDto profile) {
long now = new Date().getTime();
ActiveRuleDto activeRule = ActiveRuleDto.createFor(profile, rule)
.setSeverity(rule.getSeverityString())
.setUpdatedAt(now)
.setCreatedAt(now);
dbClient.activeRuleDao().insert(dbSession, activeRule);
return activeRule;
}
private void overrideActiveRuleSeverity(RuleDefinitionDto rule, QualityProfileDto profile, String severity) {
ruleActivator.activate(dbSession, new RuleActivation(rule.getKey()).setSeverity(severity), profile.getKey());
dbSession.commit();
activeRuleIndexer.index();
}
}