incubator-ambari-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From tbeerbo...@apache.org
Subject svn commit: r1440213 - in /incubator/ambari/trunk: ./ ambari-server/src/main/java/org/apache/ambari/server/controller/internal/ ambari-server/src/main/java/org/apache/ambari/server/controller/jdbc/ ambari-server/src/main/java/org/apache/ambari/server/c...
Date Tue, 29 Jan 2013 23:11:47 GMT
Author: tbeerbower
Date: Tue Jan 29 23:11:46 2013
New Revision: 1440213

URL: http://svn.apache.org/viewvc?rev=1440213&view=rev
Log:
AMBARI-1270 - Add predicate objects for checking empty resource category

Added:
    incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/predicate/CategoryIsEmptyPredicate.java
    incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/predicate/CategoryPredicate.java
    incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/PropertyPredicateVisitorTest.java
    incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/CategoryIsEmptyPredicateTest.java
    incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/CategoryPredicateTest.java
Modified:
    incubator/ambari/trunk/CHANGES.txt
    incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/PropertyPredicateVisitor.java
    incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/SimplifyingPredicateVisitor.java
    incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/jdbc/SQLPredicateVisitor.java
    incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/predicate/PredicateVisitor.java
    incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/predicate/PredicateVisitorAcceptor.java
    incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/SimplifyingPredicateVisitorTest.java
    incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/PredicateVisitorTest.java

Modified: incubator/ambari/trunk/CHANGES.txt
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/CHANGES.txt?rev=1440213&r1=1440212&r2=1440213&view=diff
==============================================================================
--- incubator/ambari/trunk/CHANGES.txt (original)
+++ incubator/ambari/trunk/CHANGES.txt Tue Jan 29 23:11:46 2013
@@ -169,6 +169,8 @@ Trunk (unreleased changes):
 
  AMBARI-1269. Refactor ResourceProvider SPI. (tbeerbower)
  
+ AMBARI-1270. Add predicate objects for checking empty resource category. (tbeerbower)
+
  BUG FIXES
 
  AMBARI-1278. Cannot proceed from Step 3 to Step 4 in App.testMode (Next 

Modified: incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/PropertyPredicateVisitor.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/PropertyPredicateVisitor.java?rev=1440213&r1=1440212&r2=1440213&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/PropertyPredicateVisitor.java
(original)
+++ incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/PropertyPredicateVisitor.java
Tue Jan 29 23:11:46 2013
@@ -21,6 +21,7 @@ package org.apache.ambari.server.control
 import org.apache.ambari.server.controller.predicate.AlwaysPredicate;
 import org.apache.ambari.server.controller.predicate.ArrayPredicate;
 import org.apache.ambari.server.controller.predicate.BasePredicate;
+import org.apache.ambari.server.controller.predicate.CategoryPredicate;
 import org.apache.ambari.server.controller.predicate.ComparisonPredicate;
 import org.apache.ambari.server.controller.predicate.PropertyPredicate;
 import org.apache.ambari.server.controller.predicate.PredicateVisitor;
@@ -58,6 +59,11 @@ public class PropertyPredicateVisitor im
     //Do nothing
   }
 
+  @Override
+  public void acceptCategoryPredicate(CategoryPredicate predicate) {
+    // Do nothing
+  }
+
 
   // ----- accessors ---------------------------------------------------------
 

Modified: incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/SimplifyingPredicateVisitor.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/SimplifyingPredicateVisitor.java?rev=1440213&r1=1440212&r2=1440213&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/SimplifyingPredicateVisitor.java
(original)
+++ incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/internal/SimplifyingPredicateVisitor.java
Tue Jan 29 23:11:46 2013
@@ -22,6 +22,7 @@ import org.apache.ambari.server.controll
 import org.apache.ambari.server.controller.predicate.AndPredicate;
 import org.apache.ambari.server.controller.predicate.ArrayPredicate;
 import org.apache.ambari.server.controller.predicate.BasePredicate;
+import org.apache.ambari.server.controller.predicate.CategoryPredicate;
 import org.apache.ambari.server.controller.predicate.ComparisonPredicate;
 import org.apache.ambari.server.controller.predicate.EqualsPredicate;
 import org.apache.ambari.server.controller.predicate.OrPredicate;
