69c7420521191338e0e10a3792bbe52856f71bed
[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-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
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
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=========================================================
21  */
22
23 package org.onap.policy.pap.main.notification;
24
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;
33
34 import io.micrometer.core.instrument.simple.SimpleMeterRegistry;
35 import jakarta.ws.rs.core.Response.Status;
36 import java.util.Collections;
37 import java.util.Set;
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;
58
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");
65
66     @Mock
67     private Publisher<PolicyNotification> publisher;
68
69     @Mock
70     private PolicyStatusService policyStatusService;
71
72     @Mock
73     private DeploymentStatus tracker;
74
75     @Captor
76     ArgumentCaptor<QueueToken<PolicyNotification>> notifyCaptor;
77
78     private MyNotifier notifier;
79
80     AutoCloseable closeable;
81
82     /**
83      * Creates various objects, including {@link #notifier}.
84      */
85     @BeforeEach
86     public void setUp() {
87         closeable = MockitoAnnotations.openMocks(this);
88         try {
89             when(policyStatusService.getGroupPolicyStatus(anyString())).thenReturn(Collections.emptyList());
90             Registry.registerOrReplace(PapConstants.REG_METER_REGISTRY, new SimpleMeterRegistry());
91             notifier = new MyNotifier(publisher);
92
93         } catch (PfModelException e) {
94             throw new PolicyPapRuntimeException(e);
95         }
96     }
97
98     @AfterEach
99     void tearDown() throws Exception {
100         closeable.close();
101     }
102
103     @Test
104     void testProcessResponseString() {
105         Set<ToscaConceptIdentifier> expected = Set.of(policy1);
106         Set<ToscaConceptIdentifier> actual = Set.of(policy2);
107
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());
112             return null;
113         }).when(tracker).flush(any());
114
115         notifier.processResponse(PDP1, GROUP_A, expected, actual);
116
117         verify(tracker).loadByGroup(GROUP_A);
118         verify(tracker).completeDeploy(PDP1, expected, actual);
119         verify(tracker).flush(any());
120
121         verify(publisher).enqueue(any());
122     }
123
124     @Test
125     void testProcessResponseString_Ex() {
126         doThrow(new PfModelRuntimeException(Status.BAD_REQUEST, "expected exception")).when(tracker)
127             .loadByGroup(anyString());
128
129         assertThatCode(() -> notifier.processResponse(PDP1, GROUP_A, Set.of(), Set.of())).doesNotThrowAnyException();
130     }
131
132     /**
133      * Tests publish(), when the notification is empty.
134      */
135     @Test
136     void testPublishEmpty() {
137         notifier.publish(new PolicyNotification());
138         verify(publisher, never()).enqueue(any());
139     }
140
141     /**
142      * Tests publish(), when the notification is NOT empty.
143      */
144     @Test
145     void testPublishNotEmpty() {
146         PolicyNotification notif = new PolicyNotification();
147         notif.getAdded().add(new PolicyStatus());
148
149         notifier.publish(notif);
150
151         verify(publisher).enqueue(any());
152     }
153
154     @Test
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());
160
161         verify(policyStatusService).getGroupPolicyStatus(GROUP_A);
162     }
163
164
165     private class MyNotifier extends PolicyNotifier {
166
167         public MyNotifier(Publisher<PolicyNotification> publisher) throws PfModelException {
168             super(policyStatusService);
169             super.setPublisher(publisher);
170         }
171
172         @Override
173         protected DeploymentStatus makeDeploymentTracker() {
174             return tracker;
175         }
176     }
177 }