Merge "Undeploy policies when deploy fails"
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / comm / PdpHeartbeatListenerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
4  * ================================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pap.main.comm;
22
23 import static org.junit.Assert.assertEquals;
24
25 import java.util.Arrays;
26 import java.util.List;
27
28 import org.junit.Test;
29 import org.onap.policy.common.endpoints.event.comm.Topic.CommInfrastructure;
30 import org.onap.policy.common.utils.coder.CoderException;
31 import org.onap.policy.models.base.PfModelException;
32 import org.onap.policy.models.pdp.concepts.PdpGroup;
33 import org.onap.policy.models.pdp.concepts.PdpStatus;
34 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
35 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
36 import org.onap.policy.models.pdp.enums.PdpState;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
38 import org.onap.policy.pap.main.rest.e2e.End2EndBase;
39
40 /**
41  * Class to perform unit test of {@link PdpHeartbeatListener}.
42  *
43  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
44  */
45 public class PdpHeartbeatListenerTest extends End2EndBase {
46
47     private static final String POLICY_VERSION = "1.0.0";
48     private static final String POLICY_NAME = "onap.restart.tca";
49     private static final String APEX_TYPE = "apex";
50     private static final String DEFAULT_GROUP = "defaultGroup";
51     private static final String PDP_NAME = "pdp_1";
52     private static final CommInfrastructure INFRA = CommInfrastructure.NOOP;
53     private static final String TOPIC = "my-topic";
54
55     private PdpHeartbeatListener pdpHeartbeatListener;
56
57     @Test
58     public void testPdpHeartbeatListener() throws CoderException, PfModelException {
59         addGroups("PdpGroups.json");
60         pdpHeartbeatListener = new PdpHeartbeatListener();
61
62         // Testing pdp registration success case
63         final PdpStatus status1 = new PdpStatus();
64         status1.setName(PDP_NAME);
65         status1.setState(PdpState.ACTIVE);
66         status1.setPdpGroup(DEFAULT_GROUP);
67         status1.setPdpType(APEX_TYPE);
68         status1.setHealthy(PdpHealthStatus.HEALTHY);
69         final List<ToscaPolicyIdentifier> idents1 =
70                 Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION));
71         status1.setPolicies(idents1);
72         pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status1);
73         verifyPdpGroup(DEFAULT_GROUP, 1);
74
75         // Testing pdp heartbeat success case
76         final PdpStatus status2 = new PdpStatus();
77         status2.setName(PDP_NAME);
78         status2.setState(PdpState.ACTIVE);
79         status2.setPdpGroup(DEFAULT_GROUP);
80         status2.setPdpType(APEX_TYPE);
81         status2.setHealthy(PdpHealthStatus.HEALTHY);
82         status2.setPdpSubgroup(APEX_TYPE);
83         final List<ToscaPolicyIdentifier> idents2 =
84                 Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION));
85         status2.setPolicies(idents2);
86         pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status2);
87         verifyPdpGroup(DEFAULT_GROUP, 1);
88
89         // Testing pdp heartbeat failure case with pdp missing
90         final PdpStatus status3 = new PdpStatus();
91         status3.setName("pdp_2");
92         status3.setState(PdpState.ACTIVE);
93         status3.setPdpGroup(DEFAULT_GROUP);
94         status3.setPdpType(APEX_TYPE);
95         status3.setHealthy(PdpHealthStatus.HEALTHY);
96         status3.setPdpSubgroup(APEX_TYPE);
97         final List<ToscaPolicyIdentifier> idents3 =
98                 Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION));
99         status3.setPolicies(idents3);
100         pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status3);
101         verifyPdpGroup(DEFAULT_GROUP, 1);
102
103         // Testing pdp registration failure case
104         final PdpStatus status4 = new PdpStatus();
105         status4.setName("pdp_3");
106         status4.setState(PdpState.ACTIVE);
107         status4.setPdpGroup("wrongGroup");
108         status4.setPdpType(APEX_TYPE);
109         status4.setHealthy(PdpHealthStatus.HEALTHY);
110         final List<ToscaPolicyIdentifier> idents4 =
111                 Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION));
112         status4.setPolicies(idents4);
113         pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status4);
114         verifyPdpGroup(DEFAULT_GROUP, 1);
115
116         // Testing pdp heartbeat failure case with pdp mismatch
117         final PdpStatus status5 = new PdpStatus();
118         status5.setName(PDP_NAME);
119         status5.setState(PdpState.PASSIVE);
120         status5.setPdpGroup(DEFAULT_GROUP);
121         status5.setPdpType(APEX_TYPE);
122         status5.setHealthy(PdpHealthStatus.HEALTHY);
123         status5.setPdpSubgroup(APEX_TYPE);
124         final List<ToscaPolicyIdentifier> idents5 =
125                 Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION));
126         status5.setPolicies(idents5);
127         pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status5);
128         verifyPdpGroup(DEFAULT_GROUP, 1);
129
130         // Testing pdp termination case
131         final PdpStatus status6 = new PdpStatus();
132         status6.setName(PDP_NAME);
133         status6.setState(PdpState.TERMINATED);
134         status6.setPdpGroup(DEFAULT_GROUP);
135         status6.setPdpType(APEX_TYPE);
136         status6.setPdpSubgroup(APEX_TYPE);
137         status6.setHealthy(PdpHealthStatus.HEALTHY);
138         final List<ToscaPolicyIdentifier> idents6 =
139                 Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION));
140         status6.setPolicies(idents6);
141         pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status6);
142         verifyPdpGroup(DEFAULT_GROUP, 0);
143
144     }
145
146     private void verifyPdpGroup(final String name, final int count) throws PfModelException {
147         final List<PdpGroup> fetchedGroups = fetchGroups(name);
148         for (final PdpSubGroup subGroup : fetchedGroups.get(0).getPdpSubgroups()) {
149             if (subGroup.getPdpType().equals(APEX_TYPE)) {
150                 assertEquals(count, subGroup.getPdpInstances().size());
151                 assertEquals(count, subGroup.getCurrentInstanceCount());
152                 if (count > 0) {
153                     assertEquals(PdpHealthStatus.HEALTHY, subGroup.getPdpInstances().get(0).getHealthy());
154                 }
155             }
156         }
157     }
158 }