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

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


  @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_QueryPost() throws BodyParseException {
    RequestBodyParser parser = new JsonRequestBodyParser();
    RequestBody body = parser.parse(queryPostJson).iterator().next();


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

    assertEquals(1, setProperties.size());
    boolean contains1 = false;
    boolean contains2 = false;
    boolean contains3 = false;

    for (NamedPropertySet ps : setProperties) {
      Map<String, Object> mapProps = ps.getProperties();
      assertEquals(1, mapProps.size());
      Set<Map<String, Object>> set = (Set<Map<String, Object>>) mapProps.get("services");

      for (Map<String, Object> map : set) {
        String serviceName = (String) map.get("ServiceInfo/service_name");
        if (serviceName.equals("unitTestService1")) {
          assertEquals(1, map.size());
          contains1 = true;
        } else if (serviceName.equals("unitTestService2")) {
          assertEquals("prop1Value", map.get("ServiceInfo/property1"));
          assertEquals(2, map.size());
          contains2 = true;
        } else if (serviceName.equals("unitTestService3")) {
          assertEquals("prop2Value", map.get("ServiceInfo/Category/property2"));
          assertEquals(2, map.size());
          contains3 = true;
        } else {
          fail("Unexpected service name");
        }
      }
    }
    assertTrue(contains1);
    assertTrue(contains2);
    assertTrue(contains3);

    //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(1, setProps2.size());
    assertEquals(setProperties, setProps2);
  }
View Full Code Here

  }

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


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

    assertEquals(1, setProperties.size());
    boolean contains1 = false;
    boolean contains2 = false;

    for (NamedPropertySet ps : setProperties) {

      Map<String, Object> mapProps = ps.getProperties();

      for (Map.Entry<String, Object> entry : mapProps.entrySet()) {
        Set<Map<String, Object>> set = (Set<Map<String, Object>>) entry.getValue();

        for (Map<String, Object> map : set) {

          String serviceName = (String) map.get("ServiceInfo/service_name");
          if (serviceName.equals("unitTestService1")) {
            assertEquals("foo", entry.getKey());
            assertEquals(1, map.size());
            contains1 = true;
          } else if (serviceName.equals("unitTestService2")) {
            assertEquals("bar", entry.getKey());
            assertEquals("prop2Value", map.get("ServiceInfo/Category/property2"));
            assertEquals(2, map.size());
            contains2 = true;
          } else {
            fail("Unexpected service name");
          }

        }
      }
    }
    assertTrue(contains1);
    assertTrue(contains2);

    //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(1, setProps2.size());
    assertEquals(setProperties, setProps2);
  }
View Full Code Here

  @Test
  public void testParse___QueryPost_QueryInBody() throws BodyParseException {
    RequestBodyParser parser = new JsonRequestBodyParser();

    RequestBody body = parser.parse(queryPostJsonWithQuery).iterator().next();


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

    assertEquals("foo=bar", body.getQueryString());
    assertEquals(1, setProperties.size());
    boolean contains1 = false;
    boolean contains2 = false;
    boolean contains3 = false;

    for (NamedPropertySet ps : setProperties) {
      assertEquals("", ps.getName());
      Map<String, Object> mapProps = ps.getProperties();

      for (Map.Entry<String, Object> entry : mapProps.entrySet()) {
        Set<Map<String, Object>> set = (Set<Map<String, Object>>) entry.getValue();

        for (Map<String, Object> map : set) {

          String serviceName = (String) map.get("ServiceInfo/service_name");
          if (serviceName.equals("unitTestService1")) {
            assertEquals(1, map.size());
            contains1 = true;
          } else if (serviceName.equals("unitTestService2")) {
            assertEquals("prop1Value", map.get("ServiceInfo/property1"));
            assertEquals(2, map.size());
            contains2 = true;
          } else if (serviceName.equals("unitTestService3")) {
            assertEquals("prop2Value", map.get("ServiceInfo/Category/property2"));
            assertEquals(2, map.size());
            contains3 = true;
          } else {
            fail("Unexpected service name");
          }
        }
      }
    }
    assertTrue(contains1);
    assertTrue(contains2);
    assertTrue(contains3);

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

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

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

  @Test
  public void testParse_QueryOnlyInBody() throws BodyParseException {
    RequestBodyParser parser = new JsonRequestBodyParser();

    RequestBody body = parser.parse(bodyQueryOnly).iterator().next();

    assertEquals("foo=bar", body.getQueryString());
    assertEquals(bodyQueryOnly, body.getBody());
  }
View Full Code Here

  }

  @Test
  public void testRequestInfoProps() throws Exception {
    RequestBodyParser parser = new JsonRequestBodyParser();
    RequestBody body = parser.parse(bodyWithRequestInfoProperties).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());

    Map<String, String > mapRequestInfoProps = body.getRequestInfoProperties();
    assertEquals(3, mapRequestInfoProps.size());
    assertEquals("val1", mapRequestInfoProps.get("prop1"));
    assertEquals("val2", mapRequestInfoProps.get("prop2"));
    assertEquals("foo=bar", mapRequestInfoProps.get("query"));

    //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 testRequestBlobProperties() throws Exception {
    RequestBodyParser parser = new JsonRequestBodyParser();
    RequestBody body = parser.parse(bodyWithRequestBlobProperties).iterator().next();

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

    String requestBlob = null;

    for (NamedPropertySet ps : setProps) {
      assertEquals("", ps.getName());
      Map<String, Object> mapProps = ps.getProperties();

      for (Map.Entry<String, Object> entry : mapProps.entrySet()) {
        if (entry.getKey().equals(JsonRequestBodyParser.REQUEST_BLOB_TITLE)) {
          requestBlob = (String) entry.getValue();
        }
      }
    }

    Assert.assertNotNull(requestBlob);
    body = parser.parse(requestBlob).iterator().next();

    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");

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

  String bodyWithRequestBlobProperties = "{ \"RequestBodyInfo\" : { " +"\"RequestInfo\" : { \"query\" : \"foo=bar\", \"prop1\" : \"val1\", \"prop2\" : \"val2\" }, \"Body\":" + serviceJson + "} }";

  @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

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.