-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
AbstractInitializrIntegrationTests.java
executable file
·315 lines (273 loc) · 9.8 KB
/
AbstractInitializrIntegrationTests.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
/*
* Copyright 2012-2018 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.spring.initializr.web;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.spring.initializr.metadata.InitializrMetadata;
import io.spring.initializr.metadata.InitializrMetadataBuilder;
import io.spring.initializr.metadata.InitializrMetadataProvider;
import io.spring.initializr.metadata.InitializrProperties;
import io.spring.initializr.test.generator.ProjectAssert;
import io.spring.initializr.web.AbstractInitializrIntegrationTests.Config;
import io.spring.initializr.web.mapper.InitializrMetadataVersion;
import io.spring.initializr.web.support.DefaultInitializrMetadataProvider;
import org.apache.tools.ant.Project;
import org.apache.tools.ant.taskdefs.Expand;
import org.apache.tools.ant.taskdefs.Untar;
import org.json.JSONException;
import org.json.JSONObject;
import org.junit.Before;
import org.junit.Rule;
import org.junit.rules.TemporaryFolder;
import org.junit.runner.RunWith;
import org.skyscreamer.jsonassert.JSONAssert;
import org.skyscreamer.jsonassert.JSONCompareMode;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.core.io.ClassPathResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.StreamUtils;
import org.springframework.web.client.RestTemplate;
import static org.assertj.core.api.Assertions.assertThat;
/**
* @author Stephane Nicoll
*/
@RunWith(SpringRunner.class)
@SpringBootTest(classes = Config.class)
public abstract class AbstractInitializrIntegrationTests {
protected static final MediaType CURRENT_METADATA_MEDIA_TYPE = InitializrMetadataVersion.V2_1
.getMediaType();
private static final ObjectMapper objectMapper = new ObjectMapper();
@Rule
public final TemporaryFolder folder = new TemporaryFolder();
@Autowired
private RestTemplateBuilder restTemplateBuilder;
private RestTemplate restTemplate;
@Before
public void before() {
this.restTemplate = this.restTemplateBuilder.build();
}
protected abstract String createUrl(String context);
protected String htmlHome() {
HttpHeaders headers = new HttpHeaders();
headers.setAccept(Collections.singletonList(MediaType.TEXT_HTML));
return this.restTemplate.exchange(createUrl("/"), HttpMethod.GET,
new HttpEntity<Void>(headers), String.class).getBody();
}
/**
* Validate the "Content-Type" header of the specified response.
* @param response the response
* @param expected the expected result
*/
protected void validateContentType(ResponseEntity<String> response,
MediaType expected) {
MediaType actual = response.getHeaders().getContentType();
assertThat(actual).isNotNull();
assertThat(actual.isCompatibleWith(expected))
.as("Non compatible media-type, expected " + expected + ", got " + actual)
.isTrue();
}
protected JsonNode parseJson(String text) {
try {
return objectMapper.readTree(text);
}
catch (IOException ex) {
throw new IllegalArgumentException("Invalid json", ex);
}
}
protected void validateMetadata(ResponseEntity<String> response, MediaType mediaType,
String version, JSONCompareMode compareMode) {
try {
validateContentType(response, mediaType);
JSONObject json = new JSONObject(response.getBody());
JSONObject expected = readMetadataJson(version);
JSONAssert.assertEquals(expected, json, compareMode);
}
catch (JSONException ex) {
throw new IllegalArgumentException("Invalid json", ex);
}
}
protected void validateCurrentMetadata(ResponseEntity<String> response) {
validateContentType(response, CURRENT_METADATA_MEDIA_TYPE);
validateCurrentMetadata(response.getBody());
}
protected void validateCurrentMetadata(String json) {
try {
JSONObject expected = readMetadataJson("2.1.0");
JSONAssert.assertEquals(expected, new JSONObject(json),
JSONCompareMode.STRICT);
}
catch (JSONException ex) {
throw new IllegalArgumentException("Invalid json", ex);
}
}
private JSONObject readMetadataJson(String version) {
return readJsonFrom("metadata/test-default-" + version + ".json");
}
/**
* Return a {@link ProjectAssert} for the following archive content.
* @param content the source content
* @return a project assert
*/
protected ProjectAssert zipProjectAssert(byte[] content) {
return projectAssert(content, ArchiveType.ZIP);
}
/**
* Return a {@link ProjectAssert} for the following TGZ archive.
* @param content the source content
* @return a project assert
*/
protected ProjectAssert tgzProjectAssert(byte[] content) {
return projectAssert(content, ArchiveType.TGZ);
}
protected ProjectAssert downloadZip(String context) {
byte[] body = downloadArchive(context);
return zipProjectAssert(body);
}
protected ProjectAssert downloadTgz(String context) {
byte[] body = downloadArchive(context);
return tgzProjectAssert(body);
}
protected byte[] downloadArchive(String context) {
return this.restTemplate.getForObject(createUrl(context), byte[].class);
}
protected ResponseEntity<String> invokeHome(String userAgentHeader,
String... acceptHeaders) {
return execute("/", String.class, userAgentHeader, acceptHeaders);
}
protected <T> ResponseEntity<T> execute(String contextPath, Class<T> responseType,
String userAgentHeader, String... acceptHeaders) {
HttpHeaders headers = new HttpHeaders();
if (userAgentHeader != null) {
headers.set("User-Agent", userAgentHeader);
}
if (acceptHeaders != null) {
List<MediaType> mediaTypes = new ArrayList<>();
for (String acceptHeader : acceptHeaders) {
mediaTypes.add(MediaType.parseMediaType(acceptHeader));
}
headers.setAccept(mediaTypes);
}
else {
headers.setAccept(Collections.emptyList());
}
return this.restTemplate.exchange(createUrl(contextPath), HttpMethod.GET,
new HttpEntity<Void>(headers), responseType);
}
protected ProjectAssert projectAssert(byte[] content, ArchiveType archiveType) {
try {
File archiveFile = writeArchive(content);
File project = this.folder.newFolder();
switch (archiveType) {
case ZIP:
unzip(archiveFile, project);
break;
case TGZ:
untar(archiveFile, project);
break;
}
return new ProjectAssert(project);
}
catch (Exception ex) {
throw new IllegalStateException("Cannot unpack archive", ex);
}
}
private void untar(File archiveFile, File project) {
Untar expand = new Untar();
expand.setProject(new Project());
expand.setDest(project);
expand.setSrc(archiveFile);
Untar.UntarCompressionMethod method = new Untar.UntarCompressionMethod();
method.setValue("gzip");
expand.setCompression(method);
expand.execute();
}
private void unzip(File archiveFile, File project) {
Expand expand = new Expand();
expand.setProject(new Project());
expand.setDest(project);
expand.setSrc(archiveFile);
expand.execute();
}
protected File writeArchive(byte[] body) throws IOException {
File archiveFile = this.folder.newFile();
try (FileOutputStream stream = new FileOutputStream(archiveFile)) {
stream.write(body);
}
return archiveFile;
}
protected JSONObject readJsonFrom(String path) {
try {
ClassPathResource resource = new ClassPathResource(path);
try (InputStream stream = resource.getInputStream()) {
String json = StreamUtils.copyToString(stream, Charset.forName("UTF-8"));
String placeholder = "";
if (this instanceof AbstractInitializrControllerIntegrationTests) {
placeholder = ((AbstractInitializrControllerIntegrationTests) this).host;
}
if (this instanceof AbstractFullStackInitializrIntegrationTests) {
AbstractFullStackInitializrIntegrationTests test = (AbstractFullStackInitializrIntegrationTests) this;
placeholder = test.host + ":" + test.port;
}
// Let's parse the port as it is random
// TODO: put the port back somehow so it appears in stubs
String content = json.replaceAll("@host@", placeholder);
return new JSONObject(content);
}
}
catch (Exception ex) {
throw new IllegalStateException("Cannot read JSON from path=" + path);
}
}
public RestTemplate getRestTemplate() {
return this.restTemplate;
}
private enum ArchiveType {
ZIP,
TGZ
}
@EnableAutoConfiguration
public static class Config {
@Bean
public InitializrMetadataProvider initializrMetadataProvider(
InitializrProperties properties) {
return new DefaultInitializrMetadataProvider(InitializrMetadataBuilder
.fromInitializrProperties(properties).build(), new ObjectMapper(),
new RestTemplate()) {
@Override
protected void updateInitializrMetadata(InitializrMetadata metadata) {
// Disable metadata fetching from spring.io
}
};
}
}
}