3 * Copyright (C) 2009-2019 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.server.qualityprofile;
22 import java.util.ArrayList;
23 import java.util.Collections;
24 import java.util.List;
25 import org.junit.Rule;
26 import org.junit.Test;
27 import org.junit.rules.ExpectedException;
28 import org.sonar.api.resources.Language;
29 import org.sonar.api.resources.Languages;
30 import org.sonar.api.server.profile.BuiltInQualityProfilesDefinition;
31 import org.sonar.api.utils.System2;
32 import org.sonar.db.DbClient;
33 import org.sonar.db.DbTester;
34 import org.sonar.server.language.LanguageTesting;
36 import static java.util.Arrays.asList;
37 import static org.assertj.core.api.Assertions.assertThat;
38 import static org.assertj.core.groups.Tuple.tuple;
39 import static org.mockito.Mockito.mock;
41 public class BuiltInQProfileRepositoryImplTest {
42 private static final Language FOO_LANGUAGE = LanguageTesting.newLanguage("foo", "foo", "foo");
43 private static final String SONAR_WAY_QP_NAME = "Sonar way";
46 public ExpectedException expectedException = ExpectedException.none();
48 public DbTester dbTester = DbTester.create(System2.INSTANCE);
50 private DbClient dbClient = dbTester.getDbClient();
53 public void get_throws_ISE_if_called_before_initialize() {
54 BuiltInQProfileRepositoryImpl underTest = new BuiltInQProfileRepositoryImpl(mock(DbClient.class), new Languages());
56 expectedException.expect(IllegalStateException.class);
57 expectedException.expectMessage("initialize must be called first");
63 public void initialize_throws_ISE_if_called_twice() {
64 BuiltInQProfileRepositoryImpl underTest = new BuiltInQProfileRepositoryImpl(mock(DbClient.class), new Languages());
65 underTest.initialize();
67 expectedException.expect(IllegalStateException.class);
68 expectedException.expectMessage("initialize must be called only once");
70 underTest.initialize();
74 public void initialize_throws_ISE_if_language_has_no_builtin_qp() {
75 BuiltInQProfileRepository underTest = new BuiltInQProfileRepositoryImpl(mock(DbClient.class), new Languages(FOO_LANGUAGE));
77 expectedException.expect(IllegalStateException.class);
78 expectedException.expectMessage("The following languages have no built-in quality profiles: foo");
80 underTest.initialize();
84 public void initialize_creates_no_BuiltInQProfile_when_all_definitions_apply_to_non_defined_languages() {
85 BuiltInQProfileRepository underTest = new BuiltInQProfileRepositoryImpl(mock(DbClient.class), new Languages(), new DummyProfileDefinition("foo", "P1", false));
87 underTest.initialize();
89 assertThat(underTest.get()).isEmpty();
93 public void initialize_makes_single_profile_of_a_language_default_even_if_not_flagged_as_so() {
94 BuiltInQProfileRepository underTest = new BuiltInQProfileRepositoryImpl(dbClient, new Languages(FOO_LANGUAGE), new DummyProfileDefinition("foo", "foo1", false));
96 underTest.initialize();
98 assertThat(underTest.get())
99 .extracting(BuiltInQProfile::getLanguage, BuiltInQProfile::isDefault)
100 .containsExactly(tuple(FOO_LANGUAGE.getKey(), true));
104 public void initialize_makes_single_profile_of_a_language_default_even_if_flagged_as_so() {
105 BuiltInQProfileRepository underTest = new BuiltInQProfileRepositoryImpl(dbClient, new Languages(FOO_LANGUAGE), new DummyProfileDefinition("foo", "foo1", true));
107 underTest.initialize();
109 assertThat(underTest.get())
110 .extracting(BuiltInQProfile::getLanguage, BuiltInQProfile::isDefault)
111 .containsExactly(tuple(FOO_LANGUAGE.getKey(), true));
115 public void initialize_makes_first_profile_of_a_language_default_when_none_flagged_as_so() {
116 List<DummyProfileDefinition> definitions = new ArrayList<>(
117 asList(new DummyProfileDefinition("foo", "foo1", false), new DummyProfileDefinition("foo", "foo2", false)));
118 Collections.shuffle(definitions);
119 String firstName = definitions.get(0).getName();
120 String secondName = definitions.get(1).getName();
121 BuiltInQProfileRepository underTest = new BuiltInQProfileRepositoryImpl(dbClient, new Languages(FOO_LANGUAGE), definitions.toArray(new BuiltInQualityProfilesDefinition[0]));
123 underTest.initialize();
125 assertThat(underTest.get())
126 .extracting(BuiltInQProfile::getName, BuiltInQProfile::isDefault)
127 .containsExactlyInAnyOrder(tuple(firstName, true), tuple(secondName, false));
131 public void initialize_fails_with_ISE_when_two_profiles_with_different_name_are_default_for_the_same_language() {
132 BuiltInQProfileRepository underTest = new BuiltInQProfileRepositoryImpl(dbClient, new Languages(FOO_LANGUAGE),
133 new DummyProfileDefinition("foo", "foo1", true), new DummyProfileDefinition("foo", "foo2", true));
135 expectedException.expect(IllegalStateException.class);
136 expectedException.expectMessage("Several Quality profiles are flagged as default for the language foo: [foo1, foo2]");
138 underTest.initialize();
142 public void initialize_creates_profile_Sonar_Way_as_default_if_none_other_is_defined_default_for_a_given_language() {
143 BuiltInQProfileRepository underTest = new BuiltInQProfileRepositoryImpl(
144 dbClient, new Languages(FOO_LANGUAGE),
145 new DummyProfileDefinition("foo", "doh", false), new DummyProfileDefinition("foo", "boo", false),
146 new DummyProfileDefinition("foo", SONAR_WAY_QP_NAME, false), new DummyProfileDefinition("foo", "goo", false));
148 underTest.initialize();
150 assertThat(underTest.get())
151 .filteredOn(b -> FOO_LANGUAGE.getKey().equals(b.getLanguage()))
152 .filteredOn(BuiltInQProfile::isDefault)
153 .extracting(BuiltInQProfile::getName)
154 .containsExactly(SONAR_WAY_QP_NAME);
158 public void initialize_does_not_create_Sonar_Way_as_default_if_other_profile_is_defined_as_default() {
159 BuiltInQProfileRepository underTest = new BuiltInQProfileRepositoryImpl(
160 dbClient, new Languages(FOO_LANGUAGE),
161 new DummyProfileDefinition("foo", SONAR_WAY_QP_NAME, false), new DummyProfileDefinition("foo", "goo", true));
163 underTest.initialize();
165 assertThat(underTest.get())
166 .filteredOn(b -> FOO_LANGUAGE.getKey().equals(b.getLanguage()))
167 .filteredOn(BuiltInQProfile::isDefault)
168 .extracting(BuiltInQProfile::getName)
169 .containsExactly("goo");
173 public void initialize_matches_Sonar_Way_default_with_case_sensitivity() {
174 String sonarWayInOtherCase = SONAR_WAY_QP_NAME.toUpperCase();
175 BuiltInQProfileRepository underTest = new BuiltInQProfileRepositoryImpl(
176 dbClient, new Languages(FOO_LANGUAGE),
177 new DummyProfileDefinition("foo", "goo", false), new DummyProfileDefinition("foo", sonarWayInOtherCase, false));
179 underTest.initialize();
181 assertThat(underTest.get())
182 .filteredOn(b -> FOO_LANGUAGE.getKey().equals(b.getLanguage()))
183 .filteredOn(BuiltInQProfile::isDefault)
184 .extracting(BuiltInQProfile::getName)
185 .containsExactly("goo");
188 private static final class DummyProfileDefinition implements BuiltInQualityProfilesDefinition {
189 private final String language;
190 private final String name;
191 private final boolean defaultProfile;
193 private DummyProfileDefinition(String language, String name, boolean defaultProfile) {
194 this.language = language;
196 this.defaultProfile = defaultProfile;
200 public void define(Context context) {
201 context.createBuiltInQualityProfile(name, language)
202 .setDefault(defaultProfile).done();