/
PluginWSCommonsTest.java
239 lines (210 loc) · 8.48 KB
/
PluginWSCommonsTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
* SonarQube, open source software quality management tool.
* Copyright (C) 2008-2014 SonarSource
* mailto:contact AT sonarsource DOT com
*
* SonarQube is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* SonarQube is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
package org.sonar.server.plugins.ws;
import org.junit.Test;
import org.sonar.api.utils.text.JsonWriter;
import org.sonar.core.platform.PluginInfo;
import org.sonar.server.ws.WsTester;
import org.sonar.updatecenter.common.Plugin;
import org.sonar.updatecenter.common.PluginUpdate;
import org.sonar.updatecenter.common.Release;
import org.sonar.updatecenter.common.Version;
import static org.assertj.core.api.Assertions.assertThat;
import static org.sonar.api.utils.DateUtils.parseDate;
import static org.sonar.server.plugins.ws.PluginWSCommons.toJSon;
import static org.sonar.test.JsonAssert.assertJson;
import static org.sonar.updatecenter.common.PluginUpdate.Status.COMPATIBLE;
import static org.sonar.updatecenter.common.PluginUpdate.Status.DEPENDENCIES_REQUIRE_SONAR_UPGRADE;
import static org.sonar.updatecenter.common.PluginUpdate.Status.INCOMPATIBLE;
import static org.sonar.updatecenter.common.PluginUpdate.Status.REQUIRE_SONAR_UPGRADE;
public class PluginWSCommonsTest {
WsTester.TestResponse response = new WsTester.TestResponse();
JsonWriter jsonWriter = response.newJsonWriter();
PluginWSCommons underTest = new PluginWSCommons();
@Test
public void verify_properties_written_by_writePluginMetadata() {
underTest.writePluginMetadata(jsonWriter, gitPluginInfo());
jsonWriter.close();
assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo("{" +
" \"key\": \"scmgit\"," +
" \"name\": \"Git\"," +
" \"description\": \"Git SCM Provider.\"," +
" \"version\": \"1.0\"," +
" \"license\": \"GNU LGPL 3\"," +
" \"organizationName\": \"SonarSource\"," +
" \"organizationUrl\": \"http://www.sonarsource.com\"," +
" \"homepageUrl\": \"http://redirect.sonarsource.com/plugins/scmgit.html\"," +
" \"issueTrackerUrl\": \"http://jira.codehaus.org/browse/SONARSCGIT\"" +
"}");
}
@Test
public void verify_properties_written_by_writeMetadata() {
jsonWriter.beginObject();
underTest.writeMetadata(jsonWriter, gitPluginInfo());
jsonWriter.endObject();
jsonWriter.close();
assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo("{" +
" \"key\": \"scmgit\"," +
" \"name\": \"Git\"," +
" \"description\": \"Git SCM Provider.\"," +
" \"version\": \"1.0\"," +
" \"license\": \"GNU LGPL 3\"," +
" \"organizationName\": \"SonarSource\"," +
" \"organizationUrl\": \"http://www.sonarsource.com\"," +
" \"homepageUrl\": \"http://redirect.sonarsource.com/plugins/scmgit.html\"," +
" \"issueTrackerUrl\": \"http://jira.codehaus.org/browse/SONARSCGIT\"," +
"}");
}
@Test
public void verify_properties_written_by_writePluginUpdate() {
underTest.writePluginUpdate(jsonWriter, PluginUpdate.createForPluginRelease(newRelease(), version("1.0")));
jsonWriter.close();
assertJson(response.outputAsString()).isSimilarTo("{" +
" \"key\": \"p_key\"," +
" \"name\": \"p_name\"," +
" \"description\": \"p_description\"," +
" \"category\": \"p_category\"," +
" \"license\": \"p_license\"," +
" \"organizationName\": \"p_orga_name\"," +
" \"organizationUrl\": \"p_orga_url\"," +
" \"termsAndConditionsUrl\": \"p_t_and_c_url\"" +
" \"release\": {" +
" \"version\": \"1.0\"," +
" \"date\": \"2015-04-16\"" +
" }" +
"}");
}
@Test
public void verify_properties_written_by_writeMetadata_from_plugin() {
jsonWriter.beginObject();
underTest.writeMetadata(jsonWriter, newPlugin());
jsonWriter.endObject();
jsonWriter.close();
assertJson(response.outputAsString()).isSimilarTo("{" +
" \"key\": \"p_key\"," +
" \"name\": \"p_name\"," +
" \"description\": \"p_description\"," +
" \"category\": \"p_category\"," +
" \"license\": \"p_license\"," +
" \"organizationName\": \"p_orga_name\"," +
" \"organizationUrl\": \"p_orga_url\"," +
" \"termsAndConditionsUrl\": \"p_t_and_c_url\"" +
"}");
}
@Test
public void writeRelease() {
jsonWriter.beginObject();
underTest.writeRelease(jsonWriter, newRelease());
jsonWriter.endObject();
jsonWriter.close();
assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo("{" +
" \"release\": {" +
" \"version\": \"1.0\"," +
" \"date\": \"2015-04-16\"," +
" \"description\": \"release description\"," +
" \"changeLogUrl\": \"http://change.org/plugin\"" +
" }" +
"}");
}
@Test
public void writeArtifact_from_release_writes_artifact_object_and_file_name() {
jsonWriter.beginObject();
underTest.writeArtifact(jsonWriter, release("p_key").setDownloadUrl("http://toto.com/file.jar"));
jsonWriter.endObject();
jsonWriter.close();
assertJson(response.outputAsString()).setStrictArrayOrder(true).isSimilarTo("{" +
" \"artifact\": {" +
" \"name\": \"file.jar\"," +
" \"url\": \"http://toto.com/file.jar\"" +
" }" +
"}");
}
@Test
public void status_COMPATIBLE_is_displayed_COMPATIBLE_in_JSON() {
assertThat(toJSon(COMPATIBLE)).isEqualTo("COMPATIBLE");
}
@Test
public void status_INCOMPATIBLE_is_displayed_INCOMPATIBLE_in_JSON() {
assertThat(toJSon(INCOMPATIBLE)).isEqualTo("INCOMPATIBLE");
}
@Test
public void status_REQUIRE_SONAR_UPGRADE_is_displayed_REQUIRES_UPGRADE_in_JSON() {
assertThat(toJSon(REQUIRE_SONAR_UPGRADE)).isEqualTo("REQUIRES_SYSTEM_UPGRADE");
}
@Test
public void status_DEPENDENCIES_REQUIRE_SONAR_UPGRADE_is_displayed_DEPS_REQUIRE_SYSTEM_UPGRADE_in_JSON() {
assertThat(toJSon(DEPENDENCIES_REQUIRE_SONAR_UPGRADE)).isEqualTo("DEPS_REQUIRE_SYSTEM_UPGRADE");
}
@Test
public void writeUpdate_renders_key_name_and_description_of_requirements() {
PluginUpdate pluginUpdate = new PluginUpdate();
pluginUpdate.setRelease(
new Release(newPlugin(), version("1.0")).addOutgoingDependency(newRelease())
);
jsonWriter.beginObject();
underTest.writeUpdate(jsonWriter, pluginUpdate);
jsonWriter.endObject();
jsonWriter.close();
assertJson(response.outputAsString()).isSimilarTo("{" +
" \"update\": {" +
" \"requires\": [" +
" {" +
" \"key\": \"p_key\"," +
" \"name\": \"p_name\"," +
" \"description\": \"p_description\"" +
" }" +
" ]" +
" }" +
"}");
}
private static Version version(String version) {
return Version.create(version);
}
private static Release release(String key) {
return new Release(new Plugin(key), version("1.0"));
}
private PluginInfo gitPluginInfo() {
return new PluginInfo("scmgit")
.setName("Git")
.setDescription("Git SCM Provider.")
.setVersion(Version.create("1.0"))
.setLicense("GNU LGPL 3")
.setOrganizationName("SonarSource")
.setOrganizationUrl("http://www.sonarsource.com")
.setHomepageUrl("http://redirect.sonarsource.com/plugins/scmgit.html")
.setIssueTrackerUrl("http://jira.codehaus.org/browse/SONARSCGIT");
}
private Plugin newPlugin() {
return new Plugin("p_key")
.setName("p_name")
.setCategory("p_category")
.setDescription("p_description")
.setLicense("p_license")
.setOrganization("p_orga_name")
.setOrganizationUrl("p_orga_url")
.setTermsConditionsUrl("p_t_and_c_url");
}
private Release newRelease() {
return new Release(newPlugin(), version("1.0")).setDate(parseDate("2015-04-16"))
.setDownloadUrl("http://toto.com/file.jar")
.setDescription("release description")
.setChangelogUrl("http://change.org/plugin");
}
}