Drools Controller upgrades Tests

Issue-ID: POLICY-1407
Signed-off-by: jhh <jorge.hernandez-herrero@att.com>
Change-Id: I6963fced5608b62a41b73fef5070c21cf45d6fbe
Signed-off-by: jhh <jorge.hernandez-herrero@att.com>
diff --git a/policy-management/src/test/java/org/onap/policy/drools/controller/internal/MavenDroolsController3Test.java b/policy-management/src/test/java/org/onap/policy/drools/controller/internal/MavenDroolsController3Test.java
new file mode 100644
index 0000000..e237a45
--- /dev/null
+++ b/policy-management/src/test/java/org/onap/policy/drools/controller/internal/MavenDroolsController3Test.java
@@ -0,0 +1,145 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package org.onap.policy.drools.controller.internal;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertTrue;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Paths;
+import java.util.Arrays;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+import java.util.stream.Collectors;
+import java.util.stream.Stream;
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+import org.kie.api.builder.ReleaseId;
+import org.onap.policy.drools.controller.DroolsController;
+import org.onap.policy.drools.util.KieUtils;
+import org.onap.policy.drools.utils.logging.LoggerUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class MavenDroolsController3Test {
+    private static final String DROOLS_RESOURCES_DIR = "src/test/resources/";
+    private static final String DROOLS_KJAR_RESOURCES_DIR = "src/main/resources/";
+    private static final String DRL_EXT = ".drl";
+    private static final String POM_EXT = ".pom";
+    private static final String KMODULE_EXT = ".kmodule";
+
+    private static final String RULES_BASE = "rules";
+    private static final String KBNAME_RULES = "kbRules";
+    private static final String KBSESSION_RULES = RULES_BASE;
+    private static final String KBPACKAGE_RULES = RULES_BASE;
+
+    public static final CountDownLatch running1a = new CountDownLatch(1);
+    public static final CountDownLatch running1b = new CountDownLatch(1);
+    public static final CountDownLatch running2a = new CountDownLatch(1);
+    public static final CountDownLatch running2b = new CountDownLatch(1);
+
+    private static final Logger logger = LoggerFactory.getLogger(MavenDroolsController3Test.class);
+
+    private static ReleaseId install(String name, List<File> drls) throws IOException {
+        return
+            KieUtils.installArtifact(
+                Paths.get(DROOLS_RESOURCES_DIR + RULES_BASE + KMODULE_EXT).toFile(),
+                Paths.get(DROOLS_RESOURCES_DIR + name + POM_EXT).toFile(),
+               DROOLS_KJAR_RESOURCES_DIR + KBNAME_RULES + "/" + KBPACKAGE_RULES + "/",
+                drls);
+    }
+
+    public static ReleaseId rulesDescriptor1;
+    public static ReleaseId rulesDescriptor2;
+
+    /**
+     * Test Class Initialization.
+     */
+    @BeforeClass
+    public static void setUpBeforeClass() throws IOException {
+        rulesDescriptor1 =
+            install("rules1",
+                Stream.of(Paths.get(DROOLS_RESOURCES_DIR + "rules1" + DRL_EXT).toFile()).collect(Collectors.toList()));
+
+        rulesDescriptor2 =
+            install("rules2",
+                Stream.of(Paths.get(DROOLS_RESOURCES_DIR + "rules1" + DRL_EXT).toFile(),
+                          Paths.get(DROOLS_RESOURCES_DIR + "rules2" + DRL_EXT).toFile())
+                      .collect(Collectors.toList()));
+
+        LoggerUtil.setLevel("ROOT", "WARN");
+        LoggerUtil.setLevel("org.onap.policy.drools.controller.internal", "INFO");
+    }
+
+    @Test
+    public void upgrades() throws InterruptedException {
+        DroolsController rules =
+            new MavenDroolsController(
+                rulesDescriptor1.getGroupId(), rulesDescriptor1.getArtifactId(), rulesDescriptor1.getVersion(),
+                null, null);
+
+        assertTrue(rules.start());
+        assertTrue(running1a.await(30, TimeUnit.SECONDS));
+        summary(rules);
+        assertKie(rules, Arrays.asList("SETUP.1", "VERSION.12"), 1);
+
+        rules.updateToVersion(
+            rulesDescriptor2.getGroupId(),
+            rulesDescriptor2.getArtifactId(),
+            rulesDescriptor2.getVersion(),
+            null, null);
+
+        assertTrue(running2a.await(30, TimeUnit.SECONDS));
+        assertTrue(running2b.await(30, TimeUnit.SECONDS));
+        summary(rules);
+        assertKie(rules, Arrays.asList("SETUP.1", "VERSION.12", "SETUP.2", "VERSION.2"), 2);
+
+        rules.updateToVersion(
+            rulesDescriptor1.getGroupId(),
+            rulesDescriptor1.getArtifactId(),
+            rulesDescriptor1.getVersion(),
+            null, null);
+
+        assertTrue(running1b.await(30, TimeUnit.SECONDS));
+        summary(rules);
+        assertKie(rules, Arrays.asList("SETUP.1", "VERSION.12"), 1);
+    }
+
+    private void summary(DroolsController rules) {
+        logger.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
+        logger.info("Controller: " + rules.getGroupId() + ":" + rules.getArtifactId() + ":" + rules.getVersion());
+        logger.info(".....................................................................");
+        logger.info("KIE-BASES: " + KieUtils.getBases(rules.getContainer().getKieContainer()));
+        logger.info("KIE-PACKAGE-NAMES: " + KieUtils.getPackageNames(rules.getContainer().getKieContainer()));
+        logger.info("KIE-RULE-NAMES: " + KieUtils.getRuleNames(rules.getContainer().getKieContainer()));
+        logger.info("FACTS: " + rules.facts(KBSESSION_RULES, Object.class));
+        logger.info("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
+    }
+
+    private void assertKie(DroolsController controller, List<String> expectedRuleNames, long expectedFactCount) {
+        assertEquals(Arrays.asList("kbRules"), KieUtils.getBases(controller.getContainer().getKieContainer()));
+        assertEquals(expectedRuleNames, KieUtils.getRuleNames(controller.getContainer().getKieContainer()));
+        assertEquals(expectedFactCount, controller.factCount(controller.getSessionNames().get(0)));
+    }
+}
diff --git a/policy-management/src/test/java/org/onap/policy/drools/controller/internal/MavenDroolsControllerTest.java b/policy-management/src/test/java/org/onap/policy/drools/controller/internal/MavenDroolsControllerTest.java
index 0d8bdfa..e99e044 100644
--- a/policy-management/src/test/java/org/onap/policy/drools/controller/internal/MavenDroolsControllerTest.java
+++ b/policy-management/src/test/java/org/onap/policy/drools/controller/internal/MavenDroolsControllerTest.java
@@ -2,7 +2,7 @@
  * ============LICENSE_START=======================================================
  * ONAP
  * ================================================================================
- * Copyright (C) 2018-2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
  * ================================================================================
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -34,17 +34,14 @@
 import org.onap.policy.drools.util.KieUtils;
 
 public class MavenDroolsControllerTest {
-
     public static final String JUNIT_ECHO_KSESSION = "echo";
     public static final String JUNIT_ECHO_KBASE = "onap.policies.test";
     public static final String JUNIT_ECHO_KMODULE_DRL_PATH = "src/test/resources/echo.drl";
     public static final String JUNIT_ECHO_KMODULE_POM_PATH = "src/test/resources/echo.pom";
     public static final String JUNIT_ECHO_KMODULE_PATH = "src/test/resources/echo.kmodule";
-    public static final String JUNIT_ECHO_KJAR_DRL_PATH =
-        "src/main/resources/kbEcho/org/onap/policy/drools/test/echo.drl";
+    public static final String JUNIT_ECHO_KJAR_DRL_PATH = "src/main/resources/kbEcho/org/onap/policy/drools/test/";
 
     private static volatile ReleaseId releaseId;
-
     private static volatile CountDownLatch running;
 
     /**
diff --git a/policy-management/src/test/java/org/onap/policy/drools/protocol/coders/ProtocolCoderToolsetTest.java b/policy-management/src/test/java/org/onap/policy/drools/protocol/coders/ProtocolCoderToolsetTest.java
index 7787a7b..2bbb08b 100644
--- a/policy-management/src/test/java/org/onap/policy/drools/protocol/coders/ProtocolCoderToolsetTest.java
+++ b/policy-management/src/test/java/org/onap/policy/drools/protocol/coders/ProtocolCoderToolsetTest.java
@@ -23,12 +23,11 @@
 import com.google.gson.Gson;
 import com.google.gson.GsonBuilder;
 import java.io.IOException;
-import java.nio.file.Files;
 import java.nio.file.Paths;
 import java.util.List;
 import java.util.Properties;
 import org.junit.Assert;
-import org.junit.Before;
+import org.junit.BeforeClass;
 import org.junit.Test;
 import org.kie.api.builder.ReleaseId;
 import org.onap.policy.common.endpoints.event.comm.TopicEndpointManager;
@@ -45,45 +44,30 @@
 import org.slf4j.LoggerFactory;
 
 /**
- * ProtocolCoder Toolset JUNITs.
+ * ProtocolCoder Toolset Junits.
  */
 public class ProtocolCoderToolsetTest {
     public static final String JUNIT_PROTOCOL_CODER_ARTIFACT_ID = "protocolcoder";
     public static final String JUNIT_PROTOCOL_CODER_TOPIC = JUNIT_PROTOCOL_CODER_ARTIFACT_ID;
     public static final String CONTROLLER_ID = "blah";
-    public static final String ARTIFACT_ID_ECHO = "echo";
-    public static final String ARTIFACT_ID_POM_LINE = "<artifactId>" + ARTIFACT_ID_ECHO + "</artifactId>";
 
     private static Logger logger = LoggerFactory.getLogger(ProtocolCoderToolset.class);
 
-    private volatile ReleaseId releaseId;
+    private static volatile ReleaseId releaseId;
 
+    // customCoder has to be public to be accessed in tests below
     public static final Gson customCoder = new GsonBuilder().create();
 
     /**
-     * Setup.
-     *
-     * @throws IOException throws IO Exception
+     * Test Class Initialization.
      */
-    @Before
-    public void setUp() throws IOException {
-        if (releaseId != null) {
-            return;
-        }
-
-        String pom = new String(Files.readAllBytes(Paths.get(MavenDroolsControllerTest.JUNIT_ECHO_KMODULE_POM_PATH)));
-
-        if (!pom.contains(ARTIFACT_ID_POM_LINE)) {
-            throw new IllegalArgumentException("unexpected junit test pom");
-        }
-
-        String newPom = pom.replace(ARTIFACT_ID_ECHO, JUNIT_PROTOCOL_CODER_ARTIFACT_ID);
-
-        String kmodule = new String(Files.readAllBytes(Paths.get(MavenDroolsControllerTest.JUNIT_ECHO_KMODULE_PATH)));
-
-        String drl = new String(Files.readAllBytes(Paths.get(MavenDroolsControllerTest.JUNIT_ECHO_KMODULE_DRL_PATH)));
-
-        releaseId = KieUtils.installArtifact(kmodule, newPom, MavenDroolsControllerTest.JUNIT_ECHO_KJAR_DRL_PATH, drl);
+    @BeforeClass
+    public static void setupClass() throws IOException {
+        releaseId = KieUtils.installArtifact(
+            Paths.get(MavenDroolsControllerTest.JUNIT_ECHO_KMODULE_PATH).toFile(),
+            Paths.get(MavenDroolsControllerTest.JUNIT_ECHO_KMODULE_POM_PATH).toFile(),
+            MavenDroolsControllerTest.JUNIT_ECHO_KJAR_DRL_PATH,
+            Paths.get(MavenDroolsControllerTest.JUNIT_ECHO_KMODULE_DRL_PATH).toFile());
     }
 
     @Test
diff --git a/policy-management/src/test/resources/rules.kmodule b/policy-management/src/test/resources/rules.kmodule
new file mode 100644
index 0000000..4f3f874
--- /dev/null
+++ b/policy-management/src/test/resources/rules.kmodule
@@ -0,0 +1,26 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ============LICENSE_START=======================================================
+  ONAP
+  ================================================================================
+  Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+  ================================================================================
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+  ============LICENSE_END=========================================================
+  -->
+
+<kmodule xmlns="http://jboss.org/kie/6.0.0/kmodule">
+    <kbase name="kbRules">
+        <ksession name="rules"/>
+    </kbase>
+</kmodule>
\ No newline at end of file
diff --git a/policy-management/src/test/resources/rules1.drl b/policy-management/src/test/resources/rules1.drl
new file mode 100644
index 0000000..dd7051c
--- /dev/null
+++ b/policy-management/src/test/resources/rules1.drl
@@ -0,0 +1,46 @@
+/*
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package rules;
+
+import org.onap.policy.drools.controller.internal.MavenDroolsController3Test;
+
+declare Version
+    version : String
+end
+
+rule "SETUP.1"
+when
+then
+    Version v1 = new Version();
+    v1.setVersion("1");
+    insert(v1);
+
+    MavenDroolsController3Test.running1a.countDown();
+end
+
+rule "VERSION.12"
+when
+    $v12 : Version( version == "12")
+then
+    retract($v12);
+
+    MavenDroolsController3Test.running1b.countDown();
+end
\ No newline at end of file
diff --git a/policy-management/src/test/resources/rules1.pom b/policy-management/src/test/resources/rules1.pom
new file mode 100644
index 0000000..74901bb
--- /dev/null
+++ b/policy-management/src/test/resources/rules1.pom
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ============LICENSE_START=======================================================
+  ONAP
+  ================================================================================
+  Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+  ================================================================================
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+  ============LICENSE_END=========================================================
+  -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>org.onap.policy.drools.test</groupId>
+    <artifactId>rules</artifactId>
+    <version>1.0.0</version>
+</project>
diff --git a/policy-management/src/test/resources/rules2.drl b/policy-management/src/test/resources/rules2.drl
new file mode 100644
index 0000000..22d56d0
--- /dev/null
+++ b/policy-management/src/test/resources/rules2.drl
@@ -0,0 +1,48 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * ONAP
+ * ================================================================================
+ * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * ================================================================================
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ * ============LICENSE_END=========================================================
+ */
+
+package rules;
+
+declare Version
+    version : String
+end
+
+rule "SETUP.2"
+when
+then
+    Version v2 = new Version();
+    v2.setVersion("2");
+    insert(v2);
+
+    MavenDroolsController3Test.running2a.countDown();
+end
+
+rule "VERSION.2"
+when
+    $v1 : Version( version == "1")
+then
+    retract($v1);
+
+    Version v12 = new Version();
+    v12.setVersion("12");
+    insert(v12);
+
+    MavenDroolsController3Test.running2b.countDown();
+end
diff --git a/policy-management/src/test/resources/rules2.pom b/policy-management/src/test/resources/rules2.pom
new file mode 100644
index 0000000..a9fb151
--- /dev/null
+++ b/policy-management/src/test/resources/rules2.pom
@@ -0,0 +1,30 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<!--
+  ============LICENSE_START=======================================================
+  ONAP
+  ================================================================================
+  Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+  ================================================================================
+  Licensed under the Apache License, Version 2.0 (the "License");
+  you may not use this file except in compliance with the License.
+  You may obtain a copy of the License at
+
+       http://www.apache.org/licenses/LICENSE-2.0
+
+  Unless required by applicable law or agreed to in writing, software
+  distributed under the License is distributed on an "AS IS" BASIS,
+  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+  See the License for the specific language governing permissions and
+  limitations under the License.
+  ============LICENSE_END=========================================================
+  -->
+
+<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
+
+    <modelVersion>4.0.0</modelVersion>
+
+    <groupId>org.onap.policy.drools.test</groupId>
+    <artifactId>rules</artifactId>
+    <version>2.0.0</version>
+</project>