incubator-ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From maha...@apache.org
Subject svn commit: r1389109 [5/6] - in /incubator/ambari/branches/AMBARI-666: ./ ambari-api/ ambari-api/src/ ambari-api/src/main/ ambari-api/src/main/java/ ambari-api/src/main/java/org/ ambari-api/src/main/java/org/apache/ ambari-api/src/main/java/org/apache/...
Date Sun, 23 Sep 2012 18:01:54 GMT
Added: incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/controller/utilities/PredicateBuilderTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/controller/utilities/PredicateBuilderTest.java?rev=1389109&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/controller/utilities/PredicateBuilderTest.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/controller/utilities/PredicateBuilderTest.java Sun Sep 23 18:01:49 2012
@@ -0,0 +1,806 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ambari.api.controller.utilities;
+
+import junit.framework.Assert;
+import org.apache.ambari.api.controller.internal.PropertyIdImpl;
+import org.apache.ambari.api.controller.internal.ResourceImpl;
+import org.apache.ambari.api.controller.spi.Predicate;
+import org.apache.ambari.api.controller.spi.PropertyId;
+import org.apache.ambari.api.controller.spi.Resource;
+import org.junit.Test;
+
+/**
+ *
+ */
+public class PredicateBuilderTest {
+
+  @Test
+  public void testSimple() {
+    PropertyId p1 = new PropertyIdImpl("prop1", "cat1", false);
+
+    Resource resource = new ResourceImpl(Resource.Type.Cluster);
+    resource.setProperty(p1, "foo");
+
+    PredicateBuilder pb = new PredicateBuilder();
+    Predicate predicate1 = pb.property(p1).equals("foo").toPredicate();
+
+    Assert.assertTrue(predicate1.evaluate(resource));
+
+    PredicateBuilder pb2 = new PredicateBuilder();
+    Predicate predicate2 = pb2.property(p1).equals("bar").toPredicate();
+
+    Assert.assertFalse(predicate2.evaluate(resource));
+  }
+
+  @Test
+  public void testSimpleNot() {
+    PropertyId p1 = new PropertyIdImpl("prop1", "cat1", false);
+
+    Resource resource = new ResourceImpl(Resource.Type.Cluster);
+    resource.setProperty(p1, "foo");
+
+    /*  ! p1 == "foo" */
+    PredicateBuilder pb = new PredicateBuilder();
+    Predicate predicate1 = pb.not().property(p1).equals("foo").toPredicate();
+
+    Assert.assertFalse(predicate1.evaluate(resource));
+
+    /*  ! p1 == "bar" */
+    PredicateBuilder pb2 = new PredicateBuilder();
+    Predicate predicate2 = pb2.not().property(p1).equals("bar").toPredicate();
+
+    Assert.assertTrue(predicate2.evaluate(resource));
+  }
+
+  @Test
+  public void testDone() {
+    PropertyId p1 = new PropertyIdImpl("prop1", "cat1", false);
+
+    Resource resource = new ResourceImpl(Resource.Type.Cluster);
+    resource.setProperty(p1, "foo");
+
+    PredicateBuilder pb = new PredicateBuilder();
+    Predicate predicate = pb.property(p1).equals("foo").toPredicate();
+
+    // can't reuse a builder after toPredicate is called.
+    try {
+      pb.property(p1).equals("foo").toPredicate();
+      Assert.fail("Expected IllegalStateException.");
+    } catch (IllegalStateException e) {
+      // expected
+    }
+
+    Assert.assertSame(predicate, pb.toPredicate());
+  }
+
+  @Test
+  public void testSimpleAnd() {
+    PropertyId p1 = new PropertyIdImpl("prop1", "cat1", false);
+    PropertyId p2 = new PropertyIdImpl("prop2", "cat1", false);
+
+    Resource resource = new ResourceImpl(Resource.Type.Cluster);
+    resource.setProperty(p1, "foo");
+    resource.setProperty(p2, "bar");
+
+    PredicateBuilder pb = new PredicateBuilder();
+    Predicate predicate1 = pb.property(p1).equals("foo").and().property(p2).equals("bar").toPredicate();
+
+    Assert.assertTrue(predicate1.evaluate(resource));
+
+    PredicateBuilder pb2 = new PredicateBuilder();
+    Predicate predicate2 = pb2.property(p1).equals("foo").and().property(p2).equals("car").toPredicate();
+
+    Assert.assertFalse(predicate2.evaluate(resource));
+  }
+
+  @Test
+  public void testSimpleAndNot() {
+    PropertyId p1 = new PropertyIdImpl("prop1", "cat1", false);
+    PropertyId p2 = new PropertyIdImpl("prop2", "cat1", false);
+
+    Resource resource = new ResourceImpl(Resource.Type.Cluster);
+    resource.setProperty(p1, "foo");
+    resource.setProperty(p2, "bar");
+
+    /* p1 == foo and !p2 == bar */
+    PredicateBuilder pb = new PredicateBuilder();
+    Predicate predicate1 = pb.property(p1).equals("foo").and().not().property(p2).equals("bar").toPredicate();
+
+    Assert.assertFalse(predicate1.evaluate(resource));
+
+    /* p1 == foo and !p2 == car */
+    PredicateBuilder pb2 = new PredicateBuilder();
+    Predicate predicate2 = pb2.property(p1).equals("foo").and().not().property(p2).equals("car").toPredicate();
+
+    Assert.assertTrue(predicate2.evaluate(resource));
+  }
+
+  @Test
+  public void testLongAnd() {
+    PropertyId p1 = new PropertyIdImpl("prop1", "cat1", false);
+    PropertyId p2 = new PropertyIdImpl("prop2", "cat1", false);
+    PropertyId p3 = new PropertyIdImpl("prop3", "cat1", false);
+    PropertyId p4 = new PropertyIdImpl("prop4", "cat1", false);
+
+    Resource resource = new ResourceImpl(Resource.Type.Cluster);
+    resource.setProperty(p1, "foo");
+    resource.setProperty(p2, "bar");
+    resource.setProperty(p3, "cat");
+    resource.setProperty(p4, "dog");
+
+    PredicateBuilder pb = new PredicateBuilder();
+    Predicate predicate1 = pb.property(p1).equals("foo").and().property(p2).equals("bar").and().property(p3).equals("cat").and().property(p4).equals("dog").toPredicate();
+
+    Assert.assertTrue(predicate1.evaluate(resource));
+
+    PredicateBuilder pb2 = new PredicateBuilder();
+    Predicate predicate2 = pb2.property(p1).equals("foo").and().property(p2).equals("bar").and().property(p3).equals("cat").and().property(p4).equals("dot").toPredicate();
+
+    Assert.assertFalse(predicate2.evaluate(resource));
+  }
+
+  @Test
+  public void testSimpleOr() {
+    PropertyId p1 = new PropertyIdImpl("prop1", "cat1", false);
+    PropertyId p2 = new PropertyIdImpl("prop2", "cat1", false);
+
+    Resource resource = new ResourceImpl(Resource.Type.Cluster);
+    resource.setProperty(p1, "foo");
+    resource.setProperty(p2, "bar");
+
+    PredicateBuilder pb = new PredicateBuilder();
+    Predicate predicate1 = pb.property(p1).equals("foo").or().property(p2).equals("bar").toPredicate();
+
+    Assert.assertTrue(predicate1.evaluate(resource));
+
+    PredicateBuilder pb2 = new PredicateBuilder();
+    Predicate predicate2 = pb2.property(p1).equals("foo").or().property(p2).equals("car").toPredicate();
+
+    Assert.assertTrue(predicate2.evaluate(resource));
+
+    PredicateBuilder pb3 = new PredicateBuilder();
+    Predicate predicate3 = pb3.property(p1).equals("fun").or().property(p2).equals("car").toPredicate();
+
+    Assert.assertFalse(predicate3.evaluate(resource));
+  }
+
+  @Test
+  public void testLongOr() {
+    PropertyId p1 = new PropertyIdImpl("prop1", "cat1", false);
+    PropertyId p2 = new PropertyIdImpl("prop2", "cat1", false);
+    PropertyId p3 = new PropertyIdImpl("prop3", "cat1", false);
+    PropertyId p4 = new PropertyIdImpl("prop4", "cat1", false);
+
+    Resource resource = new ResourceImpl(Resource.Type.Cluster);
+    resource.setProperty(p1, "foo");
+    resource.setProperty(p2, "bar");
+    resource.setProperty(p3, "cat");
+    resource.setProperty(p4, "dog");
+
+    PredicateBuilder pb = new PredicateBuilder();
+    Predicate predicate1 = pb.property(p1).equals("foo").or().property(p2).equals("bar").or().property(p3).equals("cat").or().property(p4).equals("dog").toPredicate();
+
+    Assert.assertTrue(predicate1.evaluate(resource));
+
+    PredicateBuilder pb2 = new PredicateBuilder();
+    Predicate predicate2 = pb2.property(p1).equals("foo").or().property(p2).equals("car").or().property(p3).equals("cat").or().property(p4).equals("dog").toPredicate();
+
+    Assert.assertTrue(predicate2.evaluate(resource));
+
+    PredicateBuilder pb3 = new PredicateBuilder();
+    Predicate predicate3 = pb3.property(p1).equals("fun").or().property(p2).equals("car").or().property(p3).equals("bat").or().property(p4).equals("dot").toPredicate();
+
+    Assert.assertFalse(predicate3.evaluate(resource));
+  }
+
+  @Test
+  public void testAndOr() {
+    PropertyId p1 = new PropertyIdImpl("prop1", "cat1", false);
+    PropertyId p2 = new PropertyIdImpl("prop2", "cat1", false);
+    PropertyId p3 = new PropertyIdImpl("prop3", "cat1", false);
+
+    Resource resource = new ResourceImpl(Resource.Type.Cluster);
+    resource.setProperty(p1, "foo");
+    resource.setProperty(p2, "bar");
+    resource.setProperty(p3, "cat");
+
+    PredicateBuilder pb1 = new PredicateBuilder();
+    Predicate predicate1 = pb1.property(p1).equals("foo").and().property(p2).equals("bar").or().property(p3).equals("cat").toPredicate();
+
+    Assert.assertTrue(predicate1.evaluate(resource));
+
+
+    PredicateBuilder pb2 = new PredicateBuilder();
+    Predicate predicate2 = pb2.property(p1).equals("foo").and().property(p2).equals("car").or().property(p3).equals("cat").toPredicate();
+
+    Assert.assertTrue(predicate2.evaluate(resource));
+
+
+    PredicateBuilder pb3 = new PredicateBuilder();
+    Predicate predicate3 = pb3.property(p1).equals("foo").and().property(p2).equals("bar").or().property(p3).equals("can").toPredicate();
+
+    Assert.assertTrue(predicate3.evaluate(resource));
+
+
+    PredicateBuilder pb4 = new PredicateBuilder();
+    Predicate predicate4 = pb4.property(p1).equals("foo").and().property(p2).equals("bat").or().property(p3).equals("can").toPredicate();
+
+    Assert.assertFalse(predicate4.evaluate(resource));
+  }
+
+
+  @Test
+  public void testBlocks() {
+    PropertyId p1 = new PropertyIdImpl("prop1", "cat1", false);
+    PropertyId p2 = new PropertyIdImpl("prop2", "cat1", false);
+    PropertyId p3 = new PropertyIdImpl("prop3", "cat1", false);
+
+    Resource resource = new ResourceImpl(Resource.Type.Cluster);
+    resource.setProperty(p1, "foo");
+    resource.setProperty(p2, "bar");
+    resource.setProperty(p3, "cat");
+
+
+    /*   (p1==foo && p2==bar) || p3 == cat   */
+    PredicateBuilder pb1 = new PredicateBuilder();
+    Predicate predicate1 = pb1.begin().property(p1).equals("foo").and().property(p2).equals("bar").end().or().property(p3).equals("cat").toPredicate();
+
+    Assert.assertTrue(predicate1.evaluate(resource));
+
+    /*   (p1==foo && p2==bat) || p3 == cat   */
+    PredicateBuilder pb2 = new PredicateBuilder();
+    Predicate predicate2 = pb2.begin().property(p1).equals("foo").and().property(p2).equals("bat").end().or().property(p3).equals("cat").toPredicate();
+
+    Assert.assertTrue(predicate2.evaluate(resource));
+
+    /*   (p1==foo && p2==bar) || p3 == can   */
+    PredicateBuilder pb3 = new PredicateBuilder();
+    Predicate predicate3 = pb3.begin().property(p1).equals("foo").and().property(p2).equals("bar").end().or().property(p3).equals("can").toPredicate();
+
+    Assert.assertTrue(predicate3.evaluate(resource));
+
+    /*   (p1==foo && p2==bat) || p3 == can   */
+    PredicateBuilder pb4 = new PredicateBuilder();
+    Predicate predicate4 = pb4.begin().property(p1).equals("foo").and().property(p2).equals("bat").end().or().property(p3).equals("can").toPredicate();
+
+    Assert.assertFalse(predicate4.evaluate(resource));
+
+
+    /*   p1==foo && (p2==bar || p3 == cat)   */
+    PredicateBuilder pb5 = new PredicateBuilder();
+    Predicate predicate5 = pb5.property(p1).equals("foo").and().begin().property(p2).equals("bar").or().property(p3).equals("cat").end().toPredicate();
+
+    Assert.assertTrue(predicate5.evaluate(resource));
+
+    /*   p1==foo && (p2==bat || p3 == cat)   */
+    PredicateBuilder pb6 = new PredicateBuilder();
+    Predicate predicate6 = pb6.property(p1).equals("foo").and().begin().property(p2).equals("bat").or().property(p3).equals("cat").end().toPredicate();
+
+    Assert.assertTrue(predicate6.evaluate(resource));
+
+    /*   p1==foo && (p2==bat || p3 == can)   */
+    PredicateBuilder pb7 = new PredicateBuilder();
+    Predicate predicate7 = pb7.property(p1).equals("foo").and().begin().property(p2).equals("bat").or().property(p3).equals("can").end().toPredicate();
+
+    Assert.assertFalse(predicate7.evaluate(resource));
+
+    /*   p1==fat && (p2==bar || p3 == cat)   */
+    PredicateBuilder pb8 = new PredicateBuilder();
+    Predicate predicate8 = pb8.property(p1).equals("fat").and().begin().property(p2).equals("bar").or().property(p3).equals("cat").end().toPredicate();
+
+    Assert.assertFalse(predicate8.evaluate(resource));
+
+    /*   p1==foo && !(p2==bar || p3 == cat)   */
+    PredicateBuilder pb9 = new PredicateBuilder();
+    Predicate predicate9 = pb9.property(p1).equals("foo").and().not().begin().property(p2).equals("bar").or().property(p3).equals("cat").end().toPredicate();
+
+    Assert.assertFalse(predicate9.evaluate(resource));
+
+
+    /*   p1==foo && !(p2==bat || p3 == car)   */
+    PredicateBuilder pb10 = new PredicateBuilder();
+    Predicate predicate10 = pb10.property(p1).equals("foo").and().not().begin().property(p2).equals("bat").or().property(p3).equals("car").end().toPredicate();
+
+    Assert.assertTrue(predicate10.evaluate(resource));
+  }
+
+  @Test
+  public void testNestedBlocks() {
+    PropertyId p1 = new PropertyIdImpl("prop1", "cat1", false);
+    PropertyId p2 = new PropertyIdImpl("prop2", "cat1", false);
+    PropertyId p3 = new PropertyIdImpl("prop3", "cat1", false);
+    PropertyId p4 = new PropertyIdImpl("prop4", "cat1", false);
+
+    Resource resource = new ResourceImpl(Resource.Type.Cluster);
+    resource.setProperty(p1, "foo");
+    resource.setProperty(p2, "bar");
+    resource.setProperty(p3, "cat");
+    resource.setProperty(p4, "dog");
+
+    /*   (p1==foo && (p2==bar || p3==cat)) || p4 == dog   */
+    PredicateBuilder pb1 = new PredicateBuilder();
+    Predicate predicate1 = pb1.
+        begin().
+        property(p1).equals("foo").and().
+        begin().
+        property(p2).equals("bar").or().property(p3).equals("cat").
+        end().
+        end().
+        or().property(p4).equals("dog").toPredicate();
+
+    Assert.assertTrue(predicate1.evaluate(resource));
+
+
+    /*   (p1==fat && (p2==bar || p3==cat)) || p4 == dot   */
+    PredicateBuilder pb2 = new PredicateBuilder();
+    Predicate predicate2 = pb2.
+        begin().
+        property(p1).equals("fat").and().
+        begin().
+        property(p2).equals("bar").or().property(p3).equals("cat").
+        end().
+        end().
+        or().property(p4).equals("dot").toPredicate();
+
+    Assert.assertFalse(predicate2.evaluate(resource));
+  }
+
+
+  @Test
+  public void testUnbalancedBlocks() {
+    PropertyId p1 = new PropertyIdImpl("prop1", "cat1", false);
+    PropertyId p2 = new PropertyIdImpl("prop2", "cat1", false);
+    PropertyId p3 = new PropertyIdImpl("prop3", "cat1", false);
+    PropertyId p4 = new PropertyIdImpl("prop4", "cat1", false);
+
+    Resource resource = new ResourceImpl(Resource.Type.Cluster);
+    resource.setProperty(p1, "foo");
+    resource.setProperty(p2, "bar");
+    resource.setProperty(p3, "cat");
+    resource.setProperty(p4, "dog");
+
+    /*   (p1==foo && (p2==bar || p3==cat) || p4 == dog   */
+    PredicateBuilder pb1 = new PredicateBuilder();
+    try {
+      pb1.
+          begin().
+          property(p1).equals("foo").and().
+          begin().
+          property(p2).equals("bar").or().property(p3).equals("cat").
+          end().
+          or().property(p4).equals("dog").toPredicate();
+      Assert.fail("Expected IllegalStateException.");
+    } catch (IllegalStateException e) {
+      // expected
+    }
+
+    /*   (p1==foo && p2==bar || p3==cat)) || p4 == dog   */
+    PredicateBuilder pb2 = new PredicateBuilder();
+    try {
+      pb2.
+          begin().
+          property(p1).equals("foo").and().
+          property(p2).equals("bar").or().property(p3).equals("cat").
+          end().
+          end().
+          or().property(p4).equals("dog").toPredicate();
+      Assert.fail("Expected IllegalStateException.");
+    } catch (IllegalStateException e) {
+      // expected
+    }
+  }
+
+  @Test
+  public void testAltProperty() {
+    PropertyId p1 = new PropertyIdImpl("prop1", "cat1", false);
+    PropertyId p2 = new PropertyIdImpl("prop2", "cat1", false);
+    PropertyId p3 = new PropertyIdImpl("prop3", null, false);
+
+    Resource resource = new ResourceImpl(Resource.Type.Cluster);
+    resource.setProperty(p1, "foo");
+    resource.setProperty(p2, "bar");
+    resource.setProperty(p3, "cat");
+
+
+    /*   (p1==foo && p2==bar) || p3 == cat   */
+    PredicateBuilder pb1 = new PredicateBuilder();
+    Predicate predicate1 = pb1.begin().property("prop1", "cat1", false).equals("foo").and().property("prop2", "cat1").equals("bar").end().or().property("prop3").equals("cat").toPredicate();
+
+    Assert.assertTrue(predicate1.evaluate(resource));
+  }
+
+
+  @Test
+  public void testEqualsString() {
+    PropertyId p1 = new PropertyIdImpl("prop1", "cat1", false);
+
+    Resource resource = new ResourceImpl(Resource.Type.Cluster);
+    resource.setProperty(p1, "foo");
+
+    PredicateBuilder pb = new PredicateBuilder();
+    Predicate predicate1 = pb.property(p1).equals("foo").toPredicate();
+
+    Assert.assertTrue(predicate1.evaluate(resource));
+
+    PredicateBuilder pb2 = new PredicateBuilder();
+    Predicate predicate2 = pb2.property(p1).equals("bar").toPredicate();
+
+    Assert.assertFalse(predicate2.evaluate(resource));
+  }
+
+  @Test
+  public void testEqualsInteger() {
+    PropertyId p1 = new PropertyIdImpl("prop1", "cat1", false);
+
+    Resource resource = new ResourceImpl(Resource.Type.Cluster);
+    resource.setProperty(p1, 1);
+
+    PredicateBuilder pb = new PredicateBuilder();
+    Predicate predicate1 = pb.property(p1).equals(1).toPredicate();
+
+    Assert.assertTrue(predicate1.evaluate(resource));
+
+    PredicateBuilder pb2 = new PredicateBuilder();
+    Predicate predicate2 = pb2.property(p1).equals(99).toPredicate();
+
+    Assert.assertFalse(predicate2.evaluate(resource));
+  }
+
+  @Test
+  public void testEqualsFloat() {
+    PropertyId p1 = new PropertyIdImpl("prop1", "cat1", false);
+
+    Resource resource = new ResourceImpl(Resource.Type.Cluster);
+    resource.setProperty(p1, (float) 1);
+
+    PredicateBuilder pb = new PredicateBuilder();
+    Predicate predicate1 = pb.property(p1).equals(Float.valueOf(1)).toPredicate();
+
+    Assert.assertTrue(predicate1.evaluate(resource));
+
+    PredicateBuilder pb2 = new PredicateBuilder();
+    Predicate predicate2 = pb2.property(p1).equals(Float.valueOf(99)).toPredicate();
+
+    Assert.assertFalse(predicate2.evaluate(resource));
+  }
+
+  @Test
+  public void testEqualsDouble() {
+    PropertyId p1 = new PropertyIdImpl("prop1", "cat1", false);
+
+    Resource resource = new ResourceImpl(Resource.Type.Cluster);
+    resource.setProperty(p1, 1.999);
+
+    PredicateBuilder pb = new PredicateBuilder();
+    Predicate predicate1 = pb.property(p1).equals(Double.valueOf(1.999)).toPredicate();
+
+    Assert.assertTrue(predicate1.evaluate(resource));
+
+    PredicateBuilder pb2 = new PredicateBuilder();
+    Predicate predicate2 = pb2.property(p1).equals(Double.valueOf(99.998)).toPredicate();
+
+    Assert.assertFalse(predicate2.evaluate(resource));
+  }
+
+  @Test
+  public void testEqualsLong() {
+    PropertyId p1 = new PropertyIdImpl("prop1", "cat1", false);
+
+    Resource resource = new ResourceImpl(Resource.Type.Cluster);
+    resource.setProperty(p1, 1L);
+
+    PredicateBuilder pb = new PredicateBuilder();
+    Predicate predicate1 = pb.property(p1).equals(1L).toPredicate();
+
+    Assert.assertTrue(predicate1.evaluate(resource));
+
+    PredicateBuilder pb2 = new PredicateBuilder();
+    Predicate predicate2 = pb2.property(p1).equals(99L).toPredicate();
+
+    Assert.assertFalse(predicate2.evaluate(resource));
+  }
+
+  @Test
+  public void testGreaterInteger() {
+    PropertyId p1 = new PropertyIdImpl("prop1", "cat1", false);
+
+    Resource resource = new ResourceImpl(Resource.Type.Cluster);
+    resource.setProperty(p1, 2);
+
+    PredicateBuilder pb = new PredicateBuilder();
+    Predicate predicate1 = pb.property(p1).greaterThan(1).toPredicate();
+
+    Assert.assertTrue(predicate1.evaluate(resource));
+
+    PredicateBuilder pb2 = new PredicateBuilder();
+    Predicate predicate2 = pb2.property(p1).greaterThan(99).toPredicate();
+
+    Assert.assertFalse(predicate2.evaluate(resource));
+  }
+
+  @Test
+  public void testGreaterFloat() {
+    PropertyId p1 = new PropertyIdImpl("prop1", "cat1", false);
+
+    Resource resource = new ResourceImpl(Resource.Type.Cluster);
+    resource.setProperty(p1, (float) 2);
+
+    PredicateBuilder pb = new PredicateBuilder();
+    Predicate predicate1 = pb.property(p1).greaterThan((float) 1).toPredicate();
+
+    Assert.assertTrue(predicate1.evaluate(resource));
+
+    PredicateBuilder pb2 = new PredicateBuilder();
+    Predicate predicate2 = pb2.property(p1).greaterThan((float) 99).toPredicate();
+
+    Assert.assertFalse(predicate2.evaluate(resource));
+  }
+
+  @Test
+  public void testGreaterDouble() {
+    PropertyId p1 = new PropertyIdImpl("prop1", "cat1", false);
+
+    Resource resource = new ResourceImpl(Resource.Type.Cluster);
+    resource.setProperty(p1, 2.999);
+
+    PredicateBuilder pb = new PredicateBuilder();
+    Predicate predicate1 = pb.property(p1).greaterThan(1.999).toPredicate();
+
+    Assert.assertTrue(predicate1.evaluate(resource));
+
+    PredicateBuilder pb2 = new PredicateBuilder();
+    Predicate predicate2 = pb2.property(p1).greaterThan(99.998).toPredicate();
+
+    Assert.assertFalse(predicate2.evaluate(resource));
+  }
+
+  @Test
+  public void testGreaterLong() {
+    PropertyId p1 = new PropertyIdImpl("prop1", "cat1", false);
+
+    Resource resource = new ResourceImpl(Resource.Type.Cluster);
+    resource.setProperty(p1, 2L);
+
+    PredicateBuilder pb = new PredicateBuilder();
+    Predicate predicate1 = pb.property(p1).greaterThan(1L).toPredicate();
+
+    Assert.assertTrue(predicate1.evaluate(resource));
+
+    PredicateBuilder pb2 = new PredicateBuilder();
+    Predicate predicate2 = pb2.property(p1).greaterThan(99L).toPredicate();
+
+    Assert.assertFalse(predicate2.evaluate(resource));
+  }
+
+  @Test
+  public void testGreaterThanEqualToInteger() {
+    PropertyId p1 = new PropertyIdImpl("prop1", "cat1", false);
+
+    Resource resource = new ResourceImpl(Resource.Type.Cluster);
+    resource.setProperty(p1, 2);
+
+    PredicateBuilder pb = new PredicateBuilder();
+    Predicate predicate1 = pb.property(p1).greaterThanEqualTo(1).toPredicate();
+
+    Assert.assertTrue(predicate1.evaluate(resource));
+
+    PredicateBuilder pb2 = new PredicateBuilder();
+    Predicate predicate2 = pb2.property(p1).greaterThanEqualTo(99).toPredicate();
+
+    Assert.assertFalse(predicate2.evaluate(resource));
+  }
+
+  @Test
+  public void testGreaterThanEqualToFloat() {
+    PropertyId p1 = new PropertyIdImpl("prop1", "cat1", false);
+
+    Resource resource = new ResourceImpl(Resource.Type.Cluster);
+    resource.setProperty(p1, (float) 2);
+
+    PredicateBuilder pb = new PredicateBuilder();
+    Predicate predicate1 = pb.property(p1).greaterThanEqualTo((float) 1).toPredicate();
+
+    Assert.assertTrue(predicate1.evaluate(resource));
+
+    PredicateBuilder pb2 = new PredicateBuilder();
+    Predicate predicate2 = pb2.property(p1).greaterThanEqualTo((float) 99).toPredicate();
+
+    Assert.assertFalse(predicate2.evaluate(resource));
+  }
+
+  @Test
+  public void testGreaterThanEqualToDouble() {
+    PropertyId p1 = new PropertyIdImpl("prop1", "cat1", false);
+
+    Resource resource = new ResourceImpl(Resource.Type.Cluster);
+    resource.setProperty(p1, 2.999);
+
+    PredicateBuilder pb = new PredicateBuilder();
+    Predicate predicate1 = pb.property(p1).greaterThanEqualTo(1.999).toPredicate();
+
+    Assert.assertTrue(predicate1.evaluate(resource));
+
+    PredicateBuilder pb2 = new PredicateBuilder();
+    Predicate predicate2 = pb2.property(p1).greaterThanEqualTo(99.998).toPredicate();
+
+    Assert.assertFalse(predicate2.evaluate(resource));
+  }
+
+  @Test
+  public void testGreaterThanEqualToLong() {
+    PropertyId p1 = new PropertyIdImpl("prop1", "cat1", false);
+
+    Resource resource = new ResourceImpl(Resource.Type.Cluster);
+    resource.setProperty(p1, 2L);
+
+    PredicateBuilder pb = new PredicateBuilder();
+    Predicate predicate1 = pb.property(p1).greaterThanEqualTo(1L).toPredicate();
+
+    Assert.assertTrue(predicate1.evaluate(resource));
+
+    PredicateBuilder pb2 = new PredicateBuilder();
+    Predicate predicate2 = pb2.property(p1).greaterThanEqualTo(99L).toPredicate();
+
+    Assert.assertFalse(predicate2.evaluate(resource));
+  }
+
+  @Test
+  public void testLessInteger() {
+    PropertyId p1 = new PropertyIdImpl("prop1", "cat1", false);
+
+    Resource resource = new ResourceImpl(Resource.Type.Cluster);
+    resource.setProperty(p1, 2);
+
+    PredicateBuilder pb = new PredicateBuilder();
+    Predicate predicate1 = pb.property(p1).lessThan(99).toPredicate();
+
+    Assert.assertTrue(predicate1.evaluate(resource));
+
+    PredicateBuilder pb2 = new PredicateBuilder();
+    Predicate predicate2 = pb2.property(p1).lessThan(1).toPredicate();
+
+    Assert.assertFalse(predicate2.evaluate(resource));
+  }
+
+  @Test
+  public void testLessFloat() {
+    PropertyId p1 = new PropertyIdImpl("prop1", "cat1", false);
+
+    Resource resource = new ResourceImpl(Resource.Type.Cluster);
+    resource.setProperty(p1, (float) 2);
+
+    PredicateBuilder pb = new PredicateBuilder();
+    Predicate predicate1 = pb.property(p1).lessThan((float) 99).toPredicate();
+
+    Assert.assertTrue(predicate1.evaluate(resource));
+
+    PredicateBuilder pb2 = new PredicateBuilder();
+    Predicate predicate2 = pb2.property(p1).lessThan((float) 1).toPredicate();
+
+    Assert.assertFalse(predicate2.evaluate(resource));
+  }
+
+  @Test
+  public void testLessDouble() {
+    PropertyId p1 = new PropertyIdImpl("prop1", "cat1", false);
+
+    Resource resource = new ResourceImpl(Resource.Type.Cluster);
+    resource.setProperty(p1, 2.999);
+
+    PredicateBuilder pb = new PredicateBuilder();
+    Predicate predicate1 = pb.property(p1).lessThan(99.999).toPredicate();
+
+    Assert.assertTrue(predicate1.evaluate(resource));
+
+    PredicateBuilder pb2 = new PredicateBuilder();
+    Predicate predicate2 = pb2.property(p1).lessThan(1.998).toPredicate();
+
+    Assert.assertFalse(predicate2.evaluate(resource));
+  }
+
+  @Test
+  public void testLessLong() {
+    PropertyId p1 = new PropertyIdImpl("prop1", "cat1", false);
+
+    Resource resource = new ResourceImpl(Resource.Type.Cluster);
+    resource.setProperty(p1, 2L);
+
+    PredicateBuilder pb = new PredicateBuilder();
+    Predicate predicate1 = pb.property(p1).lessThan(99L).toPredicate();
+
+    Assert.assertTrue(predicate1.evaluate(resource));
+
+    PredicateBuilder pb2 = new PredicateBuilder();
+    Predicate predicate2 = pb2.property(p1).lessThan(1L).toPredicate();
+
+    Assert.assertFalse(predicate2.evaluate(resource));
+  }
+
+  @Test
+  public void testLessThanEqualToInteger() {
+    PropertyId p1 = new PropertyIdImpl("prop1", "cat1", false);
+
+    Resource resource = new ResourceImpl(Resource.Type.Cluster);
+    resource.setProperty(p1, 2);
+
+    PredicateBuilder pb = new PredicateBuilder();
+    Predicate predicate1 = pb.property(p1).lessThanEqualTo(99).toPredicate();
+
+    Assert.assertTrue(predicate1.evaluate(resource));
+
+    PredicateBuilder pb2 = new PredicateBuilder();
+    Predicate predicate2 = pb2.property(p1).lessThanEqualTo(1).toPredicate();
+
+    Assert.assertFalse(predicate2.evaluate(resource));
+  }
+
+  @Test
+  public void testLessThanEqualToFloat() {
+    PropertyId p1 = new PropertyIdImpl("prop1", "cat1", false);
+
+    Resource resource = new ResourceImpl(Resource.Type.Cluster);
+    resource.setProperty(p1, (float) 2);
+
+    PredicateBuilder pb = new PredicateBuilder();
+    Predicate predicate1 = pb.property(p1).lessThanEqualTo((float) 99).toPredicate();
+
+    Assert.assertTrue(predicate1.evaluate(resource));
+
+    PredicateBuilder pb2 = new PredicateBuilder();
+    Predicate predicate2 = pb2.property(p1).lessThanEqualTo((float) 1).toPredicate();
+
+    Assert.assertFalse(predicate2.evaluate(resource));
+  }
+
+  @Test
+  public void testLessThanEqualToDouble() {
+    PropertyId p1 = new PropertyIdImpl("prop1", "cat1", false);
+
+    Resource resource = new ResourceImpl(Resource.Type.Cluster);
+    resource.setProperty(p1, 2.999);
+
+    PredicateBuilder pb = new PredicateBuilder();
+    Predicate predicate1 = pb.property(p1).lessThanEqualTo(99.999).toPredicate();
+
+    Assert.assertTrue(predicate1.evaluate(resource));
+
+    PredicateBuilder pb2 = new PredicateBuilder();
+    Predicate predicate2 = pb2.property(p1).lessThanEqualTo(1.998).toPredicate();
+
+    Assert.assertFalse(predicate2.evaluate(resource));
+  }
+
+  @Test
+  public void testLessThanEqualToLong() {
+    PropertyId p1 = new PropertyIdImpl("prop1", "cat1", false);
+
+    Resource resource = new ResourceImpl(Resource.Type.Cluster);
+    resource.setProperty(p1, 2L);
+
+    PredicateBuilder pb = new PredicateBuilder();
+    Predicate predicate1 = pb.property(p1).lessThanEqualTo(99L).toPredicate();
+
+    Assert.assertTrue(predicate1.evaluate(resource));
+
+    PredicateBuilder pb2 = new PredicateBuilder();
+    Predicate predicate2 = pb2.property(p1).lessThanEqualTo(1L).toPredicate();
+
+    Assert.assertFalse(predicate2.evaluate(resource));
+  }
+}

