Search in sources :

Example 51 with Resolver

use of io.fabric8.patch.management.conflicts.Resolver in project fabric8 by jboss-fuse.

the class DownloadManagerTest method createDownloadManager.

/**
 * Prepares DownloadManager to test
 *
 * @param remoteRepo
 * @param settingsFile
 * @param props
 * @return
 * @throws IOException
 */
private DownloadManager createDownloadManager(String remoteRepo, String settingsFile, Properties props) throws IOException {
    File mavenSettings = new File(karafHome, settingsFile);
    Hashtable<String, String> properties = new Hashtable<>();
    if (props != null) {
        for (Map.Entry<Object, Object> entry : props.entrySet()) {
            properties.put(entry.getKey().toString(), entry.getValue().toString());
        }
    }
    properties.put("org.ops4j.pax.url.mvn.localRepository", systemRepoUri);
    properties.put("org.ops4j.pax.url.mvn.repositories", remoteRepo);
    properties.put("org.ops4j.pax.url.mvn.defaultRepositories", systemRepoUri);
    properties.put("org.ops4j.pax.url.mvn.settings", mavenSettings.toURI().toString());
    MavenResolver resolver = MavenResolvers.createMavenResolver(properties, "org.ops4j.pax.url.mvn");
    return DownloadManagers.createDownloadManager(resolver, Executors.newSingleThreadScheduledExecutor());
}
Also used : Hashtable(java.util.Hashtable) MavenResolver(io.fabric8.maven.MavenResolver) File(java.io.File) Map(java.util.Map)

Example 52 with Resolver

use of io.fabric8.patch.management.conflicts.Resolver in project fabric8 by jboss-fuse.

the class MavenProxyResolutionTest method releaseIsAvailableInRemoteRepositoryUpdatingRelease.

@Test
public void releaseIsAvailableInRemoteRepositoryUpdatingRelease() throws IOException, InvalidMavenArtifactRequest {
    File remoteRepository = initFileRepository("rr");
    MavenResolver resolver = new ResolverBuilder().withRemoteRepositories(Collections.singletonList(remoteRepository)).withReleaseUpdates().withUpdatePolicy(RepositoryPolicy.UPDATE_POLICY_ALWAYS).build();
    MavenDownloadProxyServlet servlet = new MavenDownloadProxyServlet(resolver, runtime, null, 1, 0);
    mvnInstall(remoteRepository, "io.fabric8.test", "universalis-api", "0.1.0", at("10:00"), "a");
    File file = servlet.download("io/fabric8/test/universalis-api/0.1.0/universalis-api-0.1.0.jar");
    // first resolution
    assertThat(FileUtils.readFileToString(file), equalTo("a"));
    // don't do that, it's not proper use of maven. But sometimes we just have another deployment to public repository...
    mvnInstall(remoteRepository, "io.fabric8.test", "universalis-api", "0.1.0", at("11:00"), "b");
    // second resolution
    file = servlet.download("io/fabric8/test/universalis-api/0.1.0/universalis-api-0.1.0.jar");
    assertThat("Artifact will be updated though it's not proper usage of Maven", FileUtils.readFileToString(file), equalTo("b"));
}
Also used : MavenResolver(io.fabric8.maven.MavenResolver) File(java.io.File) Test(org.junit.Test)

Example 53 with Resolver

use of io.fabric8.patch.management.conflicts.Resolver in project fabric8 by jboss-fuse.

the class MavenProxyServletSupportTest method testUpload.

