2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2021, 2023 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 io.micrometer.core.instrument.simple.SimpleMeterRegistry;
35 import jakarta.ws.rs.core.Response.Status;
36 import java.util.Collections;
38 import org.junit.jupiter.api.AfterEach;
39 import org.junit.jupiter.api.BeforeEach;
40 import org.junit.jupiter.api.Test;
41 import org.junit.runner.RunWith;
42 import org.mockito.ArgumentCaptor;
43 import org.mockito.Captor;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 import org.mockito.junit.MockitoJUnitRunner;
47 import org.onap.policy.common.utils.services.Registry;
48 import org.onap.policy.models.base.PfModelException;
49 import org.onap.policy.models.base.PfModelRuntimeException;
50 import org.onap.policy.models.pap.concepts.PolicyNotification;
51 import org.onap.policy.models.pap.concepts.PolicyStatus;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
53 import org.onap.policy.pap.main.PapConstants;
54 import org.onap.policy.pap.main.PolicyPapRuntimeException;
55 import org.onap.policy.pap.main.comm.Publisher;
56 import org.onap.policy.pap.main.comm.QueueToken;
57 import org.onap.policy.pap.main.service.PolicyStatusService;
59 @RunWith(MockitoJUnitRunner.class)
60 class PolicyNotifierTest {
61 private static final String GROUP_A = "groupA";
62 private static final String PDP1 = "pdp-1";
63 private static final ToscaConceptIdentifier policy1 = new ToscaConceptIdentifier("policy1", "1.2.3");
64 private static final ToscaConceptIdentifier policy2 = new ToscaConceptIdentifier("policy2", "1.2.3");
67 private Publisher<PolicyNotification> publisher;
70 private PolicyStatusService policyStatusService;
73 private DeploymentStatus tracker;
76 ArgumentCaptor<QueueToken<PolicyNotification>> notifyCaptor;
78 private MyNotifier notifier;
80 AutoCloseable closeable;
83 * Creates various objects, including {@link #notifier}.
87 closeable = MockitoAnnotations.openMocks(this);
89 when(policyStatusService.getGroupPolicyStatus(anyString())).thenReturn(Collections.emptyList());
90 Registry.registerOrReplace(PapConstants.REG_METER_REGISTRY, new SimpleMeterRegistry());
91 notifier = new MyNotifier(publisher);
93 } catch (PfModelException e) {
94 throw new PolicyPapRuntimeException(e);
99 void tearDown() throws Exception {
104 void testProcessResponseString() {
105 Set<ToscaConceptIdentifier> expected = Set.of(policy1);
106 Set<ToscaConceptIdentifier> actual = Set.of(policy2);
108 // add a status to the notification when tracker.flush(notif) is called
109 doAnswer(invocation -> {
110 PolicyNotification notif = invocation.getArgument(0);
111 notif.getAdded().add(new PolicyStatus());
113 }).when(tracker).flush(any());
115 notifier.processResponse(PDP1, GROUP_A, expected, actual);
117 verify(tracker).loadByGroup(GROUP_A);
118 verify(tracker).completeDeploy(PDP1, expected, actual);
119 verify(tracker).flush(any());
121 verify(publisher).enqueue(any());
125 void testProcessResponseString_Ex() {
126 doThrow(new PfModelRuntimeException(Status.BAD_REQUEST, "expected exception")).when(tracker)
127 .loadByGroup(anyString());
129 assertThatCode(() -> notifier.processResponse(PDP1, GROUP_A, Set.of(), Set.of())).doesNotThrowAnyException();
133 * Tests publish(), when the notification is empty.
136 void testPublishEmpty() {
137 notifier.publish(new PolicyNotification());
138 verify(publisher, never()).enqueue(any());
142 * Tests publish(), when the notification is NOT empty.
145 void testPublishNotEmpty() {
146 PolicyNotification notif = new PolicyNotification();
147 notif.getAdded().add(new PolicyStatus());
149 notifier.publish(notif);
151 verify(publisher).enqueue(any());
155 void testMakeDeploymentTracker() {
156 // make real object, which will invoke the real makeXxx() methods
157 PolicyNotifier policyNotifier = new PolicyNotifier(policyStatusService);
158 policyNotifier.setPublisher(publisher);
159 policyNotifier.processResponse(PDP1, GROUP_A, Set.of(), Set.of());
161 verify(policyStatusService).getGroupPolicyStatus(GROUP_A);
165 private class MyNotifier extends PolicyNotifier {
167 public MyNotifier(Publisher<PolicyNotification> publisher) throws PfModelException {
168 super(policyStatusService);
169 super.setPublisher(publisher);
173 protected DeploymentStatus makeDeploymentTracker() {