Upgrade Java 17 in policy-drools-apps
[policy/drools-applications.git] / controlloop / common / rules-test / src / main / java / org / onap / policy / controlloop / common / rules / test / Rules.java
index 2e15895..8272205 100644 (file)
@@ -2,7 +2,8 @@
  * ============LICENSE_START=======================================================
  * ONAP
  * ================================================================================
- * Copyright (C) 2020 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2020-2022 AT&T Intellectual Property. All rights reserved.
+ * Modifications Copyright (C) 2023 Nordix Foundation.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -21,9 +22,9 @@
 package org.onap.policy.controlloop.common.rules.test;
 
 import static org.awaitility.Awaitility.await;
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertTrue;
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertTrue;
 
 import java.io.File;
 import java.io.FileNotFoundException;
@@ -48,11 +49,11 @@ import org.kie.api.event.rule.RuleRuntimeEventListener;
 import org.kie.api.runtime.KieSession;
 import org.onap.policy.common.utils.coder.CoderException;
 import org.onap.policy.common.utils.coder.StandardCoder;
+import org.onap.policy.common.utils.logging.LoggerUtils;
 import org.onap.policy.common.utils.resources.ResourceUtils;
 import org.onap.policy.controlloop.ControlLoopEvent;
 import org.onap.policy.controlloop.drl.legacy.ControlLoopParams;
 import org.onap.policy.controlloop.eventmanager.ControlLoopEventManager;
-import org.onap.policy.controlloop.eventmanager.ControlLoopEventManager2;
 import org.onap.policy.drools.controller.DroolsController;
 import org.onap.policy.drools.persistence.SystemPersistence;
 import org.onap.policy.drools.persistence.SystemPersistenceConstants;
@@ -62,7 +63,6 @@ import org.onap.policy.drools.system.PolicyControllerFactory;
 import org.onap.policy.drools.system.PolicyEngine;
 import org.onap.policy.drools.system.PolicyEngineConstants;
 import org.onap.policy.drools.util.KieUtils;
-import org.onap.policy.drools.utils.logging.LoggerUtil;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaServiceTemplate;
 import org.slf4j.Logger;
@@ -114,10 +114,10 @@ public class Rules {
         pdpdRepo.setConfigurationDir("src/test/resources/config");
 
         try {
-            File kmoduleFile = new File(resourceDir + "/META-INF/kmodule.xml");
-            File pomFile = new File("src/test/resources/" + controllerName + ".pom");
-            String resourceDir2 = resourceDir + "/org/onap/policy/controlloop/";
-            File ruleFile = new File(resourceDir + File.separator + controllerName + ".drl");
+            var kmoduleFile = new File(resourceDir + "/META-INF/kmodule.xml");
+            var pomFile = new File("src/test/resources/" + controllerName + ".pom");
+            var resourceDir2 = resourceDir + "/org/onap/policy/controlloop/";
+            var ruleFile = new File(resourceDir + File.separator + controllerName + ".drl");
             List<File> ruleFiles = Collections.singletonList(ruleFile);
 
             installArtifact(kmoduleFile, pomFile, resourceDir2, ruleFiles);
@@ -153,7 +153,7 @@ public class Rules {
      * as any event managers and events.
      */
     public void resetFacts() {
-        List<Class<?>> classes = List.of(ToscaPolicy.class, ControlLoopParams.class, ControlLoopEventManager2.class,
+        List<Class<?>> classes = List.of(ToscaPolicy.class, ControlLoopParams.class, ControlLoopEventManager.class,
                         ControlLoopEvent.class);
 
         // delete all objects of the listed classes
@@ -165,14 +165,6 @@ public class Rules {
             await(clazz.getSimpleName()).atMost(5, TimeUnit.SECONDS)
                             .until(() -> drools.facts(controllerName, clazz).isEmpty());
         }
-
-        /*
-         * We can't delete this class directly; we have to wait for the rules to clean it
-         * up, because the rule also cleans up a number of other associated objects.
-         */
-        Class<?> clazz = ControlLoopEventManager.class;
-        await(clazz.getSimpleName()).atMost(5, TimeUnit.SECONDS)
-                        .until(() -> drools.facts(controllerName, clazz).isEmpty());
     }
 
     /**
@@ -192,7 +184,7 @@ public class Rules {
     }
 
     private ToscaPolicy getPolicyFromTemplate(String resourcePath, String policyName) throws CoderException {
-        String policyJson = ResourceUtils.getResourceAsString(resourcePath);
+        var policyJson = ResourceUtils.getResourceAsString(resourcePath);
         if (policyJson == null) {
             throw new CoderException(new FileNotFoundException(resourcePath));
         }
@@ -232,12 +224,25 @@ public class Rules {
         }
     }
 
-    private ToscaPolicy getPolicyFromFile(String policyPath) throws CoderException {
-        String policyJson = ResourceUtils.getResourceAsString(policyPath);
+    /**
+     * Get policy from file.
+     */
+    public static ToscaPolicy getPolicyFromFile(String policyPath) throws CoderException {
+        var policyJson = ResourceUtils.getResourceAsString(policyPath);
         if (policyJson == null) {
             throw new CoderException(new FileNotFoundException(policyPath));
         }
 
+        if (policyPath.startsWith("policies/")) {
+            // using policy/models examples where policies are wrapped with the ToscaServiceTemplate
+            // for API component provisioning
+            logger.info("retrieving policy from policy models examples");
+            ToscaServiceTemplate template = coder.decode(policyJson, ToscaServiceTemplate.class);
+            if (template.getToscaTopologyTemplate().getPolicies().size() == 1) {
+                return template.getToscaTopologyTemplate().getPolicies().get(0).values().iterator().next();
+            }
+        }
+
         return coder.decode(policyJson, ToscaPolicy.class);
     }
 
@@ -263,17 +268,17 @@ public class Rules {
      * Sets up overall logging.
      */
     private void setupLogging() {
-        LoggerUtil.setLevel(LoggerUtil.ROOT_LOGGER, "WARN");
-        LoggerUtil.setLevel("org.eclipse.jetty", "WARN");
-        LoggerUtil.setLevel("org.onap.policy.controlloop", "INFO");
-        LoggerUtil.setLevel("network", "INFO");
+        LoggerUtils.setLevel(LoggerUtils.ROOT_LOGGER, "WARN");
+        LoggerUtils.setLevel("org.eclipse.jetty", "WARN");
+        LoggerUtils.setLevel("org.onap.policy.controlloop", "INFO");
+        LoggerUtils.setLevel("network", "INFO");
     }
 
     /**
      * Sets up Drools Logging for events of interest.
      */
     private void setupDroolsLogging() {
-        KieSession session = getKieSession();
+        var session = getKieSession();
 
         session.addEventListener(new RuleListenerLogger());
         session.addEventListener(new AgendaListenerLogger());