From 0b278005ad98bcd862bd348d08f664005e9eda60 Mon Sep 17 00:00:00 2001 From: Pamela Dragosh Date: Mon, 28 Oct 2019 08:51:10 -0400 Subject: Optimization improvements and test cases * StdBaseTranslator added helpful support methods and can now add obligations to either rule, policy or policy sets. * StdMatchablePolicyRequest improved to support optional policy-type as part of the request to refine the output results. * Added more tests to ensure that the decision is returning the appropriate results. * Added more Javadoc for code. * Added some sonar fix for either log or throw exception. Issue-ID: POLICY-2066 Change-Id: I90d6d90c2cdbb627e96cfce1d2632b2439a1e477 Signed-off-by: Pamela Dragosh --- .../OptimizationPdpApplicationTest.java | 282 ++++++++++++++++++++- .../resources/decision.optimization.input.json | 12 + .../vCPE.policies.optimization.input.tosca.yaml | 22 +- 3 files changed, 302 insertions(+), 14 deletions(-) create mode 100644 applications/optimization/src/test/resources/decision.optimization.input.json (limited to 'applications/optimization') diff --git a/applications/optimization/src/test/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationPdpApplicationTest.java b/applications/optimization/src/test/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationPdpApplicationTest.java index b84ec078..be553cf1 100644 --- a/applications/optimization/src/test/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationPdpApplicationTest.java +++ b/applications/optimization/src/test/java/org/onap/policy/xacml/pdp/application/optimization/OptimizationPdpApplicationTest.java @@ -32,10 +32,16 @@ import java.io.FileNotFoundException; import java.io.IOException; import java.nio.file.Files; import java.nio.file.Paths; +import java.util.Collection; import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.Map.Entry; import java.util.Properties; import java.util.ServiceLoader; +import jersey.repackaged.com.google.common.collect.Lists; import org.apache.commons.lang3.tuple.Pair; +import org.assertj.core.api.Condition; import org.junit.BeforeClass; import org.junit.ClassRule; import org.junit.FixMethodOrder; @@ -65,7 +71,7 @@ public class OptimizationPdpApplicationTest { private static File propertiesFile; private static XacmlApplicationServiceProvider service; private static StandardCoder gson = new StandardCoder(); - private static DecisionRequest requestAffinity; + private static DecisionRequest baseRequest; private static RestServerParameters clientParams; private static String[] listPolicyTypeFiles = { "onap.policies.Optimization", @@ -95,10 +101,10 @@ public class OptimizationPdpApplicationTest { // // Load Single Decision Request // - requestAffinity = gson.decode( + baseRequest = gson.decode( TextFileUtils .getTextFileAsString( - "../../main/src/test/resources/decisions/decision.optimization.affinity.input.json"), + "src/test/resources/decision.optimization.input.json"), DecisionRequest.class); // // Setup our temporary folder @@ -154,7 +160,7 @@ public class OptimizationPdpApplicationTest { } @Test - public void test1Basics() { + public void test01Basics() { // // Make sure there's an application name // @@ -175,11 +181,11 @@ public class OptimizationPdpApplicationTest { } @Test - public void test2NoPolicies() { + public void test02NoPolicies() { // - // Ask for a decision + // Ask for a decision when there are no policies loaded // - Pair decision = service.makeDecision(requestAffinity, null); + Pair decision = service.makeDecision(baseRequest, null); LOGGER.info("Decision {}", decision.getKey()); assertThat(decision.getKey()).isNotNull(); @@ -187,23 +193,275 @@ public class OptimizationPdpApplicationTest { } @Test - public void test3AddOptimizationPolicies() throws CoderException, FileNotFoundException, IOException, + public void test03OptimizationDefault() throws CoderException, FileNotFoundException, IOException, XacmlApplicationException { // - // Now load the optimization policies + // Now load all the optimization policies // TestUtils.loadPolicies("src/test/resources/vCPE.policies.optimization.input.tosca.yaml", service); // - // Ask for a decision + // Ask for a decision for default // - Pair decision = service.makeDecision(requestAffinity, null); + Pair decision = service.makeDecision(baseRequest, null); + LOGGER.info("Decision {}", decision.getKey()); + + assertThat(decision.getKey()).isNotNull(); + assertThat(decision.getKey().getPolicies().size()).isEqualTo(1); + // + // Double check that the contents are what we expect + // + LOGGER.info(gson.encode(decision.getKey())); + // + // Validate it + // + validateDecision(decision.getKey(), baseRequest); + } + + @SuppressWarnings("unchecked") + @Test + public void test04OptimizationDefaultGeography() throws CoderException { + // + // Add US to the geography list + // + ((List)baseRequest.getResource().get("geography")).add("US"); + // + // Ask for a decision for default US Policy + // + Pair decision = service.makeDecision(baseRequest, null); + LOGGER.info("Decision {}", decision.getKey()); + + assertThat(decision.getKey()).isNotNull(); + assertThat(decision.getKey().getPolicies().size()).isEqualTo(2); + // + // Double check that the contents are what we expect + // + LOGGER.info(gson.encode(decision.getKey())); + // + // Validate it + // + validateDecision(decision.getKey(), baseRequest); + } + + @SuppressWarnings("unchecked") + @Test + public void test05OptimizationDefaultGeographyAndService() throws CoderException { + // + // Add vCPE to the service list + // + ((List)baseRequest.getResource().get("services")).add("vCPE"); + // + // Ask for a decision for default US policy for vCPE service + // + Pair decision = service.makeDecision(baseRequest, null); + LOGGER.info("Decision {}", decision.getKey()); + + assertThat(decision.getKey()).isNotNull(); + assertThat(decision.getKey().getPolicies().size()).isEqualTo(5); + // + // Double check that the contents are what we expect + // + LOGGER.info(gson.encode(decision.getKey())); + // + // Validate it + // + validateDecision(decision.getKey(), baseRequest); + } + + @SuppressWarnings("unchecked") + @Test + public void test06OptimizationDefaultGeographyAndServiceAndResource() throws CoderException { + // + // Add vCPE to the service list + // + ((List)baseRequest.getResource().get("resources")).add("vG"); + // + // Ask for a decision for default US service vCPE resource vG policy + // + Pair decision = service.makeDecision(baseRequest, null); + LOGGER.info("Decision {}", decision.getKey()); + + assertThat(decision.getKey()).isNotNull(); + assertThat(decision.getKey().getPolicies().size()).isEqualTo(9); + // + // Double check that the contents are what we expect + // + LOGGER.info(gson.encode(decision.getKey())); + // + // Validate it + // + validateDecision(decision.getKey(), baseRequest); + } + + @SuppressWarnings("unchecked") + @Test + public void test07OptimizationGeographyAndServiceAndResourceAndScope() throws CoderException { + // + // Add gold as a scope + // + ((List)baseRequest.getResource().get("scope")).add("gold"); + // + // Ask for a decision for specific US vCPE vG gold + // + Pair decision = service.makeDecision(baseRequest, null); + LOGGER.info("Decision {}", decision.getKey()); + + assertThat(decision.getKey()).isNotNull(); + assertThat(decision.getKey().getPolicies().size()).isEqualTo(10); + // + // Double check that the contents are what we expect + // + LOGGER.info(gson.encode(decision.getKey())); + // + // Validate it + // + validateDecision(decision.getKey(), baseRequest); + } + + @SuppressWarnings("unchecked") + @Test + public void test08OptimizationGeographyAndServiceAndResourceAndScopeIsGoldOrPlatinum() throws CoderException { + // + // Add platinum to the scope list: this is now gold OR platinum + // + ((List)baseRequest.getResource().get("scope")).add("platinum"); + // + // Ask for a decision for specific US vCPE vG (gold or platinum) + // + Pair decision = service.makeDecision(baseRequest, null); + LOGGER.info("Decision {}", decision.getKey()); + + assertThat(decision.getKey()).isNotNull(); + assertThat(decision.getKey().getPolicies().size()).isEqualTo(11); + // + // Double check that the contents are what we expect + // + LOGGER.info(gson.encode(decision.getKey())); + // + // Validate it + // + validateDecision(decision.getKey(), baseRequest); + } + + @SuppressWarnings("unchecked") + @Test + public void test09OptimizationGeographyAndServiceAndResourceAndScopeNotGold() throws CoderException { + // + // Add gold as a scope + // + ((List)baseRequest.getResource().get("scope")).remove("gold"); + // + // Ask for a decision for specific US vCPE vG gold + // + Pair decision = service.makeDecision(baseRequest, null); + LOGGER.info("Decision {}", decision.getKey()); + + assertThat(decision.getKey()).isNotNull(); + assertThat(decision.getKey().getPolicies().size()).isEqualTo(11); + // + // Double check that the contents are what we expect + // + LOGGER.info(gson.encode(decision.getKey())); + // + // Validate it + // + validateDecision(decision.getKey(), baseRequest); + } + + @Test + public void test10OptimizationPolicyTypeDefault() throws CoderException { + // + // Remove all the other resources from the request + // + cleanOutResources(); + // + // Add in policy type + // + List policyTypes = Lists.newArrayList("onap.policies.optimization.AffinityPolicy"); + baseRequest.getResource().put("policy-type", policyTypes); + // + // Ask for a decision for default + // + Pair decision = service.makeDecision(baseRequest, null); LOGGER.info("Decision {}", decision.getKey()); assertThat(decision.getKey()).isNotNull(); assertThat(decision.getKey().getPolicies().size()).isEqualTo(4); // - // Dump it out as Json + // Double check that the contents are what we expect + // + LOGGER.info(gson.encode(decision.getKey())); + } + + @Test + public void test20OptimizationPolicyTypeDefault() throws CoderException { + // + // Remove all the other resources from the request + // + cleanOutResources(); + // + // Add in policy type + // + List policyTypes = Lists.newArrayList("onap.policies.optimization.HpaPolicy"); + baseRequest.getResource().put("policy-type", policyTypes); + // + // Ask for a decision for default + // + Pair decision = service.makeDecision(baseRequest, null); + LOGGER.info("Decision {}", decision.getKey()); + + assertThat(decision.getKey()).isNotNull(); + assertThat(decision.getKey().getPolicies().size()).isEqualTo(1); + // + // Double check that the contents are what we expect // LOGGER.info(gson.encode(decision.getKey())); } + + @SuppressWarnings("unchecked") + private void validateDecision(DecisionResponse decision, DecisionRequest request) { + for (Entry entrySet : decision.getPolicies().entrySet()) { + LOGGER.info("Decision Returned Policy {}", entrySet.getKey()); + assertThat(entrySet.getValue()).isInstanceOf(Map.class); + Map policyContents = (Map) entrySet.getValue(); + assertThat(policyContents.containsKey("properties")).isTrue(); + assertThat(policyContents.get("properties")).isInstanceOf(Map.class); + Map policyProperties = (Map) policyContents.get("properties"); + + validateMatchable((Collection) request.getResource().get("scope"), + (Collection) policyProperties.get("scope")); + + validateMatchable((Collection) request.getResource().get("services"), + (Collection) policyProperties.get("services")); + + validateMatchable((Collection) request.getResource().get("resources"), + (Collection) policyProperties.get("resources")); + + validateMatchable((Collection) request.getResource().get("geography"), + (Collection) policyProperties.get("geography")); + } + } + + private void validateMatchable(Collection requestList, Collection policyProperties) { + LOGGER.info("Validating matchable: {} with {}", policyProperties, requestList); + // + // Null or empty implies '*' - that is any value is acceptable + // for this policy. + // + if (policyProperties == null || policyProperties.isEmpty()) { + return; + } + Condition condition = new Condition<>( + requestList::contains, + "Request list is contained"); + assertThat(policyProperties).haveAtLeast(1, condition); + + } + + @SuppressWarnings("unchecked") + private void cleanOutResources() { + ((List)baseRequest.getResource().get("scope")).clear(); + ((List)baseRequest.getResource().get("services")).clear(); + ((List)baseRequest.getResource().get("resources")).clear(); + ((List)baseRequest.getResource().get("geography")).clear(); + } } diff --git a/applications/optimization/src/test/resources/decision.optimization.input.json b/applications/optimization/src/test/resources/decision.optimization.input.json new file mode 100644 index 00000000..3872ca90 --- /dev/null +++ b/applications/optimization/src/test/resources/decision.optimization.input.json @@ -0,0 +1,12 @@ +{ + "ONAPName": "OOF", + "ONAPComponent": "OOF-component", + "ONAPInstance": "OOF-component-instance", + "action": "optimize", + "resource": { + "scope": [], + "services": [], + "resources": [], + "geography": [] + } +} \ No newline at end of file diff --git a/applications/optimization/src/test/resources/vCPE.policies.optimization.input.tosca.yaml b/applications/optimization/src/test/resources/vCPE.policies.optimization.input.tosca.yaml index 80888149..919a2f8b 100644 --- a/applications/optimization/src/test/resources/vCPE.policies.optimization.input.tosca.yaml +++ b/applications/optimization/src/test/resources/vCPE.policies.optimization.input.tosca.yaml @@ -9,6 +9,24 @@ topology_template: metadata: policy-id: OSDF_CASABLANCA.Affinity_Default policy-version: 1 + properties: + scope: [] + services: [] + resources: [] + geography: [] + identity: affinity_vCPE + applicableResources: any + affinityProperties: + qualifier: same + category: complex + - + OSDF_CASABLANCA.Affinity_Default_US: + type: onap.policies.optimization.AffinityPolicy + version: 1.0.0 + type_version: 1.0.0 + metadata: + policy-id: OSDF_CASABLANCA.Affinity_Default_US + policy-version: 1 properties: scope: [] services: [] @@ -20,12 +38,12 @@ topology_template: qualifier: same category: complex - - OSDF_CASABLANCA.Affinity_vCPE_0: + OSDF_CASABLANCA.Affinity_Default_vCPE_0: type: onap.policies.optimization.AffinityPolicy version: 1.0.0 type_version: 1.0.0 metadata: - policy-id: OSDF_CASABLANCA.Affinity_vCPE_0 + policy-id: OSDF_CASABLANCA.Affinity_Default_vCPE_0 policy-version: 1 properties: scope: [] -- cgit 1.2.3-korg