FilterPredicate extends ComparisonPredicate { @Override public boolean evaluate(Resource resource) { Object propertyValue = resource.getPropertyValue(getPropertyId()); matcher.reset(propertyValue != null ? propertyValue.toString() : emptyString); return patternExpr == null ? propertyValue == null : propertyValue != null && matcher.matches(); } @SuppressWarnings("unchecked")  FilterPredicate(String propertyId, String patternExpr); @Override boolean evaluate(Resource resource); @Override String getOperator(); @Override ComparisonPredicate copy(String propertyId); @Override String toString();  }
@Test public void testApply() throws Exception { Resource resource = new ResourceImpl(Resource.Type.HostComponent); String propertyId = PropertyHelper.getPropertyId("category1", "ip"); Predicate predicate = new FilterPredicate(propertyId, IP_ADDRESS_PATTERN); resource.setProperty(propertyId, "monkey"); Assert.assertFalse(predicate.evaluate(resource)); resource.setProperty(propertyId, "10.0.0.1"); Assert.assertTrue(predicate.evaluate(resource)); resource.setProperty(propertyId, "127.0.0.1"); Assert.assertTrue(predicate.evaluate(resource)); resource.setProperty(propertyId, "0.0.0.0"); Assert.assertTrue(predicate.evaluate(resource)); propertyId = PropertyHelper.getPropertyId("category1", "fun"); predicate = new FilterPredicate(propertyId, IP_ADDRESS_PATTERN); Assert.assertFalse(predicate.evaluate(resource)); }
@Test public void testApplyNullValue() { Resource resource = new ResourceImpl(Resource.Type.HostComponent); String propertyId = PropertyHelper.getPropertyId("category1", "foo"); Predicate predicate = new FilterPredicate(propertyId, null); resource.setProperty(propertyId, "monkey"); Assert.assertFalse(predicate.evaluate(resource)); resource.setProperty(propertyId, null); Assert.assertTrue(predicate.evaluate(resource)); }
@Test public void testApplyEmptyValue() { Resource resource = new ResourceImpl(Resource.Type.HostComponent); String propertyId = PropertyHelper.getPropertyId("category1", "foo"); Predicate predicate = new FilterPredicate(propertyId, ""); resource.setProperty(propertyId, "monkey"); Assert.assertFalse(predicate.evaluate(resource)); predicate = new FilterPredicate(propertyId, "monkey"); Assert.assertTrue(predicate.evaluate(resource)); }