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