Search in sources :

Example 1 with HttpCompilationResponse

use of org.kie.workbench.common.services.backend.compiler.HttpCompilationResponse in project kie-wb-common by kiegroup.

the class MavenRestHandlerTest method post.

@Test
public void post() {
    // Start preparation FS
    try {
        fileSystemTestingUtils.setup();
        ioService = fileSystemTestingUtils.getIoService();
        final String repoName = "myrepo";
        final JGitFileSystem fs = (JGitFileSystem) ioService.newFileSystem(URI.create("git://" + repoName), new HashMap<String, Object>() {

            {
                put("init", Boolean.TRUE);
                put("internal", Boolean.TRUE);
            }
        });
        ioService.startBatch(fs);
        String pom = "target/test-classes/kjar-2-single-resources/pom.xml";
        if (!runIntoMavenCLI) {
            pom = "kie-wb-common-services/kie-wb-common-compiler/kie-wb-common-compiler-distribution/target/test-classes/kjar-2-single-resources/pom.xml";
        }
        ioService.write(fs.getPath("/kjar-2-single-resources/pom.xml"), new String(java.nio.file.Files.readAllBytes(new File(pom).toPath())));
        String personDotJava = "target/test-classes/kjar-2-single-resources/src/main/java/org/kie/maven/plugin/test/Person.java";
        if (!runIntoMavenCLI) {
            personDotJava = "kie-wb-common-services/kie-wb-common-compiler/kie-wb-common-compiler-distribution/target/test-classes/kjar-2-single-resources/src/main/java/org/kie/maven/plugin/test/Person.java";
        }
        ioService.write(fs.getPath("/kjar-2-single-resources/src/main/java/org/kie/maven/plugin/test/Person.java"), new String(java.nio.file.Files.readAllBytes(new File(personDotJava).toPath())));
        String simpleRulesDotDRL = "target/test-classes/kjar-2-single-resources/src/main/resources/AllResourcesTypes/simple-rules.drl";
        if (!runIntoMavenCLI) {
            simpleRulesDotDRL = "kie-wb-common-services/kie-wb-common-compiler/kie-wb-common-compiler-distribution/target/test-classes/kjar-2-single-resources/src/main/resources/AllResourceTypes/simple-rules.drl";
        }
        ioService.write(fs.getPath("/kjar-2-single-resources/src/main/resources/AllResourcesTypes/simple-rules.drl"), new String(java.nio.file.Files.readAllBytes(new File(simpleRulesDotDRL).toPath())));
        String kmodule = "target/test-classes/kjar-2-single-resources/src/main/resources/META-INF/kmodule.xml";
        if (!runIntoMavenCLI) {
            kmodule = "kie-wb-common-services/kie-wb-common-compiler/kie-wb-common-compiler-distribution/target/test-classes/kjar-2-single-resources/src/main/resources/META-INF/kmodule.xml";
        }
        ioService.write(fs.getPath("/kjar-2-single-resources/src/main/resources/META-INF/kmodule.xml"), new String(java.nio.file.Files.readAllBytes(new File(kmodule).toPath())));
        ioService.endBatch();
        java.nio.file.Path tmpRootCloned = java.nio.file.Files.createTempDirectory("cloned");
        java.nio.file.Path tmpCloned = java.nio.file.Files.createDirectories(java.nio.file.Paths.get(tmpRootCloned.toString(), "dummy"));
        final File gitClonedFolder = new File(tmpCloned.toFile(), ".clone.git");
        final Git cloned = Git.cloneRepository().setURI(fs.getGit().getRepository().getDirectory().toURI().toString()).setBare(false).setDirectory(gitClonedFolder).call();
        assertThat(cloned).isNotNull();
        mavenRepoPath = java.nio.file.Paths.get(System.getProperty("user.home"), ".m2", "repository");
        tmpRoot = java.nio.file.Paths.get(gitClonedFolder + "/dummy/");
        // END preparation FS
        Dispatcher dispatcher = new AsynchronousDispatcher(new ResteasyProviderFactory());
        ResteasyProviderFactory.setInstance(dispatcher.getProviderFactory());
        RegisterBuiltin.register(dispatcher.getProviderFactory());
        POJOResourceFactory noDefaults = new POJOResourceFactory(MavenRestHandler.class);
        dispatcher.getRegistry().addResourceFactory(noDefaults);
        MockHttpRequest request = MockHttpRequest.create("POST", "build/maven/");
        request.header("project", tmpRoot.toAbsolutePath().toString() + "/dummy").header("mavenrepo", mavenRepoPath.toAbsolutePath().toString());
        MockHttpResponse response = new MockHttpResponse();
        SynchronousExecutionContext synchronousExecutionContext = new SynchronousExecutionContext((SynchronousDispatcher) dispatcher, request, response);
        request.setAsynchronousContext(synchronousExecutionContext);
        dispatcher.invoke(request, response);
        assertThat(response.getStatus()).isEqualTo(200);
        byte[] serializedCompilationResponse = response.getOutput();
        HttpCompilationResponse res = RestUtils.readDefaultCompilationResponseFromBytes(serializedCompilationResponse);
        assertThat(res).isNotNull();
        assertThat(res.getDependencies()).hasSize(4);
        assertThat(res.getTargetContent()).hasSize(3);
        tearDown();
    } catch (Exception e) {
        logger.error(e.getMessage());
    } finally {
        fileSystemTestingUtils.cleanup();
    }
}
Also used : MockHttpRequest(org.jboss.resteasy.mock.MockHttpRequest) HttpCompilationResponse(org.kie.workbench.common.services.backend.compiler.HttpCompilationResponse) HashMap(java.util.HashMap) SynchronousDispatcher(org.jboss.resteasy.core.SynchronousDispatcher) AsynchronousDispatcher(org.jboss.resteasy.core.AsynchronousDispatcher) Dispatcher(org.jboss.resteasy.core.Dispatcher) SynchronousExecutionContext(org.jboss.resteasy.core.SynchronousExecutionContext) POJOResourceFactory(org.jboss.resteasy.plugins.server.resourcefactory.POJOResourceFactory) Git(org.eclipse.jgit.api.Git) AsynchronousDispatcher(org.jboss.resteasy.core.AsynchronousDispatcher) ResteasyProviderFactory(org.jboss.resteasy.spi.ResteasyProviderFactory) File(java.io.File) Path(java.nio.file.Path) JGitFileSystem(org.uberfire.java.nio.fs.jgit.JGitFileSystem) MockHttpResponse(org.jboss.resteasy.mock.MockHttpResponse) Test(org.junit.Test)

