Search in sources :

Example 66 with ClientConfig

use of org.glassfish.jersey.client.ClientConfig in project streamline by hortonworks.

the class RestIntegrationTest method testComponentAPIsCanDistinguishCluster.

/**
 * Test whether component API can distinguish cluster and service
 *
 * @throws Exception
 */
@Test
public void testComponentAPIsCanDistinguishCluster() throws Exception {
    Client client = ClientBuilder.newClient(new ClientConfig());
    Long clusterId = 1L;
    Long serviceId = 1L;
    // create Cluster and Service first
    storeTestCluster(client, clusterId);
    storeTestService(client, clusterId, serviceId);
    String componentBaseUrl = rootUrl + String.format("services/%d/components", serviceId);
    Component component = createComponent(serviceId, 1L, "testComponent:" + 1);
    String componentEntityUrl = componentBaseUrl + "/" + 1;
    client.target(componentEntityUrl).request().put(Entity.json(component), Component.class);
    Long anotherClusterId = 2L;
    Long anotherServiceId = 2L;
    componentBaseUrl = rootUrl + String.format("services/%d/components", anotherServiceId);
    String response = client.target(componentBaseUrl).request().get(String.class);
    Assert.assertEquals(Collections.emptyList(), getEntities(response, Component.class));
    componentEntityUrl = componentBaseUrl + "/" + 1;
    try {
        client.target(componentEntityUrl).request().get(String.class);
        Assert.fail("Should have thrown NotFoundException.");
    } catch (NotFoundException e) {
    // passed
    }
    removeComponent(client, serviceId, component.getId());
    removeService(client, clusterId, serviceId);
    removeCluster(client, clusterId);
}
Also used : NotFoundException(javax.ws.rs.NotFoundException) Client(javax.ws.rs.client.Client) ClientConfig(org.glassfish.jersey.client.ClientConfig) IntegrationTest(com.hortonworks.streamline.common.test.IntegrationTest) Test(org.junit.Test)

Example 67 with ClientConfig

use of org.glassfish.jersey.client.ClientConfig in project streamline by hortonworks.

the class RestIntegrationTest method testServiceAPIsCanDistinguishCluster.

/**
 * Test whether service API can distinguish cluster
 *
 * @throws Exception
 */
@Test
public void testServiceAPIsCanDistinguishCluster() throws Exception {
    Client client = ClientBuilder.newClient(new ClientConfig());
    Long clusterId = 1L;
    // create Cluster first
    storeTestCluster(client, clusterId);
    String serviceBaseUrl = rootUrl + String.format("clusters/%d/services", clusterId);
    Service service = createService(clusterId, 1L, "testService:" + 1);
    String serviceEntityUrl = serviceBaseUrl + "/" + 1;
    client.target(serviceEntityUrl).request().put(Entity.json(service), Service.class);
    Long anotherClusterId = 2L;
    serviceBaseUrl = rootUrl + String.format("clusters/%d/services", anotherClusterId);
    String response = client.target(serviceBaseUrl).request().get(String.class);
    Assert.assertEquals(Collections.emptyList(), getEntities(response, Service.class));
    serviceEntityUrl = serviceBaseUrl + "/" + 1;
    try {
        client.target(serviceEntityUrl).request().get(String.class);
        Assert.fail("Should have thrown NotFoundException.");
    } catch (NotFoundException e) {
    // passed
    }
    removeService(client, clusterId, service.getId());
    removeCluster(client, clusterId);
}
Also used : NotFoundException(javax.ws.rs.NotFoundException) Client(javax.ws.rs.client.Client) ClientConfig(org.glassfish.jersey.client.ClientConfig) IntegrationTest(com.hortonworks.streamline.common.test.IntegrationTest) Test(org.junit.Test)

Example 68 with ClientConfig

use of org.glassfish.jersey.client.ClientConfig in project streamline by hortonworks.

the class RestIntegrationTest method testNamespaceServiceClusterMapping.

