ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tbeerbo...@apache.org
Subject git commit: AMBARI-3619 - getAssociatedResource thorws Exception on POST
Date Wed, 30 Oct 2013 00:03:53 GMT
Updated Branches:
  refs/heads/trunk ac5bd63c6 -> 00484f9c9


AMBARI-3619 - getAssociatedResource thorws Exception on POST

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

Branch: refs/heads/trunk
Commit: 00484f9c9da0653a4f840643ac2575f792f098e9
Parents: ac5bd63
Author: tbeerbower <tbeerbower@hortonworks.com>
Authored: Tue Oct 29 19:21:58 2013 -0400
Committer: tbeerbower <tbeerbower@hortonworks.com>
Committed: Tue Oct 29 19:21:58 2013 -0400

----------------------------------------------------------------------
 .../services/serializers/JsonSerializer.java    | 47 +++++-----
 .../internal/AbstractResourceProvider.java      | 15 ++-
 .../controller/internal/RequestStatusImpl.java  | 12 ++-
 .../serializers/JsonSerializerTest.java         | 61 +++++++++++-
 .../internal/AbstractResourceProviderTest.java  | 98 +++++++++-----------
 .../internal/RequestStatusImplTest.java         | 48 ++++++++++
 6 files changed, 200 insertions(+), 81 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/00484f9c/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 a63a186..bb5eddb 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
