2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2021 Nordix Foundation.
7 * Modifications Copyright (C) 2022 Bell Canada. All rights reserved.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.pap.main.notification;
25 import static org.assertj.core.api.Assertions.assertThatCode;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.anyString;
28 import static org.mockito.Mockito.doAnswer;
29 import static org.mockito.Mockito.doThrow;
30 import static org.mockito.Mockito.never;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
34 import java.util.Collections;
36 import javax.ws.rs.core.Response.Status;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.ArgumentCaptor;
41 import org.mockito.Captor;
42 import org.mockito.Mock;
43 import org.mockito.junit.MockitoJUnitRunner;
44 import org.onap.policy.models.base.PfModelException;
45 import org.onap.policy.models.base.PfModelRuntimeException;
46 import org.onap.policy.models.pap.concepts.PolicyNotification;
47 import org.onap.policy.models.pap.concepts.PolicyStatus;
48 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
49 import org.onap.policy.pap.main.PolicyPapRuntimeException;
50 import org.onap.policy.pap.main.comm.Publisher;
51 import org.onap.policy.pap.main.comm.QueueToken;
52 import org.onap.policy.pap.main.service.PolicyStatusService;
54 @RunWith(MockitoJUnitRunner.class)
55 public class PolicyNotifierTest {
56 private static final String GROUP_A = "groupA";
57 private static final String PDP1 = "pdp-1";
58 private static final ToscaConceptIdentifier policy1 = new ToscaConceptIdentifier("policy1", "1.2.3");
59 private static final ToscaConceptIdentifier policy2 = new ToscaConceptIdentifier("policy2", "1.2.3");
62 private Publisher<PolicyNotification> publisher;
65 private PolicyStatusService policyStatusService;
68 private DeploymentStatus tracker;
71 private PolicyStatus status1;
74 private PolicyStatus status2;
77 private PolicyStatus status3;
80 private PolicyStatus status4;
83 ArgumentCaptor<QueueToken<PolicyNotification>> notifyCaptor;
85 private MyNotifier notifier;
88 * Creates various objects, including {@link #notifier}.
93 when(policyStatusService.getGroupPolicyStatus(anyString())).thenReturn(Collections.emptyList());
95 notifier = new MyNotifier(publisher);
97 } catch (PfModelException e) {
98 throw new PolicyPapRuntimeException(e);
103 public void testProcessResponseString() throws PfModelException {
104 Set<ToscaConceptIdentifier> expected = Set.of(policy1);
105 Set<ToscaConceptIdentifier> actual = Set.of(policy2);
107 // add a status to the notification when tracker.flush(notif) is called
108 doAnswer(invocation -> {
109 PolicyNotification notif = invocation.getArgument(0);
110 notif.getAdded().add(new PolicyStatus());
112 }).when(tracker).flush(any());
114 notifier.processResponse(PDP1, GROUP_A, expected, actual);
116 verify(tracker).loadByGroup(GROUP_A);
117 verify(tracker).completeDeploy(PDP1, expected, actual);
118 verify(tracker).flush(any());
120 verify(publisher).enqueue(any());
124 public void testProcessResponseString_Ex() throws PfModelException {
125 doThrow(new PfModelRuntimeException(Status.BAD_REQUEST, "expected exception")).when(tracker)
126 .loadByGroup(anyString());
128 assertThatCode(() -> notifier.processResponse(PDP1, GROUP_A, Set.of(), Set.of())).doesNotThrowAnyException();
132 * Tests publish(), when the notification is empty.
135 public void testPublishEmpty() {
136 notifier.publish(new PolicyNotification());
137 verify(publisher, never()).enqueue(any());
141 * Tests publish(), when the notification is NOT empty.
144 public void testPublishNotEmpty() {
145 PolicyNotification notif = new PolicyNotification();
146 notif.getAdded().add(new PolicyStatus());
148 notifier.publish(notif);
150 verify(publisher).enqueue(any());
154 public void testMakeDeploymentTracker() throws PfModelException {
155 // make real object, which will invoke the real makeXxx() methods
156 PolicyNotifier policyNotifier = new PolicyNotifier(policyStatusService);
157 policyNotifier.setPublisher(publisher);
158 policyNotifier.processResponse(PDP1, GROUP_A, Set.of(), Set.of());
160 verify(policyStatusService).getGroupPolicyStatus(GROUP_A);
164 private class MyNotifier extends PolicyNotifier {
166 public MyNotifier(Publisher<PolicyNotification> publisher) throws PfModelException {
167 super(policyStatusService);
168 super.setPublisher(publisher);
172 protected DeploymentStatus makeDeploymentTracker() {