Added: incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/controller/utilities/PropertiesTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/controller/utilities/PropertiesTest.java?rev=1389109&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/controller/utilities/PropertiesTest.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/controller/utilities/PropertiesTest.java Sun Sep 23 18:01:49 2012
@@ -0,0 +1,66 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ambari.api.controller.utilities;
+
+import org.apache.ambari.api.controller.spi.PropertyId;
+import org.apache.ambari.api.controller.spi.Resource;
+import org.junit.Test;
+
+import java.util.Map;
+import java.util.Set;
+
+/**
+ *
+ */
+public class PropertiesTest {
+
+  @Test
+  public void testGetPropertyIds() throws Exception {
+
+
+    Set<PropertyId> propertyIds = Properties.getPropertyIds(Resource.Type.HostComponent, "DB");
+
+    for (PropertyId propertyId : propertyIds) {
+//            System.out.println(propertyId);
+    }
+
+    propertyIds = Properties.getPropertyIds(Resource.Type.HostComponent, "JMX");
+
+    for (PropertyId propertyId : propertyIds) {
+//            System.out.println(propertyId);
+    }
+
+    propertyIds = Properties.getPropertyIds(Resource.Type.HostComponent, "GANGLIA");
+
+    for (PropertyId propertyId : propertyIds) {
+//            System.out.println(propertyId);
+    }
+  }
+
+
+  @Test
+  public void testGetKeyPropertyIds() throws Exception {
+
+    Map<String, PropertyId> keyProperties = Properties.getKeyPropertyIds(Resource.Type.Service);
+
+    System.out.println(keyProperties);
+  }
+
+
+}
+

