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