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