Add prefix matching for policy version 59/85259/9
authorJim Hahn <jrh3@att.com>
Sat, 13 Apr 2019 13:05:13 +0000 (09:05 -0400)
committerJim Hahn <jrh3@att.com>
Sun, 14 Apr 2019 20:43:51 +0000 (16:43 -0400)
Re-introduced regular expressions to match policy version
so that the policy version found in the metadata, which is just
the major number, can be matched with a policy's version, which
is of the form, major.minor.patch.
Simplified equals() test, as we already know "text!=null".
Added filterPrefixPred() and modified policy filter to use it when
matching versions, as it is simpler to use when matching a version
prefix.
Also added tests to PfObjectFilterTest to test each of the XxxPred()
methods.

Change-Id: I0734873f701a539e883fe25b8eecfdde60cc8b6d
Issue-ID: POLICY-1542
Signed-off-by: Jim Hahn <jrh3@att.com>
models-base/src/main/java/org/onap/policy/models/base/PfObjectFilter.java
models-base/src/test/java/org/onap/policy/models/base/PfObjectFilterTest.java
models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilter.java
models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilterTest.java

index 35319b4..f4e4571 100644 (file)
@@ -24,6 +24,9 @@ package org.onap.policy.models.base;
 import java.util.ArrayList;
 import java.util.Collections;
 import java.util.List;
+import java.util.function.Function;
+import java.util.function.Predicate;
+import java.util.regex.Pattern;
 
 /**
  * Interface for filtering a list of concepts.
@@ -41,14 +44,74 @@ public interface PfObjectFilter<T extends Comparable<T>> {
     public List<T> filter(final List<T> originalList);
 
     /**
-     * Check if a value matches a regular expression.
+     * Check if a value exactly equals some text.
      *
      * @param value the incoming value to check
-     * @param pattern the pattern to check against
+     * @param text the desired text to check against
      * @return match or not
      */
