2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2022 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2021 Nordix Foundation.
7 * Modifications Copyright (C) 2022 Bell Canada. All rights reserved.
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.pap.main.rest;
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.junit.Assert.assertEquals;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.mock;
29 import static org.mockito.Mockito.times;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
34 import java.util.ArrayList;
35 import java.util.Collections;
36 import java.util.List;
37 import java.util.stream.Collectors;
38 import org.junit.Before;
39 import org.mockito.ArgumentCaptor;
40 import org.mockito.Captor;
41 import org.mockito.Mock;
42 import org.mockito.MockitoAnnotations;
43 import org.onap.policy.common.utils.coder.Coder;
44 import org.onap.policy.common.utils.coder.CoderException;
45 import org.onap.policy.common.utils.coder.StandardCoder;
46 import org.onap.policy.common.utils.resources.ResourceUtils;
47 import org.onap.policy.common.utils.services.Registry;
48 import org.onap.policy.models.pap.concepts.PolicyNotification;
49 import org.onap.policy.models.pdp.concepts.PdpGroup;
50 import org.onap.policy.models.pdp.concepts.PdpGroups;
51 import org.onap.policy.models.pdp.concepts.PdpStateChange;
52 import org.onap.policy.models.pdp.concepts.PdpUpdate;
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.comm.PdpModifyRequestMap;
57 import org.onap.policy.pap.main.notification.PolicyNotifier;
58 import org.onap.policy.pap.main.service.PdpGroupService;
59 import org.onap.policy.pap.main.service.PolicyAuditService;
60 import org.onap.policy.pap.main.service.PolicyStatusService;
61 import org.onap.policy.pap.main.service.ToscaServiceTemplateService;
64 * Super class for TestPdpGroupDeployProviderXxx classes.
66 public class ProviderSuper {
67 private static final Coder coder = new StandardCoder();
68 public static final String DEFAULT_USER = "PAP_TEST";
71 protected PdpGroupService pdpGroupService;
74 protected PolicyStatusService policyStatusService;
77 protected PolicyAuditService policyAuditService;
80 protected ToscaServiceTemplateService toscaService;
83 protected PolicyNotifier notifier;
86 * Used to capture input to dao.updatePdpGroups() and dao.createPdpGroups().
89 private ArgumentCaptor<List<PdpGroup>> updateCaptor;
91 protected Object lockit;
92 protected PdpModifyRequestMap reqmap;
93 protected ToscaPolicy policy1;
94 protected PapStatisticsManager statsmanager;
97 * Configures DAO, captors, and various mocks.
100 public void setUp() throws Exception {
102 Registry.newRegistry();
104 MockitoAnnotations.openMocks(this);
106 reqmap = mock(PdpModifyRequestMap.class);
108 lockit = new Object();
109 policy1 = loadPolicy("policy.json");
110 statsmanager = mock(PapStatisticsManager.class);
112 List<PdpGroup> groups = loadGroups("groups.json");
114 when(pdpGroupService.getFilteredPdpGroups(any())).thenReturn(groups);
116 when(pdpGroupService.createPdpGroups(any())).thenAnswer(answer -> answer.getArgument(0, List.class));
117 when(pdpGroupService.updatePdpGroups(any())).thenAnswer(answer -> answer.getArgument(0, List.class));
119 Registry.register(PapConstants.REG_PDP_MODIFY_LOCK, lockit);
120 Registry.register(PapConstants.REG_PDP_MODIFY_MAP, reqmap);
121 Registry.register(PapConstants.REG_STATISTICS_MANAGER, statsmanager);
125 * Initialize services to the provider for tests.
127 * @param prov the provider
129 public void initialize(ProviderBase prov) {
130 prov.setPdpGroupService(pdpGroupService);
131 prov.setPolicyAuditService(policyAuditService);
132 prov.setPolicyStatusService(policyStatusService);
133 prov.setToscaService(toscaService);
134 prov.setPolicyNotifier(notifier);
138 protected void assertGroup(List<PdpGroup> groups, String name) {
139 PdpGroup group = groups.remove(0);
141 assertEquals(name, group.getName());
144 protected void assertUpdateIgnorePolicy(List<PdpUpdate> updates, String groupName, String pdpType, String pdpName) {
146 PdpUpdate update = updates.remove(0);
148 assertEquals(groupName, update.getPdpGroup());
149 assertEquals(pdpType, update.getPdpSubgroup());
150 assertEquals(pdpName, update.getName());
154 * Gets the input to the create() method.
156 * @return the input that was passed to the dao.updatePdpGroups() method
157 * @throws Exception if an error occurred
159 protected List<PdpGroup> getGroupCreates() throws Exception {
160 verify(pdpGroupService).createPdpGroups(updateCaptor.capture());
162 return copyList(updateCaptor.getValue());
166 * Gets the input to the update() method.
168 * @return the input that was passed to the dao.updatePdpGroups() method
169 * @throws Exception if an error occurred
171 protected List<PdpGroup> getGroupUpdates() throws Exception {
172 verify(pdpGroupService).updatePdpGroups(updateCaptor.capture());
174 return copyList(updateCaptor.getValue());
178 * Gets the state-changes that were added to the request map.
180 * @param count the number of times the method is expected to have been called
181 * @return the state-changes that were added to the request map
183 protected List<PdpStateChange> getStateChangeRequests(int count) {
184 ArgumentCaptor<PdpStateChange> captor = ArgumentCaptor.forClass(PdpStateChange.class);
186 verify(reqmap, times(count)).addRequest(any(), captor.capture());
188 return captor.getAllValues().stream().filter(req -> req != null).collect(Collectors.toList());
192 * Gets the updates that were added to the request map.
194 * @param count the number of times the method is expected to have been called
195 * @return the updates that were added to the request map
197 protected List<PdpUpdate> getUpdateRequests(int count) {
198 ArgumentCaptor<PdpUpdate> captor = ArgumentCaptor.forClass(PdpUpdate.class);
200 verify(reqmap, times(count)).addRequest(captor.capture(), any());
202 return captor.getAllValues().stream().filter(req -> req != null).collect(Collectors.toList());
206 * Copies a list and sorts it by group name.
208 * @param source source list to copy
209 * @return a copy of the source list
211 private List<PdpGroup> copyList(List<PdpGroup> source) {
212 List<PdpGroup> newlst = new ArrayList<>(source);
213 Collections.sort(newlst, (left, right) -> left.getName().compareTo(right.getName()));
218 * Loads a list of groups.
220 * @param fileName name of the file from which to load
221 * @return a list of groups
223 protected List<PdpGroup> loadGroups(String fileName) {
224 return loadPdpGroups(fileName).getGroups();
230 * @param fileName name of the file from which to load
231 * @return a PdpGroups
233 protected PdpGroups loadPdpGroups(String fileName) {
234 return loadFile(fileName, PdpGroups.class);
240 * @param fileName name of the file from which to load
243 protected PdpGroup loadGroup(String fileName) {
244 return loadFile(fileName, PdpGroup.class);
248 * Loads a list of policies.
250 * @param fileName name of the file from which to load
251 * @return a list of policies
253 protected List<ToscaPolicy> loadPolicies(String fileName) {
254 return loadFile(fileName, PolicyList.class).policies;
260 * @param fileName name of the file from which to load
263 protected ToscaPolicy loadPolicy(String fileName) {
264 return loadFile(fileName, ToscaPolicy.class);
268 * Loads a policy type.
270 * @param fileName name of the file from which to load
271 * @return a policy type
273 protected ToscaPolicyType loadPolicyType(String fileName) {
274 return loadFile(fileName, ToscaPolicyType.class);
278 * Loads an object from a JSON file.
280 * @param fileName name of the file from which to load
281 * @param clazz the class of the object to be loaded
282 * @return the object that was loaded from the file
284 protected <T> T loadFile(String fileName, Class<T> clazz) {
285 File propFile = new File(ResourceUtils.getFilePath4Resource("simpleDeploy/" + fileName));
287 return coder.decode(propFile, clazz);
289 } catch (CoderException e) {
290 throw new RuntimeException(e);
295 * Verifies that an empty notification was published.
297 protected void checkEmptyNotification() {
298 ArgumentCaptor<PolicyNotification> captor = ArgumentCaptor.forClass(PolicyNotification.class);
299 verify(notifier).publish(captor.capture());
300 assertThat(captor.getValue().isEmpty()).isTrue();
304 * Wraps a list of policies. The decoder doesn't work with generic lists, so we wrap
305 * the list and decode it into the wrapper before extracting the list contents.
307 private static class PolicyList {
308 private List<ToscaPolicy> policies;