@@ -101,40 +101,43 @@ public class JsonSerializer implements ResultSerializer {
   }
 
   private void processNode(TreeNode<Resource> node) throws IOException {
-    String name = node.getName();
-    Resource r = node.getObject();
-
-    if (r == null) {
-      if (name != null) {
-        if (node.getParent() == null) {
-          m_generator.writeStartObject();
-          writeHref(node);
-        }
-        m_generator.writeArrayFieldStart(name);
-      }
-    } else {
+
+    if (isObject(node)) {
       m_generator.writeStartObject();
       writeHref(node);
-      // resource props
-      handleResourceProperties(getTreeProperties(r.getPropertiesMap()));
+
+      Resource r = node.getObject();
+      if (r != null) {
+        handleResourceProperties(getTreeProperties(r.getPropertiesMap()));
+      }
+    }
+    if (isArray(node)) {
+      m_generator.writeArrayFieldStart(node.getName());
     }
 
     for (TreeNode<Resource> child : node.getChildren()) {
       processNode(child);
     }
 
-    if (r == null) {
-      if (name != null) {
-        m_generator.writeEndArray();
-        if (node.getParent() == null) {
-          m_generator.writeEndObject();
-        }
-      }
-    } else {
+    if (isArray(node)) {
+      m_generator.writeEndArray();
+    }
+    if (isObject(node)) {
       m_generator.writeEndObject();
     }
   }
 
+  // Determines whether or not the given node is an object
+  private boolean isObject(TreeNode<Resource> node) {
+    return node.getObject() != null ||
+        ((node.getName() != null) && ((node.getParent() == null) || !isObject(node.getParent())));
+  }
+
+  // Determines whether or not the given node is an array
+  private boolean isArray(TreeNode<Resource> node) {
+    return node.getObject() == null && node.getName() != null;
+  }
+
   private TreeNode<Map<String, Object>> getTreeProperties (Map<String, Map<String,
Object>> propertiesMap) {
     TreeNode<Map<String, Object>> treeProperties =
         new TreeNodeImpl<Map<String, Object>>(null, new LinkedHashMap<String,
Object>(), null);

http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/00484f9c/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AbstractResourceProvider.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AbstractResourceProvider.java
b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AbstractResourceProvider.java
index ca703aa..0c8ddc6 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AbstractResourceProvider.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/AbstractResourceProvider.java
@@ -198,14 +198,23 @@ public abstract class AbstractResourceProvider extends BaseProvider
implements R
    *
    * @return the request status
    */
-  protected RequestStatus getRequestStatus(RequestStatusResponse response) {
+  protected RequestStatus getRequestStatus(RequestStatusResponse response, Set<Resource>
associatedResources) {
     if (response != null){
       Resource requestResource = new ResourceImpl(Resource.Type.Request);
       requestResource.setProperty(PropertyHelper.getPropertyId("Requests", "id"), response.getRequestId());
       requestResource.setProperty(PropertyHelper.getPropertyId("Requests", "status"), "InProgress");
-      return new RequestStatusImpl(requestResource);
+      return new RequestStatusImpl(requestResource, associatedResources);
     }
-    return new RequestStatusImpl(null);
+    return new RequestStatusImpl(null, associatedResources);
+  }
+
+  /**
+   * Get a request status
+   *
+   * @return the request status
+   */
+  protected RequestStatus getRequestStatus(RequestStatusResponse response) {
+    return getRequestStatus(response, null) ;
   }
 
   /**

http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/00484f9c/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RequestStatusImpl.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RequestStatusImpl.java
b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RequestStatusImpl.java
index eb2f0da..977538a 100644
--- a/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RequestStatusImpl.java
+++ b/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/RequestStatusImpl.java
@@ -30,14 +30,22 @@ import java.util.Set;
 public class RequestStatusImpl implements RequestStatus{
 
   private final Resource requestResource;
+  private final Set<Resource> associatedResources;
 
   public RequestStatusImpl(Resource requestResource) {
-    this.requestResource = requestResource;
+    this.requestResource     = requestResource;
+    this.associatedResources = Collections.emptySet();
+  }
+
+  public RequestStatusImpl(Resource requestResource, Set<Resource> associatedResources)
{
+    this.requestResource     = requestResource;
+    this.associatedResources = associatedResources == null ?
+        Collections.<Resource>emptySet() : associatedResources;
   }
 
   @Override
   public Set<Resource> getAssociatedResources() {
-    return Collections.emptySet();  // TODO : handle in M4
+    return associatedResources;
   }
 
   @Override

http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/00484f9c/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 ac167be..816a251 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
@@ -22,7 +22,6 @@ import org.apache.ambari.server.api.services.Result;
 import org.apache.ambari.server.api.services.ResultImpl;
 import org.apache.ambari.server.api.services.ResultStatus;
 import org.apache.ambari.server.api.util.TreeNode;
-import org.apache.ambari.server.api.util.TreeNodeImpl;
 import org.apache.ambari.server.controller.spi.Resource;
 import org.junit.Test;
 
@@ -48,7 +47,7 @@ public class JsonSerializerTest {
     result.setResultStatus(new ResultStatus(ResultStatus.STATUS.OK));
     TreeNode<Resource> tree = result.getResultTree();
     //tree.setName("items");
-    TreeNode<Resource> child = tree.addChild(resource, "resource1");
+    tree.addChild(resource, "resource1");
     //child.addChild(resource2, "sub-resource");
 
     // resource properties
@@ -87,4 +86,62 @@ public class JsonSerializerTest {
 
     verify(uriInfo, resource/*, resource2*/);
   }
+
+
+  @Test
+  public void testSerializeResources() 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();
+
+
+    TreeNode<Resource> resourcesNode = tree.addChild(null, "resources");
+
+
+    resourcesNode.addChild(resource, "resource1");
+
+    // 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);
+
+    //execute test
+    Object o = new JsonSerializer().serialize(result);
+
+    String expected = "{\n" +
+        "  \"resources\" : [\n" +
+        "    {\n" +
+        "      \"prop2\" : \"value2\",\n" +
+        "      \"prop1\" : \"value1\",\n" +
+        "      \"category\" : {\n" +
+        "        \"catProp1\" : \"catValue1\",\n" +
+        "        \"catProp2\" : \"catValue2\"\n" +
+        "      }\n" +
+        "    }\n" +
+        "  ]\n" +
+        "}";
+
+    assertEquals(expected, o);
+
+    verify(uriInfo, resource);
+  }
 }

