Integrate using Policy Type to find Matchable
[policy/xacml-pdp.git] / applications / common / src / main / java / org / onap / policy / pdp / xacml / application / common / TestUtils.java
index fa32516..f720fec 100644 (file)
 
 package org.onap.policy.pdp.xacml.application.common;
 
+import java.util.ArrayList;
+import java.util.List;
 import java.util.Map;
-import java.util.Map.Entry;
 
 import org.onap.policy.common.utils.coder.CoderException;
 import org.onap.policy.common.utils.coder.StandardCoder;
 import org.onap.policy.common.utils.resources.ResourceUtils;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
+import org.onap.policy.models.tosca.simple.concepts.JpaToscaServiceTemplate;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.yaml.snakeyaml.Yaml;
 
 public class TestUtils {
+    private static final Logger LOGGER = LoggerFactory.getLogger(TestUtils.class);
     private static final StandardCoder standardCoder = new StandardCoder();
 
     private TestUtils() {
@@ -47,22 +52,42 @@ public class TestUtils {
      * @throws CoderException exception if it cannot be decoded
      * @throws XacmlApplicationException If the application cannot load the policy
      */
-    public static void loadPolicies(String resourceFile, XacmlApplicationServiceProvider service)
+    public static List<ToscaPolicy> loadPolicies(String resourceFile, XacmlApplicationServiceProvider service)
             throws CoderException, XacmlApplicationException {
+        //
+        // Our return object
+        //
+        List<ToscaPolicy> loadedPolicies = new ArrayList<>();
+        //
+        // Decode it
+        //
         String policyYaml = ResourceUtils.getResourceAsString(resourceFile);
         Yaml yaml = new Yaml();
         Object yamlObject = yaml.load(policyYaml);
         String yamlAsJsonString = standardCoder.encode(yamlObject);
+        //
+        // Serialize it into a class
+        //
         ToscaServiceTemplate serviceTemplate = standardCoder.decode(yamlAsJsonString, ToscaServiceTemplate.class);
         //
+        // Make sure all the fields are setup properly
+        //
+        JpaToscaServiceTemplate jtst = new JpaToscaServiceTemplate();
+        jtst.fromAuthorative(serviceTemplate);
+        ToscaServiceTemplate completedJtst = jtst.toAuthorative();
+        //
         // Get the policies
         //
-        for (Map<String, ToscaPolicy> policies : serviceTemplate.getToscaTopologyTemplate().getPolicies()) {
-            for (Entry<String, ToscaPolicy> entrySet : policies.entrySet()) {
-                service.loadPolicy(entrySet.getValue());
+        for (Map<String, ToscaPolicy> policies : completedJtst.getToscaTopologyTemplate().getPolicies()) {
+            for (ToscaPolicy policy : policies.values()) {
+                if (service.loadPolicy(policy)) {
+                    loadedPolicies.add(policy);
+                } else {
+                    LOGGER.error("Application failed to load policy");
+                }
             }
         }
-
+        return loadedPolicies;
     }
 
 }