@Test
public void testNamespaceServiceClusterMapping() {
    Client client = ClientBuilder.newClient(new ClientConfig());
    // precondition: namespace
    Long namespaceId = 999L;
    String namespaceName = "nstest";
    storeTestNamespace(client, namespaceId, namespaceName);
    NamespaceServiceClusterMap retrMapping;
    List<NamespaceServiceClusterMap> entities;
    String response;
    // add
    String serviceName = "STORM";
    Long clusterId = 1L;
    NamespaceServiceClusterMap mapping1 = new NamespaceServiceClusterMap(namespaceId, serviceName, clusterId);
    retrMapping = mapNamespaceServiceCluster(client, mapping1);
    Assert.assertEquals(mapping1, retrMapping);
    // add2
    String serviceName2 = "HDFS";
    Long cluster2Id = 2L;
    NamespaceServiceClusterMap mapping2 = new NamespaceServiceClusterMap(namespaceId, serviceName2, cluster2Id);
    retrMapping = mapNamespaceServiceCluster(client, mapping2);
    Assert.assertEquals(mapping2, retrMapping);
    // remove (used once so no extraction)
    String removeMappingUrl = rootUrl + "namespaces/" + namespaceId + "/mapping/" + serviceName + "/cluster/" + clusterId;
    response = client.target(removeMappingUrl).request().delete(String.class);
    retrMapping = getEntity(response, NamespaceServiceClusterMap.class);
    Assert.assertEquals(mapping1, retrMapping);
    entities = listMappingInNamespace(client, namespaceId);
    Assert.assertEquals(1, entities.size());
    // add3
    String serviceName3 = "HBASE";
    Long cluster3Id = 2L;
    NamespaceServiceClusterMap mapping3 = new NamespaceServiceClusterMap(namespaceId, serviceName3, cluster3Id);
    retrMapping = mapNamespaceServiceCluster(client, mapping3);
    Assert.assertEquals(mapping3, retrMapping);
    // remove all (used once so no extraction)
    String removeAllMappingsUrl = rootUrl + "namespaces/" + namespaceId + "/mapping";
    response = client.target(removeAllMappingsUrl).request().delete(String.class);
    entities = getEntities(response, NamespaceServiceClusterMap.class);
    Assert.assertEquals(2, entities.size());
    entities = listMappingInNamespace(client, namespaceId);
    Assert.assertEquals(0, entities.size());
    // cleanup : remove namespace
    String removeNamespaceUrl = rootUrl + "namespaces/" + namespaceId;
    client.target(removeNamespaceUrl).request().delete();
}
Also used : Client(javax.ws.rs.client.Client) ClientConfig(org.glassfish.jersey.client.ClientConfig) IntegrationTest(com.hortonworks.streamline.common.test.IntegrationTest) Test(org.junit.Test)

Example 69 with ClientConfig

use of org.glassfish.jersey.client.ClientConfig in project streamline by hortonworks.

the class RestIntegrationTest method testAllResources.

/**
 * For each TestResource element in resourcesToTest List, tests Post, Put, Get and Delete.
 *
 * @throws Exception
 */
