-
Notifications
You must be signed in to change notification settings - Fork 1.9k
/
RuleServiceMediumTest.java
122 lines (102 loc) · 4.17 KB
/
RuleServiceMediumTest.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
/*
* SonarQube
* Copyright (C) 2009-2016 SonarSource SA
* mailto:contact 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.rule;
import java.util.Set;
import org.junit.After;
import org.junit.Before;
import org.junit.ClassRule;
import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.rule.RuleKey;
import org.sonar.core.permission.GlobalPermissions;
import org.sonar.db.DbClient;
import org.sonar.db.DbSession;
import org.sonar.db.rule.RuleDao;
import org.sonar.db.rule.RuleTesting;
import org.sonar.server.exceptions.ForbiddenException;
import org.sonar.server.exceptions.UnauthorizedException;
import org.sonar.server.rule.index.RuleIndexer;
import org.sonar.server.tester.ServerTester;
import org.sonar.server.tester.UserSessionRule;
import static com.google.common.collect.Sets.newHashSet;
import static org.assertj.core.api.Assertions.assertThat;
public class RuleServiceMediumTest {
@ClassRule
public static ServerTester tester = new ServerTester().withEsIndexes();
@Rule
public UserSessionRule userSessionRule = UserSessionRule.forServerTester(tester);
@Rule
public ExpectedException expectedException = ExpectedException.none();
private RuleDao dao = tester.get(RuleDao.class);
private RuleService service = tester.get(RuleService.class);
private DbSession dbSession;
private RuleIndexer ruleIndexer;
@Before
public void before() {
tester.clearDbAndIndexes();
dbSession = tester.get(DbClient.class).openSession(false);
ruleIndexer = tester.get(RuleIndexer.class);
}
@After
public void after() {
dbSession.close();
}
@Test
public void listTags_returns_all_tags() {
// insert db
insertRule(RuleKey.of("javascript", "S001"), newHashSet("tag1"), newHashSet("sys1", "sys2"));
insertRule(RuleKey.of("java", "S001"), newHashSet("tag2"), newHashSet());
// all tags, including system
Set<String> tags = service.listTags();
assertThat(tags).containsOnly("tag1", "tag2", "sys1", "sys2");
}
@Test
public void listTags_returns_tags_filtered_by_name() {
insertRule(RuleKey.of("javascript", "S001"), newHashSet("tag1"), newHashSet("sys1", "sys2"));
insertRule(RuleKey.of("java", "S001"), newHashSet("tag2"), newHashSet());
assertThat(service.listTags("missing", 10)).isEmpty();
assertThat(service.listTags("tag", 10)).containsOnly("tag1", "tag2");
assertThat(service.listTags("sys", 10)).containsOnly("sys1", "sys2");
// LIMITATION: case sensitive
assertThat(service.listTags("TAG", 10)).isEmpty();
assertThat(service.listTags("TAg", 10)).isEmpty();
assertThat(service.listTags("MISSing", 10)).isEmpty();
}
@Test
public void delete_throws_UnauthorizedException_if_not_logged_in() {
expectedException.expect(UnauthorizedException.class);
expectedException.expectMessage("Authentication is required");
service.delete(RuleKey.of("java", "S001"));
}
@Test
public void delete_throws_ForbiddenException_if_not_administrator() {
userSessionRule.login().setGlobalPermissions(GlobalPermissions.SCAN_EXECUTION);
expectedException.expect(ForbiddenException.class);
expectedException.expectMessage("Insufficient privileges");
service.delete(RuleKey.of("java", "S001"));
}
private void insertRule(RuleKey key, Set<String> tags, Set<String> systemTags) {
dao.insert(dbSession,
RuleTesting.newDto(key).setTags(tags).setSystemTags(systemTags));
dbSession.commit();
ruleIndexer.index();
}
}