Search in sources :

Example 6 with ProjectDeployer

use of io.fabric8.deployer.ProjectDeployer in project fabric8 by jboss-fuse.

the class MavenProxyServletSupportTest method testDownloadMetadata.

@Test(timeout = 30000)
public void testDownloadMetadata() throws Exception {
    final String old = System.getProperty("karaf.data");
    System.setProperty("karaf.data", new File("target").getCanonicalPath());
    FileUtils.deleteDirectory(new File("target/tmp"));
    Server server = new Server(0);
    server.setHandler(new AbstractHandler() {

        @Override
        public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
            String result = null;
            if ("/repo1/org/apache/camel/camel-core/maven-metadata.xml".equals(target)) {
                result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<metadata>\n" + "  <groupId>org.apache.camel</groupId>\n" + "  <artifactId>camel-core</artifactId>\n" + "  <versioning>\n" + "    <latest>2.14.0</latest>\n" + "    <release>2.14.0</release>\n" + "    <versions>\n" + "      <version>1.6.1</version>\n" + "      <version>1.6.2</version>\n" + "      <version>1.6.3</version>\n" + "      <version>1.6.4</version>\n" + "      <version>2.0-M2</version>\n" + "      <version>2.0-M3</version>\n" + "      <version>2.0.0</version>\n" + "      <version>2.1.0</version>\n" + "      <version>2.2.0</version>\n" + "      <version>2.3.0</version>\n" + "      <version>2.4.0</version>\n" + "      <version>2.5.0</version>\n" + "      <version>2.6.0</version>\n" + "      <version>2.7.0</version>\n" + "      <version>2.7.1</version>\n" + "      <version>2.7.2</version>\n" + "      <version>2.7.3</version>\n" + "      <version>2.7.4</version>\n" + "      <version>2.7.5</version>\n" + "      <version>2.8.0</version>\n" + "      <version>2.8.1</version>\n" + "      <version>2.8.2</version>\n" + "      <version>2.8.3</version>\n" + "      <version>2.8.4</version>\n" + "      <version>2.8.5</version>\n" + "      <version>2.8.6</version>\n" + "      <version>2.9.0-RC1</version>\n" + "      <version>2.9.0</version>\n" + "      <version>2.9.1</version>\n" + "      <version>2.9.2</version>\n" + "      <version>2.9.3</version>\n" + "      <version>2.9.4</version>\n" + "      <version>2.9.5</version>\n" + "      <version>2.9.6</version>\n" + "      <version>2.9.7</version>\n" + "      <version>2.9.8</version>\n" + "      <version>2.10.0</version>\n" + "      <version>2.10.1</version>\n" + "      <version>2.10.2</version>\n" + "      <version>2.10.3</version>\n" + "      <version>2.10.4</version>\n" + "      <version>2.10.5</version>\n" + "      <version>2.10.6</version>\n" + "      <version>2.10.7</version>\n" + "      <version>2.11.0</version>\n" + "      <version>2.11.1</version>\n" + "      <version>2.11.2</version>\n" + "      <version>2.11.3</version>\n" + "      <version>2.11.4</version>\n" + "      <version>2.12.0</version>\n" + "      <version>2.12.1</version>\n" + "      <version>2.12.2</version>\n" + "      <version>2.12.3</version>\n" + "      <version>2.12.4</version>\n" + "      <version>2.13.0</version>\n" + "      <version>2.13.1</version>\n" + "      <version>2.13.2</version>\n" + "      <version>2.14.0</version>\n" + "    </versions>\n" + "    <lastUpdated>20140918132816</lastUpdated>\n" + "  </versioning>\n" + "</metadata>\n" + "\n";
            } else if ("/repo2/org/apache/camel/camel-core/maven-metadata.xml".equals(target)) {
                result = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<metadata modelVersion=\"1.1.0\">\n" + "  <groupId>org.apache.camel</groupId>\n" + "  <artifactId>camel-core</artifactId>\n" + "  <versioning>\n" + "    <latest>2.14.0.redhat-620034</latest>\n" + "    <release>2.14.0.redhat-620034</release>\n" + "    <versions>\n" + "      <version>2.10.0.redhat-60074</version>\n" + "      <version>2.12.0.redhat-610312</version>\n" + "      <version>2.12.0.redhat-610328</version>\n" + "      <version>2.12.0.redhat-610355</version>\n" + "      <version>2.12.0.redhat-610378</version>\n" + "      <version>2.12.0.redhat-610396</version>\n" + "      <version>2.12.0.redhat-610399</version>\n" + "      <version>2.12.0.redhat-610401</version>\n" + "      <version>2.12.0.redhat-610402</version>\n" + "      <version>2.12.0.redhat-611403</version>\n" + "      <version>2.12.0.redhat-611405</version>\n" + "      <version>2.12.0.redhat-611406</version>\n" + "      <version>2.12.0.redhat-611408</version>\n" + "      <version>2.12.0.redhat-611409</version>\n" + "      <version>2.12.0.redhat-611410</version>\n" + "      <version>2.12.0.redhat-611411</version>\n" + "      <version>2.12.0.redhat-611412</version>\n" + "      <version>2.14.0.redhat-620031</version>\n" + "      <version>2.14.0.redhat-620033</version>\n" + "      <version>2.14.0.redhat-620034</version>\n" + "    </versions>\n" + "    <lastUpdated>20141019130841</lastUpdated>\n" + "  </versioning>\n" + "</metadata>\n" + "\n";
            }
            if (result == null) {
                response.setStatus(HttpServletResponse.SC_NOT_FOUND);
                baseRequest.setHandled(true);
                response.getOutputStream().close();
            } else {
                response.setStatus(HttpServletResponse.SC_OK);
                baseRequest.setHandled(true);
                response.getOutputStream().write(result.getBytes());
                response.getOutputStream().close();
            }
        }
    });
    server.start();
    try {
        int localPort = ((ServerConnector) server.getConnectors()[0]).getLocalPort();
        List<String> remoteRepos = Arrays.asList("http://relevant.not/repo1@id=repo1,http://relevant.not/repo2@id=repo2");
        RuntimeProperties props = new MockRuntimeProperties();
        // TODO: local repo should point to target/tmp
        MavenResolver resolver = createResolver("target/tmp", remoteRepos, "http", "localhost", localPort, "fuse", "fuse", null);
        MavenDownloadProxyServlet servlet = new MavenDownloadProxyServlet(resolver, props, projectDeployer, 5, 0);
        AsyncContext context = EasyMock.createMock(AsyncContext.class);
        EasyMock.makeThreadSafe(context, true);
        final Map<String, Object> attributes = new HashMap<>();
        HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
        HttpServletRequest requestWrapper = new HttpServletRequestWrapper(request) {

            @Override
            public Object getAttribute(String name) {
                return attributes.get(name);
            }

            @Override
            public void setAttribute(String name, Object o) {
                attributes.put(name, o);
            }
        };
        EasyMock.makeThreadSafe(request, true);
        EasyMock.expect(request.getMethod()).andReturn("GET");
        EasyMock.expect(request.getPathInfo()).andReturn("org/apache/camel/camel-core/maven-metadata.xml");
        EasyMock.expect(request.startAsync()).andReturn(context);
        context.setTimeout(EasyMock.anyInt());
        EasyMock.expectLastCall();
        context.addListener((AsyncListener) EasyMock.anyObject());
        EasyMock.expectLastCall();
        HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class);
        EasyMock.makeThreadSafe(response, true);
        response.setStatus(EasyMock.anyInt());
        EasyMock.expectLastCall().anyTimes();
        response.setContentLength(EasyMock.anyInt());
        EasyMock.expectLastCall().anyTimes();
        response.setContentType((String) EasyMock.anyObject());
        EasyMock.expectLastCall().anyTimes();
        response.setDateHeader((String) EasyMock.anyObject(), EasyMock.anyLong());
        EasyMock.expectLastCall().anyTimes();
        response.setHeader((String) EasyMock.anyObject(), (String) EasyMock.anyObject());
        EasyMock.expectLastCall().anyTimes();
        response.flushBuffer();
        EasyMock.expectLastCall().anyTimes();
        final CountDownLatch latchDispatch = new CountDownLatch(1);
        context.dispatch();
        EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {

            @Override
            public Object answer() throws Throwable {
                latchDispatch.countDown();
                return null;
            }
        });
        EasyMock.replay(request, response, context);
        servlet.start();
        servlet.doGet(requestWrapper, response);
        latchDispatch.await();
        EasyMock.verify(request, response, context);
        EasyMock.reset(request, response, context);
        EasyMock.expect(request.getPathInfo()).andReturn("org/apache/camel/camel-core/maven-metadata.xml");
        EasyMock.expect(request.startAsync()).andReturn(context);
        context.setTimeout(EasyMock.anyInt());
        EasyMock.expectLastCall();
        context.addListener((AsyncListener) EasyMock.anyObject());
        EasyMock.expectLastCall();
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        EasyMock.expect(response.getOutputStream()).andReturn(new ServletOutputStream() {

            @Override
            public void write(int b) throws IOException {
                baos.write(b);
            }

            @Override
            public void write(byte[] b, int off, int len) throws IOException {
                baos.write(b, off, len);
            }

            @Override
            public boolean isReady() {
                return true;
            }

            @Override
            public void setWriteListener(WriteListener writeListener) {
            }
        }).anyTimes();
        response.flushBuffer();
        EasyMock.expectLastCall().anyTimes();
        final CountDownLatch latchComplete = new CountDownLatch(1);
        context.complete();
        EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {

            @Override
            public Object answer() throws Throwable {
                latchComplete.countDown();
                return null;
            }
        });
        EasyMock.replay(request, response, context);
        servlet.doGet(requestWrapper, response);
        EasyMock.verify(request, response, context);
        org.apache.maven.artifact.repository.metadata.Metadata m = new MetadataXpp3Reader().read(new ByteArrayInputStream(baos.toByteArray()), false);
        assertEquals("2.14.0.redhat-620034", m.getVersioning().getLatest());
        assertTrue(m.getVersioning().getVersions().contains("2.10.4"));
        assertTrue(m.getVersioning().getVersions().contains("2.12.0.redhat-610399"));
        EasyMock.verify(request, response, context);
    } finally {
        server.stop();
        if (old != null) {
            System.setProperty("karaf.data", old);
        }
    }
}
Also used : Server(org.eclipse.jetty.server.Server) HashMap(java.util.HashMap) ServletOutputStream(javax.servlet.ServletOutputStream) MetadataXpp3Reader(org.apache.maven.artifact.repository.metadata.io.xpp3.MetadataXpp3Reader) AsyncContext(javax.servlet.AsyncContext) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) MavenResolver(io.fabric8.maven.MavenResolver) WriteListener(javax.servlet.WriteListener) AbstractRuntimeProperties(io.fabric8.api.scr.AbstractRuntimeProperties) RuntimeProperties(io.fabric8.api.RuntimeProperties) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CountDownLatch(java.util.concurrent.CountDownLatch) ByteArrayInputStream(java.io.ByteArrayInputStream) File(java.io.File)

