Address sonar issues in drools-pdp
[policy/drools-pdp.git] / feature-lifecycle / src / test / java / org / onap / policy / drools / lifecycle / LifecycleStateActiveTest.java
index b8afd20..54f7d68 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * ============LICENSE_START=======================================================
- * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
+ * Copyright (C) 2019-2020 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.
 package org.onap.policy.drools.lifecycle;
 
 import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;
-import static org.awaitility.Awaitility.await;
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
 
 import java.io.IOException;
 import java.nio.file.Files;
 import java.nio.file.Paths;
+import java.util.ArrayList;
 import java.util.Arrays;
 import java.util.Collections;
 import java.util.List;
 import java.util.Objects;
-import java.util.concurrent.Callable;
 import java.util.concurrent.TimeUnit;
 import org.junit.Before;
 import org.junit.Test;
@@ -44,9 +44,9 @@ import org.onap.policy.common.utils.network.NetworkUtil;
 import org.onap.policy.models.pdp.concepts.PdpStateChange;
 import org.onap.policy.models.pdp.concepts.PdpStatus;
 import org.onap.policy.models.pdp.concepts.PdpUpdate;
-import org.onap.policy.models.pdp.enums.PdpMessageType;
 import org.onap.policy.models.pdp.enums.PdpState;
 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
 
 /**
  * Lifecycle State Active Test.
@@ -58,7 +58,8 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
      */
     @Before
     public void startActive() throws CoderException {
-        fsm = new LifecycleFsm();
+        fsm = makeFsmWithPseudoTime();
+
         fsm.setStatusTimerSeconds(15);
         assertTrue(fsm.start());
 
@@ -73,6 +74,7 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
         change.setState(PdpState.ACTIVE);
         change.setName(fsm.getName());
 
+        fsm.setSubGroupAction("a");
         fsm.source.offer(new StandardCoder().encode(change));
         controllerSupport.getController().start();
     }
@@ -84,7 +86,7 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
     }
 
     @Test
