Move PAP database provider to spring boot default
[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 java.util.Collections;
35 import java.util.Set;
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;
53
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");
60
61     @Mock
62     private Publisher<PolicyNotification> publisher;
63
64     @Mock
65     private PolicyStatusService policyStatusService;
66
67     @Mock
68     private DeploymentStatus tracker;
69
70     @Mock
71     private PolicyStatus status1;
72
73     @Mock
74     private PolicyStatus status2;
75
76     @Mock
77     private PolicyStatus status3;
78
79     @Mock
80     private PolicyStatus status4;
81
82     @Captor
83     ArgumentCaptor<QueueToken<PolicyNotification>> notifyCaptor;
84
85     private MyNotifier notifier;
86
87     /**
88      * Creates various objects, including {@link #notifier}.
89      */
90     @Before
91     public void setUp() {
92         try {
93             when(policyStatusService.getGroupPolicyStatus(anyString())).thenReturn(Collections.emptyList());
94
95             notifier = new MyNotifier(publisher);
96
97         } catch (PfModelException e) {
98             throw new PolicyPapRuntimeException(e);
99         }
100     }
101
102     @Test
103     public void testProcessResponseString() throws PfModelException {
104         Set<ToscaConceptIdentifier> expected = Set.of(policy1);
105         Set<ToscaConceptIdentifier> actual = Set.of(policy2);
106
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());
111             return null;
112         }).when(tracker).flush(any());
113
114         notifier.processResponse(PDP1, GROUP_A, expected, actual);
115
116         verify(tracker).loadByGroup(GROUP_A);
117         verify(tracker).completeDeploy(PDP1, expected, actual);
118         verify(tracker).flush(any());
119
120         verify(publisher).enqueue(any());
121     }
122
123     @Test
124     public void testProcessResponseString_Ex() throws PfModelException {
125         doThrow(new PfModelRuntimeException(Status.BAD_REQUEST, "expected exception")).when(tracker)
126             .loadByGroup(anyString());
127
128         assertThatCode(() -> notifier.processResponse(PDP1, GROUP_A, Set.of(), Set.of())).doesNotThrowAnyException();
129     }
130
131     /**
132      * Tests publish(), when the notification is empty.
133      */
134     @Test
135     public void testPublishEmpty() {
136         notifier.publish(new PolicyNotification());
137         verify(publisher, never()).enqueue(any());
138     }
139
140     /**
141      * Tests publish(), when the notification is NOT empty.
142      */
143     @Test
144     public void testPublishNotEmpty() {
145         PolicyNotification notif = new PolicyNotification();
146         notif.getAdded().add(new PolicyStatus());
147
148         notifier.publish(notif);
149
150         verify(publisher).enqueue(any());
151     }
152
153     @Test
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());
159
160         verify(policyStatusService).getGroupPolicyStatus(GROUP_A);
161     }
162
163
164     private class MyNotifier extends PolicyNotifier {
165
166         public MyNotifier(Publisher<PolicyNotification> publisher) throws PfModelException {
167             super(policyStatusService);
168             super.setPublisher(publisher);
169         }
170
171         @Override
172         protected DeploymentStatus makeDeploymentTracker() {
173             return tracker;
174         }
175     }
176 }