Search in sources :

Example 36 with Reference

use of org.restlet.data.Reference in project helix by apache.

the class TestClusterManagementWebapp method verifyAddCluster.

void verifyAddCluster() throws IOException, InterruptedException {
    String httpUrlBase = "http://localhost:" + ADMIN_PORT + "/clusters";
    Map<String, String> paraMap = new HashMap<String, String>();
    paraMap.put(JsonParameters.CLUSTER_NAME, clusterName);
    paraMap.put(JsonParameters.MANAGEMENT_COMMAND, ClusterSetup.addCluster);
    Reference resourceRef = new Reference(httpUrlBase);
    Request request = new Request(Method.POST, resourceRef);
    request.setEntity(JsonParameters.JSON_PARAMETERS + "=" + ClusterRepresentationUtil.ObjectToJson(paraMap), MediaType.APPLICATION_ALL);
    Response response = _gClient.handle(request);
    Representation result = response.getEntity();
    StringWriter sw = new StringWriter();
    result.write(sw);
    // System.out.println(sw.toString());
    ObjectMapper mapper = new ObjectMapper();
    ZNRecord zn = mapper.readValue(new StringReader(sw.toString()), ZNRecord.class);
    AssertJUnit.assertTrue(zn.getListField("clusters").contains(clusterName));
}
Also used : Response(org.restlet.Response) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) Reference(org.restlet.data.Reference) TypeReference(org.codehaus.jackson.type.TypeReference) Request(org.restlet.Request) StringReader(java.io.StringReader) Representation(org.restlet.representation.Representation) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) ZNRecord(org.apache.helix.ZNRecord)

Example 37 with Reference

use of org.restlet.data.Reference in project helix by apache.

the class TestClusterManagementWebapp method verifyRebalance.

void verifyRebalance() throws JsonGenerationException, JsonMappingException, IOException {
    String httpUrlBase = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/resourceGroups/" + resourceGroupName + "/idealState";
    Map<String, String> paraMap = new HashMap<String, String>();
    // Add 1 instance
    paraMap.put(JsonParameters.REPLICAS, "" + replicas);
    paraMap.put(JsonParameters.MANAGEMENT_COMMAND, ClusterSetup.rebalance);
    Reference resourceRef = new Reference(httpUrlBase);
    Request request = new Request(Method.POST, resourceRef);
    request.setEntity(JsonParameters.JSON_PARAMETERS + "=" + ClusterRepresentationUtil.ObjectToJson(paraMap), MediaType.APPLICATION_ALL);
    Response response = _gClient.handle(request);
    Representation result = response.getEntity();
    StringWriter sw = new StringWriter();
    result.write(sw);
    // System.out.println(sw.toString());
    ObjectMapper mapper = new ObjectMapper();
    ZNRecord r = mapper.readValue(new StringReader(sw.toString()), ZNRecord.class);
    for (int i = 0; i < partitions; i++) {
        String partitionName = resourceGroupName + "_" + i;
        assert (r.getMapField(partitionName).size() == replicas);
    }
    httpUrlBase = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName;
    resourceRef = new Reference(httpUrlBase);
    request = new Request(Method.GET, resourceRef);
    response = _gClient.handle(request);
    result = response.getEntity();
    sw = new StringWriter();
    result.write(sw);
}
Also used : Response(org.restlet.Response) StringWriter(java.io.StringWriter) HashMap(java.util.HashMap) Reference(org.restlet.data.Reference) TypeReference(org.codehaus.jackson.type.TypeReference) Request(org.restlet.Request) StringReader(java.io.StringReader) Representation(org.restlet.representation.Representation) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) ZNRecord(org.apache.helix.ZNRecord)

Example 38 with Reference

use of org.restlet.data.Reference in project helix by apache.

the class TestHelixAdminScenariosRest method testGetResources.