Added: incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/handlers/DelegatingRequestHandlerTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/handlers/DelegatingRequestHandlerTest.java?rev=1389109&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/handlers/DelegatingRequestHandlerTest.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/handlers/DelegatingRequestHandlerTest.java Sun Sep 23 18:01:49 2012
@@ -0,0 +1,124 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ambari.api.handlers;
+
+import org.apache.ambari.api.services.Request;
+import org.apache.ambari.api.services.Result;
+import org.junit.Test;
+
+import static org.easymock.EasyMock.*;
+import static org.junit.Assert.assertSame;
+
+/**
+ *
+ */
+public class DelegatingRequestHandlerTest {
+
+  @Test
+  public void testHandleRequest_GET() {
+    Request request = createStrictMock(Request.class);
+    RequestHandlerFactory factory = createStrictMock(RequestHandlerFactory.class);
+    RequestHandler readRequestHandler = createStrictMock(RequestHandler.class);
+    Result result = createStrictMock(Result.class);
+
+    // expectations
+    expect(request.getRequestType()).andReturn(Request.RequestType.GET);
+    expect(factory.getRequestHandler(Request.RequestType.GET)).andReturn(readRequestHandler);
+    expect(readRequestHandler.handleRequest(request)).andReturn(result);
+
+    replay(request, factory, readRequestHandler, result);
+
+    RequestHandler delegatingRequestHandler = new TestDelegatingRequestHandler(factory);
+
+    assertSame(result, delegatingRequestHandler.handleRequest(request));
+    verify(request, factory, readRequestHandler, result);
+  }
+
+  @Test
+  public void testHandleRequest_PUT() {
+    Request request = createStrictMock(Request.class);
+    RequestHandlerFactory factory = createStrictMock(RequestHandlerFactory.class);
+    RequestHandler requestHandler = createStrictMock(RequestHandler.class);
+    Result result = createStrictMock(Result.class);
+
+    // expectations
+    expect(request.getRequestType()).andReturn(Request.RequestType.PUT);
+    expect(factory.getRequestHandler(Request.RequestType.PUT)).andReturn(requestHandler);
+    expect(requestHandler.handleRequest(request)).andReturn(result);
+
+    replay(request, factory, requestHandler, result);
+
+    RequestHandler delegatingRequestHandler = new TestDelegatingRequestHandler(factory);
+
+    assertSame(result, delegatingRequestHandler.handleRequest(request));
+    verify(request, factory, requestHandler, result);
+  }
+
+  @Test
+  public void testHandleRequest_POST() {
+    Request request = createStrictMock(Request.class);
+    RequestHandlerFactory factory = createStrictMock(RequestHandlerFactory.class);
+    RequestHandler requestHandler = createStrictMock(RequestHandler.class);
+    Result result = createStrictMock(Result.class);
+
+    // expectations
+    expect(request.getRequestType()).andReturn(Request.RequestType.POST);
+    expect(factory.getRequestHandler(Request.RequestType.POST)).andReturn(requestHandler);
+    expect(requestHandler.handleRequest(request)).andReturn(result);
+
+    replay(request, factory, requestHandler, result);
+
+    RequestHandler delegatingRequestHandler = new TestDelegatingRequestHandler(factory);
+
+    assertSame(result, delegatingRequestHandler.handleRequest(request));
+    verify(request, factory, requestHandler, result);
+  }
+
+  @Test
+  public void testHandleRequest_DELETE() {
+    Request request = createStrictMock(Request.class);
+    RequestHandlerFactory factory = createStrictMock(RequestHandlerFactory.class);
+    RequestHandler requestHandler = createStrictMock(RequestHandler.class);
+    Result result = createStrictMock(Result.class);
+
+    // expectations
+    expect(request.getRequestType()).andReturn(Request.RequestType.DELETE);
+    expect(factory.getRequestHandler(Request.RequestType.DELETE)).andReturn(requestHandler);
+    expect(requestHandler.handleRequest(request)).andReturn(result);
+
+    replay(request, factory, requestHandler, result);
+
+    RequestHandler delegatingRequestHandler = new TestDelegatingRequestHandler(factory);
+
+    assertSame(result, delegatingRequestHandler.handleRequest(request));
+    verify(request, factory, requestHandler, result);
+  }
+
+  private class TestDelegatingRequestHandler extends DelegatingRequestHandler {
+    private RequestHandlerFactory m_factory;
+
+    private TestDelegatingRequestHandler(RequestHandlerFactory factory) {
+      m_factory = factory;
+    }
+
+    @Override
+    public RequestHandlerFactory getRequestHandlerFactory() {
+      return m_factory;
+    }
+  }
+}