-    public void start() {
+    public void testStart() {
         assertActive();
         assertFalse(fsm.start());
         assertActive();
@@ -94,14 +96,14 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
 
     private void assertActive() {
         assertEquals(PdpState.ACTIVE, fsm.state());
-        assertEquals("A", fsm.getGroup());
+        assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
         assertEquals("a", fsm.getSubgroup());
         assertTrue(fsm.isAlive());
-        await().atMost(fsm.getStatusTimerSeconds() + 1, TimeUnit.SECONDS).until(isStatus(PdpState.ACTIVE));
+        waitUntil(fsm.getStatusTimerSeconds() + 1, TimeUnit.SECONDS, isStatus(PdpState.ACTIVE));
     }
 
     @Test
-    public void stop() {
+    public void testStop() {
         assertTrue(fsm.stop());
         assertBasicTerminated();
 
@@ -112,11 +114,11 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
         assertEquals(PdpState.TERMINATED, fsm.state());
         assertFalse(fsm.isAlive());
         assertFalse(fsm.state.isAlive());
-        await().atMost(1, TimeUnit.SECONDS).until(isStatus(PdpState.TERMINATED));
+        waitUntil(1, TimeUnit.SECONDS, isStatus(PdpState.TERMINATED));
     }
 
     @Test
-    public void shutdown() {
+    public void testShutdown() {
         fsm.shutdown();
 
         assertBasicTerminated();
@@ -125,33 +127,24 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
         assertTrue(fsm.statusTask.isDone());
     }
 
-    private Callable<Boolean> isStatus(PdpState state) {
-        return () -> {
-            if (fsm.client.getSink().getRecentEvents().length == 0) {
-                return false;
-            }
-
-            List<String> events = Arrays.asList(fsm.client.getSink().getRecentEvents());
-            PdpStatus status =
-                new StandardCoder().decode(events.get(events.size() - 1), PdpStatus.class);
-
-            return status.getMessageName() == PdpMessageType.PDP_STATUS && state == status.getState();
-        };
-    }
-
     @Test
-    public void status() {
-        await().atMost(fsm.getStatusTimerSeconds() + 1, TimeUnit.SECONDS).until(isStatus(PdpState.ACTIVE));
+    public void testStatus() {
+        waitUntil(fsm.getStatusTimerSeconds() + 1, TimeUnit.SECONDS, isStatus(PdpState.ACTIVE));
         int preCount = fsm.client.getSink().getRecentEvents().length;
 
         assertTrue(fsm.status());
         assertEquals(preCount + 1, fsm.client.getSink().getRecentEvents().length);
 
+        fsm.start(controllerSupport.getController());
+        assertTrue(fsm.status());
+        assertEquals(preCount + 2, fsm.client.getSink().getRecentEvents().length);
+
+        fsm.stop(controllerSupport.getController());
         fsm.shutdown();
     }
 
     @Test
-    public void stateChange() throws CoderException {
+    public void testStateChange() throws CoderException {
         assertActive();
 
         /* no name and mismatching group info */
@@ -162,14 +155,14 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
 
         fsm.source.offer(new StandardCoder().encode(change));
         assertEquals(PdpState.ACTIVE, fsm.state());
-        assertNotEquals("B", fsm.getGroup());
+        assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
         assertNotEquals("b", fsm.getSubgroup());
 
         change.setName(fsm.getName());
         fsm.source.offer(new StandardCoder().encode(change));
         assertEquals(PdpState.ACTIVE, fsm.state());
-        assertEquals("B", fsm.getGroup());
-        assertEquals("b", fsm.getSubgroup());
+        assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
+        assertEquals("a", fsm.getSubgroup());
 
         change.setState(PdpState.SAFE);
         fsm.source.offer(new StandardCoder().encode(change));
@@ -182,41 +175,53 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
         change.setState(PdpState.PASSIVE);
         fsm.source.offer(new StandardCoder().encode(change));
         assertEquals(PdpState.PASSIVE, fsm.state());
-        await().atMost(fsm.getStatusTimerSeconds() + 1, TimeUnit.SECONDS).until(isStatus(PdpState.PASSIVE));
+        waitUntil(fsm.getStatusTimerSeconds() + 1, TimeUnit.SECONDS, isStatus(PdpState.PASSIVE));
 
         fsm.shutdown();
     }
 
     @Test
-    public void update() throws IOException, CoderException {
+    public void testUpdate() throws IOException, CoderException {
+
+        // TODO: extract repeated similar assertion blocks into their own helper methods
+
         PdpUpdate update = new PdpUpdate();
         update.setName(NetworkUtil.getHostname());
-        update.setPdpGroup("Z");
-        update.setPdpSubgroup("z");
+        update.setPdpGroup("W");
+        update.setPdpSubgroup("w");
         update.setPolicies(Collections.emptyList());
 
-        long originalInterval = fsm.getStatusTimerSeconds();
-        long interval = 2 * originalInterval;
-        update.setPdpHeartbeatIntervalMs(interval * 1000L);
-
         fsm.start(controllerSupport.getController());
-
         assertTrue(fsm.update(update));
 
         assertEquals(PdpState.ACTIVE, fsm.state());
-        assertEquals(interval, fsm.getStatusTimerSeconds());
-        assertEquals("Z", fsm.getGroup());
-        assertEquals("z", fsm.getSubgroup());
+        assertEquals(LifecycleFsm.DEFAULT_PDP_GROUP, fsm.getGroup());
+        assertEquals("w", fsm.getSubgroup());
 
         String restartV1 =
             new String(Files.readAllBytes(Paths.get("src/test/resources/tosca-policy-operational-restart.json")));
         ToscaPolicy toscaPolicyRestartV1 = new StandardCoder().decode(restartV1, ToscaPolicy.class);
         update.setPolicies(Arrays.asList(toscaPolicyRestartV1));
 
+        int qlength = fsm.client.getSink().getRecentEvents().length;
+
         // update with an operational.restart policy
 
         assertTrue(fsm.update(update));
-        assertEquals(1, fsm.policyTypesMap.size());
+        assertEquals(qlength + 1, fsm.client.getSink().getRecentEvents().length);
+        assertEquals(4, fsm.policyTypesMap.size());
+        assertNotNull(fsm.getPolicyTypesMap().get(
+                new ToscaPolicyTypeIdentifier("onap.policies.native.drools.Controller", "1.0.0")));
+        assertNotNull(fsm.getPolicyTypesMap().get(
+                new ToscaPolicyTypeIdentifier("onap.policies.native.drools.Artifact", "1.0.0")));
+        assertNotNull(fsm.getPolicyTypesMap().get(
+                new ToscaPolicyTypeIdentifier("onap.policies.controlloop.Operational", "1.0.0")));
+        assertNotNull(fsm.getPolicyTypesMap().get(
+                new ToscaPolicyTypeIdentifier("onap.policies.controlloop.operational.common.Drools",
+                "1.0.0")));
+        PdpStatus cachedStatus = new StandardCoder()
+                                    .decode(fsm.client.getSink().getRecentEvents()[qlength], PdpStatus.class);
+        assertEquals(new ArrayList<>(fsm.policiesMap.keySet()), cachedStatus.getPolicies());
 
         List<ToscaPolicy> factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
         assertEquals(1, factPolicies.size());
@@ -226,7 +231,12 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
         // dup update with the same operational.restart policy - nothing changes
 
         assertTrue(fsm.update(update));
-        assertEquals(1, fsm.policyTypesMap.size());
+        assertEquals(qlength + 2, fsm.client.getSink().getRecentEvents().length);
+        assertEquals(4, fsm.policyTypesMap.size());
+        cachedStatus = new StandardCoder()
+            .decode(fsm.client.getSink().getRecentEvents()[qlength + 1], PdpStatus.class);
+        assertEquals(new ArrayList<>(fsm.policiesMap.keySet()), cachedStatus.getPolicies());
+
 
         factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
         assertEquals(1, factPolicies.size());
@@ -237,7 +247,11 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
 
         update.setPolicies(Collections.emptyList());
         assertTrue(fsm.update(update));
-        assertEquals(1, fsm.policyTypesMap.size());
+        assertEquals(qlength + 3, fsm.client.getSink().getRecentEvents().length);
+        assertEquals(4, fsm.policyTypesMap.size());
+        cachedStatus = new StandardCoder()
+            .decode(fsm.client.getSink().getRecentEvents()[qlength + 2], PdpStatus.class);
+        assertEquals(new ArrayList<>(fsm.policiesMap.keySet()), cachedStatus.getPolicies());
 
         factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
         assertEquals(0, factPolicies.size());
@@ -247,7 +261,11 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
 
         update.setPolicies(Arrays.asList(toscaPolicyRestartV1));
         assertTrue(fsm.update(update));
-        assertEquals(1, fsm.policyTypesMap.size());
+        assertEquals(qlength + 4, fsm.client.getSink().getRecentEvents().length);
+        assertEquals(4, fsm.policyTypesMap.size());
+        cachedStatus = new StandardCoder()
+            .decode(fsm.client.getSink().getRecentEvents()[qlength + 3], PdpStatus.class);
+        assertEquals(new ArrayList<>(fsm.policiesMap.keySet()), cachedStatus.getPolicies());
 
         factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
         assertEquals(1, factPolicies.size());
@@ -261,7 +279,11 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
         ToscaPolicy toscaPolicyRestartV2 = new StandardCoder().decode(restartV2, ToscaPolicy.class);
         update.setPolicies(Arrays.asList(toscaPolicyRestartV2));
         assertTrue(fsm.update(update));
-        assertEquals(1, fsm.policyTypesMap.size());
+        assertEquals(qlength + 5, fsm.client.getSink().getRecentEvents().length);
+        assertEquals(4, fsm.policyTypesMap.size());
+        cachedStatus = new StandardCoder()
+            .decode(fsm.client.getSink().getRecentEvents()[qlength + 4], PdpStatus.class);
+        assertEquals(new ArrayList<>(fsm.policiesMap.keySet()), cachedStatus.getPolicies());
 
         factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
         assertEquals(1, factPolicies.size());
@@ -276,7 +298,11 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
         ToscaPolicy toscaPolicyFirewall = new StandardCoder().decode(firewall, ToscaPolicy.class);
         update.setPolicies(Arrays.asList(toscaPolicyRestartV2, toscaPolicyFirewall));
         assertTrue(fsm.update(update));
-        assertEquals(1, fsm.policyTypesMap.size());
+        assertEquals(qlength + 6, fsm.client.getSink().getRecentEvents().length);
+        assertEquals(4, fsm.policyTypesMap.size());
+        cachedStatus = new StandardCoder()
+            .decode(fsm.client.getSink().getRecentEvents()[qlength + 5], PdpStatus.class);
+        assertEquals(new ArrayList<>(fsm.policiesMap.keySet()), cachedStatus.getPolicies());
 
         factPolicies = controllerSupport.getFacts(ToscaPolicy.class);
         assertEquals(2, factPolicies.size());
@@ -285,6 +311,18 @@ public class LifecycleStateActiveTest extends LifecycleStateRunningTest {
         assertTrue(factPolicies.stream().anyMatch((ff) -> Objects.equals(toscaPolicyFirewall, ff)));
         assertEquals(2, fsm.policiesMap.size());
 
+        long originalInterval = fsm.getStatusTimerSeconds();
+        long interval = 10 * originalInterval;
+        update.setPdpHeartbeatIntervalMs(interval * 1000L);
+
+        assertTrue(fsm.update(update));
+
+        assertEquals(PdpState.ACTIVE, fsm.state());
+        assertEquals(interval, fsm.getStatusTimerSeconds());
+
+        assertTrue(controllerSupport.getController().getDrools().delete(ToscaPolicy.class));
+
         fsm.shutdown();
     }
+
 }