@@ -161,4 +162,9 @@ public class SimplifyingPredicateVisitor
     }
     return andPredicateList;
   }
+
+  @Override
+  public void acceptCategoryPredicate(CategoryPredicate predicate) {
+    lastVisited = predicate;
+  }
 }

Modified: incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/jdbc/SQLPredicateVisitor.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/jdbc/SQLPredicateVisitor.java?rev=1440213&r1=1440212&r2=1440213&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/jdbc/SQLPredicateVisitor.java
(original)
+++ incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/jdbc/SQLPredicateVisitor.java
Tue Jan 29 23:11:46 2013
@@ -21,6 +21,7 @@ package org.apache.ambari.server.control
 import org.apache.ambari.server.controller.predicate.AlwaysPredicate;
 import org.apache.ambari.server.controller.predicate.ArrayPredicate;
 import org.apache.ambari.server.controller.predicate.BasePredicate;
+import org.apache.ambari.server.controller.predicate.CategoryPredicate;
 import org.apache.ambari.server.controller.predicate.ComparisonPredicate;
 import org.apache.ambari.server.controller.predicate.PredicateVisitor;
 import org.apache.ambari.server.controller.predicate.UnaryPredicate;
@@ -83,6 +84,11 @@ public class SQLPredicateVisitor impleme
     stringBuilder.append("TRUE");
   }
 
+  @Override
+  public void acceptCategoryPredicate(CategoryPredicate predicate) {
+    // Do nothing
+  }
+
 
   // ----- SQLPredicateVisitor -----------------------------------------------
 

Added: incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/predicate/CategoryIsEmptyPredicate.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/predicate/CategoryIsEmptyPredicate.java?rev=1440213&view=auto
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/predicate/CategoryIsEmptyPredicate.java
(added)
+++ incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/predicate/CategoryIsEmptyPredicate.java
Tue Jan 29 23:11:46 2013
@@ -0,0 +1,49 @@
+/**
+ * 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.server.controller.predicate;
+
+import org.apache.ambari.server.controller.spi.Resource;
+
+import java.util.Map;
+
+/**
+ * Predicate that checks if the associated property category is empty.  If the associated
+ * property id references a Map property then treat the Map as a category.
+ */
+public class CategoryIsEmptyPredicate extends CategoryPredicate {
+
+  public CategoryIsEmptyPredicate(String propertyId) {
+    super(propertyId);
+  }
+
+  @Override
+  public boolean evaluate(Resource resource) {
+    String propertyId = getPropertyId();
+
+    // If the property exists as a Map then check isEmpty
+    Object value = resource.getPropertyValue(propertyId);
+    if (value instanceof Map) {
+      Map<?,?> mapValue = (Map) value;
+      return mapValue.isEmpty();
+    }
+    // Get the category
+    Map<String, Object> properties = resource.getPropertiesMap().get(propertyId);
+    return properties == null ? true : properties.isEmpty();
+  }
+}

Added: incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/predicate/CategoryPredicate.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/predicate/CategoryPredicate.java?rev=1440213&view=auto
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/predicate/CategoryPredicate.java
(added)
+++ incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/predicate/CategoryPredicate.java
Tue Jan 29 23:11:46 2013
@@ -0,0 +1,33 @@
+/**
+ * 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.server.controller.predicate;
+
+/**
+ * Base class for predicates based on property categories.
+ */
+public abstract class CategoryPredicate extends PropertyPredicate implements BasePredicate
{
+  public CategoryPredicate(String propertyId) {
+    super(propertyId);
+  }
+
+  @Override
+  public void accept(PredicateVisitor visitor) {
+    visitor.acceptCategoryPredicate(this);
+  }
+}

Modified: incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/predicate/PredicateVisitor.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/predicate/PredicateVisitor.java?rev=1440213&r1=1440212&r2=1440213&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/predicate/PredicateVisitor.java
(original)
+++ incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/predicate/PredicateVisitor.java
Tue Jan 29 23:11:46 2013
@@ -29,4 +29,6 @@ public interface PredicateVisitor {
   public void acceptUnaryPredicate(UnaryPredicate predicate);
 
   public void acceptAlwaysPredicate(AlwaysPredicate predicate);
+
+  public void acceptCategoryPredicate(CategoryPredicate predicate);
 }

