Optimization improvements and test cases
[policy/xacml-pdp.git] / applications / optimization / src / test / java / org / onap / policy / xacml / pdp / application / optimization / OptimizationPdpApplicationTest.java
index b84ec07..be553cf 100644 (file)
@@ -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<DecisionResponse, Response> decision = service.makeDecision(requestAffinity, null);
+        Pair<DecisionResponse, Response> 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<DecisionResponse, Response> decision = service.makeDecision(requestAffinity, null);
+        Pair<DecisionResponse, Response> 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<String>)baseRequest.getResource().get("geography")).add("US");
+        //
+        // Ask for a decision for default US Policy
+        //
+        Pair<DecisionResponse, Response> 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<String>)baseRequest.getResource().get("services")).add("vCPE");
+        //
+        // Ask for a decision for default US policy for vCPE service
+        //
+        Pair<DecisionResponse, Response> 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<String>)baseRequest.getResource().get("resources")).add("vG");
+        //
+        // Ask for a decision for default US service vCPE resource vG policy
+        //
+        Pair<DecisionResponse, Response> 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<String>)baseRequest.getResource().get("scope")).add("gold");
+        //
+        // Ask for a decision for specific US vCPE vG gold
+        //
+        Pair<DecisionResponse, Response> 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<String>)baseRequest.getResource().get("scope")).add("platinum");
+        //
+        // Ask for a decision for specific US vCPE vG (gold or platinum)
+        //
+        Pair<DecisionResponse, Response> 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<String>)baseRequest.getResource().get("scope")).remove("gold");
+        //
+        // Ask for a decision for specific US vCPE vG gold
+        //
+        Pair<DecisionResponse, Response> 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<String> policyTypes = Lists.newArrayList("onap.policies.optimization.AffinityPolicy");
+        baseRequest.getResource().put("policy-type", policyTypes);
+        //
+        // Ask for a decision for default
+        //
+        Pair<DecisionResponse, Response> 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<String> policyTypes = Lists.newArrayList("onap.policies.optimization.HpaPolicy");
+        baseRequest.getResource().put("policy-type", policyTypes);
+        //
+        // Ask for a decision for default
+        //
+        Pair<DecisionResponse, Response> 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<String, Object> entrySet : decision.getPolicies().entrySet()) {
+            LOGGER.info("Decision Returned Policy {}", entrySet.getKey());
+            assertThat(entrySet.getValue()).isInstanceOf(Map.class);
+            Map<String, Object> policyContents = (Map<String, Object>) entrySet.getValue();
+            assertThat(policyContents.containsKey("properties")).isTrue();
+            assertThat(policyContents.get("properties")).isInstanceOf(Map.class);
+            Map<String, Object> policyProperties = (Map<String, Object>) policyContents.get("properties");
+
+            validateMatchable((Collection<String>) request.getResource().get("scope"),
+                    (Collection<String>) policyProperties.get("scope"));
+
+            validateMatchable((Collection<String>) request.getResource().get("services"),
+                    (Collection<String>) policyProperties.get("services"));
+
+            validateMatchable((Collection<String>) request.getResource().get("resources"),
+                    (Collection<String>) policyProperties.get("resources"));
+
+            validateMatchable((Collection<String>) request.getResource().get("geography"),
+                    (Collection<String>) policyProperties.get("geography"));
+        }
+    }
+
+    private void validateMatchable(Collection<String> requestList, Collection<String> 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<String> condition = new Condition<>(
+                requestList::contains,
+                "Request list is contained");
+        assertThat(policyProperties).haveAtLeast(1, condition);
+
+    }
+
+    @SuppressWarnings("unchecked")
+    private void cleanOutResources() {
+        ((List<String>)baseRequest.getResource().get("scope")).clear();
+        ((List<String>)baseRequest.getResource().get("services")).clear();
+        ((List<String>)baseRequest.getResource().get("resources")).clear();
+        ((List<String>)baseRequest.getResource().get("geography")).clear();
+    }
 }