Java 17 / Spring 6 / Spring Boot 3 Upgrade
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / ProviderSuper.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
4  * ================================================================================
5  * Copyright (C) 2019-2022 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2021-2023 Nordix Foundation.
7  * Modifications Copyright (C) 2022-2023 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.rest;
24
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.junit.jupiter.api.Assertions.assertEquals;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.times;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
32
33 import io.micrometer.core.instrument.MeterRegistry;
34 import java.io.File;
35 import java.util.ArrayList;
36 import java.util.Comparator;
37 import java.util.List;
38 import java.util.Objects;
39 import java.util.stream.Collectors;
40 import org.junit.jupiter.api.AfterEach;
41 import org.junit.jupiter.api.BeforeEach;
42 import org.mockito.ArgumentCaptor;
43 import org.mockito.Captor;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 import org.onap.policy.common.utils.coder.Coder;
47 import org.onap.policy.common.utils.coder.CoderException;
48 import org.onap.policy.common.utils.coder.StandardCoder;
49 import org.onap.policy.common.utils.resources.ResourceUtils;
50 import org.onap.policy.common.utils.services.Registry;
51 import org.onap.policy.models.pap.concepts.PolicyNotification;
52 import org.onap.policy.models.pdp.concepts.PdpGroup;
53 import org.onap.policy.models.pdp.concepts.PdpGroups;
54 import org.onap.policy.models.pdp.concepts.PdpStateChange;
55 import org.onap.policy.models.pdp.concepts.PdpUpdate;
56 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
57 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
58 import org.onap.policy.pap.main.PapConstants;
59 import org.onap.policy.pap.main.comm.PdpModifyRequestMap;
60 import org.onap.policy.pap.main.notification.PolicyNotifier;
61 import org.onap.policy.pap.main.service.PdpGroupService;
62 import org.onap.policy.pap.main.service.PolicyAuditService;
63 import org.onap.policy.pap.main.service.PolicyStatusService;
64 import org.onap.policy.pap.main.service.ToscaServiceTemplateService;
65
66 /**
67  * Super class for TestPdpGroupDeployProviderXxx classes.
68  */
69 public class ProviderSuper {
70     private static final Coder coder = new StandardCoder();
71     public static final String DEFAULT_USER = "PAP_TEST";
72
73     @Mock
74     protected PdpGroupService pdpGroupService;
75
76     @Mock
77     protected PolicyStatusService policyStatusService;
78
79     @Mock
80     protected PolicyAuditService policyAuditService;
81
82     @Mock
83     protected ToscaServiceTemplateService toscaService;
84
85     @Mock
86     protected PolicyNotifier notifier;
87
88     /**
89      * Used to capture input to dao.updatePdpGroups() and dao.createPdpGroups().
90      */
91     @Captor
92     private ArgumentCaptor<List<PdpGroup>> updateCaptor;
93
94     protected Object lockit;
95     protected PdpModifyRequestMap reqmap;
96     protected ToscaPolicy policy1;
97     protected MeterRegistry meterRegistry;
98
99     AutoCloseable closeable;
100
101     /**
102      * Configures DAO, captors, and various mocks.
103      */
104     @BeforeEach
105     public void setUp() throws Exception {
106
107         Registry.newRegistry();
108
109         closeable = MockitoAnnotations.openMocks(this);
110
111         reqmap = mock(PdpModifyRequestMap.class);
112
113         lockit = new Object();
114         policy1 = loadPolicy("policy.json");
115
116         meterRegistry = mock(MeterRegistry.class);
117
118         List<PdpGroup> groups = loadGroups("groups.json");
119
120         when(pdpGroupService.getFilteredPdpGroups(any())).thenReturn(groups);
121
122         when(pdpGroupService.createPdpGroups(any())).thenAnswer(answer -> answer.getArgument(0, List.class));
123         when(pdpGroupService.updatePdpGroups(any())).thenAnswer(answer -> answer.getArgument(0, List.class));
124
125         Registry.register(PapConstants.REG_PDP_MODIFY_LOCK, lockit);
126         Registry.register(PapConstants.REG_PDP_MODIFY_MAP, reqmap);
127         Registry.register(PapConstants.REG_METER_REGISTRY, meterRegistry);
128
129     }
130
131     @AfterEach
132     public void tearDown() throws Exception {
133         closeable.close();
134     }
135
136     /**
137      * Initialize services to the provider for tests.
138      *
139      * @param prov the provider
140      */
141     public void initialize(ProviderBase prov) {
142         prov.setPdpGroupService(pdpGroupService);
143         prov.setPolicyAuditService(policyAuditService);
144         prov.setPolicyStatusService(policyStatusService);
145         prov.setToscaService(toscaService);
146         prov.setPolicyNotifier(notifier);
147         prov.initialize();
148     }
149
150     protected void assertGroup(List<PdpGroup> groups, String name) {
151         PdpGroup group = groups.remove(0);
152
153         assertEquals(name, group.getName());
154     }
155
156     protected void assertUpdateIgnorePolicy(List<PdpUpdate> updates, String groupName, String pdpType, String pdpName) {
157
158         PdpUpdate update = updates.remove(0);
159
160         assertEquals(groupName, update.getPdpGroup());
161         assertEquals(pdpType, update.getPdpSubgroup());
162         assertEquals(pdpName, update.getName());
163     }
164
165     /**
166      * Gets the input to the create() method.
167      *
168      * @return the input that was passed to the dao.updatePdpGroups() method
169      */
170     protected List<PdpGroup> getGroupCreates() {
171         verify(pdpGroupService).createPdpGroups(updateCaptor.capture());
172
173         return copyList(updateCaptor.getValue());
174     }
175
176     /**
177      * Gets the input to the update() method.
178      *
179      * @return the input that was passed to the dao.updatePdpGroups() method
180      */
181     protected List<PdpGroup> getGroupUpdates() {
182         verify(pdpGroupService).updatePdpGroups(updateCaptor.capture());
183
184         return copyList(updateCaptor.getValue());
185     }
186
187     /**
188      * Gets the state-changes that were added to the request map.
189      *
190      * @param count the number of times the method is expected to have been called
191      * @return the state-changes that were added to the request map
192      */
193     protected List<PdpStateChange> getStateChangeRequests(int count) {
194         ArgumentCaptor<PdpStateChange> captor = ArgumentCaptor.forClass(PdpStateChange.class);
195
196         verify(reqmap, times(count)).addRequest(any(), captor.capture());
197
198         return captor.getAllValues().stream().filter(Objects::nonNull).collect(Collectors.toList());
199     }
200
201     /**
202      * Gets the updates that were added to the request map.
203      *
204      * @param count the number of times the method is expected to have been called
205      * @return the updates that were added to the request map
206      */
207     protected List<PdpUpdate> getUpdateRequests(int count) {
208         ArgumentCaptor<PdpUpdate> captor = ArgumentCaptor.forClass(PdpUpdate.class);
209
210         verify(reqmap, times(count)).addRequest(captor.capture(), any());
211
212         return captor.getAllValues().stream().filter(Objects::nonNull).collect(Collectors.toList());
213     }
214
215     /**
216      * Copies a list and sorts it by group name.
217      *
218      * @param source source list to copy
219      * @return a copy of the source list
220      */
221     private List<PdpGroup> copyList(List<PdpGroup> source) {
222         List<PdpGroup> newlst = new ArrayList<>(source);
223         newlst.sort(Comparator.comparing(PdpGroup::getName));
224         return newlst;
225     }
226
227     /**
228      * Loads a list of groups.
229      *
230      * @param fileName name of the file from which to load
231      * @return a list of groups
232      */
233     protected List<PdpGroup> loadGroups(String fileName) {
234         return loadPdpGroups(fileName).getGroups();
235     }
236
237     /**
238      * Loads a PdpGroups.
239      *
240      * @param fileName name of the file from which to load
241      * @return a PdpGroups
242      */
243     protected PdpGroups loadPdpGroups(String fileName) {
244         return loadFile(fileName, PdpGroups.class);
245     }
246
247     /**
248      * Loads a group.
249      *
250      * @param fileName name of the file from which to load
251      * @return a group
252      */
253     protected PdpGroup loadGroup(String fileName) {
254         return loadFile(fileName, PdpGroup.class);
255     }
256
257     /**
258      * Loads a list of policies.
259      *
260      * @param fileName name of the file from which to load
261      * @return a list of policies
262      */
263     protected List<ToscaPolicy> loadPolicies(String fileName) {
264         return loadFile(fileName, PolicyList.class).policies;
265     }
266
267     /**
268      * Loads a policy.
269      *
270      * @param fileName name of the file from which to load
271      * @return a policy
272      */
273     protected ToscaPolicy loadPolicy(String fileName) {
274         return loadFile(fileName, ToscaPolicy.class);
275     }
276
277     /**
278      * Loads a policy type.
279      *
280      * @param fileName name of the file from which to load
281      * @return a policy type
282      */
283     protected ToscaPolicyType loadPolicyType(String fileName) {
284         return loadFile(fileName, ToscaPolicyType.class);
285     }
286
287     /**
288      * Loads an object from a JSON file.
289      *
290      * @param fileName name of the file from which to load
291      * @param clazz    the class of the object to be loaded
292      * @return the object that was loaded from the file
293      */
294     protected <T> T loadFile(String fileName, Class<T> clazz) {
295         File propFile = new File(ResourceUtils.getFilePath4Resource("simpleDeploy/" + fileName));
296         try {
297             return coder.decode(propFile, clazz);
298
299         } catch (CoderException e) {
300             throw new RuntimeException(e);
301         }
302     }
303
304     /**
305      * Verifies that an empty notification was published.
306      */
307     protected void checkEmptyNotification() {
308         ArgumentCaptor<PolicyNotification> captor = ArgumentCaptor.forClass(PolicyNotification.class);
309         verify(notifier).publish(captor.capture());
310         assertThat(captor.getValue().isEmpty()).isTrue();
311     }
312
313     /**
314      * Wraps a list of policies. The decoder doesn't work with generic lists, so we wrap
315      * the list and decode it into the wrapper before extracting the list contents.
316      */
317     private static class PolicyList {
318         private List<ToscaPolicy> policies;
319     }
320 }