Search in sources :

Example 1 with ListInstancesWrapper

use of org.apache.helix.webapp.resources.InstancesResource.ListInstancesWrapper in project helix by apache.

the class TestHelixAdminScenariosRest method testGetInstances.

@Test
public void testGetInstances() throws IOException {
    final String clusterName = "TestTagAwareness_testGetResources";
    final String[] TAGS = { "tag1", "tag2" };
    final String URL_BASE = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/instances";
    _gSetupTool.addCluster(clusterName, true);
    HelixAdmin admin = _gSetupTool.getClusterManagementTool();
    // Add 4 participants, each with differint tag characteristics
    InstanceConfig instance1 = new InstanceConfig("localhost_1");
    instance1.addTag(TAGS[0]);
    admin.addInstance(clusterName, instance1);
    InstanceConfig instance2 = new InstanceConfig("localhost_2");
    instance2.addTag(TAGS[1]);
    admin.addInstance(clusterName, instance2);
    InstanceConfig instance3 = new InstanceConfig("localhost_3");
    instance3.addTag(TAGS[0]);
    instance3.addTag(TAGS[1]);
    admin.addInstance(clusterName, instance3);
    InstanceConfig instance4 = new InstanceConfig("localhost_4");
    admin.addInstance(clusterName, instance4);
    // 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);
    ListInstancesWrapper responseWrapper = ClusterRepresentationUtil.JsonToObject(ListInstancesWrapper.class, response.getEntityAsText());
    Map<String, List<String>> tagInfo = responseWrapper.tagInfo;
    // Ensure tag ownership is reported correctly
    Assert.assertTrue(tagInfo.containsKey(TAGS[0]));
    Assert.assertTrue(tagInfo.containsKey(TAGS[1]));
    Assert.assertTrue(tagInfo.get(TAGS[0]).contains("localhost_1"));
    Assert.assertFalse(tagInfo.get(TAGS[0]).contains("localhost_2"));
    Assert.assertTrue(tagInfo.get(TAGS[0]).contains("localhost_3"));
    Assert.assertFalse(tagInfo.get(TAGS[0]).contains("localhost_4"));
    Assert.assertFalse(tagInfo.get(TAGS[1]).contains("localhost_1"));
    Assert.assertTrue(tagInfo.get(TAGS[1]).contains("localhost_2"));
    Assert.assertTrue(tagInfo.get(TAGS[1]).contains("localhost_3"));
    Assert.assertFalse(tagInfo.get(TAGS[1]).contains("localhost_4"));
}
Also used : Response(org.restlet.Response) ListInstancesWrapper(org.apache.helix.webapp.resources.InstancesResource.ListInstancesWrapper) InstanceConfig(org.apache.helix.model.InstanceConfig) Reference(org.restlet.data.Reference) Request(org.restlet.Request) List(java.util.List) HelixAdmin(org.apache.helix.HelixAdmin) Test(org.testng.annotations.Test)

Example 2 with ListInstancesWrapper

use of org.apache.helix.webapp.resources.InstancesResource.ListInstancesWrapper in project helix by apache.

the class TestClusterManagementWebapp method verifyAddInstance.

void verifyAddInstance() throws JsonGenerationException, JsonMappingException, IOException {
    String httpUrlBase = "http://localhost:" + ADMIN_PORT + "/clusters/" + clusterName + "/instances";
    Map<String, String> paraMap = new HashMap<String, String>();
    // Add 1 instance
    paraMap.put(JsonParameters.INSTANCE_NAME, instance1 + ":" + instancePort);
    paraMap.put(JsonParameters.MANAGEMENT_COMMAND, ClusterSetup.addInstance);
    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();
    TypeReference<ListInstancesWrapper> typeRef = new TypeReference<ListInstancesWrapper>() {
    };
    ListInstancesWrapper wrapper = mapper.readValue(new StringReader(sw.toString()), typeRef);
    List<ZNRecord> znList = wrapper.instanceInfo;
    AssertJUnit.assertTrue(znList.get(0).getId().equals(instance1 + "_" + instancePort));
    // the case to add more than 1 instances
    paraMap.clear();
    paraMap.put(JsonParameters.MANAGEMENT_COMMAND, ClusterSetup.addInstance);
    String[] instances = { "test2", "test3", "test4", "test5" };
    String instanceNames = "";
    boolean first = true;
    for (String instance : instances) {
        if (first == true) {
            first = false;
        } else {
            instanceNames += ";";
        }
        instanceNames += (instance + ":" + instancePort);
    }
    paraMap.put(JsonParameters.INSTANCE_NAMES, instanceNames);
    request = new Request(Method.POST, resourceRef);
    request.setEntity(JsonParameters.JSON_PARAMETERS + "=" + ClusterRepresentationUtil.ObjectToJson(paraMap), MediaType.APPLICATION_ALL);
    response = _gClient.handle(request);
    result = response.getEntity();
    sw = new StringWriter();
    result.write(sw);
    // System.out.println(sw.toString());
    mapper = new ObjectMapper();
    wrapper = mapper.readValue(new StringReader(sw.toString()), typeRef);
    znList = wrapper.instanceInfo;
    for (String instance : instances) {
        boolean found = false;
        for (ZNRecord r : znList) {
            String instanceId = instance + "_" + instancePort;
            if (r.getId().equals(instanceId)) {
                found = true;
                break;
            }
        }
        AssertJUnit.assertTrue(found);
    }
}
Also used : HashMap(java.util.HashMap) Reference(org.restlet.data.Reference) TypeReference(org.codehaus.jackson.type.TypeReference) Request(org.restlet.Request) Representation(org.restlet.representation.Representation) Response(org.restlet.Response) ListInstancesWrapper(org.apache.helix.webapp.resources.InstancesResource.ListInstancesWrapper) StringWriter(java.io.StringWriter) StringReader(java.io.StringReader) TypeReference(org.codehaus.jackson.type.TypeReference) ObjectMapper(org.codehaus.jackson.map.ObjectMapper) ZNRecord(org.apache.helix.ZNRecord)

Aggregations

ListInstancesWrapper (org.apache.helix.webapp.resources.InstancesResource.ListInstancesWrapper)2 Request (org.restlet.Request)2 Response (org.restlet.Response)2 Reference (org.restlet.data.Reference)2 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1 List (java.util.List)1 HelixAdmin (org.apache.helix.HelixAdmin)1 ZNRecord (org.apache.helix.ZNRecord)1 InstanceConfig (org.apache.helix.model.InstanceConfig)1 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)1 TypeReference (org.codehaus.jackson.type.TypeReference)1 Representation (org.restlet.representation.Representation)1 Test (org.testng.annotations.Test)1