ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From dmitriu...@apache.org
Subject [1/2] git commit: AMBARI-7676. Stack API generates invalid json files at "content" area (dlysnichenko)
Date Wed, 08 Oct 2014 11:06:19 GMT
Repository: ambari
Updated Branches:
  refs/heads/branch-1.7.0 43d506ff4 -> f78379c08
  refs/heads/trunk f072da354 -> 760e9b750


AMBARI-7676. Stack API generates invalid json files at "content" area (dlysnichenko)


Project: http://git-wip-us.apache.org/repos/asf/ambari/repo
Commit: http://git-wip-us.apache.org/repos/asf/ambari/commit/760e9b75
Tree: http://git-wip-us.apache.org/repos/asf/ambari/tree/760e9b75
Diff: http://git-wip-us.apache.org/repos/asf/ambari/diff/760e9b75

Branch: refs/heads/trunk
Commit: 760e9b7504aa7927ea78e6eb02c97b14fa98b1a2
Parents: f072da3
Author: Lisnichenko Dmitro <dlysnichenko@hortonworks.com>
Authored: Wed Oct 8 14:04:05 2014 +0300
Committer: Lisnichenko Dmitro <dlysnichenko@hortonworks.com>
Committed: Wed Oct 8 14:04:05 2014 +0300

----------------------------------------------------------------------
 .../services/serializers/JsonSerializer.java    |  9 ++-
 .../serializers/JsonSerializerTest.java         | 66 +++++++++++++++++++-
 2 files changed, 72 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ambari/blob/760e9b75/ambari-server/src/main/java/org/apache/ambari/server/api/services/serializers/JsonSerializer.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/api/services/serializers/JsonSerializer.java
b/ambari-server/src/main/java/org/apache/ambari/server/api/services/serializers/JsonSerializer.java
index 59d5a8a..7f57f7f 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/api/services/serializers/JsonSerializer.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/api/services/serializers/JsonSerializer.java
@@ -115,7 +115,10 @@ public class JsonSerializer implements ResultSerializer {
     }
 
     if (isArray(node)) {
-      m_generator.writeArrayFieldStart(node.getName());
+      if (node.getName() != null)
+        m_generator.writeArrayFieldStart(node.getName());
+      else
+        m_generator.writeStartArray();
     }
 
     for (TreeNode<Resource> child : node.getChildren()) {
@@ -139,7 +142,9 @@ public class JsonSerializer implements ResultSerializer {
 
   // Determines whether or not the given node is an array
   private boolean isArray(TreeNode<Resource> node) {
-    return node.getObject() == null && node.getName() != null;
+    return (node.getObject() == null && node.getName() != null) ||
+            (node.getObject() == null && node.getName() == null &&
+             node.getChildren().size() > 1);
   }
 
   private TreeNode<Map<String, Object>> getTreeProperties (Map<String, Map<String,
Object>> propertiesMap) {

http://git-wip-us.apache.org/repos/asf/ambari/blob/760e9b75/ambari-server/src/test/java/org/apache/ambari/server/api/services/serializers/JsonSerializerTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/api/services/serializers/JsonSerializerTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/api/services/serializers/JsonSerializerTest.java
index 90063a0..4bd12d3 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/api/services/serializers/JsonSerializerTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/api/services/serializers/JsonSerializerTest.java
@@ -101,7 +101,7 @@ public class JsonSerializerTest {
 
 
     TreeNode<Resource> resourcesNode = tree.addChild(null, "resources");
-
+    
 
     resourcesNode.addChild(resource, "resource1");
 
@@ -145,4 +145,68 @@ public class JsonSerializerTest {
 
     verify(uriInfo, resource);
   }
+  
+  @Test
+  public void testSerializeResourcesAsArray() throws Exception {
+    UriInfo uriInfo = createMock(UriInfo.class);
+    Resource resource = createMock(Resource.class);
+    //Resource resource2 = createMock(Resource.class);
+
+    Result result = new ResultImpl(true);
+    result.setResultStatus(new ResultStatus(ResultStatus.STATUS.OK));
+    TreeNode<Resource> tree = result.getResultTree();
+    //tree.setName("items");
+    TreeNode<Resource> child = tree.addChild(resource, "resource1");
+    child.setProperty("href", "this is an href");
+    tree.addChild(resource, "resource2");
+    //child.addChild(resource2, "sub-resource");
+
+    // resource properties
+    HashMap<String, Object> mapRootProps = new HashMap<String, Object>();
+    mapRootProps.put("prop1", "value1");
+    mapRootProps.put("prop2", "value2");
+
+    HashMap<String, Object> mapCategoryProps = new HashMap<String, Object>();
+    mapCategoryProps.put("catProp1", "catValue1");
+    mapCategoryProps.put("catProp2", "catValue2");
+
+    Map<String, Map<String, Object>> propertyMap = new HashMap<String, Map<String,
Object>>();
+
+    propertyMap.put(null, mapRootProps);
+    propertyMap.put("category", mapCategoryProps);
+
+    //expectations
+    expect(resource.getPropertiesMap()).andReturn(propertyMap).anyTimes();
+    expect(resource.getType()).andReturn(Resource.Type.Cluster).anyTimes();
+
+    replay(uriInfo, resource/*, resource2*/);
+
+    //execute test
+    Object o = new JsonSerializer().serialize(result);
+    String expected = "[\n" +
+    "  {\n" +
+    "    \"href\" : \"this is an href\",\n" +
+    "    \"prop2\" : \"value2\",\n" +
+    "    \"prop1\" : \"value1\",\n" +
+    "    \"category\" : {\n" +
+    "      \"catProp1\" : \"catValue1\",\n" +
+    "      \"catProp2\" : \"catValue2\"\n" +
+    "    }\n" +
+    "  },\n" +
+    "  {\n" +
+    "    \"prop2\" : \"value2\",\n" +
+    "    \"prop1\" : \"value1\",\n" +
+    "    \"category\" : {\n" +
+    "      \"catProp1\" : \"catValue1\",\n" +
+    "      \"catProp2\" : \"catValue2\"\n" +
+    "    }\n" +
+    "  }\n" +
+    "]";
+
+    assertEquals(expected, o);
+
+    verify(uriInfo, resource/*, resource2*/);
+  }
+    
+  
 }


Mime
View raw message