private Map<String, String> testUpload(String path, final byte[] contents, String location, String profile, String version, boolean hasLocationHeader) 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 {
            response.setStatus(HttpServletResponse.SC_NO_CONTENT);
        }
    });
    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();
        MavenResolver resolver = createResolver("target/tmp", remoteRepos, "http", "localhost", localPort, "fuse", "fuse", null);
        MavenUploadProxyServlet servlet = new MavenUploadProxyServlet(resolver, props, projectDeployer, new File("target/upload"), 0);
        HttpServletRequest request = EasyMock.createMock(HttpServletRequest.class);
        EasyMock.expect(request.getPathInfo()).andReturn(path);
        EasyMock.expect(request.getContentType()).andReturn("text/plain").anyTimes();
        EasyMock.expect(request.getInputStream()).andReturn(new ServletInputStream() {

            private int i;

            @Override
            public int read() throws IOException {
                if (i >= contents.length) {
                    return -1;
                }
                return (contents[i++] & 0xFF);
            }

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

            @Override
            public boolean isFinished() {
                return false;
            }

            @Override
            public void setReadListener(ReadListener readListener) {
            }
        });
        EasyMock.expect(request.getHeader("X-Location")).andReturn(location);
        EasyMock.expect(request.getParameter("profile")).andReturn(profile);
        EasyMock.expect(request.getParameter("version")).andReturn(version);
        final Map<String, String> headers = new HashMap<>();
        HttpServletResponse rm = EasyMock.createMock(HttpServletResponse.class);
        HttpServletResponse response = new HttpServletResponseWrapper(rm) {

            @Override
            public void addHeader(String name, String value) {
                headers.put(name, value);
            }
        };
        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();
        EasyMock.replay(request, rm);
        servlet.start();
        servlet.doPut(request, response);
        EasyMock.verify(request, rm);
        Assert.assertEquals(hasLocationHeader, headers.containsKey("X-Location"));
        return headers;
    } finally {
        server.stop();
        if (old != null) {
            System.setProperty("karaf.data", old);
        }
    }
}
Also used : Server(org.eclipse.jetty.server.Server) HashMap(java.util.HashMap) Request(org.eclipse.jetty.server.Request) HttpServletRequest(javax.servlet.http.HttpServletRequest) HttpServletResponseWrapper(javax.servlet.http.HttpServletResponseWrapper) HttpServletResponse(javax.servlet.http.HttpServletResponse) IOException(java.io.IOException) ReadListener(javax.servlet.ReadListener) AbstractHandler(org.eclipse.jetty.server.handler.AbstractHandler) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletException(javax.servlet.ServletException) ServerConnector(org.eclipse.jetty.server.ServerConnector) ServletInputStream(javax.servlet.ServletInputStream) MavenResolver(io.fabric8.maven.MavenResolver) File(java.io.File) AbstractRuntimeProperties(io.fabric8.api.scr.AbstractRuntimeProperties) RuntimeProperties(io.fabric8.api.RuntimeProperties)

Example 54 with Resolver

use of io.fabric8.patch.management.conflicts.Resolver 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);
}
Also used : ReadListener(javax.servlet.ReadListener) ByteArrayPartSource(org.apache.commons.httpclient.methods.multipart.ByteArrayPartSource) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletInputStream(javax.servlet.ServletInputStream) MavenResolver(io.fabric8.maven.MavenResolver) ProjectRequirements(io.fabric8.deployer.dto.ProjectRequirements) AbstractRuntimeProperties(io.fabric8.api.scr.AbstractRuntimeProperties) RuntimeProperties(io.fabric8.api.RuntimeProperties) JarOutputStream(java.util.jar.JarOutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) HttpMethodParams(org.apache.commons.httpclient.params.HttpMethodParams) DiskFileItemFactory(org.apache.commons.fileupload.disk.DiskFileItemFactory) MultipartRequestEntity(org.apache.commons.httpclient.methods.multipart.MultipartRequestEntity) FilePart(org.apache.commons.httpclient.methods.multipart.FilePart) FileInputStream(java.io.FileInputStream) File(java.io.File)

Example 55 with Resolver

use of io.fabric8.patch.management.conflicts.Resolver in project fabric8 by jboss-fuse.

the class MavenProxyServletSupportTest method testStartServlet.

@Test(timeout = 30000)
public void testStartServlet() throws Exception {
    String old = System.getProperty("karaf.data");
    System.setProperty("karaf.data", new File("target").getCanonicalPath());
    try {
        MavenResolver resolver = createResolver();
        MavenDownloadProxyServlet servlet = new MavenDownloadProxyServlet(resolver, runtimeProperties, projectDeployer, 5, 0);
        servlet.start();
    } finally {
        if (old != null) {
            System.setProperty("karaf.data", old);
        }
    }
}
Also used : MavenResolver(io.fabric8.maven.MavenResolver) File(java.io.File)

Aggregations

File (java.io.File)23 MavenResolver (io.fabric8.maven.MavenResolver)22 Test (org.junit.Test)21 IOException (java.io.IOException)20 FabricService (io.fabric8.api.FabricService)10 Container (io.fabric8.api.Container)9 RuntimeProperties (io.fabric8.api.RuntimeProperties)7 HashMap (java.util.HashMap)7 PlaceholderResolver (io.fabric8.api.PlaceholderResolver)6 HashSet (java.util.HashSet)6 Hashtable (java.util.Hashtable)6 Metadata (org.apache.maven.artifact.repository.metadata.Metadata)6 Map (java.util.Map)5 DownloadManager (io.fabric8.agent.download.DownloadManager)4 AbstractRuntimeProperties (io.fabric8.api.scr.AbstractRuntimeProperties)4 MavenConfigurationImpl (io.fabric8.maven.util.MavenConfigurationImpl)4 FileInputStream (java.io.FileInputStream)4 URI (java.net.URI)4 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4