Bläddra i källkod

AMBARI-3763 - Incorrect href for a POST response using associated resources

tbeerbower 11 år sedan
förälder
incheckning
ae1517672b

+ 4 - 2
ambari-server/src/main/java/org/apache/ambari/server/api/handlers/BaseManagementHandler.java

@@ -45,6 +45,8 @@ public abstract class BaseManagementHandler implements RequestHandler {
   protected final static Logger LOG =
       LoggerFactory.getLogger(BaseManagementHandler.class);
 
+  public static final String RESOURCES_NODE_NAME = "resources";
+
   /**
    * PersistenceManager implementation.
    */
@@ -84,10 +86,10 @@ public abstract class BaseManagementHandler implements RequestHandler {
       tree.addChild(requestStatus.getRequestResource(), "request");
     }
 
-    //todo: currently always empty
     Set<Resource> setResources = requestStatus.getAssociatedResources();
     if (! setResources.isEmpty()) {
-      TreeNode<Resource> resourcesNode = tree.addChild(null, "resources");
+      TreeNode<Resource> resourcesNode = tree.addChild(null, RESOURCES_NODE_NAME);
+      resourcesNode.setProperty("isCollection", "true");
 
       int count = 1;
       for (Resource resource : setResources) {

+ 5 - 3
ambari-server/src/main/java/org/apache/ambari/server/api/resources/BaseResourceDefinition.java

@@ -97,8 +97,6 @@ public abstract class BaseResourceDefinition implements ResourceDefinition {
       TreeNode<Resource> parent = resultNode.getParent();
 
       if (parent.getName() != null) {
-        Schema schema = getClusterController().getSchema(r.getType());
-        Object id = r.getPropertyValue(schema.getKeyPropertyId(r.getType()));
 
         int i = href.indexOf("?");
         if (i != -1) {
@@ -108,7 +106,11 @@ public abstract class BaseResourceDefinition implements ResourceDefinition {
         if (!href.endsWith("/")) {
           href = href + '/';
         }
-        href = "true".equals(parent.getProperty("isCollection")) ?
+
+        Schema schema = getClusterController().getSchema(r.getType());
+        Object id     = r.getPropertyValue(schema.getKeyPropertyId(r.getType()));
+
+        href = parent.getProperty("isCollection").equals("true") ?
             href + id : href + parent.getName() + '/' + id;
       }
       resultNode.setProperty("href", href);

+ 72 - 0
ambari-server/src/test/java/org/apache/ambari/server/api/resources/BaseResourceDefinitionTest.java

@@ -0,0 +1,72 @@
+package org.apache.ambari.server.api.resources;
+
+import org.apache.ambari.server.api.handlers.BaseManagementHandler;
+import org.apache.ambari.server.api.util.TreeNode;
+import org.apache.ambari.server.api.util.TreeNodeImpl;
+import org.apache.ambari.server.controller.internal.ResourceImpl;
+import org.apache.ambari.server.controller.spi.Resource;
+import org.junit.Assert;
+import org.junit.Test;
+
+import java.util.List;
+
+/**
+ * BaseResourceDefinition tests.
+ */
+public class BaseResourceDefinitionTest {
+
+
+  @Test
+  public void testGetPostProcessors() {
+    BaseResourceDefinition resourceDefinition = getResourceDefinition();
+
+    List<ResourceDefinition.PostProcessor> postProcessors = resourceDefinition.getPostProcessors();
+
+    Assert.assertEquals(1, postProcessors.size());
+
+    ResourceDefinition.PostProcessor processor = postProcessors.iterator().next();
+
+    Resource service = new ResourceImpl(Resource.Type.Service);
+    service.setProperty("ServiceInfo/service_name", "Service1");
+
+    TreeNode<Resource> parentNode  = new TreeNodeImpl<Resource>(null, null, "services");
+    TreeNode<Resource> serviceNode = new TreeNodeImpl<Resource>(parentNode, service, "service1");
+
+    parentNode.setProperty("isCollection", "true");
+
+    processor.process(null, serviceNode, "http://c6401.ambari.apache.org:8080/api/v1/clusters/c1/services");
+
+    String href = serviceNode.getProperty("href");
+
+    Assert.assertEquals("http://c6401.ambari.apache.org:8080/api/v1/clusters/c1/services/Service1", href);
+
+
+    Resource configGroup = new ResourceImpl(Resource.Type.ConfigGroup);
+    configGroup.setProperty("ConfigGroup/id", "2");
+
+    TreeNode<Resource> resourcesNode   = new TreeNodeImpl<Resource>(null, null, BaseManagementHandler.RESOURCES_NODE_NAME);
+    TreeNode<Resource> configGroupNode = new TreeNodeImpl<Resource>(resourcesNode, configGroup, "configGroup1");
+
+    resourcesNode.setProperty("isCollection", "true");
+
+    processor.process(null, configGroupNode, "http://c6401.ambari.apache.org:8080/api/v1/clusters/c1/config_groups");
+
+    href = configGroupNode.getProperty("href");
+
+    Assert.assertEquals("http://c6401.ambari.apache.org:8080/api/v1/clusters/c1/config_groups/2", href);
+  }
+
+  private BaseResourceDefinition getResourceDefinition() {
+    return new BaseResourceDefinition(Resource.Type.Service) {
+      @Override
+      public String getPluralName() {
+        return "pluralName";
+      }
+
+      @Override
+      public String getSingularName() {
+        return "singularName";
+      }
+    };
+  }
+}