Add ability to turn on/off pdp statistics
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / comm / PdpHeartbeatListenerTest.java
index 1439660..2344c4f 100644 (file)
@@ -1,7 +1,8 @@
 /*-
  * ============LICENSE_START=======================================================
- *  Copyright (C) 2019-2020 Nordix Foundation.
- *  Modifications Copyright (C) 2020 AT&T Intellectual Property.
+ *  Copyright (C) 2019-2021 Nordix Foundation.
+ *  Modifications Copyright (C) 2020-2021 AT&T Intellectual Property.
+ *  Modifications Copyright (C) 2021 Bell Canada. 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.pap.main.comm;
 
+import static org.assertj.core.api.Assertions.assertThat;
 import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertSame;
 
 import java.text.ParseException;
-import java.text.SimpleDateFormat;
+import java.time.Instant;
+import java.util.ArrayList;
 import java.util.Arrays;
-import java.util.Date;
 import java.util.List;
+import java.util.stream.Collectors;
 import org.junit.Test;
 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
 import org.onap.policy.common.utils.coder.CoderException;
@@ -36,9 +40,12 @@ import org.onap.policy.models.pdp.concepts.PdpGroup;
 import org.onap.policy.models.pdp.concepts.PdpStatistics;
 import org.onap.policy.models.pdp.concepts.PdpStatus;
 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
+import org.onap.policy.models.pdp.concepts.PdpUpdate;
 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
 import org.onap.policy.models.pdp.enums.PdpState;
-import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
+import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
+import org.onap.policy.pap.main.parameters.CommonTestData;
 import org.onap.policy.pap.main.parameters.PdpParameters;
 import org.onap.policy.pap.main.rest.e2e.End2EndBase;
 
@@ -50,20 +57,20 @@ import org.onap.policy.pap.main.rest.e2e.End2EndBase;
 public class PdpHeartbeatListenerTest extends End2EndBase {
 
     private static final String POLICY_VERSION = "1.0.0";
-    private static final String POLICY_NAME = "onap.policies.controlloop.operational.Apex.SampleDomain";
+    private static final String POLICY_NAME = "onap.policies.controlloop.operational.common.apex.SampleDomain";
     private static final String APEX_TYPE = "apex";
     private static final String DEFAULT_GROUP = "defaultGroup";
     private static final String PDP_NAME = "pdp_1";
     private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
     private static final String TOPIC = "my-topic";
 
-    private Date timeStamp;
+    private Instant timeStamp;
     private PdpHeartbeatListener pdpHeartbeatListener;
 
     @Test
     public void testPdpHeartbeatListener() throws CoderException, PfModelException {
         addGroups("PdpGroups.json");
-        pdpHeartbeatListener = new PdpHeartbeatListener(new PdpParameters());
+        pdpHeartbeatListener = new PdpHeartbeatListener(new PdpParameters(), true);
 
         // Testing pdp registration success case
         final PdpStatus status1 = new PdpStatus();
@@ -72,8 +79,8 @@ public class PdpHeartbeatListenerTest extends End2EndBase {
         status1.setPdpGroup(DEFAULT_GROUP);
         status1.setPdpType(APEX_TYPE);
         status1.setHealthy(PdpHealthStatus.HEALTHY);
-        final List<ToscaPolicyIdentifier> idents1 =
-                Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION));
+        final List<ToscaConceptIdentifier> idents1 =
+                Arrays.asList(new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION));
         status1.setPolicies(idents1);
         pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status1);
         verifyPdpGroup(DEFAULT_GROUP, 1);
@@ -86,8 +93,8 @@ public class PdpHeartbeatListenerTest extends End2EndBase {
         status2.setPdpType(APEX_TYPE);
         status2.setHealthy(PdpHealthStatus.HEALTHY);
         status2.setPdpSubgroup(APEX_TYPE);
-        final List<ToscaPolicyIdentifier> idents2 =
-                Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION));
+        final List<ToscaConceptIdentifier> idents2 =
+                Arrays.asList(new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION));
         status2.setPolicies(idents2);
         pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status2);
         verifyPdpGroup(DEFAULT_GROUP, 1);
@@ -100,8 +107,8 @@ public class PdpHeartbeatListenerTest extends End2EndBase {
         status3.setPdpType(APEX_TYPE);
         status3.setHealthy(PdpHealthStatus.HEALTHY);
         status3.setPdpSubgroup(APEX_TYPE);
-        final List<ToscaPolicyIdentifier> idents3 =
-                Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION));
+        final List<ToscaConceptIdentifier> idents3 =
+                Arrays.asList(new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION));
         status3.setPolicies(idents3);
         pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status3);
         verifyPdpGroup(DEFAULT_GROUP, 2);
@@ -113,8 +120,8 @@ public class PdpHeartbeatListenerTest extends End2EndBase {
         status4.setPdpGroup("wrongGroup");
         status4.setPdpType(APEX_TYPE);
         status4.setHealthy(PdpHealthStatus.HEALTHY);
-        final List<ToscaPolicyIdentifier> idents4 =
-                Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION));
+        final List<ToscaConceptIdentifier> idents4 =
+                Arrays.asList(new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION));
         status4.setPolicies(idents4);
         pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status4);
         verifyPdpGroup(DEFAULT_GROUP, 2);
@@ -127,8 +134,8 @@ public class PdpHeartbeatListenerTest extends End2EndBase {
         status5.setPdpType(APEX_TYPE);
         status5.setHealthy(PdpHealthStatus.HEALTHY);
         status5.setPdpSubgroup(APEX_TYPE);
-        final List<ToscaPolicyIdentifier> idents5 =
-                Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION));
+        final List<ToscaConceptIdentifier> idents5 =
+                Arrays.asList(new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION));
         status5.setPolicies(idents5);
         pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status5);
         verifyPdpGroup(DEFAULT_GROUP, 2);
@@ -141,9 +148,9 @@ public class PdpHeartbeatListenerTest extends End2EndBase {
         status6.setPdpType(APEX_TYPE);
         status6.setHealthy(PdpHealthStatus.HEALTHY);
         status6.setPdpSubgroup(APEX_TYPE);
-        final List<ToscaPolicyIdentifier> idents6 =
-                Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION),
-                        new ToscaPolicyIdentifier("onap.restart.tca", POLICY_VERSION));
+        final List<ToscaConceptIdentifier> idents6 =
+                Arrays.asList(new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION),
+                        new ToscaConceptIdentifier("onap.restart.tca", POLICY_VERSION));
         status6.setPolicies(idents6);
         pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status6);
         verifyPdpGroup(DEFAULT_GROUP, 2);
@@ -168,8 +175,8 @@ public class PdpHeartbeatListenerTest extends End2EndBase {
         status7b.setPdpType(APEX_TYPE);
         status7b.setPdpSubgroup(APEX_TYPE);
         status7b.setHealthy(PdpHealthStatus.HEALTHY);
-        final List<ToscaPolicyIdentifier> idents7b =
-                Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION));
+        final List<ToscaConceptIdentifier> idents7b =
+                Arrays.asList(new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION));
         status7b.setPolicies(idents7b);
         pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status7b);
         verifyPdpGroup(DEFAULT_GROUP, 2);
@@ -182,8 +189,8 @@ public class PdpHeartbeatListenerTest extends End2EndBase {
         status8.setPdpType(APEX_TYPE);
         status8.setPdpSubgroup(APEX_TYPE);
         status8.setHealthy(PdpHealthStatus.HEALTHY);
-        final List<ToscaPolicyIdentifier> idents8 =
-                Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION));
+        final List<ToscaConceptIdentifier> idents8 =
+                Arrays.asList(new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION));
         status8.setPolicies(idents8);
         pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status8);
         verifyPdpGroup(DEFAULT_GROUP, 1);
@@ -196,18 +203,40 @@ public class PdpHeartbeatListenerTest extends End2EndBase {
         status9.setPdpType(APEX_TYPE);
         status9.setPdpSubgroup(APEX_TYPE);
         status9.setHealthy(PdpHealthStatus.HEALTHY);
-        final List<ToscaPolicyIdentifier> idents9 =
-                Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION));
+        final List<ToscaConceptIdentifier> idents9 =
+                Arrays.asList(new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION));
         status9.setPolicies(idents9);
         pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status9);
         verifyPdpGroup(DEFAULT_GROUP, 0);
+
+        // Test policy lists updated in createUpdate
+        ToscaPolicy polA = new ToscaPolicy();
+        polA.setName("pol-a-1.1.1");
+        polA.setVersion("1.1.1");
+        ToscaPolicy polB = new ToscaPolicy();
+        polB.setName("pol-b-1.1.1");
+        polB.setVersion("1.1.1");
+        List<ToscaPolicy> policies = new ArrayList<>();
+        policies.add(polA);
+        policies.add(polB);
+        final CommonTestData testData = new CommonTestData();
+        PdpParameters params = testData.getPapParameterGroup(1).getPdpParameters();
+        List<ToscaConceptIdentifier> polsUndep = policies.stream().map(ToscaPolicy::getIdentifier)
+                .collect(Collectors.toList());
+        PdpStatusMessageHandler handler = new PdpStatusMessageHandler(params, true);
+        PdpUpdate update10 = handler.createPdpUpdateMessage(
+                status3.getPdpGroup(), new PdpSubGroup(), "pdp_2",
+                policies, polsUndep);
+        assertSame(update10.getPoliciesToBeDeployed(), policies);
+        assertSame(update10.getPoliciesToBeUndeployed(), polsUndep);
+        assertThat(update10.getPoliciesToBeDeployed()).isInstanceOf(List.class);
     }
 
     @Test
     public void testPdpStatistics() throws CoderException, PfModelException, ParseException {
         addGroups("PdpGroups.json");
-        pdpHeartbeatListener = new PdpHeartbeatListener(new PdpParameters());
-        timeStamp = new SimpleDateFormat("yyyy-MM-dd").parse("2020-01-01");
+        pdpHeartbeatListener = new PdpHeartbeatListener(new PdpParameters(), true);
+        timeStamp = Instant.parse("2021-02-12T17:48:01.029211400Z");
 
         // init default pdp group
         final PdpStatus status1 = new PdpStatus();
@@ -216,8 +245,8 @@ public class PdpHeartbeatListenerTest extends End2EndBase {
         status1.setPdpGroup(DEFAULT_GROUP);
         status1.setPdpType(APEX_TYPE);
         status1.setHealthy(PdpHealthStatus.HEALTHY);
-        final List<ToscaPolicyIdentifier> idents1 =
-                Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION));
+        final List<ToscaConceptIdentifier> idents1 =
+                Arrays.asList(new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION));
         status1.setPolicies(idents1);
         pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status1);
         verifyPdpGroup(DEFAULT_GROUP, 1);
@@ -230,8 +259,8 @@ public class PdpHeartbeatListenerTest extends End2EndBase {
         status2.setPdpType(APEX_TYPE);
         status2.setHealthy(PdpHealthStatus.HEALTHY);
         status2.setPdpSubgroup(APEX_TYPE);
-        final List<ToscaPolicyIdentifier> idents2 =
-                Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION));
+        final List<ToscaConceptIdentifier> idents2 =
+                Arrays.asList(new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION));
         status2.setPolicies(idents2);
         pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status2);
 
@@ -243,8 +272,8 @@ public class PdpHeartbeatListenerTest extends End2EndBase {
         status3.setPdpType(APEX_TYPE);
         status3.setHealthy(PdpHealthStatus.HEALTHY);
         status3.setPdpSubgroup(APEX_TYPE);
-        final List<ToscaPolicyIdentifier> idents3 =
-                Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION));
+        final List<ToscaConceptIdentifier> idents3 =
+                Arrays.asList(new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION));
         status3.setPolicies(idents3);
 
         PdpStatistics pdpStatistics03 = new PdpStatistics();
@@ -264,8 +293,8 @@ public class PdpHeartbeatListenerTest extends End2EndBase {
         status4.setPdpType(APEX_TYPE);
         status4.setHealthy(PdpHealthStatus.HEALTHY);
         status4.setPdpSubgroup(APEX_TYPE);
-        final List<ToscaPolicyIdentifier> idents4 =
-                Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION));
+        final List<ToscaConceptIdentifier> idents4 =
+                Arrays.asList(new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION));
         status4.setPolicies(idents4);
         status4.setStatistics(null);
         pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status4);
@@ -279,8 +308,8 @@ public class PdpHeartbeatListenerTest extends End2EndBase {
         status5.setPdpType(APEX_TYPE);
         status5.setHealthy(PdpHealthStatus.HEALTHY);
         status5.setPdpSubgroup(APEX_TYPE);
-        final List<ToscaPolicyIdentifier> idents5 =
-                Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION));
+        final List<ToscaConceptIdentifier> idents5 =
+                Arrays.asList(new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION));
         status5.setPolicies(idents5);
 
         PdpStatistics pdpStatistics05 = new PdpStatistics();
@@ -301,8 +330,8 @@ public class PdpHeartbeatListenerTest extends End2EndBase {
         status6.setPdpType(APEX_TYPE);
         status6.setHealthy(PdpHealthStatus.HEALTHY);
         status6.setPdpSubgroup(APEX_TYPE);
-        final List<ToscaPolicyIdentifier> idents6 =
-                Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION));
+        final List<ToscaConceptIdentifier> idents6 =
+                Arrays.asList(new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION));
         status5.setPolicies(idents6);
 
         PdpStatistics pdpStatistics06 = new PdpStatistics();
@@ -317,6 +346,30 @@ public class PdpHeartbeatListenerTest extends End2EndBase {
 
         pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status5);
         verifyPdpStatistics(null, DEFAULT_GROUP, null, 1);
+
+        // Test pdp statistics save disabled case, sending valid pdp status but count should still remain 1
+        pdpHeartbeatListener = new PdpHeartbeatListener(new PdpParameters(), false);
+        timeStamp = Instant.parse("2021-02-12T17:48:05.029211400Z");
+        final PdpStatus status7 = new PdpStatus();
+        status7.setName(PDP_NAME);
+        status7.setState(PdpState.ACTIVE);
+        status7.setPdpGroup(DEFAULT_GROUP);
+        status7.setPdpType(APEX_TYPE);
+        status7.setHealthy(PdpHealthStatus.HEALTHY);
+        status7.setPdpSubgroup(APEX_TYPE);
+        final List<ToscaConceptIdentifier> idents7 =
+                Arrays.asList(new ToscaConceptIdentifier(POLICY_NAME, POLICY_VERSION));
+        status7.setPolicies(idents7);
+
+        PdpStatistics pdpStatistics07 = new PdpStatistics();
+        pdpStatistics07.setPdpInstanceId(PDP_NAME);
+        pdpStatistics07.setPdpGroupName(DEFAULT_GROUP);
+        pdpStatistics07.setPdpSubGroupName(APEX_TYPE);
+        pdpStatistics07.setTimeStamp(timeStamp);
+        status7.setStatistics(pdpStatistics07);
+        pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status7);
+        verifyPdpStatistics(PDP_NAME, DEFAULT_GROUP, null, 1);
+
     }
 
     private void verifyPdpGroup(final String name, final int count) throws PfModelException {