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