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