2 * ============LICENSE_START=======================================================
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
22 package org.onap.policy.pap.main.rest;
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;
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;
61 * Super class for TestPdpGroupDeployProviderXxx classes.
63 public class ProviderSuper {
64 private static final Coder coder = new StandardCoder();
65 public static final String DEFAULT_USER = "PAP_TEST";
68 protected PolicyModelsProvider dao;
71 protected PolicyNotifier notifier;
75 * Used to capture input to dao.updatePdpGroups() and dao.createPdpGroups().
78 private ArgumentCaptor<List<PdpGroup>> updateCaptor;
80 protected Object lockit;
81 protected PdpModifyRequestMap reqmap;
82 protected PolicyModelsProviderFactoryWrapper daofact;
83 protected ToscaPolicy policy1;
87 * Configures DAO, captors, and various mocks.
90 public void setUp() throws Exception {
92 Registry.newRegistry();
94 MockitoAnnotations.openMocks(this);
96 reqmap = mock(PdpModifyRequestMap.class);
98 lockit = new Object();
99 daofact = mock(PolicyModelsProviderFactoryWrapper.class);
100 policy1 = loadPolicy("policy.json");
102 when(daofact.create()).thenReturn(dao);
104 List<PdpGroup> groups = loadGroups("groups.json");
106 when(dao.getFilteredPdpGroups(any())).thenReturn(groups);
108 when(dao.createPdpGroups(any())).thenAnswer(answer -> answer.getArgument(0, List.class));
109 when(dao.updatePdpGroups(any())).thenAnswer(answer -> answer.getArgument(0, List.class));
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);
117 protected void assertGroup(List<PdpGroup> groups, String name) {
118 PdpGroup group = groups.remove(0);
120 assertEquals(name, group.getName());
123 protected void assertUpdateIgnorePolicy(List<PdpUpdate> updates, String groupName, String pdpType, String pdpName) {
125 PdpUpdate update = updates.remove(0);
127 assertEquals(groupName, update.getPdpGroup());
128 assertEquals(pdpType, update.getPdpSubgroup());
129 assertEquals(pdpName, update.getName());
133 * Gets the input to the create() method.
135 * @return the input that was passed to the dao.updatePdpGroups() method
136 * @throws Exception if an error occurred
138 protected List<PdpGroup> getGroupCreates() throws Exception {
139 verify(dao).createPdpGroups(updateCaptor.capture());
141 return copyList(updateCaptor.getValue());
145 * Gets the input to the update() method.
147 * @return the input that was passed to the dao.updatePdpGroups() method
148 * @throws Exception if an error occurred
150 protected List<PdpGroup> getGroupUpdates() throws Exception {
151 verify(dao).updatePdpGroups(updateCaptor.capture());
153 return copyList(updateCaptor.getValue());
157 * Gets the state-changes that were added to the request map.
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
162 protected List<PdpStateChange> getStateChangeRequests(int count) {
163 ArgumentCaptor<PdpStateChange> captor = ArgumentCaptor.forClass(PdpStateChange.class);
165 verify(reqmap, times(count)).addRequest(any(), captor.capture());
167 return captor.getAllValues().stream().filter(req -> req != null).collect(Collectors.toList());
171 * Gets the updates that were added to the request map.
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
176 protected List<PdpUpdate> getUpdateRequests(int count) {
177 ArgumentCaptor<PdpUpdate> captor = ArgumentCaptor.forClass(PdpUpdate.class);
179 verify(reqmap, times(count)).addRequest(captor.capture(), any());
181 return captor.getAllValues().stream().filter(req -> req != null).collect(Collectors.toList());
185 * Copies a list and sorts it by group name.
187 * @param source source list to copy
188 * @return a copy of the source list
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()));
197 * Loads a list of groups.
199 * @param fileName name of the file from which to load
200 * @return a list of groups
202 protected List<PdpGroup> loadGroups(String fileName) {
203 return loadPdpGroups(fileName).getGroups();
209 * @param fileName name of the file from which to load
210 * @return a PdpGroups
212 protected PdpGroups loadPdpGroups(String fileName) {
213 return loadFile(fileName, PdpGroups.class);
219 * @param fileName name of the file from which to load
222 protected PdpGroup loadGroup(String fileName) {
223 return loadFile(fileName, PdpGroup.class);
227 * Loads a list of policies.
229 * @param fileName name of the file from which to load
230 * @return a list of policies
232 protected List<ToscaPolicy> loadPolicies(String fileName) {
233 return loadFile(fileName, PolicyList.class).policies;
239 * @param fileName name of the file from which to load
242 protected ToscaPolicy loadPolicy(String fileName) {
243 return loadFile(fileName, ToscaPolicy.class);
247 * Loads a policy type.
249 * @param fileName name of the file from which to load
250 * @return a policy type
252 protected ToscaPolicyType loadPolicyType(String fileName) {
253 return loadFile(fileName, ToscaPolicyType.class);
257 * Loads an object from a JSON file.
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
263 protected <T> T loadFile(String fileName, Class<T> clazz) {
264 File propFile = new File(ResourceUtils.getFilePath4Resource("simpleDeploy/" + fileName));
266 return coder.decode(propFile, clazz);
268 } catch (CoderException e) {
269 throw new RuntimeException(e);
274 * Verifies that an empty notification was published.
276 protected void checkEmptyNotification() {
277 ArgumentCaptor<PolicyNotification> captor = ArgumentCaptor.forClass(PolicyNotification.class);
278 verify(notifier).publish(captor.capture());
279 assertThat(captor.getValue().isEmpty()).isTrue();
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.
286 private static class PolicyList {
287 private List<ToscaPolicy> policies;