2 * ============LICENSE_START=======================================================
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
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.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;
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;
60 * Super class for TestPdpGroupDeployProviderXxx classes.
62 public class ProviderSuper {
63 private static final Coder coder = new StandardCoder();
66 protected PolicyModelsProvider dao;
69 protected PolicyNotifier notifier;
73 * Used to capture input to dao.updatePdpGroups() and dao.createPdpGroups().
76 private ArgumentCaptor<List<PdpGroup>> updateCaptor;
78 protected Object lockit;
79 protected PdpModifyRequestMap reqmap;
80 protected PolicyModelsProviderFactoryWrapper daofact;
81 protected ToscaPolicy policy1;
85 * Configures DAO, captors, and various mocks.
88 public void setUp() throws Exception {
90 Registry.newRegistry();
92 MockitoAnnotations.openMocks(this);
94 reqmap = mock(PdpModifyRequestMap.class);
96 lockit = new Object();
97 daofact = mock(PolicyModelsProviderFactoryWrapper.class);
98 policy1 = loadPolicy("policy.json");
100 when(daofact.create()).thenReturn(dao);
102 List<PdpGroup> groups = loadGroups("groups.json");
104 when(dao.getFilteredPdpGroups(any())).thenReturn(groups);
106 when(dao.createPdpGroups(any())).thenAnswer(answer -> answer.getArgument(0, List.class));
107 when(dao.updatePdpGroups(any())).thenAnswer(answer -> answer.getArgument(0, List.class));
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);
115 protected void assertGroup(List<PdpGroup> groups, String name) {
116 PdpGroup group = groups.remove(0);
118 assertEquals(name, group.getName());
121 protected void assertUpdateIgnorePolicy(List<PdpUpdate> updates, String groupName, String pdpType, String pdpName) {
123 PdpUpdate update = updates.remove(0);
125 assertEquals(groupName, update.getPdpGroup());
126 assertEquals(pdpType, update.getPdpSubgroup());
127 assertEquals(pdpName, update.getName());
131 * Gets the input to the create() method.
133 * @return the input that was passed to the dao.updatePdpGroups() method
134 * @throws Exception if an error occurred
136 protected List<PdpGroup> getGroupCreates() throws Exception {
137 verify(dao).createPdpGroups(updateCaptor.capture());
139 return copyList(updateCaptor.getValue());
143 * Gets the input to the update() method.
145 * @return the input that was passed to the dao.updatePdpGroups() method
146 * @throws Exception if an error occurred
148 protected List<PdpGroup> getGroupUpdates() throws Exception {
149 verify(dao).updatePdpGroups(updateCaptor.capture());
151 return copyList(updateCaptor.getValue());
155 * Gets the state-changes that were added to the request map.
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
160 protected List<PdpStateChange> getStateChangeRequests(int count) {
161 ArgumentCaptor<PdpStateChange> captor = ArgumentCaptor.forClass(PdpStateChange.class);
163 verify(reqmap, times(count)).addRequest(any(), captor.capture());
165 return captor.getAllValues().stream().filter(req -> req != null).collect(Collectors.toList());
169 * Gets the updates that were added to the request map.
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
174 protected List<PdpUpdate> getUpdateRequests(int count) {
175 ArgumentCaptor<PdpUpdate> captor = ArgumentCaptor.forClass(PdpUpdate.class);
177 verify(reqmap, times(count)).addRequest(captor.capture(), any());
179 return captor.getAllValues().stream().filter(req -> req != null).collect(Collectors.toList());
183 * Copies a list and sorts it by group name.
185 * @param source source list to copy
186 * @return a copy of the source list
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()));
195 * Loads a list of groups.
197 * @param fileName name of the file from which to load
198 * @return a list of groups
200 protected List<PdpGroup> loadGroups(String fileName) {
201 return loadPdpGroups(fileName).getGroups();
207 * @param fileName name of the file from which to load
208 * @return a PdpGroups
210 protected PdpGroups loadPdpGroups(String fileName) {
211 return loadFile(fileName, PdpGroups.class);
217 * @param fileName name of the file from which to load
220 protected PdpGroup loadGroup(String fileName) {
221 return loadFile(fileName, PdpGroup.class);
225 * Loads a list of policies.
227 * @param fileName name of the file from which to load
228 * @return a list of policies
230 protected List<ToscaPolicy> loadPolicies(String fileName) {
231 return loadFile(fileName, PolicyList.class).policies;
237 * @param fileName name of the file from which to load
240 protected ToscaPolicy loadPolicy(String fileName) {
241 return loadFile(fileName, ToscaPolicy.class);
245 * Loads a policy type.
247 * @param fileName name of the file from which to load
248 * @return a policy type
250 protected ToscaPolicyType loadPolicyType(String fileName) {
251 return loadFile(fileName, ToscaPolicyType.class);
255 * Loads an object from a JSON file.
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
261 protected <T> T loadFile(String fileName, Class<T> clazz) {
262 File propFile = new File(ResourceUtils.getFilePath4Resource("simpleDeploy/" + fileName));
264 return coder.decode(propFile, clazz);
266 } catch (CoderException e) {
267 throw new RuntimeException(e);
272 * Verifies that an empty notification was published.
274 protected void checkEmptyNotification() {
275 ArgumentCaptor<PolicyNotification> captor = ArgumentCaptor.forClass(PolicyNotification.class);
276 verify(notifier).publish(captor.capture());
277 assertThat(captor.getValue().isEmpty()).isTrue();
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.
284 private static class PolicyList {
285 private List<ToscaPolicy> policies;