@Test
public void testGetResources() throws IOException {
    final String clusterName = "TestTagAwareness_testGetResources";
    final String TAG = "tag";
    final String URL_BASE = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/resourceGroups";
    _gSetupTool.addCluster(clusterName, true);
    HelixAdmin admin = _gSetupTool.getClusterManagementTool();
    // Add a tagged resource
    IdealState taggedResource = new IdealState("taggedResource");
    taggedResource.setInstanceGroupTag(TAG);
    taggedResource.setStateModelDefRef("OnlineOffline");
    admin.addResource(clusterName, taggedResource.getId(), taggedResource);
    // Add an untagged resource
    IdealState untaggedResource = new IdealState("untaggedResource");
    untaggedResource.setStateModelDefRef("OnlineOffline");
    admin.addResource(clusterName, untaggedResource.getId(), untaggedResource);
    // Now make a REST call for all resources
    Reference resourceRef = new Reference(URL_BASE);
    Request request = new Request(Method.GET, resourceRef);
    Response response = _gClient.handle(request);
    ZNRecord responseRecord = ClusterRepresentationUtil.JsonToObject(ZNRecord.class, response.getEntityAsText());
    // Ensure that the tagged resource has information and the untagged one doesn't
    Assert.assertNotNull(responseRecord.getMapField("ResourceTags"));
    Assert.assertEquals(TAG, responseRecord.getMapField("ResourceTags").get(taggedResource.getId()));
    Assert.assertFalse(responseRecord.getMapField("ResourceTags").containsKey(untaggedResource.getId()));
}
Also used : Response(org.restlet.Response) Reference(org.restlet.data.Reference) Request(org.restlet.Request) HelixAdmin(org.apache.helix.HelixAdmin) IdealState(org.apache.helix.model.IdealState) ZNRecord(org.apache.helix.ZNRecord) Test(org.testng.annotations.Test)

Example 39 with Reference

use of org.restlet.data.Reference in project helix by apache.

the class TestHelixAdminScenariosRest method getUrl.

String getUrl(String url) throws IOException {
    Reference resourceRef = new Reference(url);
    Request request = new Request(Method.GET, resourceRef);
    Response response = _gClient.handle(request);
    Representation result = response.getEntity();
    StringWriter sw = new StringWriter();
    result.write(sw);
    return sw.toString();
}
Also used : Response(org.restlet.Response) StringWriter(java.io.StringWriter) Reference(org.restlet.data.Reference) Request(org.restlet.Request) Representation(org.restlet.representation.Representation)

Example 40 with Reference

use of org.restlet.data.Reference in project helix by apache.

the class TestHelixAdminScenariosRest method assertSuccessPostOperation.

static String assertSuccessPostOperation(String url, Map<String, String> jsonParameters, Map<String, String> extraForm, boolean hasException) throws IOException {
    Reference resourceRef = new Reference(url);
    int numRetries = 0;
    while (numRetries <= MAX_RETRIES) {
        Request request = new Request(Method.POST, resourceRef);
        if (extraForm != null) {
            String entity = JsonParameters.JSON_PARAMETERS + "=" + ClusterRepresentationUtil.ObjectToJson(jsonParameters);
            for (String key : extraForm.keySet()) {
                entity = entity + "&" + (key + "=" + extraForm.get(key));
            }
            request.setEntity(entity, MediaType.APPLICATION_ALL);
        } else {
            request.setEntity(JsonParameters.JSON_PARAMETERS + "=" + ClusterRepresentationUtil.ObjectToJson(jsonParameters), MediaType.APPLICATION_ALL);
        }
        Response response = _gClient.handle(request);
        Representation result = response.getEntity();
        StringWriter sw = new StringWriter();
        if (result != null) {
            result.write(sw);
        }
        int code = response.getStatus().getCode();
        boolean successCode = code == Status.SUCCESS_NO_CONTENT.getCode() || code == Status.SUCCESS_OK.getCode();
        if (successCode || numRetries == MAX_RETRIES) {
            Assert.assertTrue(successCode);
            Assert.assertTrue(hasException == sw.toString().toLowerCase().contains("exception"));
            return sw.toString();
        }
        numRetries++;
    }
    Assert.fail("Request failed after all retries");
    return null;
}
Also used : Response(org.restlet.Response) StringWriter(java.io.StringWriter) Reference(org.restlet.data.Reference) Request(org.restlet.Request) Representation(org.restlet.representation.Representation)

Aggregations

Reference (org.restlet.data.Reference)46 Request (org.restlet.Request)26 Response (org.restlet.Response)26 Representation (org.restlet.representation.Representation)19 StringWriter (java.io.StringWriter)15 HashMap (java.util.HashMap)11 ZNRecord (org.apache.helix.ZNRecord)11 StringReader (java.io.StringReader)10 TypeReference (org.codehaus.jackson.type.TypeReference)10 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)9 ResourceException (org.restlet.resource.ResourceException)8 Form (org.restlet.data.Form)5 IOException (java.io.IOException)3 Date (java.util.Date)3 Test (org.junit.Test)3 ContextResourceClient (org.qi4j.library.rest.client.api.ContextResourceClient)3 ContextResourceClientFactory (org.qi4j.library.rest.client.api.ContextResourceClientFactory)3 ErrorHandler (org.qi4j.library.rest.client.api.ErrorHandler)3 HandlerCommand (org.qi4j.library.rest.client.api.HandlerCommand)3 ResponseHandler (org.qi4j.library.rest.client.spi.ResponseHandler)3