Example 2 with HttpCompilationResponse

use of org.kie.workbench.common.services.backend.compiler.HttpCompilationResponse in project kie-wb-common by kiegroup.

the class MavenRestClientTest method postTest.

@Test
public void postTest() {
    try {
        fileSystemTestingUtils.setup();
        ioService = fileSystemTestingUtils.getIoService();
        final String repoName = "myrepo";
        final JGitFileSystem fs = (JGitFileSystem) ioService.newFileSystem(URI.create("git://" + repoName), new HashMap<String, Object>() {

            {
                put("init", Boolean.TRUE);
                put("internal", Boolean.TRUE);
            }
        });
        ioService.startBatch(fs);
        String pom = "target/test-classes/kjar-2-single-resources/pom.xml";
        if (!runIntoMavenCLI) {
            pom = "kie-wb-common-services/kie-wb-common-compiler/kie-wb-common-compiler-distribution/target/test-classes/kjar-2-single-resources/pom.xml";
        }
        ioService.write(fs.getPath("/kjar-2-single-resources/pom.xml"), new String(java.nio.file.Files.readAllBytes(new File(pom).toPath())));
        String personDotJava = "target/test-classes/kjar-2-single-resources/src/main/java/org/kie/maven/plugin/test/Person.java";
        if (!runIntoMavenCLI) {
            personDotJava = "kie-wb-common-services/kie-wb-common-compiler/kie-wb-common-compiler-distribution/target/test-classes/kjar-2-single-resources/src/main/java/org/kie/maven/plugin/test/Person.java";
        }
        ioService.write(fs.getPath("/kjar-2-single-resources/src/main/java/org/kie/maven/plugin/test/Person.java"), new String(java.nio.file.Files.readAllBytes(new File(personDotJava).toPath())));
        String simpleRulesDotDRL = "target/test-classes/kjar-2-single-resources/src/main/resources/AllResourcesTypes/simple-rules.drl";
        if (!runIntoMavenCLI) {
            simpleRulesDotDRL = "kie-wb-common-services/kie-wb-common-compiler/kie-wb-common-compiler-distribution/target/test-classes/kjar-2-single-resources/src/main/resources/AllResourceTypes/simple-rules.drl";
        }
        ioService.write(fs.getPath("/kjar-2-single-resources/src/main/resources/AllResourcesTypes/simple-rules.drl"), new String(java.nio.file.Files.readAllBytes(new File(simpleRulesDotDRL).toPath())));
        String kmodule = "target/test-classes/kjar-2-single-resources/src/main/resources/META-INF/kmodule.xml";
        if (!runIntoMavenCLI) {
            kmodule = "kie-wb-common-services/kie-wb-common-compiler/kie-wb-common-compiler-distribution/target/test-classes/kjar-2-single-resources/src/main/resources/META-INF/kmodule.xml";
        }
        ioService.write(fs.getPath("/kjar-2-single-resources/src/main/resources/META-INF/kmodule.xml"), new String(java.nio.file.Files.readAllBytes(new File(kmodule).toPath())));
        ioService.endBatch();
        Path tmpRootCloned = Files.createTempDirectory("cloned");
        Path tmpCloned = Files.createDirectories(Paths.get(tmpRootCloned.toString(), "dummy"));
        final File gitClonedFolder = new File(tmpCloned.toFile(), ".clone.git");
        final Git cloned = Git.cloneRepository().setURI(fs.getGit().getRepository().getDirectory().toURI().toString()).setBare(false).setDirectory(gitClonedFolder).call();
        assertThat(cloned).isNotNull();
        mavenRepoPath = Paths.get(System.getProperty("user.home"), ".m2", "repository");
        tmpRoot = Paths.get(gitClonedFolder + "/dummy/");
        Client client = ClientBuilder.newClient();
        WebTarget target = client.target(deploymentUrl.toString() + "rest/maven/");
        MultivaluedMap headersMap = new MultivaluedHashMap();
        headersMap.add("project", tmpRoot.toAbsolutePath().toString() + "/dummy");
        headersMap.add("mavenrepo", mavenRepoPath.toAbsolutePath().toString());
        headersMap.add("settings_xml", mavenSettingsPath);
        Future<Response> responseFuture = target.request().headers(headersMap).async().post(Entity.entity(String.class, MediaType.TEXT_PLAIN));
        Response response = responseFuture.get();
        assertThat(response.getStatusInfo().getStatusCode()).isEqualTo(200);
        InputStream is = response.readEntity(InputStream.class);
        byte[] serializedCompilationResponse = IOUtils.toByteArray(is);
        HttpCompilationResponse res = RestUtils.readDefaultCompilationResponseFromBytes(serializedCompilationResponse);
        assertThat(res).isNotNull();
        assertThat(res.getDependencies()).hasSize(4);
        assertThat(res.getTargetContent()).hasSize(3);
        tearDown();
    } catch (Exception e) {
        logger.error(e.getMessage());
    } finally {
        fileSystemTestingUtils.cleanup();
    }
}
Also used : Path(java.nio.file.Path) HttpCompilationResponse(org.kie.workbench.common.services.backend.compiler.HttpCompilationResponse) HashMap(java.util.HashMap) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) InputStream(java.io.InputStream) MultivaluedHashMap(javax.ws.rs.core.MultivaluedHashMap) HttpCompilationResponse(org.kie.workbench.common.services.backend.compiler.HttpCompilationResponse) Response(javax.ws.rs.core.Response) Git(org.eclipse.jgit.api.Git) WebTarget(javax.ws.rs.client.WebTarget) Client(javax.ws.rs.client.Client) MultivaluedMap(javax.ws.rs.core.MultivaluedMap) File(java.io.File) JGitFileSystem(org.uberfire.java.nio.fs.jgit.JGitFileSystem) Test(org.junit.Test)

