3 * Copyright (C) 2009-2020 SonarSource SA
4 * mailto:info 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.api.server.profile;
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;
32 import static org.assertj.core.api.Assertions.assertThat;
33 import static org.assertj.core.groups.Tuple.tuple;
35 public class BuiltInQualityProfilesDefinitionTest {
38 public ExpectedException thrown = ExpectedException.none();
41 public void coverage() {
42 assertThat(new BuiltInQualityProfilesDefinition.Context().profile("Foo", "xoo")).isNull();
46 public void createEmptyProfile() {
47 Map<String, Map<String, BuiltInQualityProfile>> profiles = define(c -> {
48 c.createBuiltInQualityProfile("Foo", "xoo").done();
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();
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");
64 NewBuiltInQualityProfile profile2 = c.createBuiltInQualityProfile("Foo2", "xoo");
66 NewBuiltInQualityProfile profile3 = c.createBuiltInQualityProfile("Foo1", "xoo2");
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]");
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]");
91 public void createDefaultProfile() {
92 Map<String, Map<String, BuiltInQualityProfile>> profiles = define(c -> {
93 c.createBuiltInQualityProfile("Foo", "xoo")
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();
106 public void duplicateProfile() {
107 thrown.expect(IllegalArgumentException.class);
108 thrown.expectMessage("There is already a quality profile with name 'Foo' for language 'xoo'");
110 c.createBuiltInQualityProfile("Foo", "xoo").done();
111 c.createBuiltInQualityProfile("Foo", "xoo").done();
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");
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())
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");
142 public void createProfileWithDuplicateRules() {
145 NewBuiltInQualityProfile profile = c.createBuiltInQualityProfile("Foo", "xoo");
146 profile.activateRule("repo", "rule");
148 thrown.expect(IllegalArgumentException.class);
149 thrown.expectMessage("The rule 'repo:rule' is already activated");
151 profile.activateRule("repo", "rule");
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();
161 private static class FakeProfile implements BuiltInQualityProfilesDefinition {
163 private Consumer<Context> consumer;
165 public FakeProfile(Consumer<Context> consumer) {
166 this.consumer = consumer;
170 public void define(Context context) {
171 consumer.accept(context);