policy/pap jdk11 upgrades
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / notification / PolicyNotifierTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
4  * ================================================================================
5  * Copyright (C) 2019-2020 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.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertSame;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.Mockito.doAnswer;
29 import static org.mockito.Mockito.never;
30 import static org.mockito.Mockito.times;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
33
34 import java.util.ArrayList;
35 import java.util.Arrays;
36 import java.util.Collections;
37 import java.util.Iterator;
38 import java.util.List;
39 import java.util.Optional;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.mockito.ArgumentCaptor;
43 import org.mockito.Captor;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 import org.mockito.stubbing.Answer;
47 import org.onap.policy.models.base.PfModelException;
48 import org.onap.policy.models.pap.concepts.PolicyNotification;
49 import org.onap.policy.models.pap.concepts.PolicyStatus;
50 import org.onap.policy.models.pdp.concepts.Pdp;
51 import org.onap.policy.models.pdp.concepts.PdpGroup;
52 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
53 import org.onap.policy.models.provider.PolicyModelsProvider;
54 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyIdentifier;
56 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
57 import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper;
58 import org.onap.policy.pap.main.PolicyPapRuntimeException;
59 import org.onap.policy.pap.main.comm.Publisher;
60 import org.onap.policy.pap.main.comm.QueueToken;
61
62 public class PolicyNotifierTest extends PolicyCommonSupport {
63
64     @Mock
65     private Publisher<PolicyNotification> publisher;
66
67     @Mock
68     private PolicyModelsProviderFactoryWrapper daoFactory;
69
70     @Mock
71     private PolicyModelsProvider dao;
72
73     @Mock
74     private PolicyDeployTracker deploy;
75
76     @Mock
77     private PolicyUndeployTracker undeploy;
78
79     @Mock
80     private PolicyStatus status1;
81
82     @Mock
83     private PolicyStatus status2;
84
85     @Mock
86     private PolicyStatus status3;
87
88     @Mock
89     private PolicyStatus status4;
90
91     @Captor
92     ArgumentCaptor<QueueToken<PolicyNotification>> notifyCaptor;
93
94     private MyNotifier notifier;
95
96     /**
97      * Creates various objects, including {@link #notifier}.
98      */
99     @Before
100     public void setUp() {
101         MockitoAnnotations.initMocks(this);
102
103         super.setUp();
104
105         try {
106             when(daoFactory.create()).thenReturn(dao);
107             when(dao.getPdpGroups(null)).thenReturn(Collections.emptyList());
108
109             notifier = new MyNotifier(publisher);
110
111         } catch (PfModelException e) {
112             throw new PolicyPapRuntimeException(e);
113         }
114     }
115
116     @Test
117     public void testLoadPoliciesPolicyModelsProviderFactoryWrapper() throws PfModelException {
118         final PdpGroup group1 = makeGroup("my group #1", makeSubGroup("sub #1 A", 2, policy1, policy4),
119                         makeSubGroup("sub #1 B", 1, policy2));
120
121         // one policy is a duplicate
122         final PdpGroup group2 = makeGroup("my group #2", makeSubGroup("sub #2 A", 1, policy1, policy3));
123
124         when(dao.getPdpGroups(null)).thenReturn(Arrays.asList(group1, group2));
125
126         ToscaPolicyTypeIdentifier type2 = new ToscaPolicyTypeIdentifier("my other type", "8.8.8");
127
128         // note: no mapping for policy4
129         when(dao.getFilteredPolicyList(any())).thenReturn(Arrays.asList(makePolicy(policy1, type),
130                         makePolicy(policy2, type2), makePolicy(policy3, type)));
131
132         // load it
133         notifier = new MyNotifier(publisher);
134
135         ArgumentCaptor<PolicyPdpNotificationData> captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class);
136
137         // should have added policy1, policy2, policy1 (duplicate), policy3, but not
138         // policy4
139         verify(deploy, times(4)).addData(captor.capture());
140
141         Iterator<PolicyPdpNotificationData> iter = captor.getAllValues().iterator();
142         PolicyPdpNotificationData data = iter.next();
143         assertEquals(policy1, data.getPolicyId());
144         assertEquals(type, data.getPolicyType());
145         assertEquals("[sub #1 A 0, sub #1 A 1]", data.getPdps().toString());
146
147         data = iter.next();
148         assertEquals(policy2, data.getPolicyId());
149         assertEquals(type2, data.getPolicyType());
150         assertEquals("[sub #1 B 0]", data.getPdps().toString());
151
152         data = iter.next();
153         assertEquals(policy1, data.getPolicyId());
154         assertEquals(type, data.getPolicyType());
155         assertEquals("[sub #2 A 0]", data.getPdps().toString());
156
157         data = iter.next();
158         assertEquals(policy3, data.getPolicyId());
159         assertEquals(type, data.getPolicyType());
160         assertEquals("[sub #2 A 0]", data.getPdps().toString());
161     }
162
163     private ToscaPolicy makePolicy(ToscaPolicyIdentifier policyId, ToscaPolicyTypeIdentifier type) {
164         ToscaPolicy policy = new ToscaPolicy();
165
166         policy.setName(policyId.getName());
167         policy.setVersion(policyId.getVersion());
168         policy.setType(type.getName());
169         policy.setTypeVersion(type.getVersion());
170
171         return policy;
172     }
173
174     private PdpGroup makeGroup(String name, PdpSubGroup... subgrps) {
175         final PdpGroup group = new PdpGroup();
176         group.setName(name);
177
178         group.setPdpSubgroups(Arrays.asList(subgrps));
179
180         return group;
181     }
182
183     private PdpSubGroup makeSubGroup(String name, int numPdps, ToscaPolicyIdentifier... policies) {
184         final PdpSubGroup subgrp = new PdpSubGroup();
185         subgrp.setPdpType(name);
186         subgrp.setPdpInstances(new ArrayList<>(numPdps));
187
188         for (int x = 0; x < numPdps; ++x) {
189             Pdp pdp = new Pdp();
190             pdp.setInstanceId(name + " " + x);
191
192             subgrp.getPdpInstances().add(pdp);
193         }
194
195         subgrp.setPolicies(Arrays.asList(policies));
196
197         return subgrp;
198     }
199
200     @Test
201     public void testGetStatus() {
202         List<PolicyStatus> statusList = Arrays.asList(status1);
203         when(deploy.getStatus()).thenReturn(statusList);
204
205         assertSame(statusList, notifier.getStatus());
206     }
207
208     @Test
209     public void testGetStatusString() {
210         List<PolicyStatus> statusList = Arrays.asList(status1);
211         when(deploy.getStatus("a policy")).thenReturn(statusList);
212
213         assertSame(statusList, notifier.getStatus("a policy"));
214     }
215
216     @Test
217     public void testGetStatusToscaPolicyIdentifier() {
218         Optional<PolicyStatus> status = Optional.of(status1);
219         when(deploy.getStatus(policy1)).thenReturn(status);
220
221         assertSame(status, notifier.getStatus(policy1));
222     }
223
224     @Test
225     public void testAddDeploymentData() {
226         doAnswer(addStatus(1, status1, status2)).when(undeploy).removeData(any(), any());
227
228         PolicyPdpNotificationData data = makeData(policy1, PDP1, PDP2);
229         notifier.addDeploymentData(data);
230
231         verify(deploy).addData(data);
232         verify(undeploy).removeData(eq(data), any());
233
234         PolicyNotification notification = getNotification();
235         assertEquals(Arrays.asList(status1, status2), notification.getDeleted());
236         assertTrue(notification.getAdded().isEmpty());
237     }
238
239     @Test
240     public void testAddUndeploymentData() {
241         doAnswer(addStatus(1, status1, status2)).when(deploy).removeData(any(), any());
242
243         PolicyPdpNotificationData data = makeData(policy1, PDP1, PDP2);
244         notifier.addUndeploymentData(data);
245
246         verify(undeploy).addData(data);
247         verify(deploy).removeData(eq(data), any());
248
249         PolicyNotification notification = getNotification();
250         assertEquals(Arrays.asList(status1, status2), notification.getAdded());
251         assertTrue(notification.getDeleted().isEmpty());
252     }
253
254     @Test
255     public void testProcessResponseString() {
256         doAnswer(addStatus(2, status1, status2)).when(deploy).processResponse(eq(PDP1), any(), any());
257         doAnswer(addStatus(2, status3, status4)).when(undeploy).processResponse(eq(PDP1), any(), any());
258
259         List<ToscaPolicyIdentifier> activePolicies = Arrays.asList(policy1, policy2);
260         notifier.processResponse(PDP1, activePolicies);
261
262         PolicyNotification notification = getNotification();
263         assertEquals(Arrays.asList(status1, status2), notification.getAdded());
264         assertEquals(Arrays.asList(status3, status4), notification.getDeleted());
265     }
266
267     @Test
268     public void testRemovePdp() {
269         doAnswer(addStatus(1, status1, status2)).when(deploy).removePdp(eq(PDP1), any());
270         doAnswer(addStatus(1, status3, status4)).when(undeploy).removePdp(eq(PDP1), any());
271
272         notifier.removePdp(PDP1);
273
274         PolicyNotification notification = getNotification();
275         assertEquals(Arrays.asList(status1, status2), notification.getAdded());
276         assertEquals(Arrays.asList(status3, status4), notification.getDeleted());
277     }
278
279     /**
280      * Tests publish(), when the notification is empty.
281      */
282     @Test
283     public void testPublishEmpty() {
284         notifier.removePdp(PDP1);
285
286         verify(publisher, never()).enqueue(any());
287     }
288
289     /**
290      * Tests publish(), when the notification is NOT empty.
291      */
292     @Test
293     public void testPublishNotEmpty() {
294         doAnswer(addStatus(1, status1, status2)).when(deploy).removePdp(eq(PDP1), any());
295
296         notifier.removePdp(PDP1);
297
298         verify(publisher).enqueue(any());
299     }
300
301     @Test
302     public void testMakeDeploymentTracker_testMakeUndeploymentTracker() throws PfModelException {
303         // make real object, which will invoke the real makeXxx() methods
304         new PolicyNotifier(publisher, daoFactory).removePdp(PDP1);
305
306         verify(publisher, never()).enqueue(any());
307     }
308
309     /**
310      * Creates an answer that adds status updates to a status list.
311      *
312      * @param listIndex index of the status list within the argument list
313      * @param status status updates to be added
314      * @return an answer that adds the given status updates
315      */
316     private Answer<Void> addStatus(int listIndex, PolicyStatus... status) {
317         return invocation -> {
318             @SuppressWarnings("unchecked")
319             List<PolicyStatus> statusList = invocation.getArgument(listIndex, List.class);
320             statusList.addAll(Arrays.asList(status));
321             return null;
322         };
323     }
324
325     /**
326      * Gets the notification that was published.
327      *
328      * @return the notification that was published
329      */
330     private PolicyNotification getNotification() {
331         verify(publisher).enqueue(notifyCaptor.capture());
332         return notifyCaptor.getValue().get();
333     }
334
335
336     private class MyNotifier extends PolicyNotifier {
337
338         public MyNotifier(Publisher<PolicyNotification> publisher) throws PfModelException {
339             super(publisher, daoFactory);
340         }
341
342         @Override
343         protected PolicyDeployTracker makeDeploymentTracker() {
344             return deploy;
345         }
346
347         @Override
348         protected PolicyUndeployTracker makeUndeploymentTracker() {
349             return undeploy;
350         }
351     }
352 }