Modified: incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/predicate/PredicateVisitorAcceptor.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/predicate/PredicateVisitorAcceptor.java?rev=1440213&r1=1440212&r2=1440213&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/predicate/PredicateVisitorAcceptor.java
(original)
+++ incubator/ambari/trunk/ambari-server/src/main/java/org/apache/ambari/server/controller/predicate/PredicateVisitorAcceptor.java
Tue Jan 29 23:11:46 2013
@@ -22,5 +22,10 @@ package org.apache.ambari.server.control
  */
 public interface PredicateVisitorAcceptor {
 
+  /**
+   * Accept the given visitor.
+   *
+   * @param visitor  the visitor
+   */
   public void accept(PredicateVisitor visitor);
 }

Added: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/PropertyPredicateVisitorTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/PropertyPredicateVisitorTest.java?rev=1440213&view=auto
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/PropertyPredicateVisitorTest.java
(added)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/PropertyPredicateVisitorTest.java
Tue Jan 29 23:11:46 2013
@@ -0,0 +1,72 @@
+/**
+ * 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.server.controller.internal;
+
+import junit.framework.Assert;
+import org.apache.ambari.server.controller.predicate.AndPredicate;
+import org.apache.ambari.server.controller.predicate.BasePredicate;
+import org.apache.ambari.server.controller.predicate.CategoryIsEmptyPredicate;
+import org.apache.ambari.server.controller.predicate.OrPredicate;
+import org.apache.ambari.server.controller.utilities.PredicateBuilder;
+import org.apache.ambari.server.controller.utilities.PropertyHelper;
+import org.junit.Test;
+
+import java.util.Map;
+
+/**
+ * Tests for the property predicate visitor.
+ */
+public class PropertyPredicateVisitorTest {
+
+  private static final String PROPERTY_A = PropertyHelper.getPropertyId("category", "A");
+  private static final String PROPERTY_B = PropertyHelper.getPropertyId("category", "B");
+
+  private static final BasePredicate PREDICATE_1 = new PredicateBuilder().property(PROPERTY_A).equals("Monkey").toPredicate();
+  private static final BasePredicate PREDICATE_2 = new PredicateBuilder().property(PROPERTY_B).equals("Runner").toPredicate();
+  private static final BasePredicate PREDICATE_3 = new AndPredicate(PREDICATE_1, PREDICATE_2);
+  private static final BasePredicate PREDICATE_4 = new OrPredicate(PREDICATE_1, PREDICATE_2);
+  private static final BasePredicate PREDICATE_5 = new CategoryIsEmptyPredicate("cat1");
+
+  @Test
+  public void testVisit() {
+    PropertyPredicateVisitor visitor = new PropertyPredicateVisitor();
+    PREDICATE_1.accept(visitor);
+    Map<String, Object> properties = visitor.getProperties();
+    Assert.assertEquals(1, properties.size());
+    Assert.assertEquals("Monkey", properties.get(PROPERTY_A));
+
+    visitor = new PropertyPredicateVisitor();
+    PREDICATE_3.accept(visitor);
+    properties = visitor.getProperties();
+    Assert.assertEquals(2, properties.size());
+    Assert.assertEquals("Monkey", properties.get(PROPERTY_A));
+    Assert.assertEquals("Runner", properties.get(PROPERTY_B));
+
+    visitor = new PropertyPredicateVisitor();
+    PREDICATE_4.accept(visitor);
+    properties = visitor.getProperties();
+    Assert.assertEquals(2, properties.size());
+    Assert.assertEquals("Monkey", properties.get(PROPERTY_A));
+    Assert.assertEquals("Runner", properties.get(PROPERTY_B));
+
+    visitor = new PropertyPredicateVisitor();
+    PREDICATE_5.accept(visitor);
+    properties = visitor.getProperties();
+    Assert.assertTrue(properties.isEmpty());
+  }
+}