@Test
public void testAllResources() throws Exception {
    Client client = ClientBuilder.newClient(new ClientConfig());
    client.register(MultiPartFeature.class);
    for (ResourceTestElement resourceToTest : resourcesToTest) {
        String url = resourceToTest.url;
        Object resourceToPost = resourceToTest.resourceToPost;
        Object resourceToPut = resourceToTest.resourceToPut;
        List<ResourceTestElement> resourcesToPostFirst = resourceToTest.resourcesToPostFirst;
        for (ResourceTestElement dependantResource : resourcesToPostFirst) {
            Response response;
            if (dependantResource.multipart) {
                response = client.target(dependantResource.url).request().post(Entity.entity(getMultiPart(dependantResource, dependantResource.resourceToPost), MediaType.MULTIPART_FORM_DATA));
            } else {
                response = client.target(dependantResource.url).request().post(Entity.json(dependantResource.resourceToPost));
            }
            Assert.assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus());
        }
        String id = resourceToTest.id;
        Response response;
        if (resourceToTest.multipart) {
            response = client.target(url).request().post(Entity.entity(getMultiPart(resourceToTest, resourceToPost), MediaType.MULTIPART_FORM_DATA));
        } else {
            System.out.println("url " + url);
            response = client.target(url).request().post(Entity.json(resourceToPost));
        }
        Assert.assertEquals(Response.Status.CREATED.getStatusCode(), response.getStatus());
        String jsonResponse = client.target(url).request().get(String.class);
        Assert.assertEquals(Lists.newArrayList(filterFields(resourceToPost, resourceToTest.fieldsToIgnore)), getEntities(jsonResponse, resourceToPost.getClass(), resourceToTest.fieldsToIgnore));
        url = url + "/" + id;
        Object entityResponse = client.target(url).request().get(resourceToPost.getClass());
        Assert.assertEquals(resourceToPost, filterFields(entityResponse, resourceToTest.fieldsToIgnore));
        if (resourceToPut != null) {
            if (resourceToTest.multipart) {
                client.target(url).request().put(Entity.entity(getMultiPart(resourceToTest, resourceToPut), MediaType.MULTIPART_FORM_DATA));
            } else {
                client.target(url).request().put(Entity.json(resourceToPut));
            }
            entityResponse = client.target(url).request().get(resourceToPut.getClass());
            Assert.assertEquals(filterFields(resourceToPut, resourceToTest.fieldsToIgnore), filterFields(entityResponse, resourceToTest.fieldsToIgnore));
        }
        try {
            entityResponse = client.target(url).request().delete(resourceToPut.getClass());
            Assert.assertEquals(resourceToPut, filterFields(entityResponse, resourceToTest.fieldsToIgnore));
        } finally {
            for (ResourceTestElement dependantResource : Lists.reverse(resourcesToPostFirst)) {
                entityResponse = client.target(dependantResource.url + "/" + dependantResource.id).request().delete(dependantResource.resourceToPost.getClass());
                Assert.assertEquals(filterFields(dependantResource.resourceToPost, dependantResource.fieldsToIgnore), filterFields(entityResponse, dependantResource.fieldsToIgnore));
            }
        }
        try {
            client.target(url).request().get(String.class);
            Assert.fail("Should have thrown NotFoundException.");
        } catch (NotFoundException e) {
        // passed
        }
    }
}
Also used : Response(javax.ws.rs.core.Response) NotFoundException(javax.ws.rs.NotFoundException) Client(javax.ws.rs.client.Client) ClientConfig(org.glassfish.jersey.client.ClientConfig) IntegrationTest(com.hortonworks.streamline.common.test.IntegrationTest) Test(org.junit.Test)

Example 70 with ClientConfig

use of org.glassfish.jersey.client.ClientConfig in project streamline by hortonworks.

the class RestIntegrationTest method testCustomProcessorInfos.

