48e3f57ee7069d578f64d2730533f78ab6ea045a
[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 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 java.util.Collections;
36 import java.util.Set;
37 import javax.ws.rs.core.Response.Status;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.ArgumentCaptor;
42 import org.mockito.Captor;
43 import org.mockito.Mock;
44 import org.mockito.junit.MockitoJUnitRunner;
45 import org.onap.policy.common.utils.services.Registry;
46 import org.onap.policy.models.base.PfModelException;
47 import org.onap.policy.models.base.PfModelRuntimeException;
48 import org.onap.policy.models.pap.concepts.PolicyNotification;
49 import org.onap.policy.models.pap.concepts.PolicyStatus;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
51 import org.onap.policy.pap.main.PapConstants;
52 import org.onap.policy.pap.main.PolicyPapRuntimeException;
53 import org.onap.policy.pap.main.comm.Publisher;
54 import org.onap.policy.pap.main.comm.QueueToken;
55 import org.onap.policy.pap.main.service.PolicyStatusService;
56
57 @RunWith(MockitoJUnitRunner.class)
58 public class PolicyNotifierTest {
59     private static final String GROUP_A = "groupA";
60     private static final String PDP1 = "pdp-1";
61     private static final ToscaConceptIdentifier policy1 = new ToscaConceptIdentifier("policy1", "1.2.3");
62     private static final ToscaConceptIdentifier policy2 = new ToscaConceptIdentifier("policy2", "1.2.3");
63
64     @Mock
65     private Publisher<PolicyNotification> publisher;
66
67     @Mock
68     private PolicyStatusService policyStatusService;
69
70     @Mock
71     private DeploymentStatus tracker;
72
73     @Mock
74     private PolicyStatus status1;
75
76     @Mock
77     private PolicyStatus status2;
78
79     @Mock
80     private PolicyStatus status3;
81
82     @Mock
83     private PolicyStatus status4;
84
85     @Captor
86     ArgumentCaptor<QueueToken<PolicyNotification>> notifyCaptor;
87
88     private MyNotifier notifier;
89
90     /**
91      * Creates various objects, including {@link #notifier}.
92      */
93     @Before
94     public void setUp() {
95         try {
96             when(policyStatusService.getGroupPolicyStatus(anyString())).thenReturn(Collections.emptyList());
97             Registry.registerOrReplace(PapConstants.REG_METER_REGISTRY, new SimpleMeterRegistry());
98             notifier = new MyNotifier(publisher);
99
100         } catch (PfModelException e) {
101             throw new PolicyPapRuntimeException(e);
102         }
103     }
104
105     @Test
106     public void testProcessResponseString() throws PfModelException {
107         Set<ToscaConceptIdentifier> expected = Set.of(policy1);
108         Set<ToscaConceptIdentifier> actual = Set.of(policy2);
109
110         // add a status to the notification when tracker.flush(notif) is called
111         doAnswer(invocation -> {
112             PolicyNotification notif = invocation.getArgument(0);
113             notif.getAdded().add(new PolicyStatus());
114             return null;
115         }).when(tracker).flush(any());
116
117         notifier.processResponse(PDP1, GROUP_A, expected, actual);
118
119         verify(tracker).loadByGroup(GROUP_A);
120         verify(tracker).completeDeploy(PDP1, expected, actual);
121         verify(tracker).flush(any());
122
123         verify(publisher).enqueue(any());
124     }
125
126     @Test
127     public void testProcessResponseString_Ex() throws PfModelException {
128         doThrow(new PfModelRuntimeException(Status.BAD_REQUEST, "expected exception")).when(tracker)
129             .loadByGroup(anyString());
130
131         assertThatCode(() -> notifier.processResponse(PDP1, GROUP_A, Set.of(), Set.of())).doesNotThrowAnyException();
132     }
133
134     /**
135      * Tests publish(), when the notification is empty.
136      */
137     @Test
138     public void testPublishEmpty() {
139         notifier.publish(new PolicyNotification());
140         verify(publisher, never()).enqueue(any());
141     }
142
143     /**
144      * Tests publish(), when the notification is NOT empty.
145      */
146     @Test
147     public void testPublishNotEmpty() {
148         PolicyNotification notif = new PolicyNotification();
149         notif.getAdded().add(new PolicyStatus());
150
151         notifier.publish(notif);
152
153         verify(publisher).enqueue(any());
154     }
155
156     @Test
157     public void testMakeDeploymentTracker() throws PfModelException {
158         // make real object, which will invoke the real makeXxx() methods
159         PolicyNotifier policyNotifier = new PolicyNotifier(policyStatusService);
160         policyNotifier.setPublisher(publisher);
161         policyNotifier.processResponse(PDP1, GROUP_A, Set.of(), Set.of());
162
163         verify(policyStatusService).getGroupPolicyStatus(GROUP_A);
164     }
165
166
167     private class MyNotifier extends PolicyNotifier {
168
169         public MyNotifier(Publisher<PolicyNotification> publisher) throws PfModelException {
170             super(policyStatusService);
171             super.setPublisher(publisher);
172         }
173
174         @Override
175         protected DeploymentStatus makeDeploymentTracker() {
176             return tracker;
177         }
178     }
179 }