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.ws;
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;
29 import static org.assertj.core.api.Assertions.assertThat;
30 import static org.sonar.server.language.LanguageTesting.newLanguage;
32 public class QProfileReferenceTest {
35 public ExpectedException expectedException = ExpectedException.none();
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");
45 public void getLanguage_throws_ISE_on_reference_by_key() {
46 QProfileReference ref = QProfileReference.fromKey("foo");
48 expectedException.expect(IllegalStateException.class);
49 expectedException.expectMessage("Language is not defined. Please call hasKey().");
54 public void getName_throws_ISE_on_reference_by_key() {
55 QProfileReference ref = QProfileReference.fromKey("foo");
57 expectedException.expect(IllegalStateException.class);
58 expectedException.expectMessage("Name is not defined. Please call hasKey().");
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");
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");
81 public void getKey_throws_ISE_on_reference_by_name() {
82 QProfileReference ref = QProfileReference.fromName(null, "js", "Sonar way");
84 expectedException.expect(IllegalStateException.class);
85 expectedException.expectMessage("Key is not defined. Please call hasKey().");
91 public void getOrganization_throws_ISE_on_reference_by_key() {
92 QProfileReference ref = QProfileReference.fromKey("foo");
94 expectedException.expect(IllegalStateException.class);
95 expectedException.expectMessage("Organization is not defined. Please call hasKey().");
97 ref.getOrganizationKey();
101 public void from_reads_request_parameters_and_creates_reference_by_key() {
102 SimpleGetRequest req = new SimpleGetRequest();
103 req.setParam("key", "foo");
105 QProfileReference ref = QProfileReference.from(req);
106 assertThat(ref.getKey()).isEqualTo("foo");
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");
115 QProfileReference ref = QProfileReference.from(req);
116 assertThat(ref.getOrganizationKey()).isEmpty();
117 assertThat(ref.getLanguage()).isEqualTo("js");
118 assertThat(ref.getName()).isEqualTo("Sonar way");
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");
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");
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");
139 expectedException.expect(IllegalArgumentException.class);
140 expectedException.expectMessage("If 'key' is not specified, 'qualityProfile' and 'language' must be set");
142 QProfileReference.from(req);
146 public void throw_IAE_if_request_does_not_define_ref() {
147 SimpleGetRequest req = new SimpleGetRequest();
149 expectedException.expect(IllegalArgumentException.class);
150 QProfileReference.from(req);
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) -> {
160 Languages languages = new Languages(newLanguage("java"), newLanguage("js"));
161 QProfileReference.defineParams(newAction, languages);
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();
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");
178 assertThat(key1.equals(key1)).isTrue();
179 assertThat(key1.equals(key1bis)).isTrue();
180 assertThat(key1.equals(key2)).isFalse();
181 assertThat(key1.equals(name)).isFalse();
183 assertThat(key1.hashCode()).isEqualTo(key1.hashCode());
184 assertThat(key1.hashCode()).isEqualTo(key1bis.hashCode());
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");
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();
201 assertThat(name1.hashCode()).isEqualTo(name1.hashCode());
202 assertThat(name1.hashCode()).isEqualTo(name1bis.hashCode());