You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ambari.apache.org by ma...@apache.org on 2012/10/16 08:43:23 UTC

svn commit: r1398672 - in /incubator/ambari/branches/AMBARI-666: ./ ambari-server/src/main/java/org/apache/ambari/server/api/query/ ambari-server/src/main/java/org/apache/ambari/server/controller/predicate/ ambari-server/src/test/java/org/apache/ambari...

Author: mahadev
Date: Tue Oct 16 06:43:23 2012
New Revision: 1398672

URL: http://svn.apache.org/viewvc?rev=1398672&view=rev
Log:
AMBARI-862. API query against /clusters doesn't return any data. (John Speidel via mahadev)

Modified:
    incubator/ambari/branches/AMBARI-666/AMBARI-666-CHANGES.txt
    incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/api/query/QueryImpl.java
    incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/predicate/ArrayPredicate.java
    incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/api/query/QueryImplTest.java

Modified: incubator/ambari/branches/AMBARI-666/AMBARI-666-CHANGES.txt
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/AMBARI-666-CHANGES.txt?rev=1398672&r1=1398671&r2=1398672&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/AMBARI-666-CHANGES.txt (original)
+++ incubator/ambari/branches/AMBARI-666/AMBARI-666-CHANGES.txt Tue Oct 16 06:43:23 2012
@@ -12,6 +12,9 @@ AMBARI-666 branch (unreleased changes)
 
   NEW FEATURES
 