Added: incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/handlers/ReadRequestHandlerTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/handlers/ReadRequestHandlerTest.java?rev=1389109&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/handlers/ReadRequestHandlerTest.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/handlers/ReadRequestHandlerTest.java Sun Sep 23 18:01:49 2012
@@ -0,0 +1,56 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ambari.api.handlers;
+
+import org.apache.ambari.api.query.Query;
+import org.apache.ambari.api.resource.ResourceDefinition;
+import org.apache.ambari.api.services.Request;
+import org.apache.ambari.api.services.Result;
+import org.junit.Test;
+
+
+import static org.easymock.EasyMock.*;
+import static org.junit.Assert.assertSame;
+
+/**
+ *
+ */
+public class ReadRequestHandlerTest {
+
+  @Test
+  public void testHandlerRequest() {
+    Request request = createStrictMock(Request.class);
+    ResourceDefinition resourceDefinition = createStrictMock(ResourceDefinition.class);
+    Query query = createStrictMock(Query.class);
+    Result result = createStrictMock(Result.class);
+
+    //expectations
+    expect(request.getResource()).andReturn(resourceDefinition);
+    expect(resourceDefinition.getQuery()).andReturn(query);
+    expect(query.execute()).andReturn(result);
+
+    replay(request, resourceDefinition, query, result);
+
+    //test
+    ReadRequestHandler handler = new ReadRequestHandler();
+    assertSame(result, handler.handleRequest(request));
+
+    verify(request, resourceDefinition, query, result);
+
+  }
+}