http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/00484f9c/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/AbstractResourceProviderTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/AbstractResourceProviderTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/AbstractResourceProviderTest.java
index 94c6f05..de2ddcb 100644
--- a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/AbstractResourceProviderTest.java
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/AbstractResourceProviderTest.java
@@ -23,11 +23,12 @@ import org.apache.ambari.server.controller.AmbariManagementController;
 import org.apache.ambari.server.controller.ClusterRequest;
 import org.apache.ambari.server.controller.ConfigurationRequest;
 import org.apache.ambari.server.controller.HostRequest;
+import org.apache.ambari.server.controller.RequestStatusResponse;
 import org.apache.ambari.server.controller.ServiceComponentHostRequest;
-import org.apache.ambari.server.controller.ServiceComponentRequest;
 import org.apache.ambari.server.controller.StackConfigurationRequest;
 import org.apache.ambari.server.controller.TaskStatusRequest;
 import org.apache.ambari.server.controller.UserRequest;
+import org.apache.ambari.server.controller.spi.RequestStatus;
 import org.apache.ambari.server.controller.spi.Resource;
 import org.easymock.EasyMock;
 import org.easymock.IArgumentMatcher;
@@ -113,6 +114,50 @@ public class AbstractResourceProviderTest {
     Assert.assertTrue(supportedPropertyIds.containsAll(propertyIds));
   }
 
+  @Test
+  public void testGetRequestStatus() {
+    Set<String> propertyIds = new HashSet<String>();
+    Map<Resource.Type, String> keyPropertyIds = new HashMap<Resource.Type, String>();
+    AmbariManagementController managementController = createMock(AmbariManagementController.class);
+
+    AbstractResourceProvider provider =
+        (AbstractResourceProvider) AbstractControllerResourceProvider.getResourceProvider(
+            Resource.Type.Service,
+            propertyIds,
+            keyPropertyIds,
+            managementController);
+
+    RequestStatus status = provider.getRequestStatus(null);
+
+    Assert.assertNull(status.getRequestResource());
+    Assert.assertEquals(Collections.emptySet(), status.getAssociatedResources());
+
+    RequestStatusResponse response = new RequestStatusResponse(99L);
+
+    status = provider.getRequestStatus(response);
+    Resource resource = status.getRequestResource();
+
+    Assert.assertEquals(99L, resource.getPropertyValue("Requests/id"));
+    Assert.assertEquals(Collections.emptySet(), status.getAssociatedResources());
+
+
+    status = provider.getRequestStatus(response, null);
+    resource = status.getRequestResource();
+
+    Assert.assertEquals(99L, resource.getPropertyValue("Requests/id"));
+    Assert.assertEquals(Collections.emptySet(), status.getAssociatedResources());
+
+
+    Resource associatedResource = new ResourceImpl(Resource.Type.Service);
+
+    Set<Resource> associatedResources = Collections.singleton(associatedResource);
+    status = provider.getRequestStatus(response, associatedResources);
+    resource = status.getRequestResource();
+
+    Assert.assertEquals(99L, resource.getPropertyValue("Requests/id"));
+    Assert.assertEquals(associatedResources, status.getAssociatedResources());
+  }
+
 
   // ----- helper methods ----------------------------------------------------
 
@@ -163,15 +208,6 @@ public class AbstractResourceProviderTest {
       return null;
     }
 
-    public static Set<ServiceComponentRequest> getComponentRequestSet(String clusterName,
String serviceName,
-                                                                      String componentName,
-                                                                      Map<String, String>
configVersions,
-                                                                      String desiredState)
-    {
-      EasyMock.reportMatcher(new ComponentRequestSetMatcher(clusterName, serviceName, componentName,
-          configVersions, desiredState));
-      return null;
-    }
 
     public static Set<HostRequest> getHostRequestSet(String hostname, String clusterName,
                                                      Map<String, String> hostAttributes)
@@ -331,48 +367,6 @@ public class AbstractResourceProviderTest {
     }
   }
 
