use of io.fabric8.deployer.dto.ProjectRequirements in project fabric8 by jboss-fuse.
the class MavenProxyServletSupportTest method testUploadWithMimeMultipartFormData.
@Test
public void testUploadWithMimeMultipartFormData() throws Exception {
new File("target/maven/proxy/tmp/multipart").mkdirs();
System.setProperty("karaf.data", new File("target").getCanonicalPath());
ByteArrayOutputStream baos = new ByteArrayOutputStream();
JarOutputStream jas = new JarOutputStream(baos);
addEntry(jas, "META-INF/MANIFEST.MF", "Manifest-Version: 1.0\n".getBytes());
addEntry(jas, "META-INF/maven/io.fabric8/mybundle/pom.properties", "groupId=io.fabric8\nartifactId=mybundle\nversion=1.0\n".getBytes());
jas.close();
byte[] jarBytes = baos.toByteArray();
RuntimeProperties props = new MockRuntimeProperties();
MavenResolver resolver = EasyMock.createMock(MavenResolver.class);
MavenUploadProxyServlet servlet = new MavenUploadProxyServlet(resolver, props, projectDeployer, new File("target/upload"), 0);
servlet.setFileItemFactory(new DiskFileItemFactory(0, new File("target/maven/proxy/tmp/multipart")));
HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class);
FilePart part = new FilePart("file[]", new ByteArrayPartSource("mybundle-1.0.jar", jarBytes));
MultipartRequestEntity entity = new MultipartRequestEntity(new Part[] { part }, new HttpMethodParams());
final ByteArrayOutputStream requestBytes = new ByteArrayOutputStream();
entity.writeRequest(requestBytes);
final byte[] multipartRequestBytes = requestBytes.toByteArray();
EasyMock.expect(request.getPathInfo()).andReturn("/mybundle-1.0.jar");
EasyMock.expect(request.getHeader(MavenProxyServletSupport.LOCATION_HEADER)).andReturn(null);
EasyMock.expect(request.getParameter("profile")).andReturn("my");
EasyMock.expect(request.getParameter("version")).andReturn("1.0");
EasyMock.expect(request.getContentType()).andReturn(entity.getContentType()).anyTimes();
EasyMock.expect(request.getHeader("Content-length")).andReturn(Long.toString(entity.getContentLength())).anyTimes();
EasyMock.expect(request.getContentLength()).andReturn((int) entity.getContentLength()).anyTimes();
EasyMock.expect(request.getCharacterEncoding()).andReturn("ISO-8859-1").anyTimes();
Capture<String> location = EasyMock.newCapture(CaptureType.ALL);
response.setStatus(HttpServletResponse.SC_ACCEPTED);
EasyMock.expect(request.getInputStream()).andReturn(new ServletInputStream() {
private int pos = 0;
@Override
public int read() throws IOException {
return pos >= multipartRequestBytes.length ? -1 : (multipartRequestBytes[pos++] & 0xFF);
}
@Override
public boolean isFinished() {
return false;
}
@Override
public boolean isReady() {
return true;
}
@Override
public void setReadListener(ReadListener readListener) {
}
});
Capture<ProjectRequirements> requirementsCapture = EasyMock.newCapture(CaptureType.FIRST);
EasyMock.expect(projectDeployer.deployProject(EasyMock.capture(requirementsCapture), EasyMock.eq(true))).andReturn(null);
EasyMock.replay(resolver, request, response, projectDeployer);
servlet.doPut(request, response);
FileInputStream fis = new FileInputStream("target/upload/io.fabric8/mybundle/1.0/mybundle-1.0.jar");
ByteArrayOutputStream storedBundleBytes = new ByteArrayOutputStream();
IOUtils.copy(fis, storedBundleBytes);
fis.close();
Assert.assertArrayEquals(jarBytes, storedBundleBytes.toByteArray());
ProjectRequirements pr = requirementsCapture.getValue();
List<String> bundles = pr.getBundles();
assertThat(bundles.size(), equalTo(1));
assertThat(bundles.get(0), equalTo("mvn:io.fabric8/mybundle/1.0"));
assertThat(pr.getProfileId(), equalTo("my"));
assertThat(pr.getVersion(), equalTo("1.0"));
assertThat(pr.getGroupId(), nullValue());
assertThat(pr.getArtifactId(), nullValue());
EasyMock.verify(resolver, request, response, projectDeployer);
}
Aggregations