2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2023 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pap.main.notification;
24 import static org.assertj.core.api.Assertions.assertThat;
26 import java.util.ArrayList;
27 import java.util.Collection;
28 import java.util.Comparator;
29 import java.util.Iterator;
30 import java.util.List;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
33 import org.onap.policy.models.pap.concepts.PolicyNotification;
34 import org.onap.policy.models.pap.concepts.PolicyStatus;
35 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus;
36 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus.PdpPolicyStatusBuilder;
37 import org.onap.policy.models.pdp.concepts.PdpPolicyStatus.State;
38 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
40 class DeploymentTrackerTest {
41 private static final String VERSION = "1.2.3";
42 private static final String MY_GROUP = "MyGroup";
43 private static final String MY_PDP_TYPE = "MyPdpType";
44 private static final ToscaConceptIdentifier POLICY_A = new ToscaConceptIdentifier("PolicyA", VERSION);
45 private static final ToscaConceptIdentifier POLICY_B = new ToscaConceptIdentifier("PolicyB", VERSION);
46 private static final ToscaConceptIdentifier POLICY_TYPE = new ToscaConceptIdentifier("MyPolicyType", VERSION);
47 private static final String PDP_A = "pdpA";
48 private static final String PDP_B = "pdpB";
50 private DeploymentTracker tracker;
51 private PdpPolicyStatusBuilder builder;
54 * Sets up test objects.
58 tracker = new DeploymentTracker();
59 builder = PdpPolicyStatus.builder().deploy(true).state(State.SUCCESS).pdpGroup(MY_GROUP).pdpType(MY_PDP_TYPE)
60 .policy(POLICY_A).policyType(POLICY_TYPE).pdpId(PDP_A);
64 void testGetDeploymentStatus() {
65 assertThat(tracker.getDeploymentStatus()).isEmpty();
67 tracker.add(builder.build());
68 tracker.add(builder.policy(POLICY_B).build());
69 assertThat(tracker.getDeploymentStatus()).hasSize(2);
71 assertThat(tracker.getUndeploymentStatus()).isEmpty();
75 void testGetUndeploymentStatus() {
76 builder.deploy(false);
78 assertThat(tracker.getUndeploymentStatus()).isEmpty();
80 tracker.add(builder.build());
81 tracker.add(builder.policy(POLICY_B).build());
82 assertThat(tracker.getUndeploymentStatus()).hasSize(2);
84 assertThat(tracker.getDeploymentStatus()).isEmpty();
88 void testAddNotifications() {
89 DeploymentTracker newTracker = new DeploymentTracker();
91 newTracker.add(builder.build());
92 newTracker.add(builder.policy(POLICY_B).deploy(false).build());
94 PolicyNotification notif = new PolicyNotification();
95 tracker.addNotifications(notif, newTracker);
97 assertThat(notif.getAdded()).hasSize(1);
98 assertThat(notif.getAdded().get(0).getPolicy()).isEqualTo(POLICY_A);
100 assertThat(notif.getDeleted()).hasSize(1);
101 assertThat(notif.getDeleted().get(0).getPolicy()).isEqualTo(POLICY_B);
106 DeploymentTracker newTracker = new DeploymentTracker();
109 tracker.add(builder.build());
110 newTracker.add(builder.build());
112 // only appears in the new tracker
113 newTracker.add(builder.policy(POLICY_B).build());
115 PolicyNotification notif = new PolicyNotification();
116 tracker.addNotifications(notif, newTracker);
118 assertThat(notif.getDeleted()).isEmpty();
120 // only policy B should appear
121 assertThat(notif.getAdded()).hasSize(1);
122 assertThat(notif.getAdded().get(0).getPolicy()).isEqualTo(POLICY_B);
126 void testNeedNotification() {
127 final PolicyStatus oldStat = new PolicyStatus();
128 final PolicyStatus newStat = new PolicyStatus();
130 // new, complete policy - notify
131 assertThat(tracker.needNotification(null, newStat)).isTrue();
133 // new, incomplete policy - don't notify
134 newStat.setIncompleteCount(1);
135 assertThat(tracker.needNotification(null, newStat)).isFalse();
136 newStat.setIncompleteCount(0);
138 // unchanged - don't notify
139 assertThat(tracker.needNotification(oldStat, newStat)).isFalse();
141 // was incomplete, now complete - notify
142 oldStat.setIncompleteCount(1);
143 assertThat(tracker.needNotification(oldStat, newStat)).isTrue();
144 oldStat.setIncompleteCount(0);
146 // was failed, now ok - notify
147 oldStat.setFailureCount(1);
148 assertThat(tracker.needNotification(oldStat, newStat)).isTrue();
149 oldStat.setFailureCount(0);
151 // was failed & incomplete, now complete - notify
152 oldStat.setIncompleteCount(1);
153 oldStat.setFailureCount(1);
154 assertThat(tracker.needNotification(oldStat, newStat)).isTrue();
155 oldStat.setIncompleteCount(0);
156 oldStat.setFailureCount(0);
158 // was complete, now incomplete - notify
159 newStat.setIncompleteCount(1);
160 assertThat(tracker.needNotification(oldStat, newStat)).isTrue();
161 newStat.setIncompleteCount(0);
163 // was incomplete, still incomplete - don't notify
164 newStat.setIncompleteCount(1);
165 oldStat.setIncompleteCount(1);
166 assertThat(tracker.needNotification(oldStat, newStat)).isFalse();
167 newStat.setIncompleteCount(0);
168 oldStat.setIncompleteCount(0);
172 void testAddMissing() {
173 DeploymentTracker newTracker = new DeploymentTracker();
175 // appears in both, not waiting
176 tracker.add(builder.build());
177 newTracker.add(builder.build());
179 // appears only in the tracker, not waiting
180 tracker.add(builder.policy(POLICY_B).build());
182 // appears in both, waiting
183 tracker.add(builder.policy(new ToscaConceptIdentifier("PolicyX", VERSION)).state(State.WAITING).build());
184 newTracker.add(builder.build());
186 // appears only in the tracker, waiting
187 tracker.add(builder.policy(new ToscaConceptIdentifier("PolicyY", VERSION)).state(State.WAITING).build());
189 // now extract the notifications
190 PolicyNotification notif = new PolicyNotification();
191 tracker.addNotifications(notif, newTracker);
193 assertThat(notif.getDeleted()).isEmpty();
195 // only policy B should appear
196 assertThat(notif.getAdded()).hasSize(1);
197 assertThat(notif.getAdded().get(0).getPolicy()).isEqualTo(POLICY_B);
201 void testAddStatusAction() {
202 tracker.add(new StatusAction(StatusAction.Action.DELETED, builder.build()));
203 assertThat(tracker.getDeploymentStatus()).isEmpty();
205 tracker.add(new StatusAction(StatusAction.Action.UNCHANGED, builder.build()));
206 tracker.add(new StatusAction(StatusAction.Action.CREATED, builder.build()));
207 tracker.add(new StatusAction(StatusAction.Action.UPDATED, builder.build()));
209 Collection<PolicyStatus> result = tracker.getDeploymentStatus();
210 assertThat(result).hasSize(1);
211 PolicyStatus status = result.iterator().next();
212 assertThat(status.getSuccessCount()).isEqualTo(3);
216 void testAddPdpPolicyStatus() {
217 tracker.add(builder.build());
218 Collection<PolicyStatus> result = tracker.getDeploymentStatus();
219 assertThat(result).hasSize(1);
220 PolicyStatus status = result.iterator().next();
222 assertThat(status.getFailureCount()).isZero();
223 assertThat(status.getIncompleteCount()).isZero();
224 assertThat(status.getSuccessCount()).isEqualTo(1);
225 assertThat(status.getPolicy()).isEqualTo(POLICY_A);
226 assertThat(status.getPolicyType()).isEqualTo(POLICY_TYPE);
228 // add another, failed
229 tracker.add(builder.pdpId(PDP_B).state(State.FAILURE).build());
230 result = tracker.getDeploymentStatus();
231 assertThat(result).hasSize(1);
232 status = result.iterator().next();
234 assertThat(status.getFailureCount()).isEqualTo(1);
235 assertThat(status.getIncompleteCount()).isZero();
236 assertThat(status.getSuccessCount()).isEqualTo(1);
238 // add another, waiting
239 tracker.add(builder.pdpId(PDP_A).state(State.WAITING).build());
240 result = tracker.getDeploymentStatus();
241 assertThat(result).hasSize(1);
242 status = result.iterator().next();
244 assertThat(status.getFailureCount()).isEqualTo(1);
245 assertThat(status.getIncompleteCount()).isEqualTo(1);
246 assertThat(status.getSuccessCount()).isEqualTo(1);
249 tracker.add(builder.policy(POLICY_B).pdpId(PDP_A).state(State.WAITING).build());
250 result = tracker.getDeploymentStatus();
251 assertThat(result).hasSize(2);
253 List<PolicyStatus> list = new ArrayList<>(result);
254 list.sort(Comparator.comparing(PolicyStatus::getPolicy));
255 Iterator<PolicyStatus> iter = list.iterator();
257 status = iter.next();
258 assertThat(status.getPolicy()).isEqualTo(POLICY_A);
259 assertThat(status.getFailureCount()).isEqualTo(1);
260 assertThat(status.getIncompleteCount()).isEqualTo(1);
261 assertThat(status.getSuccessCount()).isEqualTo(1);
263 status = iter.next();
264 assertThat(status.getPolicy()).isEqualTo(POLICY_B);
265 assertThat(status.getFailureCount()).isZero();
266 assertThat(status.getIncompleteCount()).isEqualTo(1);
267 assertThat(status.getSuccessCount()).isZero();
269 // add undeployment record
270 tracker.add(builder.deploy(false).build());
271 assertThat(tracker.getDeploymentStatus()).hasSize(2);
272 assertThat(tracker.getUndeploymentStatus()).hasSize(1);