]> source.dussan.org Git - sonarqube.git/blob
0ca116e26265be01acb2f4d504acadf460b6626f
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2020 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.api.server.profile;
21
22 import java.util.Map;
23 import java.util.function.Consumer;
24 import org.junit.Rule;
25 import org.junit.Test;
26 import org.junit.rules.ExpectedException;
27 import org.sonar.api.rule.RuleKey;
28 import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.BuiltInQualityProfile;
29 import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.NewBuiltInActiveRule;
30 import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition.NewBuiltInQualityProfile;
31
32 import static org.assertj.core.api.Assertions.assertThat;
33 import static org.assertj.core.groups.Tuple.tuple;
34
35 public class BuiltInQualityProfilesDefinitionTest {
36
37   @Rule
38   public ExpectedException thrown = ExpectedException.none();
39
40   @Test
41   public void coverage() {
42     assertThat(new BuiltInQualityProfilesDefinition.Context().profile("Foo", "xoo")).isNull();
43   }
44
45   @Test
46   public void createEmptyProfile() {
47     Map<String, Map<String, BuiltInQualityProfile>> profiles = define(c -> {
48       c.createBuiltInQualityProfile("Foo", "xoo").done();
49     });
50     assertThat(profiles).containsOnlyKeys("xoo");
51     assertThat(profiles.get("xoo")).containsOnlyKeys("Foo");
52     BuiltInQualityProfile profile = profiles.get("xoo").get("Foo");
53     assertThat(profile.name()).isEqualTo("Foo");
54     assertThat(profile.language()).isEqualTo("xoo");
55     assertThat(profile.isDefault()).isFalse();
56   }
57
58   @Test
59   public void sanityCheck() {
60     Map<String, Map<String, BuiltInQualityProfile>> profiles = define(c -> {
61       NewBuiltInQualityProfile profile1 = c.createBuiltInQualityProfile("Foo1", "xoo");
62       NewBuiltInActiveRule rule = profile1.activateRule("repo", "rule");
63       profile1.done();
64       NewBuiltInQualityProfile profile2 = c.createBuiltInQualityProfile("Foo2", "xoo");
65       profile2.done();
66       NewBuiltInQualityProfile profile3 = c.createBuiltInQualityProfile("Foo1", "xoo2");
67       profile3.done();
68       assertThat(profile1).isEqualTo(profile1);
69       assertThat(profile1).isNotEqualTo(null);
70       assertThat(profile1).isNotEqualTo("Foo");
71       assertThat(profile1).isNotEqualTo(profile2);
72       assertThat(profile1).isNotEqualTo(profile3);
73       assertThat(profile1.hashCode()).isNotEqualTo(profile2.hashCode());
74       assertThat(profile1.toString()).isEqualTo("NewBuiltInQualityProfile{name='Foo1', language='xoo', default='false'}");
75       assertThat(rule.toString()).isEqualTo("[repository=repo, key=rule]");
76     });
77     BuiltInQualityProfile profile1 = profiles.get("xoo").get("Foo1");
78     BuiltInQualityProfile profile2 = profiles.get("xoo").get("Foo2");
79     BuiltInQualityProfile profile3 = profiles.get("xoo2").get("Foo1");
80     assertThat(profile1).isEqualTo(profile1);
81     assertThat(profile1).isNotEqualTo(null);
82     assertThat(profile1).isNotEqualTo("Foo");
83     assertThat(profile1).isNotEqualTo(profile2);
84     assertThat(profile1).isNotEqualTo(profile3);
85     assertThat(profile1.hashCode()).isNotEqualTo(profile2.hashCode());
86     assertThat(profile1.toString()).isEqualTo("BuiltInQualityProfile{name='Foo1', language='xoo', default='false'}");
87     assertThat(profile1.rule(RuleKey.of("repo", "rule")).toString()).isEqualTo("[repository=repo, key=rule]");
88   }
89
90   @Test
91   public void createDefaultProfile() {
92     Map<String, Map<String, BuiltInQualityProfile>> profiles = define(c -> {
93       c.createBuiltInQualityProfile("Foo", "xoo")
94         .setDefault(true)
95         .done();
96     });
97     assertThat(profiles).containsOnlyKeys("xoo");
98     assertThat(profiles.get("xoo")).containsOnlyKeys("Foo");
99     BuiltInQualityProfile profile = profiles.get("xoo").get("Foo");
100     assertThat(profile.name()).isEqualTo("Foo");
101     assertThat(profile.language()).isEqualTo("xoo");
102     assertThat(profile.isDefault()).isTrue();
103   }
104
105   @Test
106   public void duplicateProfile() {
107     thrown.expect(IllegalArgumentException.class);
108     thrown.expectMessage("There is already a quality profile with name 'Foo' for language 'xoo'");
109     define(c -> {
110       c.createBuiltInQualityProfile("Foo", "xoo").done();
111       c.createBuiltInQualityProfile("Foo", "xoo").done();
112     });
113   }
114
115   @Test
116   public void createProfileWithRules() {
117     Map<String, Map<String, BuiltInQualityProfile>> profiles = define(c -> {
118       NewBuiltInQualityProfile profile = c.createBuiltInQualityProfile("Foo", "xoo");
119       profile.activateRule("repo", "ruleWithoutParam");
120       profile.activateRule("repo", "ruleWithSeverity").overrideSeverity("CRITICAL");
121       profile.activateRule("repo", "ruleWithParam").overrideParam("param", "value");
122       profile.done();
123     });
124     assertThat(profiles).containsOnlyKeys("xoo");
125     assertThat(profiles.get("xoo")).containsOnlyKeys("Foo");
126     BuiltInQualityProfile profile = profiles.get("xoo").get("Foo");
127     assertThat(profile.name()).isEqualTo("Foo");
128     assertThat(profile.language()).isEqualTo("xoo");
129     assertThat(profile.isDefault()).isFalse();
130     assertThat(profile.rules())
131       .extracting(BuiltInQualityProfilesDefinition.BuiltInActiveRule::repoKey, BuiltInQualityProfilesDefinition.BuiltInActiveRule::ruleKey,
132         BuiltInQualityProfilesDefinition.BuiltInActiveRule::overriddenSeverity, r -> r.overriddenParams().size())
133       .containsOnly(
134         tuple("repo", "ruleWithoutParam", null, 0),
135         tuple("repo", "ruleWithSeverity", "CRITICAL", 0),
136         tuple("repo", "ruleWithParam", null, 1));
137     assertThat(profile.rule(RuleKey.of("repo", "ruleWithParam")).overriddenParam("param").key()).isEqualTo("param");
138     assertThat(profile.rule(RuleKey.of("repo", "ruleWithParam")).overriddenParam("param").overriddenValue()).isEqualTo("value");
139   }
140
141   @Test
142   public void createProfileWithDuplicateRules() {
143
144     define(c -> {
145       NewBuiltInQualityProfile profile = c.createBuiltInQualityProfile("Foo", "xoo");
146       profile.activateRule("repo", "rule");
147
148       thrown.expect(IllegalArgumentException.class);
149       thrown.expectMessage("The rule 'repo:rule' is already activated");
150
151       profile.activateRule("repo", "rule");
152     });
153   }
154
155   private Map<String, Map<String, BuiltInQualityProfile>> define(Consumer<BuiltInQualityProfilesDefinition.Context> consumer) {
156     BuiltInQualityProfilesDefinition.Context context = new BuiltInQualityProfilesDefinition.Context();
157     new FakeProfile(consumer).define(context);
158     return context.profilesByLanguageAndName();
159   }
160
161   private static class FakeProfile implements BuiltInQualityProfilesDefinition {
162
163     private Consumer<Context> consumer;
164
165     public FakeProfile(Consumer<Context> consumer) {
166       this.consumer = consumer;
167     }
168
169     @Override
170     public void define(Context context) {
171       consumer.accept(context);
172     }
173
174   }
175
176 }