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