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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pap.main.comm;
23 import static org.junit.Assert.assertEquals;
25 import java.util.Arrays;
26 import java.util.List;
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;
41 * Class to perform unit test of {@link PdpHeartbeatListener}.
43 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
45 public class PdpHeartbeatListenerTest extends End2EndBase {
47 private static final String POLICY_VERSION = "1.0.0";
48 private static final String POLICY_NAME = "onap.policies.controlloop.operational.Apex.SampleDomain";
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";
55 private PdpHeartbeatListener pdpHeartbeatListener;
58 public void testPdpHeartbeatListener() throws CoderException, PfModelException {
59 addGroups("PdpGroups.json");
60 pdpHeartbeatListener = new PdpHeartbeatListener();
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);
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);
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, 2);
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, 2);
116 // Testing pdp heartbeat failure case with pdp state 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, 2);
130 // Testing pdp heartbeat failure case with pdp policies mismatch
131 final PdpStatus status6 = new PdpStatus();
132 status6.setName(PDP_NAME);
133 status6.setState(PdpState.ACTIVE);
134 status6.setPdpGroup(DEFAULT_GROUP);
135 status6.setPdpType(APEX_TYPE);
136 status6.setHealthy(PdpHealthStatus.HEALTHY);
137 status6.setPdpSubgroup(APEX_TYPE);
138 final List<ToscaPolicyIdentifier> idents6 =
139 Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION),
140 new ToscaPolicyIdentifier("onap.restart.tca", POLICY_VERSION));
141 status6.setPolicies(idents6);
142 pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status6);
143 verifyPdpGroup(DEFAULT_GROUP, 2);
145 // Testing pdp heartbeat failure case with pdp no policies
146 final PdpStatus status7 = new PdpStatus();
147 status7.setName(PDP_NAME);
148 status7.setState(PdpState.ACTIVE);
149 status7.setPdpGroup(DEFAULT_GROUP);
150 status7.setPdpType(APEX_TYPE);
151 status7.setHealthy(PdpHealthStatus.HEALTHY);
152 status7.setPdpSubgroup(APEX_TYPE);
153 pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status7);
154 verifyPdpGroup(DEFAULT_GROUP, 2);
156 // Testing pdp termination case for pdp_1
157 final PdpStatus status8 = new PdpStatus();
158 status8.setName(PDP_NAME);
159 status8.setState(PdpState.TERMINATED);
160 status8.setPdpGroup(DEFAULT_GROUP);
161 status8.setPdpType(APEX_TYPE);
162 status8.setPdpSubgroup(APEX_TYPE);
163 status8.setHealthy(PdpHealthStatus.HEALTHY);
164 final List<ToscaPolicyIdentifier> idents8 =
165 Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION));
166 status8.setPolicies(idents8);
167 pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status8);
168 verifyPdpGroup(DEFAULT_GROUP, 1);
170 // Testing pdp termination case for pdp_2
171 final PdpStatus status9 = new PdpStatus();
172 status9.setName("pdp_2");
173 status9.setState(PdpState.TERMINATED);
174 status9.setPdpGroup(DEFAULT_GROUP);
175 status9.setPdpType(APEX_TYPE);
176 status9.setPdpSubgroup(APEX_TYPE);
177 status9.setHealthy(PdpHealthStatus.HEALTHY);
178 final List<ToscaPolicyIdentifier> idents9 =
179 Arrays.asList(new ToscaPolicyIdentifier(POLICY_NAME, POLICY_VERSION));
180 status9.setPolicies(idents9);
181 pdpHeartbeatListener.onTopicEvent(INFRA, TOPIC, status9);
182 verifyPdpGroup(DEFAULT_GROUP, 0);
186 private void verifyPdpGroup(final String name, final int count) throws PfModelException {
187 final List<PdpGroup> fetchedGroups = fetchGroups(name);
188 for (final PdpSubGroup subGroup : fetchedGroups.get(0).getPdpSubgroups()) {
189 if (subGroup.getPdpType().equals(APEX_TYPE)) {
190 assertEquals(count, subGroup.getPdpInstances().size());
191 assertEquals(count, subGroup.getCurrentInstanceCount());
193 assertEquals(PdpHealthStatus.HEALTHY, subGroup.getPdpInstances().get(0).getHealthy());