-  /**
-   * Matcher for a ServiceComponentRequest set containing a single request.
-   */
-  public static class ComponentRequestSetMatcher extends HashSet<ServiceComponentRequest>
implements IArgumentMatcher {
-
-    private final ServiceComponentRequest serviceComponentRequest;
-
-    public ComponentRequestSetMatcher(String clusterName, String serviceName, String componentName,
-                                   Map<String, String> configVersions, String desiredState)
{
-      this.serviceComponentRequest =
-          new ServiceComponentRequest(clusterName, serviceName, componentName, configVersions,
desiredState);
-      add(this.serviceComponentRequest);
-    }
-
-    @Override
-    public boolean matches(Object o) {
-
-      if (!(o instanceof Set)) {
-        return false;
-      }
-
-      Set set = (Set) o;
-
-      if (set.size() != 1) {
-        return false;
-      }
-
-      Object request = set.iterator().next();
-
-      return request instanceof ServiceComponentRequest &&
-          eq(((ServiceComponentRequest) request).getClusterName(), serviceComponentRequest.getClusterName())
&&
-          eq(((ServiceComponentRequest) request).getServiceName(), serviceComponentRequest.getServiceName())
&&
-          eq(((ServiceComponentRequest) request).getComponentName(), serviceComponentRequest.getComponentName())
&&
-          eq(((ServiceComponentRequest) request).getConfigVersions(), serviceComponentRequest.getConfigVersions())
&&
-          eq(((ServiceComponentRequest) request).getDesiredState(), serviceComponentRequest.getDesiredState());
-    }
-
-    @Override
-    public void appendTo(StringBuffer stringBuffer) {
-      stringBuffer.append("ComponentRequestSetMatcher(").append(serviceComponentRequest).append(")");
-    }
-  }
 
   /**
    * Matcher for a HostRequest set containing a single request.

http://git-wip-us.apache.org/repos/asf/incubator-ambari/blob/00484f9c/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestStatusImplTest.java
----------------------------------------------------------------------
diff --git a/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestStatusImplTest.java
b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestStatusImplTest.java
new file mode 100644
index 0000000..4863784
--- /dev/null
+++ b/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/RequestStatusImplTest.java
@@ -0,0 +1,48 @@
+package org.apache.ambari.server.controller.internal;
+
+import org.apache.ambari.server.controller.spi.RequestStatus;
+import org.apache.ambari.server.controller.spi.Resource;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.Collections;
+import java.util.Set;
+
+/**
+ * RequestStatusImpl Tests
+ */
+public class RequestStatusImplTest {
+  @Test
+  public void testGetAssociatedResources() throws Exception {
+    RequestStatusImpl status = new RequestStatusImpl(null);
+    Assert.assertEquals(Collections.emptySet(), status.getAssociatedResources());
+
+
+    Resource associatedResource = new ResourceImpl(Resource.Type.Service);
+    Set<Resource> associatedResources = Collections.singleton(associatedResource);
+    status = new RequestStatusImpl(null, associatedResources);
+    Assert.assertEquals(associatedResources, status.getAssociatedResources());
+  }
+
+  @Test
+  public void testGetRequestResource() throws Exception {
+    RequestStatusImpl status = new RequestStatusImpl(null);
+    Assert.assertNull(status.getRequestResource());
+
+    Resource requestResource = new ResourceImpl(Resource.Type.Request);
+    status = new RequestStatusImpl(requestResource);
+
+    Assert.assertEquals(requestResource, status.getRequestResource());
+  }
+
+  @Test
+  public void testGetStatus() throws Exception {
+    RequestStatusImpl status = new RequestStatusImpl(null);
+    Assert.assertEquals(RequestStatus.Status.Complete, status.getStatus());
+
+    Resource requestResource = new ResourceImpl(Resource.Type.Request);
+    requestResource.setProperty("Requests/status", "InProgress");
+    status = new RequestStatusImpl(requestResource);
+    Assert.assertEquals(RequestStatus.Status.InProgress, status.getStatus());
+  }
+}


Mime
View raw message