Package org.apache.ambari.server.api.services

Examples of org.apache.ambari.server.api.services.RequestBody


  }

  @Test
  public void testParse_Array() throws BodyParseException {
    RequestBodyParser parser = new JsonRequestBodyParser();
    RequestBody body = parser.parse(arrayJson).iterator().next();

    Set<NamedPropertySet> setProps = body.getNamedPropertySets();

    assertEquals(3, setProps.size());

    boolean cluster1Matches = false;
    boolean cluster2Matches = false;
    boolean cluster3Matches = false;

    Map<String, String> mapCluster1 = new HashMap<String, String>();
    mapCluster1.put(PropertyHelper.getPropertyId("Clusters", "cluster_name"), "unitTestCluster1");

    Map<String, String> mapCluster2 = new HashMap<String, String>();
    mapCluster2.put(PropertyHelper.getPropertyId("Clusters", "cluster_name"), "unitTestCluster2");
    mapCluster2.put(PropertyHelper.getPropertyId("Clusters", "property1"), "prop1Value");


    Map<String, String> mapCluster3 = new HashMap<String, String>();
    mapCluster3.put(PropertyHelper.getPropertyId("Clusters", "cluster_name"), "unitTestCluster3");
    mapCluster3.put(PropertyHelper.getPropertyId("Clusters/Category", "property2"), "prop2Value");


    for (NamedPropertySet propertySet : setProps) {
      assertEquals("", propertySet.getName());
      Map<String, Object> mapProps = propertySet.getProperties();
      if (mapProps.equals(mapCluster1)) {
        cluster1Matches = true;
      } else if (mapProps.equals(mapCluster2)) {
        cluster2Matches = true;
      } else if (mapProps.equals(mapCluster3)) {
        cluster3Matches = true;
      }
    }

    assertTrue(cluster1Matches);
    assertTrue(cluster2Matches);
    assertTrue(cluster3Matches);

    //assert body is correct by checking that properties match
    String b = body.getBody();

    body = parser.parse(b).iterator().next();

    Set<NamedPropertySet> setProps2 = body.getNamedPropertySets();
    assertEquals(3, setProps2.size());
    assertEquals(setProps, setProps2);
  }
View Full Code Here


  }

  @Test
  public void testParse___Array_NoArrayBrackets() throws BodyParseException {
    RequestBodyParser parser = new JsonRequestBodyParser();
    RequestBody body = parser.parse(arrayJson2).iterator().next();

    Set<NamedPropertySet> setProps = body.getNamedPropertySets();

    assertEquals(3, setProps.size());

    boolean cluster1Matches = false;
    boolean cluster2Matches = false;
    boolean cluster3Matches = false;

    Map<String, String> mapCluster1 = new HashMap<String, String>();
    mapCluster1.put(PropertyHelper.getPropertyId("Clusters", "cluster_name"), "unitTestCluster1");

    Map<String, String> mapCluster2 = new HashMap<String, String>();
    mapCluster2.put(PropertyHelper.getPropertyId("Clusters", "cluster_name"), "unitTestCluster2");
    mapCluster2.put(PropertyHelper.getPropertyId("Clusters", "property1"), "prop1Value");


    Map<String, String> mapCluster3 = new HashMap<String, String>();
    mapCluster3.put(PropertyHelper.getPropertyId("Clusters", "cluster_name"), "unitTestCluster3");
    mapCluster3.put(PropertyHelper.getPropertyId("Clusters/Category", "property2"), "prop2Value");


    for (NamedPropertySet propertySet : setProps) {
      Map<String, Object> mapProps = propertySet.getProperties();
      if (mapProps.equals(mapCluster1)) {
        cluster1Matches = true;
      } else if (mapProps.equals(mapCluster2)) {
        cluster2Matches = true;
      } else if (mapProps.equals(mapCluster3)) {
        cluster3Matches = true;
      }
    }

    assertTrue(cluster1Matches);
    assertTrue(cluster2Matches);
    assertTrue(cluster3Matches);

    //assert body is correct by checking that properties match
    String b = body.getBody();
    body = parser.parse(b).iterator().next();

    Set<NamedPropertySet> setProps2 = body.getNamedPropertySets();
    assertEquals(3, setProps2.size());
    assertEquals(setProps, setProps2);
  }
