Merge "PolicyAudit creation when deploy/undeploy triggered."
[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-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2021 Nordix Foundation.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.main.rest;
23
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.junit.Assert.assertEquals;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.Mockito.mock;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31
32 import java.io.File;
33 import java.util.ArrayList;
34 import java.util.Collections;
35 import java.util.List;
36 import java.util.stream.Collectors;
37 import org.junit.Before;
38 import org.mockito.ArgumentCaptor;
39 import org.mockito.Captor;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.onap.policy.common.utils.coder.Coder;
43 import org.onap.policy.common.utils.coder.CoderException;
44 import org.onap.policy.common.utils.coder.StandardCoder;
45 import org.onap.policy.common.utils.resources.ResourceUtils;
46 import org.onap.policy.common.utils.services.Registry;
47 import org.onap.policy.models.pap.concepts.PolicyNotification;
48 import org.onap.policy.models.pdp.concepts.PdpGroup;
49 import org.onap.policy.models.pdp.concepts.PdpGroups;
50 import org.onap.policy.models.pdp.concepts.PdpStateChange;
51 import org.onap.policy.models.pdp.concepts.PdpUpdate;
52 import org.onap.policy.models.provider.PolicyModelsProvider;
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.PolicyModelsProviderFactoryWrapper;
57 import org.onap.policy.pap.main.comm.PdpModifyRequestMap;
58 import org.onap.policy.pap.main.notification.PolicyNotifier;
59
60 /**
61  * Super class for TestPdpGroupDeployProviderXxx classes.
62  */
63 public class ProviderSuper {
64     private static final Coder coder = new StandardCoder();
65     public static final String DEFAULT_USER = "PAP_TEST";
66
67     @Mock
68     protected PolicyModelsProvider dao;
69
70     @Mock
71     protected PolicyNotifier notifier;
72
73
74     /**
75      * Used to capture input to dao.updatePdpGroups() and dao.createPdpGroups().
76      */
77     @Captor
78     private ArgumentCaptor<List<PdpGroup>> updateCaptor;
79
80     protected Object lockit;
81     protected PdpModifyRequestMap reqmap;
82     protected PolicyModelsProviderFactoryWrapper daofact;
83     protected ToscaPolicy policy1;
84
85
86     /**
87      * Configures DAO, captors, and various mocks.
88      */
89     @Before
90     public void setUp() throws Exception {
91
92         Registry.newRegistry();
93
94         MockitoAnnotations.openMocks(this);
95
96         reqmap = mock(PdpModifyRequestMap.class);
97
98         lockit = new Object();
99         daofact = mock(PolicyModelsProviderFactoryWrapper.class);
100         policy1 = loadPolicy("policy.json");
101
102         when(daofact.create()).thenReturn(dao);
103
104         List<PdpGroup> groups = loadGroups("groups.json");
105
106         when(dao.getFilteredPdpGroups(any())).thenReturn(groups);
107
108         when(dao.createPdpGroups(any())).thenAnswer(answer -> answer.getArgument(0, List.class));
109         when(dao.updatePdpGroups(any())).thenAnswer(answer -> answer.getArgument(0, List.class));
110
111         Registry.register(PapConstants.REG_PDP_MODIFY_LOCK, lockit);
112         Registry.register(PapConstants.REG_PDP_MODIFY_MAP, reqmap);
113         Registry.register(PapConstants.REG_PAP_DAO_FACTORY, daofact);
114         Registry.register(PapConstants.REG_POLICY_NOTIFIER, notifier);
115     }
116
117     protected void assertGroup(List<PdpGroup> groups, String name) {
118         PdpGroup group = groups.remove(0);
119
120         assertEquals(name, group.getName());
121     }
122
123     protected void assertUpdateIgnorePolicy(List<PdpUpdate> updates, String groupName, String pdpType, String pdpName) {
124
125         PdpUpdate update = updates.remove(0);
126
127         assertEquals(groupName, update.getPdpGroup());
128         assertEquals(pdpType, update.getPdpSubgroup());
129         assertEquals(pdpName, update.getName());
130     }
131
132     /**
133      * Gets the input to the create() method.
134      *
135      * @return the input that was passed to the dao.updatePdpGroups() method
136      * @throws Exception if an error occurred
137      */
138     protected List<PdpGroup> getGroupCreates() throws Exception {
139         verify(dao).createPdpGroups(updateCaptor.capture());
140
141         return copyList(updateCaptor.getValue());
142     }
143
144     /**
145      * Gets the input to the update() method.
146      *
147      * @return the input that was passed to the dao.updatePdpGroups() method
148      * @throws Exception if an error occurred
149      */
150     protected List<PdpGroup> getGroupUpdates() throws Exception {
151         verify(dao).updatePdpGroups(updateCaptor.capture());
152
153         return copyList(updateCaptor.getValue());
154     }
155
156     /**
157      * Gets the state-changes that were added to the request map.
158      *
159      * @param count the number of times the method is expected to have been called
160      * @return the state-changes that were added to the request map
161      */
162     protected List<PdpStateChange> getStateChangeRequests(int count) {
163         ArgumentCaptor<PdpStateChange> captor = ArgumentCaptor.forClass(PdpStateChange.class);
164
165         verify(reqmap, times(count)).addRequest(any(), captor.capture());
166
167         return captor.getAllValues().stream().filter(req -> req != null).collect(Collectors.toList());
168     }
169
170     /**
171      * Gets the updates that were added to the request map.
172      *
173      * @param count the number of times the method is expected to have been called
174      * @return the updates that were added to the request map
175      */
176     protected List<PdpUpdate> getUpdateRequests(int count) {
177         ArgumentCaptor<PdpUpdate> captor = ArgumentCaptor.forClass(PdpUpdate.class);
178
179         verify(reqmap, times(count)).addRequest(captor.capture(), any());
180
181         return captor.getAllValues().stream().filter(req -> req != null).collect(Collectors.toList());
182     }
183
184     /**
185      * Copies a list and sorts it by group name.
186      *
187      * @param source source list to copy
188      * @return a copy of the source list
189      */
190     private List<PdpGroup> copyList(List<PdpGroup> source) {
191         List<PdpGroup> newlst = new ArrayList<>(source);
192         Collections.sort(newlst, (left, right) -> left.getName().compareTo(right.getName()));
193         return newlst;
194     }
195
196     /**
197      * Loads a list of groups.
198      *
199      * @param fileName name of the file from which to load
200      * @return a list of groups
201      */
202     protected List<PdpGroup> loadGroups(String fileName) {
203         return loadPdpGroups(fileName).getGroups();
204     }
205
206     /**
207      * Loads a PdpGroups.
208      *
209      * @param fileName name of the file from which to load
210      * @return a PdpGroups
211      */
212     protected PdpGroups loadPdpGroups(String fileName) {
213         return loadFile(fileName, PdpGroups.class);
214     }
215
216     /**
217      * Loads a group.
218      *
219      * @param fileName name of the file from which to load
220      * @return a group
221      */
222     protected PdpGroup loadGroup(String fileName) {
223         return loadFile(fileName, PdpGroup.class);
224     }
225
226     /**
227      * Loads a list of policies.
228      *
229      * @param fileName name of the file from which to load
230      * @return a list of policies
231      */
232     protected List<ToscaPolicy> loadPolicies(String fileName) {
233         return loadFile(fileName, PolicyList.class).policies;
234     }
235
236     /**
237      * Loads a policy.
238      *
239      * @param fileName name of the file from which to load
240      * @return a policy
241      */
242     protected ToscaPolicy loadPolicy(String fileName) {
243         return loadFile(fileName, ToscaPolicy.class);
244     }
245
246     /**
247      * Loads a policy type.
248      *
249      * @param fileName name of the file from which to load
250      * @return a policy type
251      */
252     protected ToscaPolicyType loadPolicyType(String fileName) {
253         return loadFile(fileName, ToscaPolicyType.class);
254     }
255
256     /**
257      * Loads an object from a JSON file.
258      *
259      * @param fileName name of the file from which to load
260      * @param clazz the class of the object to be loaded
261      * @return the object that was loaded from the file
262      */
263     protected <T> T loadFile(String fileName, Class<T> clazz) {
264         File propFile = new File(ResourceUtils.getFilePath4Resource("simpleDeploy/" + fileName));
265         try {
266             return coder.decode(propFile, clazz);
267
268         } catch (CoderException e) {
269             throw new RuntimeException(e);
270         }
271     }
272
273     /**
274      * Verifies that an empty notification was published.
275      */
276     protected void checkEmptyNotification() {
277         ArgumentCaptor<PolicyNotification> captor = ArgumentCaptor.forClass(PolicyNotification.class);
278         verify(notifier).publish(captor.capture());
279         assertThat(captor.getValue().isEmpty()).isTrue();
280     }
281
282     /**
283      * Wraps a list of policies. The decoder doesn't work with generic lists, so we wrap
284      * the list and decode it into the wrapper before extracting the list contents.
285      */
286     private static class PolicyList {
287         private List<ToscaPolicy> policies;
288     }
289 }