-    public default boolean filterString(final String value, final String pattern) {
-        return value == null || pattern == null || value.equals(pattern);
+    public default boolean filterString(final String value, final String text) {
+        return value == null || text == null || value.equals(text);
+    }
+
+    /**
+     * Gets a predicate used to filter an item in a list by exactly matching an extracted value
+     * with some text.
+     *
+     * @param text the desired text to check against, or {@code null} if to accept everything
+     * @param extractor function to extract the value, to be matched, from a list item
+     * @return a predicate to match a value from a list item
+     */
+    public default Predicate<T> filterStringPred(final String text, Function<T, String> extractor) {
+        // if null text, then everything matches
+        if (text == null) {
+            return item -> true;
+        }
+
+        return item -> text.equals(extractor.apply(item));
+    }
+
+    /**
+     * Gets a predicate used to filter an item in a list by comparing the start of an
+     * extracted value with a prefix.
+     *
+     * @param prefix the desired prefix to check against, or {@code null} if to accept
+     *        everything
+     * @param extractor function to extract the value, to be matched, from a list item
+     * @return a predicate to match a prefix with a value from a list item
+     */
+    public default Predicate<T> filterPrefixPred(final String prefix, Function<T, String> extractor) {
+        // if null prefix, then everything matches
+        if (prefix == null) {
+            return item -> true;
+        }
+
+        return item -> {
+            String value = extractor.apply(item);
+            return (value != null && value.startsWith(prefix));
+        };
+    }
+
+    /**
+     * Gets a predicate used to filter an item in a list by matching an extracted value
+     * with a regular expression.
+     *
+     * @param pattern regular expression to match, or {@code null} if to accept everything
+     * @param extractor function to extract the value, to be matched, from a list item
+     * @return a predicate to match a value from a list item using a regular expression
+     */
+    public default Predicate<T> filterRegexpPred(final String pattern, Function<T, String> extractor) {
+        // if null pattern, then everything matches
+        if (pattern == null) {
+            return item -> true;
+        }
+
+        Pattern pat = Pattern.compile(pattern);
+
+        return item -> {
+            String value = extractor.apply(item);
+            return (value != null && pat.matcher(value).matches());
+        };
     }
 
     /**
index 6863421..291a7d4 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019 Nordix Foundation.
+ *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -26,7 +27,9 @@ import static org.junit.Assert.assertTrue;
 
 import java.util.ArrayList;
 import java.util.List;
-
+import java.util.function.BiFunction;
+import java.util.function.Function;
+import java.util.function.Predicate;
 import org.junit.Test;
 
 import org.onap.policy.models.base.testconcepts.DummyPfObject;
@@ -100,5 +103,51 @@ public class PfObjectFilterTest {
         latestVersionList.remove(1);
         List<DummyPfObject> newestVersionList = dof.latestVersionFilter(latestVersionList);
         assertEquals(latestVersionList, newestVersionList);
+
+        MyFilter filter = new MyFilter();
+
+        assertEquals(true, filter.filterString(null, "Hello"));
+
+        DummyPfObject doNullVersion = new DummyPfObject();
+        do5.setName("bbbbb");
+
+        assertEquals(false, filter(filter::filterStringPred, DummyPfObject::getVersion, doNullVersion, "1.0.0"));
+        assertEquals(false, filter(filter::filterStringPred, DummyPfObject::getVersion, do0, "1"));
+        assertEquals(false, filter(filter::filterStringPred, DummyPfObject::getVersion, do0, "2.0.0"));
+        assertEquals(true, filter(filter::filterStringPred, DummyPfObject::getVersion, doNullVersion, null));
+        assertEquals(true, filter(filter::filterStringPred, DummyPfObject::getVersion, do0, null));
+        assertEquals(true, filter(filter::filterStringPred, DummyPfObject::getVersion, do0, "1.0.0"));
+
+        assertEquals(false, filter(filter::filterPrefixPred, DummyPfObject::getVersion, doNullVersion, "1."));
+        assertEquals(false, filter(filter::filterPrefixPred, DummyPfObject::getVersion, do0, "1.1"));
+        assertEquals(false, filter(filter::filterPrefixPred, DummyPfObject::getVersion, do0, "1.1"));
+        assertEquals(false, filter(filter::filterPrefixPred, DummyPfObject::getVersion, do0, "2"));
+        assertEquals(true, filter(filter::filterPrefixPred, DummyPfObject::getVersion, doNullVersion, null));
+        assertEquals(true, filter(filter::filterPrefixPred, DummyPfObject::getVersion, do0, null));
+        assertEquals(true, filter(filter::filterPrefixPred, DummyPfObject::getVersion, do0, "1."));
+        assertEquals(true, filter(filter::filterPrefixPred, DummyPfObject::getVersion, do0, "1.0."));
+        assertEquals(true, filter(filter::filterPrefixPred, DummyPfObject::getVersion, do0, "1.0.0"));
+
+        assertEquals(false, filter(filter::filterRegexpPred, DummyPfObject::getVersion, doNullVersion, "1[.].*"));
+        assertEquals(false, filter(filter::filterRegexpPred, DummyPfObject::getVersion, do0, "2[.].*"));
+        assertEquals(true, filter(filter::filterRegexpPred, DummyPfObject::getVersion, doNullVersion, null));
+        assertEquals(true, filter(filter::filterRegexpPred, DummyPfObject::getVersion, do0, null));
+        assertEquals(true, filter(filter::filterRegexpPred, DummyPfObject::getVersion, do0, "1[.].*"));
+        assertEquals(true, filter(filter::filterRegexpPred, DummyPfObject::getVersion, do0, "1[.]0[.].*"));
+        assertEquals(true, filter(filter::filterRegexpPred, DummyPfObject::getVersion, do0, "1[.]0[.]0"));
+        assertEquals(true, filter(filter::filterRegexpPred, DummyPfObject::getVersion, do0, "1...."));
+    }
+
+    private boolean filter(BiFunction<String, Function<DummyPfObject, String>, Predicate<DummyPfObject>> predMaker,
+                    Function<DummyPfObject, String> extractor, DummyPfObject dpo, String text) {
+        Predicate<DummyPfObject> pred = predMaker.apply(text, extractor);
+        return pred.test(dpo);
+    }
+
+    private static class MyFilter implements PfObjectFilter<DummyPfObject> {
+        @Override
+        public List<DummyPfObject> filter(List<DummyPfObject> originalList) {
+            return null;
+        }
     }
 }
index bb0026e..012f7de 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019 Nordix Foundation.
+ *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -39,16 +40,19 @@ import org.onap.policy.models.base.PfObjectFilter;
 public class ToscaPolicyFilter implements PfObjectFilter<ToscaPolicy> {
     public static final String LATEST_VERSION = "LATEST";
 
-    // Regular expression
+    // Exact expression
     private String name;
 
-    // Regular Expression, set to LATEST_VERRSION to get the latest version
+    // Exact match, set to LATEST_VERSION to get the latest version
     private String version;
 
-    // Regular expression
+    // version prefix
+    private String versionPrefix;
+
+    // Exact expression
     private String type;
 
-    // Regular Expression, set to LATEST_VERRSION to get the latest version
+    // Exact Expression, set to LATEST_VERSION to get the latest version
     private String typeVersion;
 
     @Override
@@ -56,11 +60,11 @@ public class ToscaPolicyFilter implements PfObjectFilter<ToscaPolicy> {
 
         // @formatter:off
         List<ToscaPolicy> returnList = originalList.stream()
-                .filter(p -> filterString(p.getName(),        name))
-                .filter(p -> LATEST_VERSION.equals(version)
-                        || filterString(p.getVersion(), version))
-                .filter(p -> filterString(p.getType(),        type))
-                .filter(p -> filterString(p.getTypeVersion(), typeVersion))
+                .filter(filterStringPred(name, ToscaPolicy::getName))
+                .filter(filterStringPred((LATEST_VERSION.equals(version) ? null : version), ToscaPolicy::getVersion))
+                .filter(filterPrefixPred(versionPrefix, ToscaPolicy::getVersion))
+                .filter(filterStringPred(type, ToscaPolicy::getType))
+                .filter(filterStringPred(typeVersion, ToscaPolicy::getTypeVersion))
                 .collect(Collectors.toList());
         // @formatter:off
 
index 84a0df0..f7c9c7e 100644 (file)
@@ -1,6 +1,7 @@
 /*-
  * ============LICENSE_START=======================================================
  *  Copyright (C) 2019 Nordix Foundation.
+ *  Modifications Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -186,6 +187,22 @@ public class ToscaPolicyFilterTest {
         assertEquals(2, filteredList.size());
     }
 
+    @Test
+    public void testFilterVersionPrefix() {
+        // null pattern
+        ToscaPolicyFilter filter = ToscaPolicyFilter.builder().versionPrefix(null).build();
+        List<ToscaPolicy> filteredList = filter.filter(policyList);
+        assertEquals(17, filteredList.size());
+
+        filter = ToscaPolicyFilter.builder().versionPrefix("1.").build();
+        filteredList = filter.filter(policyList);
+        assertEquals(17, filteredList.size());
+
+        filter = ToscaPolicyFilter.builder().versionPrefix("100.").build();
+        filteredList = filter.filter(policyList);
+        assertEquals(0, filteredList.size());
+    }
+
     @Test
     public void testFilterTypeVersion() {
         ToscaPolicyFilter filter = ToscaPolicyFilter.builder().type("onap.policies.controlloop.Operational").build();