View Full Code Here

  }

  @Test
  public void testParse_QueryInBody() throws BodyParseException {
    RequestBodyParser parser = new JsonRequestBodyParser();
    RequestBody body = parser.parse(serviceJsonWithQuery).iterator().next();


    Set<NamedPropertySet> setProps = body.getNamedPropertySets();
    assertEquals(1, setProps.size());

    Map<String, Object> mapExpected = new HashMap<String, Object>();
    mapExpected.put(PropertyHelper.getPropertyId("Services", "service_name"), "HDFS");
    mapExpected.put(PropertyHelper.getPropertyId("Services", "display_name"), "HDFS");
    mapExpected.put(PropertyHelper.getPropertyId("ServiceInfo", "cluster_name"), "tbmetrictest");
    mapExpected.put(PropertyHelper.getPropertyId("Services", "description"), "Apache Hadoop Distributed File System");
    mapExpected.put(PropertyHelper.getPropertyId("ServiceInfo", "state"), "STARTED");
    mapExpected.put(PropertyHelper.getPropertyId("OuterCategory", "propName"), "100");
    mapExpected.put(PropertyHelper.getPropertyId("OuterCategory/nested1/nested2", "innerPropName"), "innerPropValue");
    mapExpected.put(PropertyHelper.getPropertyId(null, "topLevelProp"), "value");

    assertEquals(mapExpected, setProps.iterator().next().getProperties());
    assertEquals("foo=bar", body.getQueryString());

    //assert body is correct by checking that properties match
    String b = body.getBody();
    body = parser.parse(b).iterator().next();

    Set<NamedPropertySet> setProps2 = body.getNamedPropertySets();
    assertEquals(mapExpected, setProps2.iterator().next().getProperties());
  }
