2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.pap.main.notification;
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;
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;
62 public class PolicyNotifierTest extends PolicyCommonSupport {
65 private Publisher<PolicyNotification> publisher;
68 private PolicyModelsProviderFactoryWrapper daoFactory;
71 private PolicyModelsProvider dao;
74 private PolicyDeployTracker deploy;
77 private PolicyUndeployTracker undeploy;
80 private PolicyStatus status1;
83 private PolicyStatus status2;
86 private PolicyStatus status3;
89 private PolicyStatus status4;
92 ArgumentCaptor<QueueToken<PolicyNotification>> notifyCaptor;
94 private MyNotifier notifier;
97 * Creates various objects, including {@link #notifier}.
100 public void setUp() {
101 MockitoAnnotations.initMocks(this);
106 when(daoFactory.create()).thenReturn(dao);
107 when(dao.getPdpGroups(null)).thenReturn(Collections.emptyList());
109 notifier = new MyNotifier(publisher);
111 } catch (PfModelException e) {
112 throw new PolicyPapRuntimeException(e);
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));
121 // one policy is a duplicate
122 final PdpGroup group2 = makeGroup("my group #2", makeSubGroup("sub #2 A", 1, policy1, policy3));
124 when(dao.getPdpGroups(null)).thenReturn(Arrays.asList(group1, group2));
126 ToscaPolicyTypeIdentifier type2 = new ToscaPolicyTypeIdentifier("my other type", "8.8.8");
128 // note: no mapping for policy4
129 when(dao.getFilteredPolicyList(any())).thenReturn(Arrays.asList(makePolicy(policy1, type),
130 makePolicy(policy2, type2), makePolicy(policy3, type)));
133 notifier = new MyNotifier(publisher);
135 ArgumentCaptor<PolicyPdpNotificationData> captor = ArgumentCaptor.forClass(PolicyPdpNotificationData.class);
137 // should have added policy1, policy2, policy1 (duplicate), policy3, but not
139 verify(deploy, times(4)).addData(captor.capture());
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());
148 assertEquals(policy2, data.getPolicyId());
149 assertEquals(type2, data.getPolicyType());
150 assertEquals("[sub #1 B 0]", data.getPdps().toString());
153 assertEquals(policy1, data.getPolicyId());
154 assertEquals(type, data.getPolicyType());
155 assertEquals("[sub #2 A 0]", data.getPdps().toString());
158 assertEquals(policy3, data.getPolicyId());
159 assertEquals(type, data.getPolicyType());
160 assertEquals("[sub #2 A 0]", data.getPdps().toString());
163 private ToscaPolicy makePolicy(ToscaPolicyIdentifier policyId, ToscaPolicyTypeIdentifier type) {
164 ToscaPolicy policy = new ToscaPolicy();
166 policy.setName(policyId.getName());
167 policy.setVersion(policyId.getVersion());
168 policy.setType(type.getName());
169 policy.setTypeVersion(type.getVersion());
174 private PdpGroup makeGroup(String name, PdpSubGroup... subgrps) {
175 final PdpGroup group = new PdpGroup();
178 group.setPdpSubgroups(Arrays.asList(subgrps));
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));
188 for (int x = 0; x < numPdps; ++x) {
190 pdp.setInstanceId(name + " " + x);
192 subgrp.getPdpInstances().add(pdp);
195 subgrp.setPolicies(Arrays.asList(policies));
201 public void testGetStatus() {
202 List<PolicyStatus> statusList = Arrays.asList(status1);
203 when(deploy.getStatus()).thenReturn(statusList);
205 assertSame(statusList, notifier.getStatus());
209 public void testGetStatusString() {
210 List<PolicyStatus> statusList = Arrays.asList(status1);
211 when(deploy.getStatus("a policy")).thenReturn(statusList);
213 assertSame(statusList, notifier.getStatus("a policy"));
217 public void testGetStatusToscaPolicyIdentifier() {
218 Optional<PolicyStatus> status = Optional.of(status1);
219 when(deploy.getStatus(policy1)).thenReturn(status);
221 assertSame(status, notifier.getStatus(policy1));
225 public void testAddDeploymentData() {
226 doAnswer(addStatus(1, status1, status2)).when(undeploy).removeData(any(), any());
228 PolicyPdpNotificationData data = makeData(policy1, PDP1, PDP2);
229 notifier.addDeploymentData(data);
231 verify(deploy).addData(data);
232 verify(undeploy).removeData(eq(data), any());
234 PolicyNotification notification = getNotification();
235 assertEquals(Arrays.asList(status1, status2), notification.getDeleted());
236 assertTrue(notification.getAdded().isEmpty());
240 public void testAddUndeploymentData() {
241 doAnswer(addStatus(1, status1, status2)).when(deploy).removeData(any(), any());
243 PolicyPdpNotificationData data = makeData(policy1, PDP1, PDP2);
244 notifier.addUndeploymentData(data);
246 verify(undeploy).addData(data);
247 verify(deploy).removeData(eq(data), any());
249 PolicyNotification notification = getNotification();
250 assertEquals(Arrays.asList(status1, status2), notification.getAdded());
251 assertTrue(notification.getDeleted().isEmpty());
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());
259 List<ToscaPolicyIdentifier> activePolicies = Arrays.asList(policy1, policy2);
260 notifier.processResponse(PDP1, activePolicies);
262 PolicyNotification notification = getNotification();
263 assertEquals(Arrays.asList(status1, status2), notification.getAdded());
264 assertEquals(Arrays.asList(status3, status4), notification.getDeleted());
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());
272 notifier.removePdp(PDP1);
274 PolicyNotification notification = getNotification();
275 assertEquals(Arrays.asList(status1, status2), notification.getAdded());
276 assertEquals(Arrays.asList(status3, status4), notification.getDeleted());
280 * Tests publish(), when the notification is empty.
283 public void testPublishEmpty() {
284 notifier.removePdp(PDP1);
286 verify(publisher, never()).enqueue(any());
290 * Tests publish(), when the notification is NOT empty.
293 public void testPublishNotEmpty() {
294 doAnswer(addStatus(1, status1, status2)).when(deploy).removePdp(eq(PDP1), any());
296 notifier.removePdp(PDP1);
298 verify(publisher).enqueue(any());
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);
306 verify(publisher, never()).enqueue(any());
310 * Creates an answer that adds status updates to a status list.
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
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));
326 * Gets the notification that was published.
328 * @return the notification that was published
330 private PolicyNotification getNotification() {
331 verify(publisher).enqueue(notifyCaptor.capture());
332 return notifyCaptor.getValue().get();
336 private class MyNotifier extends PolicyNotifier {
338 public MyNotifier(Publisher<PolicyNotification> publisher) throws PfModelException {
339 super(publisher, daoFactory);
343 protected PolicyDeployTracker makeDeploymentTracker() {
348 protected PolicyUndeployTracker makeUndeploymentTracker() {