Example 3 with HttpCompilationResponse

use of org.kie.workbench.common.services.backend.compiler.HttpCompilationResponse in project kie-wb-common by kiegroup.

the class RestUtils method readDefaultCompilationResponseFromBytes.

public static HttpCompilationResponse readDefaultCompilationResponseFromBytes(byte[] bytes) {
    ObjectInput in = null;
    ByteArrayOutputStream bos = null;
    try {
        in = new ObjectInputStream(new ByteArrayInputStream(bytes));
        Object newObj = in.readObject();
        return (HttpCompilationResponse) newObj;
    } catch (NotSerializableException nse) {
        logger.warn("Object is not serializable {}", nse.getMessage());
        StringBuilder sb = new StringBuilder("NotSerializableException:").append(nse.getMessage());
        logger.error(sb.toString());
    } catch (IOException ioe) {
        StringBuilder sb = new StringBuilder("IOException:").append(ioe.getMessage());
        logger.error(sb.toString());
    } catch (ClassNotFoundException cnfe) {
        StringBuilder sb = new StringBuilder("ClassNotFoundException:").append(cnfe.getMessage());
        logger.error(sb.toString());
    } catch (Exception e) {
        StringBuilder sb = new StringBuilder("Exception:").append(e.getMessage());
        logger.error(sb.toString());
    } finally {
        try {
            if (bos != null) {
                bos.close();
            }
            if (in != null) {
                in.close();
            }
        } catch (IOException ex) {
            logger.error(ex.getMessage());
        }
    }
    return new DefaultHttpCompilationResponse(Boolean.FALSE);
}
Also used : NotSerializableException(java.io.NotSerializableException) HttpCompilationResponse(org.kie.workbench.common.services.backend.compiler.HttpCompilationResponse) DefaultHttpCompilationResponse(org.kie.workbench.common.services.backend.compiler.impl.DefaultHttpCompilationResponse) ByteArrayInputStream(java.io.ByteArrayInputStream) DefaultHttpCompilationResponse(org.kie.workbench.common.services.backend.compiler.impl.DefaultHttpCompilationResponse) ObjectInput(java.io.ObjectInput) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) IOException(java.io.IOException) NotSerializableException(java.io.NotSerializableException) ObjectInputStream(java.io.ObjectInputStream)

Aggregations

HttpCompilationResponse (org.kie.workbench.common.services.backend.compiler.HttpCompilationResponse)3 File (java.io.File)2 Path (java.nio.file.Path)2 HashMap (java.util.HashMap)2 Git (org.eclipse.jgit.api.Git)2 Test (org.junit.Test)2 JGitFileSystem (org.uberfire.java.nio.fs.jgit.JGitFileSystem)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 NotSerializableException (java.io.NotSerializableException)1 ObjectInput (java.io.ObjectInput)1 ObjectInputStream (java.io.ObjectInputStream)1 Client (javax.ws.rs.client.Client)1 WebTarget (javax.ws.rs.client.WebTarget)1 MultivaluedHashMap (javax.ws.rs.core.MultivaluedHashMap)1 MultivaluedMap (javax.ws.rs.core.MultivaluedMap)1 Response (javax.ws.rs.core.Response)1 AsynchronousDispatcher (org.jboss.resteasy.core.AsynchronousDispatcher)1