Example 7 with ProjectDeployer

use of io.fabric8.deployer.ProjectDeployer in project fabric8 by jboss-fuse.

the class MavenProxyServletSupportTest method testDownload.

private void testDownload(Handler serverHandler) throws Exception {
    final String old = System.getProperty("karaf.data");
    System.setProperty("karaf.data", new File("target").getCanonicalPath());
    FileUtils.deleteDirectory(new File("target/tmp"));
    Server server = new Server(0);
    server.setHandler(serverHandler);
    server.start();
    try {
        int localPort = ((ServerConnector) server.getConnectors()[0]).getLocalPort();
        List<String> remoteRepos = Arrays.asList("http://relevant.not/maven2@id=central");
        RuntimeProperties props = new MockRuntimeProperties();
        // TODO: local repo should point to target/tmp
        MavenResolver resolver = createResolver("target/tmp", remoteRepos, "http", "localhost", localPort, "fuse", "fuse", null);
        MavenDownloadProxyServlet servlet = new MavenDownloadProxyServlet(resolver, props, projectDeployer, 5, 0);
        AsyncContext context = EasyMock.createMock(AsyncContext.class);
        EasyMock.makeThreadSafe(context, true);
        final Map<String, Object> attributes = new HashMap<>();
        HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
        HttpServletRequest requestWrapper = new HttpServletRequestWrapper(request) {

            @Override
            public Object getAttribute(String name) {
                return attributes.get(name);
            }

            @Override
            public void setAttribute(String name, Object o) {
                attributes.put(name, o);
            }
        };
        EasyMock.makeThreadSafe(request, true);
        EasyMock.expect(request.getMethod()).andReturn("GET");
        EasyMock.expect(request.getPathInfo()).andReturn("org.apache.camel/camel-core/2.13.0/camel-core-2.13.0-sources.jar");
        EasyMock.expect(request.startAsync()).andReturn(context);
        context.setTimeout(EasyMock.anyInt());
        EasyMock.expectLastCall();
        context.addListener((AsyncListener) EasyMock.anyObject());
        EasyMock.expectLastCall();
        HttpServletResponse response = EasyMock.createMock(HttpServletResponse.class);
        EasyMock.makeThreadSafe(response, true);
        response.setStatus(EasyMock.anyInt());
        EasyMock.expectLastCall();
        response.setContentLength(EasyMock.anyInt());
        EasyMock.expectLastCall();
        response.setContentType((String) EasyMock.anyObject());
        EasyMock.expectLastCall();
        response.setDateHeader((String) EasyMock.anyObject(), EasyMock.anyLong());
        EasyMock.expectLastCall();
        response.setHeader((String) EasyMock.anyObject(), (String) EasyMock.anyObject());
        EasyMock.expectLastCall().anyTimes();
        final CountDownLatch latchDispatch = new CountDownLatch(1);
        context.dispatch();
        EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {

            @Override
            public Object answer() throws Throwable {
                latchDispatch.countDown();
                return null;
            }
        });
        EasyMock.replay(request, response, context);
        servlet.start();
        servlet.doGet(requestWrapper, response);
        latchDispatch.await();
        EasyMock.verify(request, response, context);
        // 
        // Subsequent call
        // 
        final ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ServletOutputStream outputStream = new ServletOutputStream() {

            @Override
            public void write(int b) throws IOException {
                baos.write(b);
            }

            @Override
            public void write(byte[] b, int off, int len) throws IOException {
                baos.write(b, off, len);
            }

            @Override
            public boolean isReady() {
                return true;
            }

            @Override
            public void setWriteListener(WriteListener writeListener) {
            }
        };
        while (true) {
            long size = (Long) attributes.get(AsynchronousFileChannel.class.getName() + ".size");
            long pos = (Long) attributes.get(AsynchronousFileChannel.class.getName() + ".position");
            ByteBuffer buffer = (ByteBuffer) attributes.get(ByteBuffer.class.getName());
            if (pos + buffer.position() >= size) {
                break;
            }
            EasyMock.reset(request, response, context);
            EasyMock.expect(request.getPathInfo()).andReturn("org.apache.camel/camel-core/2.13.0/camel-core-2.13.0-sources.jar");
            EasyMock.expect(request.startAsync()).andReturn(context);
            context.setTimeout(EasyMock.anyInt());
            EasyMock.expectLastCall();
            context.addListener((AsyncListener) EasyMock.anyObject());
            EasyMock.expectLastCall();
            final CountDownLatch latch = new CountDownLatch(1);
            context.dispatch();
            EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {

                @Override
                public Object answer() throws Throwable {
                    latch.countDown();
                    return null;
                }
            });
            EasyMock.expect(response.getOutputStream()).andReturn(outputStream);
            EasyMock.replay(request, response, context);
            servlet.doGet(requestWrapper, response);
            latch.await();
            EasyMock.verify(request, response, context);
        }
        // 
        // Last calls
        // 
        EasyMock.reset(request, response, context);
        EasyMock.expect(request.getPathInfo()).andReturn("org.apache.camel/camel-core/2.13.0/camel-core-2.13.0-sources.jar");
        EasyMock.expect(request.startAsync()).andReturn(context);
        context.setTimeout(EasyMock.anyInt());
        EasyMock.expectLastCall();
        context.addListener((AsyncListener) EasyMock.anyObject());
        EasyMock.expectLastCall();
        final CountDownLatch latchComplete = new CountDownLatch(1);
        context.complete();
        EasyMock.expectLastCall().andAnswer(new IAnswer<Object>() {

            @Override
            public Object answer() throws Throwable {
                latchComplete.countDown();
                return null;
            }
        });
        EasyMock.expect(response.getOutputStream()).andReturn(outputStream);
        response.flushBuffer();
        EasyMock.expectLastCall();
        EasyMock.replay(request, response, context);
        servlet.doGet(requestWrapper, response);
        latchComplete.await();
        EasyMock.verify(request, response, context);
        Assert.assertArrayEquals(new byte[] { 0x42 }, baos.toByteArray());
    } finally {
        server.stop();
        if (old != null) {
            System.setProperty("karaf.data", old);
        }
    }
}
Also used : Server(org.eclipse.jetty.server.Server) HashMap(java.util.HashMap) ServletOutputStream(javax.servlet.ServletOutputStream) AsyncContext(javax.servlet.AsyncContext) ServerConnector(org.eclipse.jetty.server.ServerConnector) HttpServletRequest(javax.servlet.http.HttpServletRequest) AsynchronousFileChannel(java.nio.channels.AsynchronousFileChannel) HttpServletRequestWrapper(javax.servlet.http.HttpServletRequestWrapper) MavenResolver(io.fabric8.maven.MavenResolver) WriteListener(javax.servlet.WriteListener) AbstractRuntimeProperties(io.fabric8.api.scr.AbstractRuntimeProperties) RuntimeProperties(io.fabric8.api.RuntimeProperties) HttpServletResponse(javax.servlet.http.HttpServletResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuffer(java.nio.ByteBuffer) File(java.io.File)

Aggregations

RuntimeProperties (io.fabric8.api.RuntimeProperties)6 File (java.io.File)6 AbstractRuntimeProperties (io.fabric8.api.scr.AbstractRuntimeProperties)5 MavenResolver (io.fabric8.maven.MavenResolver)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 IOException (java.io.IOException)3 HashMap (java.util.HashMap)3 Server (org.eclipse.jetty.server.Server)3 ServerConnector (org.eclipse.jetty.server.ServerConnector)3 CountDownLatch (java.util.concurrent.CountDownLatch)2 AsyncContext (javax.servlet.AsyncContext)2 ReadListener (javax.servlet.ReadListener)2 ServletException (javax.servlet.ServletException)2 ServletInputStream (javax.servlet.ServletInputStream)2 ServletOutputStream (javax.servlet.ServletOutputStream)2 WriteListener (javax.servlet.WriteListener)2 HttpServletRequestWrapper (javax.servlet.http.HttpServletRequestWrapper)2 Request (org.eclipse.jetty.server.Request)2