Modified: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/SimplifyingPredicateVisitorTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/SimplifyingPredicateVisitorTest.java?rev=1440213&r1=1440212&r2=1440213&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/SimplifyingPredicateVisitorTest.java
(original)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/internal/SimplifyingPredicateVisitorTest.java
Tue Jan 29 23:11:46 2013
@@ -20,6 +20,7 @@ package org.apache.ambari.server.control
 import junit.framework.Assert;
 import org.apache.ambari.server.controller.predicate.AndPredicate;
 import org.apache.ambari.server.controller.predicate.BasePredicate;
+import org.apache.ambari.server.controller.predicate.CategoryIsEmptyPredicate;
 import org.apache.ambari.server.controller.predicate.OrPredicate;
 import org.apache.ambari.server.controller.utilities.PredicateBuilder;
 import org.apache.ambari.server.controller.utilities.PropertyHelper;
@@ -54,6 +55,7 @@ public class SimplifyingPredicateVisitor
   private static final BasePredicate PREDICATE_13 = new AndPredicate(PREDICATE_1, PREDICATE_12);
   private static final BasePredicate PREDICATE_14 = new PredicateBuilder().property(PROPERTY_D).greaterThan(12).toPredicate();
   private static final BasePredicate PREDICATE_15 = new AndPredicate(PREDICATE_1, PREDICATE_14);
+  private static final BasePredicate PREDICATE_16 = new CategoryIsEmptyPredicate("cat1");
 
   @Test
   public void testVisit() {
@@ -133,5 +135,12 @@ public class SimplifyingPredicateVisitor
 
     Assert.assertEquals(1, simplifiedPredicates.size());
     Assert.assertEquals(PREDICATE_1, simplifiedPredicates.get(0));
+
+    PREDICATE_16.accept(visitor);
+
+    simplifiedPredicates = visitor.getSimplifiedPredicates();
+
+    Assert.assertEquals(1, simplifiedPredicates.size());
+    Assert.assertEquals(PREDICATE_16, simplifiedPredicates.get(0));
   }
 }

Added: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/CategoryIsEmptyPredicateTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/CategoryIsEmptyPredicateTest.java?rev=1440213&view=auto
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/CategoryIsEmptyPredicateTest.java
(added)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/CategoryIsEmptyPredicateTest.java
Tue Jan 29 23:11:46 2013
@@ -0,0 +1,70 @@
+/**
+ * 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.server.controller.predicate;
+
+import junit.framework.Assert;
+import org.apache.ambari.server.controller.internal.ResourceImpl;
+import org.apache.ambari.server.controller.spi.Predicate;
+import org.apache.ambari.server.controller.spi.Resource;
+import org.apache.ambari.server.controller.utilities.PropertyHelper;
+import org.junit.Test;
+
+import java.util.HashMap;
+import java.util.Map;
+
+/**
+ * Tests for CategoryIsEmptyPredicate.
+ */
+public class CategoryIsEmptyPredicateTest {
+
+  @Test
+  public void testApply() {
+    Resource resource = new ResourceImpl(Resource.Type.HostComponent);
+    String categoryId = PropertyHelper.getPropertyId("category1", null);
+    Predicate predicate = new CategoryIsEmptyPredicate(categoryId);
+
+    Assert.assertTrue(predicate.evaluate(resource));
+
+    resource.addCategory(categoryId);
+    Assert.assertTrue(predicate.evaluate(resource));
+
+    String propertyId = PropertyHelper.getPropertyId("category1", "bar");
+    resource.setProperty(propertyId, "value1");
+    Assert.assertFalse(predicate.evaluate(resource));
+  }
+
+  @Test
+  public void testApplyWithMap() {
+    Resource resource = new ResourceImpl(Resource.Type.HostComponent);
+    String propertyId = PropertyHelper.getPropertyId("category1", "mapProperty");
+    Predicate predicate = new CategoryIsEmptyPredicate(propertyId);
+
+    Assert.assertTrue(predicate.evaluate(resource));
+
+    Map<String, String> mapProperty = new HashMap<String, String>();
+
+    resource.setProperty(propertyId, mapProperty);
+    Assert.assertTrue(predicate.evaluate(resource));
+
+    mapProperty.put("foo", "bar");
+
+    Assert.assertFalse(predicate.evaluate(resource));
+  }
+
+}

