2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.pap.main.rest;
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;
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;
58 * Super class for TestPdpGroupDeployProviderXxx classes.
60 public class ProviderSuper {
61 private static final Coder coder = new StandardCoder();
64 protected PolicyModelsProvider dao;
67 protected PolicyNotifier notifier;
71 * Used to capture input to dao.updatePdpGroups() and dao.createPdpGroups().
74 private ArgumentCaptor<List<PdpGroup>> updateCaptor;
76 protected Object lockit;
77 protected PdpModifyRequestMap reqmap;
78 protected PolicyModelsProviderFactoryWrapper daofact;
79 protected ToscaPolicy policy1;
83 * Configures DAO, captors, and various mocks.
86 public void setUp() throws Exception {
88 Registry.newRegistry();
90 MockitoAnnotations.initMocks(this);
92 reqmap = mock(PdpModifyRequestMap.class);
94 lockit = new Object();
95 daofact = mock(PolicyModelsProviderFactoryWrapper.class);
96 policy1 = loadPolicy("policy.json");
98 when(daofact.create()).thenReturn(dao);
100 List<PdpGroup> groups = loadGroups("groups.json");
102 when(dao.getFilteredPdpGroups(any())).thenReturn(groups);
104 when(dao.createPdpGroups(any())).thenAnswer(answer -> answer.getArgument(0, List.class));
105 when(dao.updatePdpGroups(any())).thenAnswer(answer -> answer.getArgument(0, List.class));
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);
113 protected void assertGroup(List<PdpGroup> groups, String name) {
114 PdpGroup group = groups.remove(0);
116 assertEquals(name, group.getName());
119 protected void assertUpdateIgnorePolicy(List<PdpUpdate> updates, String groupName, String pdpType, String pdpName) {
121 PdpUpdate update = updates.remove(0);
123 assertEquals(groupName, update.getPdpGroup());
124 assertEquals(pdpType, update.getPdpSubgroup());
125 assertEquals(pdpName, update.getName());
129 * Gets the input to the create() method.
131 * @return the input that was passed to the dao.updatePdpGroups() method
132 * @throws Exception if an error occurred
134 protected List<PdpGroup> getGroupCreates() throws Exception {
135 verify(dao).createPdpGroups(updateCaptor.capture());
137 return copyList(updateCaptor.getValue());
141 * Gets the input to the update() method.
143 * @return the input that was passed to the dao.updatePdpGroups() method
144 * @throws Exception if an error occurred
146 protected List<PdpGroup> getGroupUpdates() throws Exception {
147 verify(dao).updatePdpGroups(updateCaptor.capture());
149 return copyList(updateCaptor.getValue());
153 * Gets the state-changes that were added to the request map.
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
158 protected List<PdpStateChange> getStateChangeRequests(int count) {
159 ArgumentCaptor<PdpStateChange> captor = ArgumentCaptor.forClass(PdpStateChange.class);
161 verify(reqmap, times(count)).addRequest(any(), captor.capture());
163 return captor.getAllValues().stream().filter(req -> req != null).collect(Collectors.toList());
167 * Gets the updates that were added to the request map.
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
172 protected List<PdpUpdate> getUpdateRequests(int count) {
173 ArgumentCaptor<PdpUpdate> captor = ArgumentCaptor.forClass(PdpUpdate.class);
175 verify(reqmap, times(count)).addRequest(captor.capture(), any());
177 return captor.getAllValues().stream().filter(req -> req != null).collect(Collectors.toList());
181 * Copies a list and sorts it by group name.
183 * @param source source list to copy
184 * @return a copy of the source list
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()));
193 * Loads a list of groups.
195 * @param fileName name of the file from which to load
196 * @return a list of groups
198 protected List<PdpGroup> loadGroups(String fileName) {
199 return loadPdpGroups(fileName).getGroups();
205 * @param fileName name of the file from which to load
206 * @return a PdpGroups
208 protected PdpGroups loadPdpGroups(String fileName) {
209 return loadFile(fileName, PdpGroups.class);
215 * @param fileName name of the file from which to load
218 protected PdpGroup loadGroup(String fileName) {
219 return loadFile(fileName, PdpGroup.class);
223 * Loads a list of policies.
225 * @param fileName name of the file from which to load
226 * @return a list of policies
228 protected List<ToscaPolicy> loadPolicies(String fileName) {
229 return loadFile(fileName, PolicyList.class).policies;
235 * @param fileName name of the file from which to load
238 protected ToscaPolicy loadPolicy(String fileName) {
239 return loadFile(fileName, ToscaPolicy.class);
243 * Loads a policy type.
245 * @param fileName name of the file from which to load
246 * @return a policy type
248 protected ToscaPolicyType loadPolicyType(String fileName) {
249 return loadFile(fileName, ToscaPolicyType.class);
253 * Loads an object from a JSON file.
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
259 protected <T> T loadFile(String fileName, Class<T> clazz) {
260 File propFile = new File(ResourceUtils.getFilePath4Resource("simpleDeploy/" + fileName));
262 return coder.decode(propFile, clazz);
264 } catch (CoderException e) {
265 throw new RuntimeException(e);
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.
273 private static class PolicyList {
274 private List<ToscaPolicy> policies;