+  AMBARI-862. API query against /clusters doesn't return any data.
+  (John Speidel via mahadev)
+
   AMBARI-866. Add ORM layer for the FSM's in the server. (mahadev)
 
   AMBARI-853. Add more complete JMX metrics. (Tom Beerbower via mahadev)

Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/api/query/QueryImpl.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/api/query/QueryImpl.java?rev=1398672&r1=1398671&r2=1398672&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/api/query/QueryImpl.java (original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/api/query/QueryImpl.java Tue Oct 16 06:43:23 2012
@@ -236,10 +236,18 @@ public class QueryImpl implements Query 
   }
 
   private Predicate createPredicate(ResourceDefinition resourceDefinition) {
+    Predicate predicate = null;
+    //todo: change reference type to Predicate when predicate hierarchy is fixed
     BasePredicate internalPredicate = createInternalPredicate(resourceDefinition);
-    //todo: remove cast when predicate hierarchy is fixed
-    return m_userPredicate == null ? internalPredicate :
-        new AndPredicate((BasePredicate) m_userPredicate, internalPredicate);
+    if (internalPredicate == null) {
+      if (m_userPredicate != null) {
+        predicate = m_userPredicate;
+      }
+    } else {
+      predicate = (m_userPredicate == null) ? internalPredicate :
+          new AndPredicate((BasePredicate) m_userPredicate, internalPredicate);
+    }
+    return predicate;
   }
 
   ClusterController getClusterController() {

Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/predicate/ArrayPredicate.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/predicate/ArrayPredicate.java?rev=1398672&r1=1398671&r2=1398672&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/predicate/ArrayPredicate.java (original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/main/java/org/apache/ambari/server/controller/predicate/ArrayPredicate.java Tue Oct 16 06:43:23 2012
@@ -65,7 +65,8 @@ public abstract class ArrayPredicate imp
 
   @Override
   public int hashCode() {
-    int result = predicates != null ? Arrays.hashCode(predicates) : 0;
+    // don't care about array order
+    int result = predicates != null ? new HashSet<BasePredicate>(Arrays.asList(predicates)).hashCode() : 0;
     result = 31 * result + (propertyIds != null ? propertyIds.hashCode() : 0);
     return result;
   }

Modified: incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/api/query/QueryImplTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/api/query/QueryImplTest.java?rev=1398672&r1=1398671&r2=1398672&view=diff
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/api/query/QueryImplTest.java (original)
+++ incubator/ambari/branches/AMBARI-666/ambari-server/src/test/java/org/apache/ambari/server/api/query/QueryImplTest.java Tue Oct 16 06:43:23 2012
@@ -2,6 +2,8 @@ package org.apache.ambari.server.api.que
 
 import org.apache.ambari.server.api.util.TreeNode;
 import org.apache.ambari.server.controller.internal.PropertyIdImpl;
+import org.apache.ambari.server.controller.predicate.AndPredicate;
+import org.apache.ambari.server.controller.predicate.BasePredicate;
 import org.apache.ambari.server.controller.spi.*;
 import org.apache.ambari.server.controller.utilities.PredicateBuilder;
 import org.apache.ambari.server.api.resources.ResourceDefinition;
@@ -95,13 +97,9 @@ public class QueryImplTest {
     TreeNode<Resource> tree = createStrictMock(TreeNode.class);
     TreeNode<Resource> componentNode = createStrictMock(TreeNode.class);
     ResourceDefinition componentResourceDef = createMock(ResourceDefinition.class);
-    ResourceDefinition hostComponentResourceDef = createStrictMock(ResourceDefinition.class);
     Schema componentSchema = createMock(Schema.class);
     Resource componentResource = createStrictMock(Resource.class);
     PropertyId componentPropertyId = PropertyHelper.getPropertyId("componentId", "");
-    Query hostComponentQuery = createStrictMock(Query.class);
-    Result hostComponentQueryResult = createStrictMock(Result.class);
-    TreeNode<Resource> hostComponentResultTree = createMock(TreeNode.class);
 
     Map<String, Set<String>> mapProperties = new HashMap<String, Set<String>>();
     mapProperties.put("", Collections.singleton("componentId"));
@@ -139,14 +137,163 @@ public class QueryImplTest {
 
     expect(tree.addChild(componentResource, null)).andReturn(componentNode);
 
-    replay(m_controller, result, tree, componentNode, componentResourceDef, hostComponentResourceDef, componentSchema, componentResource,
-        hostComponentQuery, hostComponentQueryResult, hostComponentResultTree);
+    replay(m_controller, result, tree, componentNode, componentResourceDef, componentSchema, componentResource);
 
     QueryImpl query = new TestQuery(componentResourceDef, result);
     query.execute();
 
-    verify(m_controller, result, tree, componentNode, componentResourceDef, hostComponentResourceDef, componentSchema, componentResource,
-        hostComponentQuery, hostComponentQueryResult, hostComponentResultTree);
+    verify(m_controller, result, tree, componentNode, componentResourceDef, componentSchema, componentResource);
+  }
+
+  @Test
+  public void testExecute__collection_nullInternalPredicate_nullUserPredicate() throws Exception {
+    Result result = createStrictMock(Result.class);
+    TreeNode<Resource> tree = createStrictMock(TreeNode.class);
+    TreeNode<Resource> clusterNode = createStrictMock(TreeNode.class);
+    ResourceDefinition clusterResourceDef = createMock(ResourceDefinition.class);
+    Schema clusterSchema = createMock(Schema.class);
+    Resource clusterResource = createStrictMock(Resource.class);
+    PropertyId clusterPropertyId = PropertyHelper.getPropertyId("clusterId", "");
+
+
+    Map<String, Set<String>> mapProperties = new HashMap<String, Set<String>>();
+    mapProperties.put("", Collections.singleton("clusterId"));
+
+    List<Resource> listResources = Collections.singletonList(clusterResource);
+
+    Map<Resource.Type, String> mapResourceIds = new HashMap<Resource.Type, String>();
+
+    // expectations
+    expect(clusterResourceDef.getId()).andReturn(null).atLeastOnce();
+    expect(m_controller.getSchema(Resource.Type.Component)).andReturn(clusterSchema).atLeastOnce();
+    expect(clusterSchema.getCategories()).andReturn(mapProperties);
+
+    expect(clusterSchema.getKeyPropertyId(Resource.Type.Component)).andReturn(clusterPropertyId).atLeastOnce();
+    expect(result.getResultTree()).andReturn(tree).atLeastOnce();
+    tree.setProperty("isCollection", "true");
+
+    expect(clusterResourceDef.getType()).andReturn(Resource.Type.Component).atLeastOnce();
+    expect(clusterResourceDef.getResourceIds()).andReturn(mapResourceIds);
+
+    expect(m_controller.getResources(eq(Resource.Type.Component), eq(PropertyHelper.getReadRequest(Collections.singleton(clusterPropertyId))),
+        (Predicate) isNull())).andReturn(listResources);
+
+
+    expect(tree.addChild(clusterResource, null)).andReturn(clusterNode);
+
+    replay(m_controller, result, tree, clusterNode, clusterResourceDef, clusterSchema, clusterResource);
+
+    QueryImpl query = new TestQuery(clusterResourceDef, result);
+    query.execute();
+
+    verify(m_controller, result, tree, clusterNode, clusterResourceDef, clusterSchema, clusterResource);
+  }
+
+  @Test
+  public void testExecute__collection_nullInternalPredicate_nonNullUserPredicate() throws Exception {
+    Result result = createStrictMock(Result.class);
+    TreeNode<Resource> tree = createStrictMock(TreeNode.class);
+    TreeNode<Resource> clusterNode = createStrictMock(TreeNode.class);
+    ResourceDefinition clusterResourceDef = createMock(ResourceDefinition.class);
+    Schema clusterSchema = createMock(Schema.class);
+    Resource clusterResource = createStrictMock(Resource.class);
+    PropertyId clusterPropertyId = PropertyHelper.getPropertyId("clusterId", "");
+    Predicate userPredicate = createMock(Predicate.class);
+
+
+    Map<String, Set<String>> mapProperties = new HashMap<String, Set<String>>();
+    mapProperties.put("", Collections.singleton("clusterId"));
+
+    List<Resource> listResources = Collections.singletonList(clusterResource);
+
+    Map<Resource.Type, String> mapResourceIds = new HashMap<Resource.Type, String>();
+
+    // expectations
+    expect(clusterResourceDef.getId()).andReturn(null).atLeastOnce();
+    expect(m_controller.getSchema(Resource.Type.Component)).andReturn(clusterSchema).atLeastOnce();
+    expect(clusterSchema.getCategories()).andReturn(mapProperties);
+
+    expect(clusterSchema.getKeyPropertyId(Resource.Type.Component)).andReturn(clusterPropertyId).atLeastOnce();
+    expect(result.getResultTree()).andReturn(tree).atLeastOnce();
+    tree.setProperty("isCollection", "true");
+
+    expect(clusterResourceDef.getType()).andReturn(Resource.Type.Component).atLeastOnce();
+    expect(clusterResourceDef.getResourceIds()).andReturn(mapResourceIds);
+
+    expect(m_controller.getResources(eq(Resource.Type.Component), eq(PropertyHelper.getReadRequest(Collections.singleton(clusterPropertyId))),
+        eq(userPredicate))).andReturn(listResources);
+
+
+    expect(tree.addChild(clusterResource, null)).andReturn(clusterNode);
+
+    replay(m_controller, result, tree, clusterNode, clusterResourceDef, clusterSchema, clusterResource, userPredicate);
+
+    QueryImpl query = new TestQuery(clusterResourceDef, result);
+    query.setUserPredicate(userPredicate);
+    query.execute();
+
+    verify(m_controller, result, tree, clusterNode, clusterResourceDef, clusterSchema, clusterResource, userPredicate);
+  }
+
+  @Test
+  public void testExecute__collection_nonNullInternalPredicate_nonNullUserPredicate() throws Exception {
+    Result result = createStrictMock(Result.class);
+    TreeNode<Resource> tree = createStrictMock(TreeNode.class);
+    TreeNode<Resource> componentNode = createStrictMock(TreeNode.class);
+    ResourceDefinition componentResourceDef = createMock(ResourceDefinition.class);
+    Schema componentSchema = createMock(Schema.class);
+    Resource componentResource = createStrictMock(Resource.class);
+    PropertyId componentPropertyId = PropertyHelper.getPropertyId("componentId", "");
+
+    Map<String, Set<String>> mapProperties = new HashMap<String, Set<String>>();
+    mapProperties.put("", Collections.singleton("componentId"));
+
+    List<Resource> listResources = Collections.singletonList(componentResource);
+
+    Map<Resource.Type, String> mapResourceIds = new HashMap<Resource.Type, String>();
+    mapResourceIds.put(Resource.Type.Cluster, "clusterName");
+    mapResourceIds.put(Resource.Type.Service, "serviceName");
+    mapResourceIds.put(Resource.Type.Component, "componentName");
+
+    PredicateBuilder pb = new PredicateBuilder();
+    Predicate internalPredicate = pb.property("clusterId", "").equals("clusterName").and().
+        property("serviceId", "").equals("serviceName").and().
+        property("componentId", "").equals("componentName").toPredicate();
+
+    pb = new PredicateBuilder();
+    Predicate userPredicate = pb.property("foo", "").equals("bar").toPredicate();
+    // combine internal predicate and user predicate
+    //todo: for now, need to cast to BasePredicate
+    Predicate predicate = new AndPredicate((BasePredicate) internalPredicate, (BasePredicate) userPredicate);
+
+    // expectations
+    expect(componentResourceDef.getId()).andReturn(null).atLeastOnce();
+    expect(m_controller.getSchema(Resource.Type.Component)).andReturn(componentSchema).atLeastOnce();
+    expect(componentSchema.getCategories()).andReturn(mapProperties);
+
+    expect(componentSchema.getKeyPropertyId(Resource.Type.Component)).andReturn(componentPropertyId).atLeastOnce();
+    expect(result.getResultTree()).andReturn(tree).atLeastOnce();
+    tree.setProperty("isCollection", "true");
+
+    expect(componentResourceDef.getType()).andReturn(Resource.Type.Component).atLeastOnce();
+    expect(componentResourceDef.getResourceIds()).andReturn(mapResourceIds);
+    expect(componentSchema.getKeyPropertyId(Resource.Type.Cluster)).andReturn(new PropertyIdImpl("clusterId", "", false));
+    expect(componentSchema.getKeyPropertyId(Resource.Type.Service)).andReturn(new PropertyIdImpl("serviceId", "", false));
+
+
+    expect(m_controller.getResources(eq(Resource.Type.Component), eq(PropertyHelper.getReadRequest(Collections.singleton(componentPropertyId))),
+        eq(predicate))).andReturn(listResources);
+
+
+    expect(tree.addChild(componentResource, null)).andReturn(componentNode);
+
+    replay(m_controller, result, tree, componentNode, componentResourceDef, componentSchema, componentResource);
+
+    QueryImpl query = new TestQuery(componentResourceDef, result);
+    query.setUserPredicate(userPredicate);
+    query.execute();
+
+    verify(m_controller, result, tree, componentNode, componentResourceDef, componentSchema, componentResource);
   }
 
   @Test
@@ -239,10 +386,10 @@ public class QueryImplTest {
 
   //todo: sub-resource with property and with sub-path
 
-  @Test
-  public void testAddProperty__invalidProperty() {
-
-  }
+//  @Test
+//  public void testAddProperty__invalidProperty() {
+//
+//  }
 
   private class TestQuery extends QueryImpl {