Added: incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/query/QueryImplTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/query/QueryImplTest.java?rev=1389109&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/query/QueryImplTest.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/query/QueryImplTest.java Sun Sep 23 18:01:49 2012
@@ -0,0 +1,266 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ambari.api.query;
+
+import org.apache.ambari.api.controller.internal.PropertyIdImpl;
+import org.apache.ambari.api.controller.predicate.EqualsPredicate;
+import org.apache.ambari.api.resource.ResourceDefinition;
+import org.apache.ambari.api.services.Result;
+import org.apache.ambari.api.controller.spi.ClusterController;
+import org.apache.ambari.api.controller.spi.Predicate;
+import org.apache.ambari.api.controller.spi.Request;
+import org.apache.ambari.api.controller.spi.Resource;
+import org.apache.ambari.api.controller.spi.Schema;
+import org.apache.ambari.api.controller.utilities.PredicateBuilder;
+import org.junit.Test;
+
+import java.util.*;
+
+import static org.easymock.EasyMock.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+
+
+/**
+ *
+ */
+public class QueryImplTest {
+  @Test
+  public void testExecute__Component_Instance() {
+    ResourceDefinition componentResourceDef = createMock(ResourceDefinition.class);
+    ResourceDefinition hostComponentResourceDef = createStrictMock(ResourceDefinition.class);
+    Request request = createStrictMock(Request.class);
+    Result result = createStrictMock(Result.class);
+    ClusterController controller = createStrictMock(ClusterController.class);
+    Schema componentSchema = createMock(Schema.class);
+    Resource componentResource = createStrictMock(Resource.class);
+    Query hostComponentQuery = createStrictMock(Query.class);
+    Result hostComponentQueryResult = createStrictMock(Result.class);
+    Resource hostComponentResource = createStrictMock(Resource.class);
+
+    List<Resource> listResources = new ArrayList<Resource>();
+    listResources.add(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");
+
+    Set<ResourceDefinition> setChildren = new HashSet<ResourceDefinition>();
+    Set<ResourceDefinition> setForeign = new HashSet<ResourceDefinition>();
+    setForeign.add(hostComponentResourceDef);
+
+    Map<String, List<Resource>> mapHostComponentResources = new HashMap<String, List<Resource>>();
+    mapHostComponentResources.put("/", Collections.singletonList(hostComponentResource));
+
+    // expectations
+    expect(componentResourceDef.getType()).andReturn(Resource.Type.Component).atLeastOnce();
+    expect(componentResourceDef.getResourceIds()).andReturn(mapResourceIds);
+    expect(controller.getSchema(Resource.Type.Component)).andReturn(componentSchema).atLeastOnce();
+    expect(componentSchema.getKeyPropertyId(Resource.Type.Cluster)).andReturn(new PropertyIdImpl("clusterId", "", false));
+    expect(componentSchema.getKeyPropertyId(Resource.Type.Service)).andReturn(new PropertyIdImpl("serviceId", "", false));
+    expect(componentSchema.getKeyPropertyId(Resource.Type.Component)).andReturn(new PropertyIdImpl("componentId", "", false));
+
+    expect(componentResourceDef.getId()).andReturn("componentName").atLeastOnce();
+    expect(componentResourceDef.getChildren()).andReturn(setChildren);
+    expect(componentResourceDef.getRelations()).andReturn(setForeign);
+    expect(hostComponentResourceDef.getQuery()).andReturn(hostComponentQuery);
+
+    PredicateBuilder pb = new PredicateBuilder();
+    Predicate predicate = pb.property("clusterId", "").equals("clusterName").and().
+        property("serviceId", "").equals("serviceName").and().
+        property("componentId", "").equals("componentName").toPredicate();
+
+    expect(controller.getResources(eq(Resource.Type.Component), eq(request), eq(predicate))).
+        andReturn(listResources);
+
+    result.addResources("/", listResources);
+
+    expect(hostComponentQuery.execute()).andReturn(hostComponentQueryResult);
+    expect(hostComponentQueryResult.getResources()).andReturn(mapHostComponentResources);
+    expect(hostComponentResourceDef.getId()).andReturn("hostComponentName");
+    expect(hostComponentResourceDef.getSingularName()).andReturn("host_component");
+    result.addResources("host_component", Collections.singletonList(hostComponentResource));
+
+    replay(componentResourceDef, request, result, controller, componentSchema, componentResource,
+        hostComponentResourceDef, hostComponentQuery, hostComponentQueryResult, hostComponentResource);
+
+    QueryImpl query = new TestQuery(componentResourceDef, result, request, controller);
+    Result testResult = query.execute();
+    // todo: assert return value.  This is currently a mock.
+
+    verify(componentResourceDef, request, result, controller, componentSchema, componentResource,
+        hostComponentResourceDef, hostComponentQuery, hostComponentQueryResult, hostComponentResource);
+  }
+
+  @Test
+  public void testExecute__Component_Collection() {
+    ResourceDefinition componentResourceDef = createMock(ResourceDefinition.class);
+    Request request = createStrictMock(Request.class);
+    Result result = createStrictMock(Result.class);
+    ClusterController controller = createStrictMock(ClusterController.class);
+    Schema componentSchema = createMock(Schema.class);
+    Resource componentResource1 = createStrictMock(Resource.class);
+    Resource componentResource2 = createStrictMock(Resource.class);
+
+    List<Resource> listResources = new ArrayList<Resource>();
+    listResources.add(componentResource1);
+    listResources.add(componentResource2);
+
+    Map<Resource.Type, String> mapResourceIds = new HashMap<Resource.Type, String>();
+    mapResourceIds.put(Resource.Type.Cluster, "clusterName");
+    mapResourceIds.put(Resource.Type.Service, "serviceName");
+
+    // expectations
+    expect(componentResourceDef.getType()).andReturn(Resource.Type.Component).atLeastOnce();
+    expect(componentResourceDef.getResourceIds()).andReturn(mapResourceIds);
+    expect(controller.getSchema(Resource.Type.Component)).andReturn(componentSchema).atLeastOnce();
+    expect(componentSchema.getKeyPropertyId(Resource.Type.Cluster)).andReturn(new PropertyIdImpl("clusterId", "", false));
+    expect(componentSchema.getKeyPropertyId(Resource.Type.Service)).andReturn(new PropertyIdImpl("serviceId", "", false));
+
+    expect(componentResourceDef.getId()).andReturn(null).atLeastOnce();
+
+    PredicateBuilder pb = new PredicateBuilder();
+    Predicate predicate = pb.property("clusterId", "").equals("clusterName").and().
+        property("serviceId", "").equals("serviceName").toPredicate();
+
+    expect(controller.getResources(eq(Resource.Type.Component), eq(request), eq(predicate))).
+        andReturn(listResources);
+
+    result.addResources("/", listResources);
+
+    replay(componentResourceDef, request, result, controller, componentSchema, componentResource1, componentResource2);
+
+    QueryImpl query = new TestQuery(componentResourceDef, result, request, controller);
+    Result testResult = query.execute();
+    // todo: assert return value.  This is currently a mock.
+
+    verify(componentResourceDef, request, result, controller, componentSchema, componentResource1, componentResource2);
+  }
+
+  @Test
+  public void testExecute__Cluster_Instance() {
+    ResourceDefinition clusterResourceDef = createMock(ResourceDefinition.class);
+    ResourceDefinition serviceResourceDef = createMock(ResourceDefinition.class);
+    ResourceDefinition hostResourceDef = createMock(ResourceDefinition.class);
+    Request request = createStrictMock(Request.class);
+    Result result = createMock(Result.class);
+    ClusterController controller = createStrictMock(ClusterController.class);
+    Schema clusterSchema = createMock(Schema.class);
+    Resource clusterResource = createStrictMock(Resource.class);
+    Query serviceQuery = createStrictMock(Query.class);
+    Result serviceQueryResult = createStrictMock(Result.class);
+    Resource serviceResource = createStrictMock(Resource.class);
+    Resource serviceResource2 = createStrictMock(Resource.class);
+    Query hostQuery = createStrictMock(Query.class);
+    Result hostQueryResult = createStrictMock(Result.class);
+    Resource hostResource = createStrictMock(Resource.class);
+
+    List<Resource> listResources = new ArrayList<Resource>();
+    listResources.add(clusterResource);
+
+    Map<Resource.Type, String> mapResourceIds = new HashMap<Resource.Type, String>();
+    mapResourceIds.put(Resource.Type.Cluster, "clusterName");
+
+    Set<ResourceDefinition> setChildren = new HashSet<ResourceDefinition>();
+    setChildren.add(serviceResourceDef);
+    setChildren.add(hostResourceDef);
+    Set<ResourceDefinition> setForeign = new HashSet<ResourceDefinition>();
+
+    Map<String, List<Resource>> mapServiceResources = new HashMap<String, List<Resource>>();
+    List<Resource> listServiceResources = new ArrayList<Resource>();
+    listServiceResources.add(serviceResource);
+    listServiceResources.add(serviceResource2);
+    mapServiceResources.put("/", listServiceResources);
+
+    Map<String, List<Resource>> mapHostResources = new HashMap<String, List<Resource>>();
+    mapHostResources.put("/", Collections.singletonList(hostResource));
+
+    // expectations
+    expect(clusterResourceDef.getType()).andReturn(Resource.Type.Cluster).atLeastOnce();
+    expect(clusterResourceDef.getResourceIds()).andReturn(mapResourceIds);
+    expect(controller.getSchema(Resource.Type.Cluster)).andReturn(clusterSchema).atLeastOnce();
+    expect(clusterSchema.getKeyPropertyId(Resource.Type.Cluster)).andReturn(new PropertyIdImpl("clusterId", "", false));
+    expect(clusterResourceDef.getId()).andReturn("clusterName").atLeastOnce();
+
+    expect(clusterResourceDef.getChildren()).andReturn(setChildren);
+    expect(serviceResourceDef.getQuery()).andReturn(serviceQuery);
+    expect(hostResourceDef.getQuery()).andReturn(hostQuery);
+    expect(clusterResourceDef.getRelations()).andReturn(setForeign);
+
+    Predicate clusterEqualsPredicate = new EqualsPredicate(new PropertyIdImpl("clusterId", "", false), "clusterName");
+
+    expect(controller.getResources(eq(Resource.Type.Cluster), eq(request), eq(clusterEqualsPredicate))).
+        andReturn(listResources);
+
+    result.addResources("/", listResources);
+
+    expect(serviceQuery.execute()).andReturn(serviceQueryResult);
+    expect(serviceQueryResult.getResources()).andReturn(mapServiceResources);
+    expect(serviceResourceDef.getId()).andReturn(null);
+    expect(serviceResourceDef.getPluralName()).andReturn("services");
+    result.addResources("services", listServiceResources);
+
+    expect(hostQuery.execute()).andReturn(hostQueryResult);
+    expect(hostQueryResult.getResources()).andReturn(mapHostResources);
+    expect(hostResourceDef.getId()).andReturn(null);
+    expect(hostResourceDef.getPluralName()).andReturn("hosts");
+    result.addResources("hosts", Collections.singletonList(hostResource));
+
+    replay(clusterResourceDef, request, result, controller, clusterSchema, clusterResource,
+        serviceResourceDef, serviceQuery, serviceQueryResult, serviceResource, serviceResource2,
+        hostResourceDef, hostQuery, hostQueryResult, hostResource);
+
+    QueryImpl query = new TestQuery(clusterResourceDef, result, request, controller);
+    Result testResult = query.execute();
+    // todo: assert return value.  This is currently a mock.
+
+    verify(clusterResourceDef, request, result, controller, clusterSchema, clusterResource,
+        serviceResourceDef, serviceQuery, serviceQueryResult, serviceResource, serviceResource2,
+        hostResourceDef, hostQuery, hostQueryResult, hostResource);
+  }
+
+  private class TestQuery extends QueryImpl {
+
+    private Result m_result;
+    private Request m_request;
+    private ClusterController m_clusterController;
+
+    public TestQuery(ResourceDefinition resourceDefinition, Result result, Request request, ClusterController controller) {
+      super(resourceDefinition);
+      m_result = result;
+      m_request = request;
+      m_clusterController = controller;
+    }
+
+    @Override
+    Result createResult() {
+      return m_result;
+    }
+
+    @Override
+    Request createRequest() {
+      return m_request;
+    }
+
+    @Override
+    ClusterController getClusterController() {
+      return m_clusterController;
+    }
+  }
+}

Added: incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/services/ClusterServiceTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/services/ClusterServiceTest.java?rev=1389109&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/services/ClusterServiceTest.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/services/ClusterServiceTest.java Sun Sep 23 18:01:49 2012
@@ -0,0 +1,160 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ambari.api.services;
+
+import org.apache.ambari.api.handlers.RequestHandler;
+import org.apache.ambari.api.resource.ResourceDefinition;
+import org.apache.ambari.api.services.formatters.ResultFormatter;
+import org.junit.Test;
+
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.UriInfo;
+
+import static org.easymock.EasyMock.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+
+
+/**
+ *
+ */
+public class ClusterServiceTest {
+
+  @Test
+  public void testGetCluster() {
+    ResourceDefinition resourceDef = createStrictMock(ResourceDefinition.class);
+    ResultFormatter resultFormatter = createStrictMock(ResultFormatter.class);
+    Object formattedResult = new Object();
+    Serializer serializer = createStrictMock(Serializer.class);
+    Object serializedResult = new Object();
+    RequestFactory requestFactory = createStrictMock(RequestFactory.class);
+    ResponseFactory responseFactory = createStrictMock(ResponseFactory.class);
+    Request request = createNiceMock(Request.class);
+    RequestHandler requestHandler = createStrictMock(RequestHandler.class);
+    Result result = createStrictMock(Result.class);
+    Response response = createStrictMock(Response.class);
+
+    HttpHeaders httpHeaders = createNiceMock(HttpHeaders.class);
+    UriInfo uriInfo = createNiceMock(UriInfo.class);
+
+    String clusterName = "clusterName";
+
+    // expectations
+    expect(requestFactory.createRequest(eq(httpHeaders), eq(uriInfo), eq(Request.RequestType.GET),
+        eq(resourceDef))).andReturn(request);
+
+    expect(requestHandler.handleRequest(request)).andReturn(result);
+    expect(resourceDef.getResultFormatter()).andReturn(resultFormatter);
+    expect(resultFormatter.format(result, uriInfo)).andReturn(formattedResult);
+    expect(request.getSerializer()).andReturn(serializer);
+    expect(serializer.serialize(formattedResult)).andReturn(serializedResult);
+
+    expect(responseFactory.createResponse(serializedResult)).andReturn(response);
+
+    replay(resourceDef, resultFormatter, serializer, requestFactory, responseFactory, request, requestHandler,
+        result, response, httpHeaders, uriInfo);
+
+    //test
+    ClusterService clusterService = new TestClusterService(resourceDef, clusterName, requestFactory, responseFactory, requestHandler);
+    assertSame(response, clusterService.getCluster(httpHeaders, uriInfo, clusterName));
+
+    verify(resourceDef, resultFormatter, serializer, requestFactory, responseFactory, request, requestHandler,
+        result, response, httpHeaders, uriInfo);
+  }
+
+  @Test
+  public void testGetClusters() {
+    ResourceDefinition resourceDef = createStrictMock(ResourceDefinition.class);
+    ResultFormatter resultFormatter = createStrictMock(ResultFormatter.class);
+    Object formattedResult = new Object();
+    Serializer serializer = createStrictMock(Serializer.class);
+    Object serializedResult = new Object();
+    RequestFactory requestFactory = createStrictMock(RequestFactory.class);
+    ResponseFactory responseFactory = createStrictMock(ResponseFactory.class);
+    Request request = createNiceMock(Request.class);
+    RequestHandler requestHandler = createStrictMock(RequestHandler.class);
+    Result result = createStrictMock(Result.class);
+    Response response = createStrictMock(Response.class);
+
+    HttpHeaders httpHeaders = createNiceMock(HttpHeaders.class);
+    UriInfo uriInfo = createNiceMock(UriInfo.class);
+
+    // expectations
+    expect(requestFactory.createRequest(eq(httpHeaders), eq(uriInfo), eq(Request.RequestType.GET),
+        eq(resourceDef))).andReturn(request);
+
+    expect(requestHandler.handleRequest(request)).andReturn(result);
+    expect(resourceDef.getResultFormatter()).andReturn(resultFormatter);
+    expect(resultFormatter.format(result, uriInfo)).andReturn(formattedResult);
+    expect(request.getSerializer()).andReturn(serializer);
+    expect(serializer.serialize(formattedResult)).andReturn(serializedResult);
+
+    expect(responseFactory.createResponse(serializedResult)).andReturn(response);
+
+    replay(resourceDef, resultFormatter, serializer, requestFactory, responseFactory, request, requestHandler,
+        result, response, httpHeaders, uriInfo);
+
+    //test
+    ClusterService clusterService = new TestClusterService(resourceDef, null, requestFactory, responseFactory, requestHandler);
+    assertSame(response, clusterService.getClusters(httpHeaders, uriInfo));
+
+    verify(resourceDef, resultFormatter, serializer, requestFactory, responseFactory, request, requestHandler,
+        result, response, httpHeaders, uriInfo);
+  }
+
+  private class TestClusterService extends ClusterService {
+    private RequestFactory m_requestFactory;
+    private ResponseFactory m_responseFactory;
+    private RequestHandler m_requestHandler;
+    private ResourceDefinition m_resourceDef;
+    private String m_clusterId;
+
+    private TestClusterService(ResourceDefinition resourceDef, String clusterId, RequestFactory requestFactory,
+                               ResponseFactory responseFactory, RequestHandler handler) {
+      m_resourceDef = resourceDef;
+      m_requestFactory = requestFactory;
+      m_responseFactory = responseFactory;
+      m_requestHandler = handler;
+      m_clusterId = clusterId;
+    }
+
+    @Override
+    ResourceDefinition createResourceDefinition(String clusterName) {
+      assertEquals(m_clusterId, clusterName);
+      return m_resourceDef;
+    }
+
+    @Override
+    RequestFactory getRequestFactory() {
+      return m_requestFactory;
+    }
+
+    @Override
+    ResponseFactory getResponseFactory() {
+      return m_responseFactory;
+    }
+
+    @Override
+    RequestHandler getRequestHandler() {
+      return m_requestHandler;
+    }
+  }
+
+  //todo: test getHostHandler, getServiceHandler
+}

Added: incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/services/ComponentServiceTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/services/ComponentServiceTest.java?rev=1389109&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/services/ComponentServiceTest.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/services/ComponentServiceTest.java Sun Sep 23 18:01:49 2012
@@ -0,0 +1,170 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ambari.api.services;
+
+
+import org.apache.ambari.api.handlers.RequestHandler;
+import org.apache.ambari.api.resource.ResourceDefinition;
+import org.apache.ambari.api.services.formatters.ResultFormatter;
+import org.junit.Test;
+
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.UriInfo;
+
+import static org.easymock.EasyMock.*;
+import static org.easymock.EasyMock.eq;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+
+/**
+ *
+ */
+public class ComponentServiceTest {
+
+  @Test
+  public void testGetComponent() {
+    ResourceDefinition resourceDef = createStrictMock(ResourceDefinition.class);
+    ResultFormatter resultFormatter = createStrictMock(ResultFormatter.class);
+    Object formattedResult = new Object();
+    Serializer serializer = createStrictMock(Serializer.class);
+    Object serializedResult = new Object();
+    RequestFactory requestFactory = createStrictMock(RequestFactory.class);
+    ResponseFactory responseFactory = createStrictMock(ResponseFactory.class);
+    Request request = createNiceMock(Request.class);
+    RequestHandler requestHandler = createStrictMock(RequestHandler.class);
+    Result result = createStrictMock(Result.class);
+    Response response = createStrictMock(Response.class);
+
+    HttpHeaders httpHeaders = createNiceMock(HttpHeaders.class);
+    UriInfo uriInfo = createNiceMock(UriInfo.class);
+
+    String clusterName = "clusterName";
+    String serviceName = "serviceName";
+    String componentName = "componentName";
+
+    // expectations
+    expect(requestFactory.createRequest(eq(httpHeaders), eq(uriInfo), eq(Request.RequestType.GET),
+        eq(resourceDef))).andReturn(request);
+
+    expect(requestHandler.handleRequest(request)).andReturn(result);
+    expect(resourceDef.getResultFormatter()).andReturn(resultFormatter);
+    expect(resultFormatter.format(result, uriInfo)).andReturn(formattedResult);
+    expect(request.getSerializer()).andReturn(serializer);
+    expect(serializer.serialize(formattedResult)).andReturn(serializedResult);
+
+    expect(responseFactory.createResponse(serializedResult)).andReturn(response);
+
+    replay(resourceDef, resultFormatter, serializer, requestFactory, responseFactory, request, requestHandler,
+        result, response, httpHeaders, uriInfo);
+
+    //test
+    ComponentService componentService = new TestComponentService(resourceDef, clusterName, serviceName, componentName,
+        requestFactory, responseFactory, requestHandler);
+    assertSame(response, componentService.getComponent(httpHeaders, uriInfo, componentName));
+
+    verify(resourceDef, resultFormatter, serializer, requestFactory, responseFactory, request, requestHandler,
+        result, response, httpHeaders, uriInfo);
+  }
+
+  @Test
+  public void testGetComponents() {
+    ResourceDefinition resourceDef = createStrictMock(ResourceDefinition.class);
+    ResultFormatter resultFormatter = createStrictMock(ResultFormatter.class);
+    Object formattedResult = new Object();
+    Serializer serializer = createStrictMock(Serializer.class);
+    Object serializedResult = new Object();
+    RequestFactory requestFactory = createStrictMock(RequestFactory.class);
+    ResponseFactory responseFactory = createStrictMock(ResponseFactory.class);
+    Request request = createNiceMock(Request.class);
+    RequestHandler requestHandler = createStrictMock(RequestHandler.class);
+    Result result = createStrictMock(Result.class);
+    Response response = createStrictMock(Response.class);
+
+    HttpHeaders httpHeaders = createNiceMock(HttpHeaders.class);
+    UriInfo uriInfo = createNiceMock(UriInfo.class);
+
+    String clusterName = "clusterName";
+    String serviceName = "serviceName";
+
+    // expectations
+    expect(requestFactory.createRequest(eq(httpHeaders), eq(uriInfo), eq(Request.RequestType.GET),
+        eq(resourceDef))).andReturn(request);
+
+    expect(requestHandler.handleRequest(request)).andReturn(result);
+    expect(resourceDef.getResultFormatter()).andReturn(resultFormatter);
+    expect(resultFormatter.format(result, uriInfo)).andReturn(formattedResult);
+    expect(request.getSerializer()).andReturn(serializer);
+    expect(serializer.serialize(formattedResult)).andReturn(serializedResult);
+
+    expect(responseFactory.createResponse(serializedResult)).andReturn(response);
+    replay(resourceDef, resultFormatter, serializer, requestFactory, responseFactory, request, requestHandler,
+        result, response, httpHeaders, uriInfo);
+
+    //test
+    ComponentService componentService = new TestComponentService(resourceDef, clusterName, serviceName, null, requestFactory, responseFactory, requestHandler);
+    assertSame(response, componentService.getComponents(httpHeaders, uriInfo));
+
+    verify(resourceDef, resultFormatter, serializer, requestFactory, responseFactory, request, requestHandler,
+        result, response, httpHeaders, uriInfo);
+  }
+
+  private class TestComponentService extends ComponentService {
+    private RequestFactory m_requestFactory;
+    private ResponseFactory m_responseFactory;
+    private RequestHandler m_requestHandler;
+    private ResourceDefinition m_resourceDef;
+    private String m_clusterId;
+    private String m_serviceId;
+    private String m_componentId;
+
+    private TestComponentService(ResourceDefinition resourceDef, String clusterId, String serviceId, String componentId, RequestFactory requestFactory, ResponseFactory responseFactory, RequestHandler handler) {
+      super(clusterId, serviceId);
+      m_requestFactory = requestFactory;
+      m_responseFactory = responseFactory;
+      m_requestHandler = handler;
+      m_resourceDef = resourceDef;
+      m_clusterId = clusterId;
+      m_serviceId = serviceId;
+      m_componentId = componentId;
+    }
+
+    @Override
+    ResourceDefinition createResourceDefinition(String componentName, String clusterName, String serviceName) {
+      assertEquals(m_clusterId, clusterName);
+      assertEquals(m_serviceId, serviceName);
+      assertEquals(m_componentId, componentName);
+      return m_resourceDef;
+    }
+
+    @Override
+    RequestFactory getRequestFactory() {
+      return m_requestFactory;
+    }
+
+    @Override
+    ResponseFactory getResponseFactory() {
+      return m_responseFactory;
+    }
+
+    @Override
+    RequestHandler getRequestHandler() {
+      return m_requestHandler;
+    }
+  }
+}

Added: incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/services/HostComponentServiceTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/services/HostComponentServiceTest.java?rev=1389109&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/services/HostComponentServiceTest.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/services/HostComponentServiceTest.java Sun Sep 23 18:01:49 2012
@@ -0,0 +1,173 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ambari.api.services;
+
+import org.apache.ambari.api.handlers.RequestHandler;
+import org.apache.ambari.api.resource.ResourceDefinition;
+import org.apache.ambari.api.services.formatters.ResultFormatter;
+import org.junit.Test;
+
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.UriInfo;
+
+import static org.easymock.EasyMock.*;
+import static org.easymock.EasyMock.eq;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+
+/**
+ *
+ */
+public class HostComponentServiceTest {
+  @Test
+  public void testGetHostComponent() {
+    ResourceDefinition resourceDef = createStrictMock(ResourceDefinition.class);
+    ResultFormatter resultFormatter = createStrictMock(ResultFormatter.class);
+    Object formattedResult = new Object();
+    Serializer serializer = createStrictMock(Serializer.class);
+    Object serializedResult = new Object();
+    RequestFactory requestFactory = createStrictMock(RequestFactory.class);
+    ResponseFactory responseFactory = createStrictMock(ResponseFactory.class);
+    Request request = createNiceMock(Request.class);
+    RequestHandler requestHandler = createStrictMock(RequestHandler.class);
+    Result result = createStrictMock(Result.class);
+    Response response = createStrictMock(Response.class);
+
+    HttpHeaders httpHeaders = createNiceMock(HttpHeaders.class);
+    UriInfo uriInfo = createNiceMock(UriInfo.class);
+
+    String clusterName = "clusterName";
+    String hostName = "hostName";
+    String hostComponentName = "hostComponentName";
+
+    // expectations
+    expect(requestFactory.createRequest(eq(httpHeaders), eq(uriInfo), eq(Request.RequestType.GET),
+        eq(resourceDef))).andReturn(request);
+
+    expect(requestHandler.handleRequest(request)).andReturn(result);
+    expect(resourceDef.getResultFormatter()).andReturn(resultFormatter);
+    expect(resultFormatter.format(result, uriInfo)).andReturn(formattedResult);
+    expect(request.getSerializer()).andReturn(serializer);
+    expect(serializer.serialize(formattedResult)).andReturn(serializedResult);
+
+    expect(responseFactory.createResponse(serializedResult)).andReturn(response);
+
+    replay(resourceDef, resultFormatter, serializer, requestFactory, responseFactory, request, requestHandler,
+        result, response, httpHeaders, uriInfo);
+
+    //test
+    HostComponentService hostComponentService = new TestHostComponentService(resourceDef, clusterName, hostName, hostComponentName,
+        requestFactory, responseFactory, requestHandler);
+    assertSame(response, hostComponentService.getHostComponent(httpHeaders, uriInfo, hostComponentName));
+
+    verify(resourceDef, resultFormatter, serializer, requestFactory, responseFactory, request, requestHandler,
+        result, response, httpHeaders, uriInfo);
+  }
+
+  @Test
+  public void testGetHostComponents() {
+    ResourceDefinition resourceDef = createStrictMock(ResourceDefinition.class);
+    ResultFormatter resultFormatter = createStrictMock(ResultFormatter.class);
+    Object formattedResult = new Object();
+    Serializer serializer = createStrictMock(Serializer.class);
+    Object serializedResult = new Object();
+    RequestFactory requestFactory = createStrictMock(RequestFactory.class);
+    ResponseFactory responseFactory = createStrictMock(ResponseFactory.class);
+    Request request = createNiceMock(Request.class);
+    RequestHandler requestHandler = createStrictMock(RequestHandler.class);
+    Result result = createStrictMock(Result.class);
+    Response response = createStrictMock(Response.class);
+
+    HttpHeaders httpHeaders = createNiceMock(HttpHeaders.class);
+    UriInfo uriInfo = createNiceMock(UriInfo.class);
+
+    String clusterName = "clusterName";
+    String hostName = "hostName";
+
+    // expectations
+    expect(requestFactory.createRequest(eq(httpHeaders), eq(uriInfo), eq(Request.RequestType.GET),
+        eq(resourceDef))).andReturn(request);
+
+    expect(requestHandler.handleRequest(request)).andReturn(result);
+    expect(resourceDef.getResultFormatter()).andReturn(resultFormatter);
+    expect(resultFormatter.format(result, uriInfo)).andReturn(formattedResult);
+    expect(request.getSerializer()).andReturn(serializer);
+    expect(serializer.serialize(formattedResult)).andReturn(serializedResult);
+
+    expect(responseFactory.createResponse(serializedResult)).andReturn(response);
+
+    replay(resourceDef, resultFormatter, serializer, requestFactory, responseFactory, request, requestHandler,
+        result, response, httpHeaders, uriInfo);
+
+    //test
+    HostComponentService componentService = new TestHostComponentService(resourceDef, clusterName, hostName, null, requestFactory,
+        responseFactory, requestHandler);
+    assertSame(response, componentService.getHostComponents(httpHeaders, uriInfo));
+
+    verify(resourceDef, resultFormatter, serializer, requestFactory, responseFactory, request, requestHandler,
+        result, response, httpHeaders, uriInfo);
+  }
+
+  private class TestHostComponentService extends HostComponentService {
+    private RequestFactory m_requestFactory;
+    private ResponseFactory m_responseFactory;
+    private RequestHandler m_requestHandler;
+    private ResourceDefinition m_resourceDef;
+    private String m_clusterId;
+    private String m_hostId;
+    private String m_hostComponentId;
+
+    private TestHostComponentService(ResourceDefinition resourceDef, String clusterId, String hostId, String hostComponentId,
+                                     RequestFactory requestFactory, ResponseFactory responseFactory, RequestHandler handler) {
+      super(clusterId, hostId);
+      m_resourceDef = resourceDef;
+      m_clusterId = clusterId;
+      m_hostId = hostId;
+      m_hostComponentId = hostComponentId;
+      m_requestFactory = requestFactory;
+      m_responseFactory = responseFactory;
+      m_requestHandler = handler;
+    }
+
+
+    @Override
+    ResourceDefinition createResourceDefinition(String hostComponentName, String clusterName, String hostName) {
+      assertEquals(m_clusterId, clusterName);
+      assertEquals(m_hostId, hostName);
+      assertEquals(m_hostComponentId, hostComponentName);
+      return m_resourceDef;
+    }
+
+    @Override
+    RequestFactory getRequestFactory() {
+      return m_requestFactory;
+    }
+
+    @Override
+    ResponseFactory getResponseFactory() {
+      return m_responseFactory;
+    }
+
+    @Override
+    RequestHandler getRequestHandler() {
+      return m_requestHandler;
+    }
+  }
+
+}

Added: incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/services/HostServiceTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/services/HostServiceTest.java?rev=1389109&view=auto
==============================================================================
--- incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/services/HostServiceTest.java (added)
+++ incubator/ambari/branches/AMBARI-666/ambari-api/src/test/java/org/apache/ambari/api/services/HostServiceTest.java Sun Sep 23 18:01:49 2012
@@ -0,0 +1,167 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *     http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.ambari.api.services;
+
+
+import org.apache.ambari.api.handlers.RequestHandler;
+import org.apache.ambari.api.resource.ResourceDefinition;
+import org.apache.ambari.api.services.formatters.ResultFormatter;
+import org.junit.Test;
+
+import javax.ws.rs.core.HttpHeaders;
+import javax.ws.rs.core.Response;
+import javax.ws.rs.core.UriInfo;
+
+import static org.easymock.EasyMock.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
+
+/**
+ *
+ */
+public class HostServiceTest {
+
+  @Test
+  public void testGetHost() {
+    ResourceDefinition resourceDef = createStrictMock(ResourceDefinition.class);
+    ResultFormatter resultFormatter = createStrictMock(ResultFormatter.class);
+    Object formattedResult = new Object();
+    Serializer serializer = createStrictMock(Serializer.class);
+    Object serializedResult = new Object();
+    RequestFactory requestFactory = createStrictMock(RequestFactory.class);
+    ResponseFactory responseFactory = createStrictMock(ResponseFactory.class);
+    Request request = createNiceMock(Request.class);
+    RequestHandler requestHandler = createStrictMock(RequestHandler.class);
+    Result result = createStrictMock(Result.class);
+    Response response = createStrictMock(Response.class);
+
+    HttpHeaders httpHeaders = createNiceMock(HttpHeaders.class);
+    UriInfo uriInfo = createNiceMock(UriInfo.class);
+
+    String clusterName = "clusterName";
+    String hostName = "hostName";
+
+    // expectations
+    expect(requestFactory.createRequest(eq(httpHeaders), eq(uriInfo), eq(Request.RequestType.GET),
+        eq(resourceDef))).andReturn(request);
+
+    expect(requestHandler.handleRequest(request)).andReturn(result);
+    expect(resourceDef.getResultFormatter()).andReturn(resultFormatter);
+    expect(resultFormatter.format(result, uriInfo)).andReturn(formattedResult);
+    expect(request.getSerializer()).andReturn(serializer);
+    expect(serializer.serialize(formattedResult)).andReturn(serializedResult);
+
+    expect(responseFactory.createResponse(serializedResult)).andReturn(response);
+
+    replay(resourceDef, resultFormatter, serializer, requestFactory, responseFactory, request, requestHandler,
+        result, response, httpHeaders, uriInfo);
+
+    //test
+    HostService hostService = new TestHostService(resourceDef, clusterName, hostName, requestFactory, responseFactory, requestHandler);
+    assertSame(response, hostService.getHost(httpHeaders, uriInfo, hostName));
+
+    verify(resourceDef, resultFormatter, serializer, requestFactory, responseFactory, request, requestHandler,
+        result, response, httpHeaders, uriInfo);
+  }
+
+  @Test
+  public void testGetHosts() {
+    ResourceDefinition resourceDef = createStrictMock(ResourceDefinition.class);
+    ResultFormatter resultFormatter = createStrictMock(ResultFormatter.class);
+    Object formattedResult = new Object();
+    Serializer serializer = createStrictMock(Serializer.class);
+    Object serializedResult = new Object();
+    RequestFactory requestFactory = createStrictMock(RequestFactory.class);
+    ResponseFactory responseFactory = createStrictMock(ResponseFactory.class);
+    Request request = createNiceMock(Request.class);
+    RequestHandler requestHandler = createStrictMock(RequestHandler.class);
+    Result result = createStrictMock(Result.class);
+    Response response = createStrictMock(Response.class);
+
+    HttpHeaders httpHeaders = createNiceMock(HttpHeaders.class);
+    UriInfo uriInfo = createNiceMock(UriInfo.class);
+
+    String clusterName = "clusterName";
+
+    // expectations
+    expect(requestFactory.createRequest(eq(httpHeaders), eq(uriInfo), eq(Request.RequestType.GET),
+        eq(resourceDef))).andReturn(request);
+
+    expect(requestHandler.handleRequest(request)).andReturn(result);
+    expect(resourceDef.getResultFormatter()).andReturn(resultFormatter);
+    expect(resultFormatter.format(result, uriInfo)).andReturn(formattedResult);
+    expect(request.getSerializer()).andReturn(serializer);
+    expect(serializer.serialize(formattedResult)).andReturn(serializedResult);
+
+    expect(responseFactory.createResponse(serializedResult)).andReturn(response);
+
+    replay(resourceDef, resultFormatter, serializer, requestFactory, responseFactory, request, requestHandler,
+        result, response, httpHeaders, uriInfo);
+
+    //test
+    HostService hostService = new TestHostService(resourceDef, clusterName, null, requestFactory, responseFactory, requestHandler);
+    assertSame(response, hostService.getHosts(httpHeaders, uriInfo));
+
+    verify(resourceDef, resultFormatter, serializer, requestFactory, responseFactory, request, requestHandler,
+        result, response, httpHeaders, uriInfo);
+  }
+
+  private class TestHostService extends HostService {
+    private RequestFactory m_requestFactory;
+    private ResponseFactory m_responseFactory;
+    private RequestHandler m_requestHandler;
+    private ResourceDefinition m_resourceDef;
+    private String m_clusterId;
+    private String m_hostId;
+
+    private TestHostService(ResourceDefinition resourceDef, String clusterId, String hostId, RequestFactory requestFactory,
+                            ResponseFactory responseFactory, RequestHandler handler) {
+      super(clusterId);
+      m_resourceDef = resourceDef;
+      m_clusterId = clusterId;
+      m_hostId = hostId;
+      m_requestFactory = requestFactory;
+      m_responseFactory = responseFactory;
+      m_requestHandler = handler;
+    }
+
+    @Override
+    ResourceDefinition createResourceDefinition(String hostName, String clusterName) {
+      assertEquals(m_clusterId, clusterName);
+      assertEquals(m_hostId, hostName);
+      return m_resourceDef;
+    }
+
+    @Override
+    RequestFactory getRequestFactory() {
+      return m_requestFactory;
+    }
+
+    @Override
+    ResponseFactory getResponseFactory() {
+      return m_responseFactory;
+    }
+
+    @Override
+    RequestHandler getRequestHandler() {
+      return m_requestHandler;
+    }
+  }
+}
+
+



Mime
View raw message