@Test
@Ignore
public void testCustomProcessorInfos() throws Exception {
    // Some issue with sending multi part for requests using this client and hence this test case is ignored for now. Fix later.
    String response;
    String prefixUrl = rootUrl + "streams/componentbundles/PROCESSOR/custom";
    CustomProcessorInfo customProcessorInfo = createCustomProcessorInfo();
    String prefixQueryParam = "?streamingEngine=STORM";
    List<String> getUrlQueryParms = new ArrayList<String>();
    getUrlQueryParms.add(prefixQueryParam + "&name=ConsoleCustomProcessor");
    getUrlQueryParms.add(prefixQueryParam + "&jarFileName=streamline-core.jar");
    getUrlQueryParms.add(prefixQueryParam + "&customProcessorImpl=" + ConsoleCustomProcessor.class.getCanonicalName());
    List<List<CustomProcessorInfo>> getResults = new ArrayList<List<CustomProcessorInfo>>();
    getResults.add(Arrays.asList(customProcessorInfo));
    getResults.add(Arrays.asList(customProcessorInfo));
    getResults.add(Arrays.asList(customProcessorInfo));
    getResults.add(Arrays.asList(customProcessorInfo));
    List<String> getUrls = new ArrayList<String>();
    for (String queryParam : getUrlQueryParms) {
        getUrls.add(prefixUrl + queryParam);
    }
    ClientConfig clientConfig = new ClientConfig();
    clientConfig.register(MultiPartWriter.class);
    Client client = ClientBuilder.newClient(clientConfig);
    for (String getUrl : getUrls) {
        try {
            client.target(getUrl).request().get(String.class);
            Assert.fail("Should have thrown NotFoundException.");
        } catch (NotFoundException e) {
        // pass
        }
    }
    /*FileDataBodyPart imageFileBodyPart = new FileDataBodyPart(TopologyCatalogResource.IMAGE_FILE_PARAM_NAME, getCpImageFile(), MediaType
                .APPLICATION_OCTET_STREAM_TYPE);
        FileDataBodyPart jarFileBodyPart = new FileDataBodyPart(TopologyCatalogResource.JAR_FILE_PARAM_NAME, getCpJarFile(), MediaType
                .APPLICATION_OCTET_STREAM_TYPE);*/
    MultiPart multiPart = new MultiPart(MediaType.MULTIPART_FORM_DATA_TYPE);
    multiPart.bodyPart(new StreamDataBodyPart(TopologyComponentBundleResource.JAR_FILE_PARAM_NAME, JAR_FILE_STREAM));
    multiPart.bodyPart(new FormDataBodyPart(TopologyComponentBundleResource.CP_INFO_PARAM_NAME, customProcessorInfo, MediaType.APPLICATION_JSON_TYPE));
    client.target(prefixUrl).request(MediaType.MULTIPART_FORM_DATA_TYPE).post(Entity.entity(multiPart, multiPart.getMediaType()));
    for (int i = 0; i < getUrls.size(); ++i) {
        String getUrl = getUrls.get(i);
        List<CustomProcessorInfo> expectedResults = getResults.get(i);
        response = client.target(getUrl).request().get(String.class);
        Assert.assertEquals(expectedResults, getEntities(response, expectedResults.get(i).getClass()));
    }
}
Also used : StreamDataBodyPart(org.glassfish.jersey.media.multipart.file.StreamDataBodyPart) NotFoundException(javax.ws.rs.NotFoundException) CustomProcessorInfo(com.hortonworks.streamline.streams.catalog.processor.CustomProcessorInfo) MultiPart(org.glassfish.jersey.media.multipart.MultiPart) FormDataBodyPart(org.glassfish.jersey.media.multipart.FormDataBodyPart) ClientConfig(org.glassfish.jersey.client.ClientConfig) Client(javax.ws.rs.client.Client) Ignore(org.junit.Ignore) IntegrationTest(com.hortonworks.streamline.common.test.IntegrationTest) Test(org.junit.Test)

Aggregations

ClientConfig (org.glassfish.jersey.client.ClientConfig)169 Client (javax.ws.rs.client.Client)129 Test (org.junit.Test)85 Response (javax.ws.rs.core.Response)66 JerseyTest (org.glassfish.jersey.test.JerseyTest)52 WebTarget (javax.ws.rs.client.WebTarget)48 ClientBuilder (javax.ws.rs.client.ClientBuilder)17 SSLContext (javax.net.ssl.SSLContext)12 Invocation (javax.ws.rs.client.Invocation)12 ClientResponse (org.glassfish.jersey.client.ClientResponse)12 ApacheConnectorProvider (org.glassfish.jersey.apache.connector.ApacheConnectorProvider)10 IOException (java.io.IOException)9 IntegrationTest (com.hortonworks.streamline.common.test.IntegrationTest)8 URI (java.net.URI)7 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)7 CredentialsProvider (org.apache.http.client.CredentialsProvider)7 PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)7 CountDownLatch (java.util.concurrent.CountDownLatch)6 JerseyClient (org.glassfish.jersey.client.JerseyClient)6 JacksonJsonProvider (com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider)5