Added: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/CategoryPredicateTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/CategoryPredicateTest.java?rev=1440213&view=auto
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/CategoryPredicateTest.java
(added)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/CategoryPredicateTest.java
Tue Jan 29 23:11:46 2013
@@ -0,0 +1,60 @@
+package org.apache.ambari.server.controller.predicate;
+
+import junit.framework.Assert;
+import org.apache.ambari.server.controller.spi.Resource;
+import org.apache.ambari.server.controller.utilities.PropertyHelper;
+import org.junit.Test;
+
+/**
+ * Tests for bas category predicate.
+ */
+public class CategoryPredicateTest {
+  @Test
+  public void testAccept() {
+    String propertyId = PropertyHelper.getPropertyId("category1", "foo");
+    TestCategoryPredicate predicate = new TestCategoryPredicate(propertyId);
+
+    TestPredicateVisitor visitor = new TestPredicateVisitor();
+    predicate.accept(visitor);
+
+    Assert.assertSame(predicate, visitor.visitedCategoryPredicate);
+  }
+
+  public static class TestCategoryPredicate extends CategoryPredicate {
+
+    public TestCategoryPredicate(String propertyId) {
+      super(propertyId);
+    }
+
+    @Override
+    public boolean evaluate(Resource resource) {
+      return false;
+    }
+  }
+
+  public static class TestPredicateVisitor implements PredicateVisitor {
+
+    CategoryPredicate visitedCategoryPredicate = null;
+
+    @Override
+    public void acceptComparisonPredicate(ComparisonPredicate predicate) {
+    }
+
+    @Override
+    public void acceptArrayPredicate(ArrayPredicate predicate) {
+    }
+
+    @Override
+    public void acceptUnaryPredicate(UnaryPredicate predicate) {
+    }
+
+    @Override
+    public void acceptAlwaysPredicate(AlwaysPredicate predicate) {
+    }
+
+    @Override
+    public void acceptCategoryPredicate(CategoryPredicate predicate) {
+      visitedCategoryPredicate = predicate;
+    }
+  }
+}

Modified: incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/PredicateVisitorTest.java
URL: http://svn.apache.org/viewvc/incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/PredicateVisitorTest.java?rev=1440213&r1=1440212&r2=1440213&view=diff
==============================================================================
--- incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/PredicateVisitorTest.java
(original)
+++ incubator/ambari/trunk/ambari-server/src/test/java/org/apache/ambari/server/controller/predicate/PredicateVisitorTest.java
Tue Jan 29 23:11:46 2013
@@ -22,7 +22,7 @@ import org.apache.ambari.server.controll
 import org.junit.Test;
 
 /**
- *
+ *  Tests for predicate visitors.
  */
 public class PredicateVisitorTest {
 
@@ -56,6 +56,17 @@ public class PredicateVisitorTest {
     Assert.assertNull(visitor.visitedComparisonPredicate);
     Assert.assertNull(visitor.visitedArrayPredicate);
     Assert.assertSame(notPredicate, visitor.visitedUnaryPredicate);
+
+
+    CategoryPredicate categoryPredicate = new CategoryIsEmptyPredicate("cat1");
+
+    visitor = new TestPredicateVisitor();
+    categoryPredicate.accept(visitor);
+
+    Assert.assertNull(visitor.visitedComparisonPredicate);
+    Assert.assertNull(visitor.visitedArrayPredicate);
+    Assert.assertNull(visitor.visitedUnaryPredicate);
+    Assert.assertSame(categoryPredicate, visitor.visitedCategoryPredicate);
   }
 
   public static class TestPredicateVisitor implements PredicateVisitor {
@@ -64,6 +75,7 @@ public class PredicateVisitorTest {
     ArrayPredicate visitedArrayPredicate = null;
     UnaryPredicate visitedUnaryPredicate = null;
     AlwaysPredicate visitedAlwaysPredicate = null;
+    CategoryPredicate visitedCategoryPredicate = null;
 
     @Override
     public void acceptComparisonPredicate(ComparisonPredicate predicate) {
@@ -84,6 +96,10 @@ public class PredicateVisitorTest {
     public void acceptAlwaysPredicate(AlwaysPredicate predicate) {
       visitedAlwaysPredicate = predicate;
     }
-  }
 
+    @Override
+    public void acceptCategoryPredicate(CategoryPredicate predicate) {
+      visitedCategoryPredicate = predicate;
+    }
+  }
 }



Mime
View raw message