Polish CLI init command
Rename a few classes and methods and extract some logic into helper classes. Also change 2 char shortcuts to a single char. Closes gh-1751
This commit is contained in:
@@ -34,18 +34,19 @@ import org.springframework.core.io.ClassPathResource;
|
||||
import org.springframework.core.io.Resource;
|
||||
import org.springframework.util.StreamUtils;
|
||||
|
||||
import static org.mockito.BDDMockito.given;
|
||||
import static org.mockito.Matchers.argThat;
|
||||
import static org.mockito.Matchers.isA;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Abstract base class for tests that use a mock {@link CloseableHttpClient}.
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public abstract class AbstractHttpClientMockTests {
|
||||
|
||||
protected final CloseableHttpClient httpClient = mock(CloseableHttpClient.class);
|
||||
protected final CloseableHttpClient http = mock(CloseableHttpClient.class);
|
||||
|
||||
protected void mockSuccessfulMetadataGet() throws IOException {
|
||||
mockSuccessfulMetadataGet("1.1.0");
|
||||
@@ -58,34 +59,31 @@ public abstract class AbstractHttpClientMockTests {
|
||||
byte[] content = StreamUtils.copyToByteArray(resource.getInputStream());
|
||||
mockHttpEntity(response, content, "application/json");
|
||||
mockStatus(response, 200);
|
||||
when(this.httpClient.execute(argThat(getForJsonData()))).thenReturn(response);
|
||||
given(this.http.execute(argThat(getForJsonData()))).willReturn(response);
|
||||
}
|
||||
|
||||
protected void mockSuccessfulProjectGeneration(
|
||||
MockHttpProjectGenerationRequest request) throws IOException {
|
||||
// Required for project generation as the metadata is read first
|
||||
mockSuccessfulMetadataGet();
|
||||
|
||||
CloseableHttpResponse response = mock(CloseableHttpResponse.class);
|
||||
mockHttpEntity(response, request.content, request.contentType);
|
||||
mockStatus(response, 200);
|
||||
|
||||
String header = request.fileName != null ? contentDispositionValue(request.fileName)
|
||||
: null;
|
||||
String header = (request.fileName != null ? contentDispositionValue(request.fileName)
|
||||
: null);
|
||||
mockHttpHeader(response, "Content-Disposition", header);
|
||||
when(this.httpClient.execute(argThat(getForNonJsonData()))).thenReturn(response);
|
||||
given(this.http.execute(argThat(getForNonJsonData()))).willReturn(response);
|
||||
}
|
||||
|
||||
protected void mockProjectGenerationError(int status, String message)
|
||||
throws IOException {
|
||||
// Required for project generation as the metadata is read first
|
||||
mockSuccessfulMetadataGet();
|
||||
|
||||
CloseableHttpResponse response = mock(CloseableHttpResponse.class);
|
||||
mockHttpEntity(response, createJsonError(status, message).getBytes(),
|
||||
"application/json");
|
||||
mockStatus(response, status);
|
||||
when(this.httpClient.execute(isA(HttpGet.class))).thenReturn(response);
|
||||
given(this.http.execute(isA(HttpGet.class))).willReturn(response);
|
||||
}
|
||||
|
||||
protected void mockMetadataGetError(int status, String message) throws IOException {
|
||||
@@ -93,35 +91,35 @@ public abstract class AbstractHttpClientMockTests {
|
||||
mockHttpEntity(response, createJsonError(status, message).getBytes(),
|
||||
"application/json");
|
||||
mockStatus(response, status);
|
||||
when(this.httpClient.execute(isA(HttpGet.class))).thenReturn(response);
|
||||
given(this.http.execute(isA(HttpGet.class))).willReturn(response);
|
||||
}
|
||||
|
||||
protected HttpEntity mockHttpEntity(CloseableHttpResponse response, byte[] content,
|
||||
String contentType) {
|
||||
try {
|
||||
HttpEntity entity = mock(HttpEntity.class);
|
||||
when(entity.getContent()).thenReturn(new ByteArrayInputStream(content));
|
||||
given(entity.getContent()).willReturn(new ByteArrayInputStream(content));
|
||||
Header contentTypeHeader = contentType != null ? new BasicHeader(
|
||||
"Content-Type", contentType) : null;
|
||||
when(entity.getContentType()).thenReturn(contentTypeHeader);
|
||||
when(response.getEntity()).thenReturn(entity);
|
||||
given(entity.getContentType()).willReturn(contentTypeHeader);
|
||||
given(response.getEntity()).willReturn(entity);
|
||||
return entity;
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new IllegalStateException("Should not happen", e);
|
||||
catch (IOException ex) {
|
||||
throw new IllegalStateException("Should not happen", ex);
|
||||
}
|
||||
}
|
||||
|
||||
protected void mockStatus(CloseableHttpResponse response, int status) {
|
||||
StatusLine statusLine = mock(StatusLine.class);
|
||||
when(statusLine.getStatusCode()).thenReturn(status);
|
||||
when(response.getStatusLine()).thenReturn(statusLine);
|
||||
given(statusLine.getStatusCode()).willReturn(status);
|
||||
given(response.getStatusLine()).willReturn(statusLine);
|
||||
}
|
||||
|
||||
protected void mockHttpHeader(CloseableHttpResponse response, String headerName,
|
||||
String value) {
|
||||
Header header = value != null ? new BasicHeader(headerName, value) : null;
|
||||
when(response.getFirstHeader(headerName)).thenReturn(header);
|
||||
given(response.getFirstHeader(headerName)).willReturn(header);
|
||||
}
|
||||
|
||||
protected Matcher<HttpGet> getForJsonData() {
|
||||
@@ -153,6 +151,10 @@ public abstract class AbstractHttpClientMockTests {
|
||||
|
||||
byte[] content = new byte[] { 0, 0, 0, 0 };
|
||||
|
||||
public MockHttpProjectGenerationRequest(String contentType, String fileName) {
|
||||
this(contentType, fileName, new byte[] { 0, 0, 0, 0 });
|
||||
}
|
||||
|
||||
public MockHttpProjectGenerationRequest(String contentType, String fileName,
|
||||
byte[] content) {
|
||||
this.contentType = contentType;
|
||||
@@ -160,9 +162,6 @@ public abstract class AbstractHttpClientMockTests {
|
||||
this.content = content;
|
||||
}
|
||||
|
||||
public MockHttpProjectGenerationRequest(String contentType, String fileName) {
|
||||
this(contentType, fileName, new byte[] { 0, 0, 0, 0 });
|
||||
}
|
||||
}
|
||||
|
||||
private static class HasAcceptHeader extends ArgumentMatcher<HttpGet> {
|
||||
@@ -186,10 +185,7 @@ public abstract class AbstractHttpClientMockTests {
|
||||
if (this.shouldMatch) {
|
||||
return acceptHeader != null && this.value.equals(acceptHeader.getValue());
|
||||
}
|
||||
else {
|
||||
return acceptHeader == null
|
||||
|| !this.value.equals(acceptHeader.getValue());
|
||||
}
|
||||
return acceptHeader == null || !this.value.equals(acceptHeader.getValue());
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -26,7 +26,6 @@ import java.util.zip.ZipOutputStream;
|
||||
|
||||
import joptsimple.OptionSet;
|
||||
|
||||
import org.apache.http.impl.client.CloseableHttpClient;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
@@ -45,12 +44,17 @@ import static org.junit.Assert.assertTrue;
|
||||
public class InitCommandTests extends AbstractHttpClientMockTests {
|
||||
|
||||
@Rule
|
||||
public final TemporaryFolder folder = new TemporaryFolder();
|
||||
public final TemporaryFolder temporaryFolder = new TemporaryFolder();
|
||||
|
||||
private final TestableInitCommandOptionHandler handler = new TestableInitCommandOptionHandler(
|
||||
this.httpClient);
|
||||
private final TestableInitCommandOptionHandler handler;
|
||||
|
||||
private final InitCommand command = new InitCommand(this.handler);
|
||||
private final InitCommand command;
|
||||
|
||||
public InitCommandTests() {
|
||||
InitializrService initializrService = new InitializrService(this.http);
|
||||
this.handler = new TestableInitCommandOptionHandler(initializrService);
|
||||
this.command = new InitCommand(this.handler);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void listServiceCapabilities() throws Exception {
|
||||
@@ -61,160 +65,143 @@ public class InitCommandTests extends AbstractHttpClientMockTests {
|
||||
@Test
|
||||
public void generateProject() throws Exception {
|
||||
String fileName = UUID.randomUUID().toString() + ".zip";
|
||||
File f = new File(fileName);
|
||||
assertFalse("file should not exist", f.exists());
|
||||
|
||||
MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest(
|
||||
File file = new File(fileName);
|
||||
assertFalse("file should not exist", file.exists());
|
||||
MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest(
|
||||
"application/zip", fileName);
|
||||
mockSuccessfulProjectGeneration(mockHttpRequest);
|
||||
|
||||
mockSuccessfulProjectGeneration(request);
|
||||
try {
|
||||
assertEquals(ExitStatus.OK, this.command.run());
|
||||
assertTrue("file should have been created", f.exists());
|
||||
assertTrue("file should have been created", file.exists());
|
||||
}
|
||||
finally {
|
||||
assertTrue("failed to delete test file", f.delete());
|
||||
assertTrue("failed to delete test file", file.delete());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void generateProjectNoFileNameAvailable() throws Exception {
|
||||
MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest(
|
||||
MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest(
|
||||
"application/zip", null);
|
||||
mockSuccessfulProjectGeneration(mockHttpRequest);
|
||||
mockSuccessfulProjectGeneration(request);
|
||||
assertEquals(ExitStatus.ERROR, this.command.run());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void generateProjectAndExtract() throws Exception {
|
||||
File f = this.folder.newFolder();
|
||||
|
||||
File folder = this.temporaryFolder.newFolder();
|
||||
byte[] archive = createFakeZipArchive("test.txt", "Fake content");
|
||||
MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest(
|
||||
MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest(
|
||||
"application/zip", "demo.zip", archive);
|
||||
mockSuccessfulProjectGeneration(mockHttpRequest);
|
||||
|
||||
mockSuccessfulProjectGeneration(request);
|
||||
assertEquals(ExitStatus.OK,
|
||||
this.command.run("--extract", "--output=" + f.getAbsolutePath()));
|
||||
File archiveFile = new File(f, "test.txt");
|
||||
assertTrue(
|
||||
"Archive not extracted properly " + f.getAbsolutePath() + " not found",
|
||||
archiveFile.exists());
|
||||
this.command.run("--extract", "--output=" + folder.getAbsolutePath()));
|
||||
File archiveFile = new File(folder, "test.txt");
|
||||
assertTrue("Archive not extracted properly " + folder.getAbsolutePath()
|
||||
+ " not found", archiveFile.exists());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void generateProjectAndExtractUnsupportedArchive() throws Exception {
|
||||
File f = this.folder.newFolder();
|
||||
File folder = this.temporaryFolder.newFolder();
|
||||
String fileName = UUID.randomUUID().toString() + ".zip";
|
||||
File archiveFile = new File(fileName);
|
||||
assertFalse("file should not exist", archiveFile.exists());
|
||||
|
||||
File file = new File(fileName);
|
||||
assertFalse("file should not exist", file.exists());
|
||||
try {
|
||||
byte[] archive = createFakeZipArchive("test.txt", "Fake content");
|
||||
MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest(
|
||||
MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest(
|
||||
"application/foobar", fileName, archive);
|
||||
mockSuccessfulProjectGeneration(mockHttpRequest);
|
||||
|
||||
mockSuccessfulProjectGeneration(request);
|
||||
assertEquals(ExitStatus.OK,
|
||||
this.command.run("--extract", "--output=" + f.getAbsolutePath()));
|
||||
assertTrue("file should have been saved instead", archiveFile.exists());
|
||||
this.command.run("--extract", "--output=" + folder.getAbsolutePath()));
|
||||
assertTrue("file should have been saved instead", file.exists());
|
||||
}
|
||||
finally {
|
||||
assertTrue("failed to delete test file", archiveFile.delete());
|
||||
assertTrue("failed to delete test file", file.delete());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void generateProjectAndExtractUnknownContentType() throws Exception {
|
||||
File f = this.folder.newFolder();
|
||||
File folder = this.temporaryFolder.newFolder();
|
||||
String fileName = UUID.randomUUID().toString() + ".zip";
|
||||
File archiveFile = new File(fileName);
|
||||
assertFalse("file should not exist", archiveFile.exists());
|
||||
|
||||
File file = new File(fileName);
|
||||
assertFalse("file should not exist", file.exists());
|
||||
try {
|
||||
byte[] archive = createFakeZipArchive("test.txt", "Fake content");
|
||||
MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest(
|
||||
MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest(
|
||||
null, fileName, archive);
|
||||
mockSuccessfulProjectGeneration(mockHttpRequest);
|
||||
|
||||
mockSuccessfulProjectGeneration(request);
|
||||
assertEquals(ExitStatus.OK,
|
||||
this.command.run("--extract", "--output=" + f.getAbsolutePath()));
|
||||
assertTrue("file should have been saved instead", archiveFile.exists());
|
||||
this.command.run("--extract", "--output=" + folder.getAbsolutePath()));
|
||||
assertTrue("file should have been saved instead", file.exists());
|
||||
}
|
||||
finally {
|
||||
assertTrue("failed to delete test file", archiveFile.delete());
|
||||
assertTrue("failed to delete test file", file.delete());
|
||||
}
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fileNotOverwrittenByDefault() throws Exception {
|
||||
File f = this.folder.newFile();
|
||||
long fileLength = f.length();
|
||||
|
||||
MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest(
|
||||
"application/zip", f.getAbsolutePath());
|
||||
mockSuccessfulProjectGeneration(mockHttpRequest);
|
||||
|
||||
File file = this.temporaryFolder.newFile();
|
||||
long fileLength = file.length();
|
||||
MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest(
|
||||
"application/zip", file.getAbsolutePath());
|
||||
mockSuccessfulProjectGeneration(request);
|
||||
assertEquals("Should have failed", ExitStatus.ERROR, this.command.run());
|
||||
assertEquals("File should not have changed", fileLength, f.length());
|
||||
assertEquals("File should not have changed", fileLength, file.length());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void overwriteFile() throws Exception {
|
||||
File f = this.folder.newFile();
|
||||
long fileLength = f.length();
|
||||
|
||||
MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest(
|
||||
"application/zip", f.getAbsolutePath());
|
||||
mockSuccessfulProjectGeneration(mockHttpRequest);
|
||||
File file = this.temporaryFolder.newFile();
|
||||
long fileLength = file.length();
|
||||
MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest(
|
||||
"application/zip", file.getAbsolutePath());
|
||||
mockSuccessfulProjectGeneration(request);
|
||||
assertEquals("Should not have failed", ExitStatus.OK, this.command.run("--force"));
|
||||
assertTrue("File should have changed", fileLength != f.length());
|
||||
assertTrue("File should have changed", fileLength != file.length());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void fileInArchiveNotOverwrittenByDefault() throws Exception {
|
||||
File f = this.folder.newFolder();
|
||||
File conflict = new File(f, "test.txt");
|
||||
File folder = this.temporaryFolder.newFolder();
|
||||
File conflict = new File(folder, "test.txt");
|
||||
assertTrue("Should have been able to create file", conflict.createNewFile());
|
||||
long fileLength = conflict.length();
|
||||
|
||||
// also contains test.txt
|
||||
byte[] archive = createFakeZipArchive("test.txt", "Fake content");
|
||||
MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest(
|
||||
MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest(
|
||||
"application/zip", "demo.zip", archive);
|
||||
mockSuccessfulProjectGeneration(mockHttpRequest);
|
||||
|
||||
mockSuccessfulProjectGeneration(request);
|
||||
assertEquals(ExitStatus.ERROR,
|
||||
this.command.run("--extract", "--output=" + f.getAbsolutePath()));
|
||||
this.command.run("--extract", "--output=" + folder.getAbsolutePath()));
|
||||
assertEquals("File should not have changed", fileLength, conflict.length());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void overwriteFileInArchive() throws Exception {
|
||||
File f = this.folder.newFolder();
|
||||
File conflict = new File(f, "test.txt");
|
||||
File folder = this.temporaryFolder.newFolder();
|
||||
File conflict = new File(folder, "test.txt");
|
||||
assertTrue("Should have been able to create file", conflict.createNewFile());
|
||||
long fileLength = conflict.length();
|
||||
|
||||
// also contains test.txt
|
||||
byte[] archive = createFakeZipArchive("test.txt", "Fake content");
|
||||
MockHttpProjectGenerationRequest mockHttpRequest = new MockHttpProjectGenerationRequest(
|
||||
MockHttpProjectGenerationRequest request = new MockHttpProjectGenerationRequest(
|
||||
"application/zip", "demo.zip", archive);
|
||||
mockSuccessfulProjectGeneration(mockHttpRequest);
|
||||
|
||||
mockSuccessfulProjectGeneration(request);
|
||||
assertEquals(
|
||||
ExitStatus.OK,
|
||||
this.command.run("--force", "--extract",
|
||||
"--output=" + f.getAbsolutePath()));
|
||||
"--output=" + folder.getAbsolutePath()));
|
||||
assertTrue("File should have changed", fileLength != conflict.length());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parseProjectOptions() throws Exception {
|
||||
this.handler.disableProjectGeneration();
|
||||
this.command.run("-bv=1.2.0.RELEASE", "-d=web,data-jpa", "-jv=1.9", "-p=war",
|
||||
this.command.run("-b=1.2.0.RELEASE", "-d=web,data-jpa", "-j=1.9", "-p=war",
|
||||
"--build=grunt", "--format=web", "-t=ant-project");
|
||||
|
||||
assertEquals("1.2.0.RELEASE", this.handler.lastRequest.getBootVersion());
|
||||
List<String> dependencies = this.handler.lastRequest.getDependencies();
|
||||
assertEquals(2, dependencies.size());
|
||||
@@ -261,36 +248,34 @@ public class InitCommandTests extends AbstractHttpClientMockTests {
|
||||
public void parseOutput() throws Exception {
|
||||
this.handler.disableProjectGeneration();
|
||||
this.command.run("--output=foobar.zip");
|
||||
|
||||
assertEquals("foobar.zip", this.handler.lastRequest.getOutput());
|
||||
}
|
||||
|
||||
private byte[] createFakeZipArchive(String fileName, String content)
|
||||
throws IOException {
|
||||
ByteArrayOutputStream out = new ByteArrayOutputStream();
|
||||
ZipOutputStream zos = new ZipOutputStream(out);
|
||||
ByteArrayOutputStream bos = new ByteArrayOutputStream();
|
||||
ZipOutputStream zos = new ZipOutputStream(bos);
|
||||
try {
|
||||
ZipEntry entry = new ZipEntry(fileName);
|
||||
|
||||
zos.putNextEntry(entry);
|
||||
zos.write(content.getBytes());
|
||||
zos.closeEntry();
|
||||
}
|
||||
finally {
|
||||
out.close();
|
||||
bos.close();
|
||||
}
|
||||
return out.toByteArray();
|
||||
return bos.toByteArray();
|
||||
}
|
||||
|
||||
private static class TestableInitCommandOptionHandler extends
|
||||
InitCommandOptionHandler {
|
||||
InitCommand.InitOptionHandler {
|
||||
|
||||
private boolean disableProjectGeneration;
|
||||
|
||||
ProjectGenerationRequest lastRequest;
|
||||
private ProjectGenerationRequest lastRequest;
|
||||
|
||||
TestableInitCommandOptionHandler(CloseableHttpClient httpClient) {
|
||||
super(httpClient);
|
||||
public TestableInitCommandOptionHandler(InitializrService initializrService) {
|
||||
super(initializrService);
|
||||
}
|
||||
|
||||
void disableProjectGeneration() {
|
||||
@@ -298,14 +283,13 @@ public class InitCommandTests extends AbstractHttpClientMockTests {
|
||||
}
|
||||
|
||||
@Override
|
||||
protected ExitStatus generateProject(OptionSet options,
|
||||
CloseableHttpClient httpClient) {
|
||||
protected void generateProject(OptionSet options) throws IOException {
|
||||
this.lastRequest = createProjectGenerationRequest(options);
|
||||
if (!this.disableProjectGeneration) {
|
||||
return super.generateProject(options, httpClient);
|
||||
super.generateProject(options);
|
||||
}
|
||||
return ExitStatus.OK;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -80,8 +80,8 @@ public class InitializrServiceMetadataTests {
|
||||
try {
|
||||
return new InitializrServiceMetadata(readJson(version));
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new IllegalStateException("Failed to read json", e);
|
||||
catch (IOException ex) {
|
||||
throw new IllegalStateException("Failed to read json", ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -90,8 +90,8 @@ public class InitializrServiceMetadataTests {
|
||||
+ ".json");
|
||||
InputStream stream = resource.getInputStream();
|
||||
try {
|
||||
String json = StreamUtils.copyToString(stream, Charset.forName("UTF-8"));
|
||||
return new JSONObject(json);
|
||||
return new JSONObject(StreamUtils.copyToString(stream,
|
||||
Charset.forName("UTF-8")));
|
||||
}
|
||||
finally {
|
||||
stream.close();
|
||||
|
||||
@@ -33,17 +33,16 @@ import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
/**
|
||||
* Tests for {@link InitializrServiceHttpInvoker}
|
||||
* Tests for {@link InitializrService}
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class InitializrServiceHttpInvokerTests extends AbstractHttpClientMockTests {
|
||||
public class InitializrServiceTests extends AbstractHttpClientMockTests {
|
||||
|
||||
@Rule
|
||||
public final ExpectedException thrown = ExpectedException.none();
|
||||
|
||||
private final InitializrServiceHttpInvoker invoker = new InitializrServiceHttpInvoker(
|
||||
this.httpClient);
|
||||
private final InitializrService invoker = new InitializrService(this.http);
|
||||
|
||||
@Test
|
||||
public void loadMetadata() throws IOException {
|
||||
@@ -86,8 +85,7 @@ public class InitializrServiceHttpInvokerTests extends AbstractHttpClientMockTes
|
||||
mockProjectGenerationError(400, jsonMessage);
|
||||
ProjectGenerationRequest request = new ProjectGenerationRequest();
|
||||
request.getDependencies().add("foo:bar");
|
||||
|
||||
this.thrown.expect(ProjectGenerationException.class);
|
||||
this.thrown.expect(ReportableException.class);
|
||||
this.thrown.expectMessage(jsonMessage);
|
||||
this.invoker.generate(request);
|
||||
}
|
||||
@@ -95,9 +93,8 @@ public class InitializrServiceHttpInvokerTests extends AbstractHttpClientMockTes
|
||||
@Test
|
||||
public void generateProjectBadRequestNoExtraMessage() throws IOException {
|
||||
mockProjectGenerationError(400, null);
|
||||
|
||||
ProjectGenerationRequest request = new ProjectGenerationRequest();
|
||||
this.thrown.expect(ProjectGenerationException.class);
|
||||
this.thrown.expect(ReportableException.class);
|
||||
this.thrown.expectMessage("unexpected 400 error");
|
||||
this.invoker.generate(request);
|
||||
}
|
||||
@@ -105,14 +102,11 @@ public class InitializrServiceHttpInvokerTests extends AbstractHttpClientMockTes
|
||||
@Test
|
||||
public void generateProjectNoContent() throws IOException {
|
||||
mockSuccessfulMetadataGet();
|
||||
|
||||
CloseableHttpResponse response = mock(CloseableHttpResponse.class);
|
||||
mockStatus(response, 500);
|
||||
when(this.httpClient.execute(isA(HttpGet.class))).thenReturn(response);
|
||||
|
||||
when(this.http.execute(isA(HttpGet.class))).thenReturn(response);
|
||||
ProjectGenerationRequest request = new ProjectGenerationRequest();
|
||||
|
||||
this.thrown.expect(ProjectGenerationException.class);
|
||||
this.thrown.expect(ReportableException.class);
|
||||
this.thrown.expectMessage("No content received from server");
|
||||
this.invoker.generate(request);
|
||||
}
|
||||
@@ -122,8 +116,7 @@ public class InitializrServiceHttpInvokerTests extends AbstractHttpClientMockTes
|
||||
String jsonMessage = "whatever error on the server";
|
||||
mockMetadataGetError(500, jsonMessage);
|
||||
ProjectGenerationRequest request = new ProjectGenerationRequest();
|
||||
|
||||
this.thrown.expect(ProjectGenerationException.class);
|
||||
this.thrown.expect(ReportableException.class);
|
||||
this.thrown.expectMessage(jsonMessage);
|
||||
this.invoker.generate(request);
|
||||
}
|
||||
@@ -133,10 +126,9 @@ public class InitializrServiceHttpInvokerTests extends AbstractHttpClientMockTes
|
||||
CloseableHttpResponse response = mock(CloseableHttpResponse.class);
|
||||
mockHttpEntity(response, "Foo-Bar-Not-JSON".getBytes(), "application/json");
|
||||
mockStatus(response, 200);
|
||||
when(this.httpClient.execute(isA(HttpGet.class))).thenReturn(response);
|
||||
|
||||
when(this.http.execute(isA(HttpGet.class))).thenReturn(response);
|
||||
ProjectGenerationRequest request = new ProjectGenerationRequest();
|
||||
this.thrown.expect(ProjectGenerationException.class);
|
||||
this.thrown.expect(ReportableException.class);
|
||||
this.thrown.expectMessage("Invalid content received from server");
|
||||
this.invoker.generate(request);
|
||||
}
|
||||
@@ -145,11 +137,9 @@ public class InitializrServiceHttpInvokerTests extends AbstractHttpClientMockTes
|
||||
public void loadMetadataNoContent() throws IOException {
|
||||
CloseableHttpResponse response = mock(CloseableHttpResponse.class);
|
||||
mockStatus(response, 500);
|
||||
when(this.httpClient.execute(isA(HttpGet.class))).thenReturn(response);
|
||||
|
||||
when(this.http.execute(isA(HttpGet.class))).thenReturn(response);
|
||||
ProjectGenerationRequest request = new ProjectGenerationRequest();
|
||||
|
||||
this.thrown.expect(ProjectGenerationException.class);
|
||||
this.thrown.expect(ReportableException.class);
|
||||
this.thrown.expectMessage("No content received from server");
|
||||
this.invoker.generate(request);
|
||||
}
|
||||
@@ -105,7 +105,6 @@ public class ProjectGenerationRequestTests {
|
||||
ProjectType projectType = new ProjectType("custom", "Custom Type", "/foo", true,
|
||||
EMPTY_TAGS);
|
||||
InitializrServiceMetadata metadata = new InitializrServiceMetadata(projectType);
|
||||
|
||||
this.request.setType("custom");
|
||||
this.request.getDependencies().add("data-rest");
|
||||
assertEquals(new URI(ProjectGenerationRequest.DEFAULT_SERVICE_URL
|
||||
@@ -116,8 +115,7 @@ public class ProjectGenerationRequestTests {
|
||||
public void buildNoMatch() {
|
||||
InitializrServiceMetadata metadata = readMetadata();
|
||||
setBuildAndFormat("does-not-exist", null);
|
||||
|
||||
this.thrown.expect(ProjectGenerationException.class);
|
||||
this.thrown.expect(ReportableException.class);
|
||||
this.thrown.expectMessage("does-not-exist");
|
||||
this.request.generateUrl(metadata);
|
||||
}
|
||||
@@ -126,8 +124,7 @@ public class ProjectGenerationRequestTests {
|
||||
public void buildMultipleMatch() {
|
||||
InitializrServiceMetadata metadata = readMetadata("types-conflict");
|
||||
setBuildAndFormat("gradle", null);
|
||||
|
||||
this.thrown.expect(ProjectGenerationException.class);
|
||||
this.thrown.expect(ReportableException.class);
|
||||
this.thrown.expectMessage("gradle-project");
|
||||
this.thrown.expectMessage("gradle-project-2");
|
||||
this.request.generateUrl(metadata);
|
||||
@@ -137,7 +134,6 @@ public class ProjectGenerationRequestTests {
|
||||
public void buildOneMatch() {
|
||||
InitializrServiceMetadata metadata = readMetadata();
|
||||
setBuildAndFormat("gradle", null);
|
||||
|
||||
assertEquals(createDefaultUrl("?type=gradle-project"),
|
||||
this.request.generateUrl(metadata));
|
||||
}
|
||||
@@ -145,15 +141,13 @@ public class ProjectGenerationRequestTests {
|
||||
@Test
|
||||
public void invalidType() throws URISyntaxException {
|
||||
this.request.setType("does-not-exist");
|
||||
|
||||
this.thrown.expect(ProjectGenerationException.class);
|
||||
this.thrown.expect(ReportableException.class);
|
||||
this.request.generateUrl(createDefaultMetadata());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void noTypeAndNoDefault() throws URISyntaxException {
|
||||
|
||||
this.thrown.expect(ProjectGenerationException.class);
|
||||
this.thrown.expect(ReportableException.class);
|
||||
this.thrown.expectMessage("no default is defined");
|
||||
this.request.generateUrl(readMetadata("types-conflict"));
|
||||
}
|
||||
@@ -163,8 +157,8 @@ public class ProjectGenerationRequestTests {
|
||||
return new URI(ProjectGenerationRequest.DEFAULT_SERVICE_URL + "/starter.zip"
|
||||
+ param);
|
||||
}
|
||||
catch (URISyntaxException e) {
|
||||
throw new IllegalStateException(e);
|
||||
catch (URISyntaxException ex) {
|
||||
throw new IllegalStateException(ex);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -193,8 +187,8 @@ public class ProjectGenerationRequestTests {
|
||||
JSONObject json = new JSONObject(content);
|
||||
return new InitializrServiceMetadata(json);
|
||||
}
|
||||
catch (IOException e) {
|
||||
throw new IllegalStateException("Failed to read metadata", e);
|
||||
catch (IOException ex) {
|
||||
throw new IllegalStateException("Failed to read metadata", ex);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -23,19 +23,19 @@ import org.junit.Test;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
/**
|
||||
* Tests for {@link ListMetadataCommand}
|
||||
* Tests for {@link ServiceCapabilitiesReportGenerator}
|
||||
*
|
||||
* @author Stephane Nicoll
|
||||
*/
|
||||
public class ListMetadataCommandTests extends AbstractHttpClientMockTests {
|
||||
public class ServiceCapabilitiesReportGeneratorTests extends AbstractHttpClientMockTests {
|
||||
|
||||
private final ListMetadataCommand command = new ListMetadataCommand(this.httpClient);
|
||||
private final ServiceCapabilitiesReportGenerator command = new ServiceCapabilitiesReportGenerator(
|
||||
new InitializrService(this.http));
|
||||
|
||||
@Test
|
||||
public void listMetadata() throws IOException {
|
||||
mockSuccessfulMetadataGet();
|
||||
String content = this.command.generateReport("http://localhost");
|
||||
|
||||
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"));
|
||||
Reference in New Issue
Block a user