Support of spring initializr meta-data v2.1

Update the `init` command to support the latest meta-data format. Recent
Spring Initializr version also supports Spring Boot CLI now and generates
a textual service capabilities when requested. The command no longer
generates the capabilities of the service unless said service does not
support it.

Closes gh-2515
This commit is contained in:
Stephane Nicoll
2015-02-18 17:38:13 +01:00
parent 9af30450c4
commit 9d0e50c6ac
8 changed files with 345 additions and 47 deletions

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors.
* Copyright 2012-2015 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.
@@ -48,37 +48,51 @@ public abstract class AbstractHttpClientMockTests {
protected final CloseableHttpClient http = mock(CloseableHttpClient.class);
protected void mockSuccessfulMetadataGet() throws IOException {
mockSuccessfulMetadataGet("2.0.0");
protected void mockSuccessfulMetadataTextGet() throws IOException {
mockSuccessfulMetadataGet("metadata/service-metadata-2.1.0.txt", "text/plain", true);
}
protected void mockSuccessfulMetadataGet(String version) throws IOException {
protected void mockSuccessfulMetadataGet(boolean serviceCapabilities) throws IOException {
mockSuccessfulMetadataGet("metadata/service-metadata-2.1.0.json",
"application/vnd.initializr.v2.1+json", serviceCapabilities);
}
protected void mockSuccessfulMetadataGetV2(boolean serviceCapabilities) throws IOException {
mockSuccessfulMetadataGet("metadata/service-metadata-2.0.0.json",
"application/vnd.initializr.v2+json", serviceCapabilities);
}
protected void mockSuccessfulMetadataGet(String contentPath, String contentType,
boolean serviceCapabilities) throws IOException {
CloseableHttpResponse response = mock(CloseableHttpResponse.class);
Resource resource = new ClassPathResource("metadata/service-metadata-" + version
+ ".json");
byte[] content = StreamUtils.copyToByteArray(resource.getInputStream());
mockHttpEntity(response, content, "application/vnd.initializr.v2+json");
byte[] content = readClasspathResource(contentPath);
mockHttpEntity(response, content, contentType);
mockStatus(response, 200);
given(this.http.execute(argThat(getForJsonMetadata()))).willReturn(response);
given(this.http.execute(argThat(getForMetadata(serviceCapabilities)))).willReturn(response);
}
protected byte[] readClasspathResource(String contentPath) throws IOException {
Resource resource = new ClassPathResource(contentPath);
return StreamUtils.copyToByteArray(resource.getInputStream());
}
protected void mockSuccessfulProjectGeneration(
MockHttpProjectGenerationRequest request) throws IOException {
// Required for project generation as the metadata is read first
mockSuccessfulMetadataGet();
mockSuccessfulMetadataGet(false);
CloseableHttpResponse response = mock(CloseableHttpResponse.class);
mockHttpEntity(response, request.content, request.contentType);
mockStatus(response, 200);
String header = (request.fileName != null ? contentDispositionValue(request.fileName)
: null);
mockHttpHeader(response, "Content-Disposition", header);
given(this.http.execute(argThat(getForNonJsonMetadata()))).willReturn(response);
given(this.http.execute(argThat(getForNonMetadata()))).willReturn(response);
}
protected void mockProjectGenerationError(int status, String message)
throws IOException {
// Required for project generation as the metadata is read first
mockSuccessfulMetadataGet();
mockSuccessfulMetadataGet(false);
CloseableHttpResponse response = mock(CloseableHttpResponse.class);
mockHttpEntity(response, createJsonError(status, message).getBytes(),
"application/json");
@@ -122,12 +136,17 @@ public abstract class AbstractHttpClientMockTests {
given(response.getFirstHeader(headerName)).willReturn(header);
}
protected Matcher<HttpGet> getForJsonMetadata() {
return new HasAcceptHeader("application/vnd.initializr.v2+json", true);
private Matcher<HttpGet> getForMetadata(boolean serviceCapabilities) {
if (serviceCapabilities) {
return new HasAcceptHeader(InitializrService.ACCEPT_SERVICE_CAPABILITIES, true);
}
else {
return new HasAcceptHeader(InitializrService.ACCEPT_META_DATA, true);
}
}
protected Matcher<HttpGet> getForNonJsonMetadata() {
return new HasAcceptHeader("application/vnd.initializr.v2+json", false);
private Matcher<HttpGet> getForNonMetadata() {
return new HasAcceptHeader(InitializrService.ACCEPT_META_DATA, false);
}
private String contentDispositionValue(String fileName) {

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors.
* Copyright 2012-2015 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.
@@ -73,9 +73,21 @@ public class InitCommandTests extends AbstractHttpClientMockTests {
this.command = new InitCommand(this.handler);
}
@Test
public void listServiceCapabilitiesText() throws Exception {
mockSuccessfulMetadataTextGet();
this.command.run("--list", "--target=http://fake-service");
}
@Test
public void listServiceCapabilities() throws Exception {
mockSuccessfulMetadataGet();
mockSuccessfulMetadataGet(true);
this.command.run("--list", "--target=http://fake-service");
}
@Test
public void listServiceCapabilitiesV2() throws Exception {
mockSuccessfulMetadataGetV2(true);
this.command.run("--list", "--target=http://fake-service");
}

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors.
* Copyright 2012-2015 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.
@@ -46,7 +46,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
@Test
public void loadMetadata() throws IOException {
mockSuccessfulMetadataGet();
mockSuccessfulMetadataGet(false);
InitializrServiceMetadata metadata = this.invoker.loadMetadata("http://foo/bar");
assertNotNull(metadata);
}
@@ -101,7 +101,7 @@ public class InitializrServiceTests extends AbstractHttpClientMockTests {
@Test
public void generateProjectNoContent() throws IOException {
mockSuccessfulMetadataGet();
mockSuccessfulMetadataGet(false);
CloseableHttpResponse response = mock(CloseableHttpResponse.class);
mockStatus(response, 500);
when(this.http.execute(isA(HttpGet.class))).thenReturn(response);

View File

@@ -1,5 +1,5 @@
/*
* Copyright 2012-2014 the original author or authors.
* Copyright 2012-2015 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.
@@ -20,7 +20,9 @@ import java.io.IOException;
import org.junit.Test;
import static org.junit.Assert.assertTrue;
import static org.hamcrest.CoreMatchers.equalTo;
import static org.hamcrest.core.StringContains.containsString;
import static org.junit.Assert.assertThat;
/**
* Tests for {@link ServiceCapabilitiesReportGenerator}
@@ -33,13 +35,31 @@ public class ServiceCapabilitiesReportGeneratorTests extends AbstractHttpClientM
new InitializrService(this.http));
@Test
public void listMetadata() throws IOException {
mockSuccessfulMetadataGet();
public void listMetadataFromServer() throws IOException {
mockSuccessfulMetadataTextGet();
String expected = new String(readClasspathResource("metadata/service-metadata-2.1.0.txt"));
String content = this.command.generate("http://localhost");
assertTrue(content.contains("aop - AOP"));
assertTrue(content.contains("security - Security: Security description"));
assertTrue(content.contains("type: maven-project"));
assertTrue(content.contains("packaging: jar"));
assertThat(content, equalTo(expected));
}
@Test
public void listMetadata() throws IOException {
mockSuccessfulMetadataGet(true);
doTestGenerateCapabilitiesFromJson();
}
@Test
public void listMetadataV2() throws IOException {
mockSuccessfulMetadataGetV2(true);
doTestGenerateCapabilitiesFromJson();
}
private void doTestGenerateCapabilitiesFromJson() throws IOException {
String content = this.command.generate("http://localhost");
assertThat(content, containsString("aop - AOP"));
assertThat(content, containsString("security - Security: Security description"));
assertThat(content, containsString("type: maven-project"));
assertThat(content, containsString("packaging: jar"));
}
}