From c8d0a3d8b7df3b09ce3405f1192754fdef7026d3 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Sat, 13 Apr 2019 09:05:13 -0400 Subject: Add prefix matching for policy version 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 --- .../authorative/concepts/ToscaPolicyFilter.java | 22 +++++++++++++--------- .../concepts/ToscaPolicyFilterTest.java | 17 +++++++++++++++++ 2 files changed, 30 insertions(+), 9 deletions(-) (limited to 'models-tosca/src') diff --git a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilter.java b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilter.java index bb0026e9a..012f7de34 100644 --- a/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilter.java +++ b/models-tosca/src/main/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilter.java @@ -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 { 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 { // @formatter:off List 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 diff --git a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilterTest.java b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilterTest.java index 84a0df00b..f7c9c7ef0 100644 --- a/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilterTest.java +++ b/models-tosca/src/test/java/org/onap/policy/models/tosca/authorative/concepts/ToscaPolicyFilterTest.java @@ -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 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(); -- cgit 1.2.3-korg