From 1bedb591cc68c10c7db916fda8a3f02d67c2314d Mon Sep 17 00:00:00 2001 From: Pamela Dragosh Date: Sun, 3 Nov 2019 17:42:09 -0500 Subject: More examples of optimization policies and cleanup Fixed a couple of sonar issues - log exception and do not nest more 3 if-else-try. Cleaned up the JUnit test to make debugging a bit easier. Added more examples for testing optimization tests. Moved the target back into the Policy and kept the Condition on the Rule. Works exactly the same, just a bit cleaner and one less rule to deal with. Issue-ID: POLICY-2066 Change-Id: Ife28dc2ce959dcf1fb8ca72061ebc5dca862a7f4 Signed-off-by: Pamela Dragosh --- .../OptimizationPdpApplicationTest.java | 188 ++++++++++----------- 1 file changed, 92 insertions(+), 96 deletions(-) (limited to 'applications/optimization/src/test/java/org/onap') 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 d7e85c47..1bcb5222 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 @@ -29,7 +29,6 @@ import static org.mockito.Mockito.when; import com.att.research.xacml.api.Response; import com.google.common.collect.Lists; - import java.io.File; import java.io.FileNotFoundException; import java.io.IOException; @@ -183,10 +182,11 @@ public class OptimizationPdpApplicationTest { } @Test - public void test02NoPolicies() { + public void test02NoPolicies() throws CoderException { // // Ask for a decision when there are no policies loaded // + LOGGER.info("Request {}", gson.encode(baseRequest)); Pair decision = service.makeDecision(baseRequest, null); LOGGER.info("Decision {}", decision.getKey()); @@ -202,26 +202,50 @@ public class OptimizationPdpApplicationTest { // TestUtils.loadPolicies("src/test/resources/vCPE.policies.optimization.input.tosca.yaml", service); // - // Ask for a decision for default + // Ask for a decision for available default policies // - Pair decision = service.makeDecision(baseRequest, null); - LOGGER.info("Decision {}", decision.getKey()); + DecisionResponse response = makeDecision(); - assertThat(decision.getKey()).isNotNull(); - assertThat(decision.getKey().getPolicies().size()).isEqualTo(1); + assertThat(response).isNotNull(); + assertThat(response.getPolicies().size()).isEqualTo(2); + // + // Validate it + // + validateDecision(response, baseRequest); + } + + @SuppressWarnings("unchecked") + @Test + public void test04OptimizationDefaultHpa() throws CoderException, FileNotFoundException, IOException, + XacmlApplicationException { + // + // Add in policy type // - // Double check that the contents are what we expect + List policyTypes = Lists.newArrayList("onap.policies.optimization.HpaPolicy"); + baseRequest.getResource().put("policy-type", policyTypes); // - LOGGER.info(gson.encode(decision.getKey())); + // Ask for a decision for default HPA policy + // + DecisionResponse response = makeDecision(); + + assertThat(response).isNotNull(); + assertThat(response.getPolicies().size()).isEqualTo(1); + response.getPolicies().forEach((key, value) -> { + assertThat(((Map) value).get("type")).isEqualTo(("onap.policies.optimization.HpaPolicy")); + }); // // Validate it // - validateDecision(decision.getKey(), baseRequest); + validateDecision(response, baseRequest); } @SuppressWarnings("unchecked") @Test - public void test04OptimizationDefaultGeography() throws CoderException { + public void test05OptimizationDefaultGeography() throws CoderException { + // + // Remove all the policy-type resources from the request + // + cleanOutResources(); // // Add US to the geography list // @@ -229,24 +253,18 @@ public class OptimizationPdpApplicationTest { // // 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())); + DecisionResponse response = makeDecision(); + assertThat(response).isNotNull(); + assertThat(response.getPolicies().size()).isEqualTo(3); // Should be 1 // // Validate it // - validateDecision(decision.getKey(), baseRequest); + validateDecision(response, baseRequest); } @SuppressWarnings("unchecked") @Test - public void test05OptimizationDefaultGeographyAndService() throws CoderException { + public void test06OptimizationDefaultGeographyAndService() throws CoderException { // // Add vCPE to the service list // @@ -254,24 +272,19 @@ public class OptimizationPdpApplicationTest { // // Ask for a decision for default US policy for vCPE service // - Pair decision = service.makeDecision(baseRequest, null); - LOGGER.info("Decision {}", decision.getKey()); + DecisionResponse response = makeDecision(); - 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())); + assertThat(response).isNotNull(); + assertThat(response.getPolicies().size()).isEqualTo(6); // should be 1 // // Validate it // - validateDecision(decision.getKey(), baseRequest); + validateDecision(response, baseRequest); } @SuppressWarnings("unchecked") @Test - public void test06OptimizationDefaultGeographyAndServiceAndResource() throws CoderException { + public void test07OptimizationDefaultGeographyAndServiceAndResource() throws CoderException { // // Add vCPE to the service list // @@ -279,24 +292,19 @@ public class OptimizationPdpApplicationTest { // // Ask for a decision for default US service vCPE resource vG policy // - Pair decision = service.makeDecision(baseRequest, null); - LOGGER.info("Decision {}", decision.getKey()); + DecisionResponse response = makeDecision(); - 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())); + assertThat(response).isNotNull(); + assertThat(response.getPolicies().size()).isEqualTo(11); // should be 4 // // Validate it // - validateDecision(decision.getKey(), baseRequest); + validateDecision(response, baseRequest); } @SuppressWarnings("unchecked") @Test - public void test07OptimizationGeographyAndServiceAndResourceAndScope() throws CoderException { + public void test08OptimizationGeographyAndServiceAndResourceAndScope() throws CoderException { // // Add gold as a scope // @@ -304,24 +312,19 @@ public class OptimizationPdpApplicationTest { // // Ask for a decision for specific US vCPE vG gold // - Pair decision = service.makeDecision(baseRequest, null); - LOGGER.info("Decision {}", decision.getKey()); + DecisionResponse response = makeDecision(); - 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())); + assertThat(response).isNotNull(); + assertThat(response.getPolicies().size()).isEqualTo(12); // should be 1 // // Validate it // - validateDecision(decision.getKey(), baseRequest); + validateDecision(response, baseRequest); } @SuppressWarnings("unchecked") @Test - public void test08OptimizationGeographyAndServiceAndResourceAndScopeIsGoldOrPlatinum() throws CoderException { + public void test09OptimizationGeographyAndServiceAndResourceAndScopeIsGoldOrPlatinum() throws CoderException { // // Add platinum to the scope list: this is now gold OR platinum // @@ -329,24 +332,19 @@ public class OptimizationPdpApplicationTest { // // Ask for a decision for specific US vCPE vG (gold or platinum) // - Pair decision = service.makeDecision(baseRequest, null); - LOGGER.info("Decision {}", decision.getKey()); + DecisionResponse response = makeDecision(); - 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())); + assertThat(response).isNotNull(); + assertThat(response.getPolicies().size()).isEqualTo(14); // should be 3 // // Validate it // - validateDecision(decision.getKey(), baseRequest); + validateDecision(response, baseRequest); } @SuppressWarnings("unchecked") @Test - public void test09OptimizationGeographyAndServiceAndResourceAndScopeNotGold() throws CoderException { + public void test10OptimizationGeographyAndServiceAndResourceAndScopeNotGold() throws CoderException { // // Add gold as a scope // @@ -354,27 +352,18 @@ public class OptimizationPdpApplicationTest { // // Ask for a decision for specific US vCPE vG gold // - Pair decision = service.makeDecision(baseRequest, null); - LOGGER.info("Decision {}", decision.getKey()); + DecisionResponse response = makeDecision(); - 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())); + assertThat(response).isNotNull(); + assertThat(response.getPolicies().size()).isEqualTo(13); // should be 2 // // Validate it // - validateDecision(decision.getKey(), baseRequest); + validateDecision(response, baseRequest); } @Test - public void test10OptimizationPolicyTypeDefault() throws CoderException { - // - // Remove all the other resources from the request - // - cleanOutResources(); + public void test11OptimizationPolicyTypeDefault() throws CoderException { // // Add in policy type // @@ -383,40 +372,44 @@ public class OptimizationPdpApplicationTest { // // Ask for a decision for default // - Pair decision = service.makeDecision(baseRequest, null); - LOGGER.info("Decision {}", decision.getKey()); + DecisionResponse response = makeDecision(); - assertThat(decision.getKey()).isNotNull(); - assertThat(decision.getKey().getPolicies().size()).isEqualTo(4); + assertThat(response).isNotNull(); + assertThat(response.getPolicies().size()).isEqualTo(4); // should be 1 // - // Double check that the contents are what we expect + // Validate it // - LOGGER.info(gson.encode(decision.getKey())); + validateDecision(response, baseRequest); } + @SuppressWarnings("unchecked") @Test - public void test20OptimizationPolicyTypeDefault() throws CoderException { - // - // Remove all the other resources from the request - // - cleanOutResources(); + public void test12OptimizationPolicyTypeDefault() throws CoderException { // - // Add in policy type + // Add in another policy type // - List policyTypes = Lists.newArrayList("onap.policies.optimization.HpaPolicy"); - baseRequest.getResource().put("policy-type", policyTypes); + ((List) baseRequest.getResource().get("policy-type")).add("onap.policies.optimization.HpaPolicy"); // // Ask for a decision for default // - Pair decision = service.makeDecision(baseRequest, null); - LOGGER.info("Decision {}", decision.getKey()); + DecisionResponse response = makeDecision(); - assertThat(decision.getKey()).isNotNull(); - assertThat(decision.getKey().getPolicies().size()).isEqualTo(1); + assertThat(response).isNotNull(); + assertThat(response.getPolicies().size()).isEqualTo(6); // should be 2 // - // Double check that the contents are what we expect + // Validate it // - LOGGER.info(gson.encode(decision.getKey())); + validateDecision(response, baseRequest); + } + + private DecisionResponse makeDecision() { + Pair decision = service.makeDecision(baseRequest, null); + LOGGER.info("Request Resources {}", baseRequest.getResource()); + LOGGER.info("Decision {}", decision.getKey()); + for (Entry entrySet : decision.getKey().getPolicies().entrySet()) { + LOGGER.info("Policy {}", entrySet.getKey()); + } + return decision.getKey(); } @SuppressWarnings("unchecked") @@ -465,5 +458,8 @@ public class OptimizationPdpApplicationTest { ((List)baseRequest.getResource().get("services")).clear(); ((List)baseRequest.getResource().get("resources")).clear(); ((List)baseRequest.getResource().get("geography")).clear(); + if (((List)baseRequest.getResource().get("policy-type")) != null) { + baseRequest.getResource().remove("policy-type"); + } } } -- cgit 1.2.3-korg