]> source.dussan.org Git - sonarqube.git/blob
1fdb8c069ae8baa18a18e772fee9e058f8d972f3
[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.ws;
21
22 import org.junit.Rule;
23 import org.junit.Test;
24 import org.junit.rules.ExpectedException;
25 import org.sonar.api.impl.ws.SimpleGetRequest;
26 import org.sonar.api.resources.Languages;
27 import org.sonar.api.server.ws.WebService;
28
29 import static org.assertj.core.api.Assertions.assertThat;
30 import static org.sonar.server.language.LanguageTesting.newLanguage;
31
32 public class QProfileReferenceTest {
33
34   @Rule
35   public ExpectedException expectedException = ExpectedException.none();
36
37   @Test
38   public void fromKey_creates_reference_by_key() {
39     QProfileReference ref = QProfileReference.fromKey("foo");
40     assertThat(ref.hasKey()).isTrue();
41     assertThat(ref.getKey()).isEqualTo("foo");
42   }
43
44   @Test
45   public void getLanguage_throws_ISE_on_reference_by_key() {
46     QProfileReference ref = QProfileReference.fromKey("foo");
47
48     expectedException.expect(IllegalStateException.class);
49     expectedException.expectMessage("Language is not defined. Please call hasKey().");
50     ref.getLanguage();
51   }
52
53   @Test
54   public void getName_throws_ISE_on_reference_by_key() {
55     QProfileReference ref = QProfileReference.fromKey("foo");
56
57     expectedException.expect(IllegalStateException.class);
58     expectedException.expectMessage("Name is not defined. Please call hasKey().");
59     ref.getName();
60   }
61
62   @Test
63   public void fromName_creates_reference_by_name_on_default_organization() {
64     QProfileReference ref = QProfileReference.fromName(null, "js", "Sonar way");
65     assertThat(ref.hasKey()).isFalse();
66     assertThat(ref.getOrganizationKey()).isEmpty();
67     assertThat(ref.getLanguage()).isEqualTo("js");
68     assertThat(ref.getName()).isEqualTo("Sonar way");
69   }
70
71   @Test
72   public void fromName_creates_reference_by_name_on_specified_organization() {
73     QProfileReference ref = QProfileReference.fromName("my-org", "js", "Sonar way");
74     assertThat(ref.hasKey()).isFalse();
75     assertThat(ref.getOrganizationKey()).hasValue("my-org");
76     assertThat(ref.getLanguage()).isEqualTo("js");
77     assertThat(ref.getName()).isEqualTo("Sonar way");
78   }
79
80   @Test
81   public void getKey_throws_ISE_on_reference_by_name() {
82     QProfileReference ref = QProfileReference.fromName(null, "js", "Sonar way");
83
84     expectedException.expect(IllegalStateException.class);
85     expectedException.expectMessage("Key is not defined. Please call hasKey().");
86
87     ref.getKey();
88   }
89
90   @Test
91   public void getOrganization_throws_ISE_on_reference_by_key() {
92     QProfileReference ref = QProfileReference.fromKey("foo");
93
94     expectedException.expect(IllegalStateException.class);
95     expectedException.expectMessage("Organization is not defined. Please call hasKey().");
96
97     ref.getOrganizationKey();
98   }
99
100   @Test
101   public void from_reads_request_parameters_and_creates_reference_by_key() {
102     SimpleGetRequest req = new SimpleGetRequest();
103     req.setParam("key", "foo");
104
105     QProfileReference ref = QProfileReference.from(req);
106     assertThat(ref.getKey()).isEqualTo("foo");
107   }
108
109   @Test
110   public void from_reads_request_parameters_and_creates_reference_by_name_on_default_organization() {
111     SimpleGetRequest req = new SimpleGetRequest();
112     req.setParam("language", "js");
113     req.setParam("qualityProfile", "Sonar way");
114
115     QProfileReference ref = QProfileReference.from(req);
116     assertThat(ref.getOrganizationKey()).isEmpty();
117     assertThat(ref.getLanguage()).isEqualTo("js");
118     assertThat(ref.getName()).isEqualTo("Sonar way");
119   }
120
121   @Test
122   public void from_reads_request_parameters_and_creates_reference_by_name_on_specified_organization() {
123     SimpleGetRequest req = new SimpleGetRequest();
124     req.setParam("organization", "my-org");
125     req.setParam("language", "js");
126     req.setParam("qualityProfile", "Sonar way");
127
128     QProfileReference ref = QProfileReference.from(req);
129     assertThat(ref.getOrganizationKey()).hasValue("my-org");
130     assertThat(ref.getLanguage()).isEqualTo("js");
131     assertThat(ref.getName()).isEqualTo("Sonar way");
132   }
133
134   @Test
135   public void from_reads_request_parameters_and_throws_IAE_if_language_is_missing() {
136     SimpleGetRequest req = new SimpleGetRequest();
137     req.setParam("profileName", "the name");
138
139     expectedException.expect(IllegalArgumentException.class);
140     expectedException.expectMessage("If 'key' is not specified, 'qualityProfile' and 'language' must be set");
141
142     QProfileReference.from(req);
143   }
144
145   @Test
146   public void throw_IAE_if_request_does_not_define_ref() {
147     SimpleGetRequest req = new SimpleGetRequest();
148
149     expectedException.expect(IllegalArgumentException.class);
150     QProfileReference.from(req);
151   }
152
153   @Test
154   public void define_ws_parameters() {
155     WebService.Context context = new WebService.Context();
156     WebService.NewController controller = context.createController("api/qualityprofiles");
157     WebService.NewAction newAction = controller.createAction("do").setHandler((request, response) -> {
158     });
159
160     Languages languages = new Languages(newLanguage("java"), newLanguage("js"));
161     QProfileReference.defineParams(newAction, languages);
162
163     controller.done();
164     WebService.Action action = context.controller("api/qualityprofiles").action("do");
165     assertThat(action.param("language")).isNotNull();
166     assertThat(action.param("language").possibleValues()).containsOnly("java", "js");
167     assertThat(action.param("key")).isNotNull();
168     assertThat(action.param("qualityProfile")).isNotNull();
169   }
170
171   @Test
172   public void test_equals_and_hashCode_of_key_ref() {
173     QProfileReference key1 = QProfileReference.fromKey("one");
174     QProfileReference key1bis = QProfileReference.fromKey("one");
175     QProfileReference key2 = QProfileReference.fromKey("two");
176     QProfileReference name = QProfileReference.fromName("my-org", "js", "one");
177
178     assertThat(key1.equals(key1)).isTrue();
179     assertThat(key1.equals(key1bis)).isTrue();
180     assertThat(key1.equals(key2)).isFalse();
181     assertThat(key1.equals(name)).isFalse();
182
183     assertThat(key1.hashCode()).isEqualTo(key1.hashCode());
184     assertThat(key1.hashCode()).isEqualTo(key1bis.hashCode());
185   }
186
187   @Test
188   public void test_equals_and_hashCode_of_name_ref() {
189     QProfileReference name1 = QProfileReference.fromName("org1", "js", "one");
190     QProfileReference name1bis = QProfileReference.fromName("org1", "js", "one");
191     QProfileReference name2 = QProfileReference.fromName("org1", "js", "two");
192     QProfileReference name1OtherLang = QProfileReference.fromName("org1", "java", "one");
193     QProfileReference key = QProfileReference.fromKey("one");
194
195     assertThat(name1.equals(name1)).isTrue();
196     assertThat(name1.equals(name1bis)).isTrue();
197     assertThat(name1.equals(name2)).isFalse();
198     assertThat(name1.equals(name1OtherLang)).isFalse();
199     assertThat(name1.equals(key)).isFalse();
200
201     assertThat(name1.hashCode()).isEqualTo(name1.hashCode());
202     assertThat(name1.hashCode()).isEqualTo(name1bis.hashCode());
203   }
204
205 }