View Full Code Here

  @Override
  public Set<RequestBody> parse(String body) throws BodyParseException {

    Set<RequestBody> requestBodySet = new HashSet<RequestBody>();
    RequestBody      rootBody       = new RequestBody();
    rootBody.setBody(body);

    if (body != null && body.length() != 0) {
      ObjectMapper mapper = new ObjectMapper();
      try {
        JsonNode root = mapper.readTree(ensureArrayFormat(body));

        Iterator<JsonNode> iterator = root.getElements();
        while (iterator.hasNext()) {
          JsonNode            node             = iterator.next();
          Map<String, Object> mapProperties    = new HashMap<String, Object>();
          Map<String, String> requestInfoProps = new HashMap<String, String>();
          NamedPropertySet    propertySet      = new NamedPropertySet("", mapProperties);

          processNode(node, "", propertySet, requestInfoProps);

          if (!requestInfoProps.isEmpty()) {
            // If this node has request info properties then add it as a
            // separate request body
            RequestBody requestBody = new RequestBody();
            requestBody.setBody(body);

            for (Map.Entry<String, String> entry : requestInfoProps.entrySet()) {
              String key   = entry.getKey();
              String value = entry.getValue();

              requestBody.addRequestInfoProperty(key, value);

              if (key.equals(QUERY_FIELD_NAME)) {
                requestBody.setQueryString(value);
              }
            }
            if (!propertySet.getProperties().isEmpty()) {
              requestBody.addPropertySet(propertySet);
            }
            requestBodySet.add(requestBody);
          } else {
            // If this node does not have request info properties then add it
            // as a new property set to the root request body
View Full Code Here

  @Override
  public Set<RequestBody> parse(String body) throws BodyParseException {

    Set<RequestBody> requestBodySet = new HashSet<RequestBody>();
    RequestBody      rootBody       = new RequestBody();
    rootBody.setBody(body);

    if (body != null && body.length() != 0) {
      ObjectMapper mapper = new ObjectMapper();
      try {
        JsonNode root = mapper.readTree(ensureArrayFormat(body));

        Iterator<JsonNode> iterator = root.getElements();
        while (iterator.hasNext()) {
          JsonNode            node             = iterator.next();
          Map<String, Object> mapProperties    = new HashMap<String, Object>();
          Map<String, String> requestInfoProps = new HashMap<String, String>();
          NamedPropertySet    propertySet      = new NamedPropertySet("", mapProperties);

          processNode(node, "", propertySet, requestInfoProps);

          if (!requestInfoProps.isEmpty()) {
            // If this node has request info properties then add it as a
            // separate request body
            RequestBody requestBody = new RequestBody();
            requestBody.setBody(body);

            for (Map.Entry<String, String> entry : requestInfoProps.entrySet()) {
              String key   = entry.getKey();
              String value = entry.getValue();

              requestBody.addRequestInfoProperty(key, value);

              if (key.equals(QUERY_FIELD_NAME)) {
                requestBody.setQueryString(value);
              }
            }
            if (!propertySet.getProperties().isEmpty()) {
              requestBody.addPropertySet(propertySet);
            }
            requestBodySet.add(requestBody);
          } else {
            // If this node does not have request info properties then add it
            // as a new property set to the root request body
View Full Code Here


  @Test
  public void testParse() throws BodyParseException {
    RequestBodyParser parser = new JsonRequestBodyParser();
    RequestBody body = parser.parse(serviceJson).iterator().next();

    Set<NamedPropertySet> setProps = body.getNamedPropertySets();
    assertEquals(1, setProps.size());

    Map<String, Object> mapExpected = new HashMap<String, Object>();
    mapExpected.put(PropertyHelper.getPropertyId("Services", "service_name"), "HDFS");
    mapExpected.put(PropertyHelper.getPropertyId("Services", "display_name"), "HDFS");
    mapExpected.put(PropertyHelper.getPropertyId("ServiceInfo", "cluster_name"), "tbmetrictest");
    mapExpected.put(PropertyHelper.getPropertyId("Services", "description"), "Apache Hadoop Distributed File System");
    mapExpected.put(PropertyHelper.getPropertyId("ServiceInfo", "state"), "STARTED");
    mapExpected.put(PropertyHelper.getPropertyId("OuterCategory", "propName"), "100");
    mapExpected.put(PropertyHelper.getPropertyId("OuterCategory/nested1/nested2", "innerPropName"), "innerPropValue");
    mapExpected.put(PropertyHelper.getPropertyId(null, "topLevelProp"), "value");

    assertEquals(mapExpected, setProps.iterator().next().getProperties());

    //assert body is correct by checking that properties match
    String b = body.getBody();
    body = parser.parse(b).iterator().next();
    Set<NamedPropertySet> setProps2 = body.getNamedPropertySets();
    assertEquals(mapExpected, setProps2.iterator().next().getProperties());
  }
View Full Code Here

  }

  @Test
  public void testParse_NullBody() throws BodyParseException {
    RequestBodyParser parser = new JsonRequestBodyParser();
    RequestBody body = parser.parse(null).iterator().next();

    assertNotNull(body.getNamedPropertySets());
    assertEquals(0, body.getNamedPropertySets().size());
    assertNull(body.getQueryString());
    assertNull(body.getPartialResponseFields());
    assertNull(body.getBody());
  }
View Full Code Here

  }

  @Test
  public void testParse_EmptyBody() throws BodyParseException {
    RequestBodyParser parser = new JsonRequestBodyParser();
    RequestBody body = parser.parse("").iterator().next();

    assertNotNull(body.getNamedPropertySets());
    assertEquals(0, body.getNamedPropertySets().size());
    assertNull(body.getQueryString());
    assertNull(body.getPartialResponseFields());
    assertNull(body.getBody());
  }
View Full Code Here

  }

  @Test
  public void testParse_Array() throws BodyParseException {
    RequestBodyParser parser = new JsonRequestBodyParser();
    RequestBody body = parser.parse(arrayJson).iterator().next();

    Set<NamedPropertySet> setProps = body.getNamedPropertySets();

    assertEquals(3, setProps.size());

    boolean cluster1Matches = false;
    boolean cluster2Matches = false;
    boolean cluster3Matches = false;

    Map<String, String> mapCluster1 = new HashMap<String, String>();
    mapCluster1.put(PropertyHelper.getPropertyId("Clusters", "cluster_name"), "unitTestCluster1");

    Map<String, String> mapCluster2 = new HashMap<String, String>();
    mapCluster2.put(PropertyHelper.getPropertyId("Clusters", "cluster_name"), "unitTestCluster2");
    mapCluster2.put(PropertyHelper.getPropertyId("Clusters", "property1"), "prop1Value");


    Map<String, String> mapCluster3 = new HashMap<String, String>();
    mapCluster3.put(PropertyHelper.getPropertyId("Clusters", "cluster_name"), "unitTestCluster3");
    mapCluster3.put(PropertyHelper.getPropertyId("Clusters/Category", "property2"), "prop2Value");


    for (NamedPropertySet propertySet : setProps) {
      assertEquals("", propertySet.getName());
      Map<String, Object> mapProps = propertySet.getProperties();
      if (mapProps.equals(mapCluster1)) {
        cluster1Matches = true;
      } else if (mapProps.equals(mapCluster2)) {
        cluster2Matches = true;
      } else if (mapProps.equals(mapCluster3)) {
        cluster3Matches = true;
      }
    }

    assertTrue(cluster1Matches);
    assertTrue(cluster2Matches);
    assertTrue(cluster3Matches);

    //assert body is correct by checking that properties match
    String b = body.getBody();

    body = parser.parse(b).iterator().next();

    Set<NamedPropertySet> setProps2 = body.getNamedPropertySets();
    assertEquals(3, setProps2.size());
    assertEquals(setProps, setProps2);
  }
View Full Code Here

  }

  @Test
  public void testParse___Array_NoArrayBrackets() throws BodyParseException {
    RequestBodyParser parser = new JsonRequestBodyParser();
    RequestBody body = parser.parse(arrayJson2).iterator().next();

    Set<NamedPropertySet> setProps = body.getNamedPropertySets();

    assertEquals(3, setProps.size());

    boolean cluster1Matches = false;
    boolean cluster2Matches = false;
    boolean cluster3Matches = false;

    Map<String, String> mapCluster1 = new HashMap<String, String>();
    mapCluster1.put(PropertyHelper.getPropertyId("Clusters", "cluster_name"), "unitTestCluster1");

    Map<String, String> mapCluster2 = new HashMap<String, String>();
    mapCluster2.put(PropertyHelper.getPropertyId("Clusters", "cluster_name"), "unitTestCluster2");
    mapCluster2.put(PropertyHelper.getPropertyId("Clusters", "property1"), "prop1Value");


    Map<String, String> mapCluster3 = new HashMap<String, String>();
    mapCluster3.put(PropertyHelper.getPropertyId("Clusters", "cluster_name"), "unitTestCluster3");
    mapCluster3.put(PropertyHelper.getPropertyId("Clusters/Category", "property2"), "prop2Value");


    for (NamedPropertySet propertySet : setProps) {
      Map<String, Object> mapProps = propertySet.getProperties();
      if (mapProps.equals(mapCluster1)) {
        cluster1Matches = true;
      } else if (mapProps.equals(mapCluster2)) {
        cluster2Matches = true;
      } else if (mapProps.equals(mapCluster3)) {
        cluster3Matches = true;
      }
    }

    assertTrue(cluster1Matches);
    assertTrue(cluster2Matches);
    assertTrue(cluster3Matches);

    //assert body is correct by checking that properties match
    String b = body.getBody();
    body = parser.parse(b).iterator().next();

    Set<NamedPropertySet> setProps2 = body.getNamedPropertySets();
    assertEquals(3, setProps2.size());
    assertEquals(setProps, setProps2);
  }
View Full Code Here

TOP

Related Classes of org.apache.ambari.server.api.services.RequestBody

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.