]> source.dussan.org Git - sonarqube.git/blob
7a7d1a9373e72c87f7728ec72005e5db36f9e3ed
[sonarqube.git] /
1 /*
2  * SonarQube
3  * Copyright (C) 2009-2019 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.server.qualityprofile;
21
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;
35
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;
40
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";
44
45   @Rule
46   public ExpectedException expectedException = ExpectedException.none();
47   @Rule
48   public DbTester dbTester = DbTester.create(System2.INSTANCE);
49
50   private DbClient dbClient = dbTester.getDbClient();
51
52   @Test
53   public void get_throws_ISE_if_called_before_initialize() {
54     BuiltInQProfileRepositoryImpl underTest = new BuiltInQProfileRepositoryImpl(mock(DbClient.class), new Languages());
55
56     expectedException.expect(IllegalStateException.class);
57     expectedException.expectMessage("initialize must be called first");
58
59     underTest.get();
60   }
61
62   @Test
63   public void initialize_throws_ISE_if_called_twice() {
64     BuiltInQProfileRepositoryImpl underTest = new BuiltInQProfileRepositoryImpl(mock(DbClient.class), new Languages());
65     underTest.initialize();
66
67     expectedException.expect(IllegalStateException.class);
68     expectedException.expectMessage("initialize must be called only once");
69
70     underTest.initialize();
71   }
72
73   @Test
74   public void initialize_throws_ISE_if_language_has_no_builtin_qp() {
75     BuiltInQProfileRepository underTest = new BuiltInQProfileRepositoryImpl(mock(DbClient.class), new Languages(FOO_LANGUAGE));
76
77     expectedException.expect(IllegalStateException.class);
78     expectedException.expectMessage("The following languages have no built-in quality profiles: foo");
79     
80     underTest.initialize();
81   }
82
83   @Test
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));
86
87     underTest.initialize();
88
89     assertThat(underTest.get()).isEmpty();
90   }
91
92   @Test
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));
95
96     underTest.initialize();
97
98     assertThat(underTest.get())
99       .extracting(BuiltInQProfile::getLanguage, BuiltInQProfile::isDefault)
100       .containsExactly(tuple(FOO_LANGUAGE.getKey(), true));
101   }
102
103   @Test
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));
106
107     underTest.initialize();
108
109     assertThat(underTest.get())
110       .extracting(BuiltInQProfile::getLanguage, BuiltInQProfile::isDefault)
111       .containsExactly(tuple(FOO_LANGUAGE.getKey(), true));
112   }
113
114   @Test
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]));
122
123     underTest.initialize();
124
125     assertThat(underTest.get())
126       .extracting(BuiltInQProfile::getName, BuiltInQProfile::isDefault)
127       .containsExactlyInAnyOrder(tuple(firstName, true), tuple(secondName, false));
128   }
129
130   @Test
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));
134
135     expectedException.expect(IllegalStateException.class);
136     expectedException.expectMessage("Several Quality profiles are flagged as default for the language foo: [foo1, foo2]");
137
138     underTest.initialize();
139   }
140
141   @Test
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));
147
148     underTest.initialize();
149
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);
155   }
156
157   @Test
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));
162
163     underTest.initialize();
164
165     assertThat(underTest.get())
166       .filteredOn(b -> FOO_LANGUAGE.getKey().equals(b.getLanguage()))
167       .filteredOn(BuiltInQProfile::isDefault)
168       .extracting(BuiltInQProfile::getName)
169       .containsExactly("goo");
170   }
171
172   @Test
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));
178
179     underTest.initialize();
180
181     assertThat(underTest.get())
182       .filteredOn(b -> FOO_LANGUAGE.getKey().equals(b.getLanguage()))
183       .filteredOn(BuiltInQProfile::isDefault)
184       .extracting(BuiltInQProfile::getName)
185       .containsExactly("goo");
186   }
187
188   private static final class DummyProfileDefinition implements BuiltInQualityProfilesDefinition {
189     private final String language;
190     private final String name;
191     private final boolean defaultProfile;
192
193     private DummyProfileDefinition(String language, String name, boolean defaultProfile) {
194       this.language = language;
195       this.name = name;
196       this.defaultProfile = defaultProfile;
197     }
198
199     @Override
200     public void define(Context context) {
201       context.createBuiltInQualityProfile(name, language)
202         .setDefault(defaultProfile).done();
203     }
204
205     String getName() {
206       return name;
207     }
208
209   }
210
211 }