2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2022 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2021-2022 Nordix Foundation.
7 * Modifications Copyright (C) 2022-2023 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;
33 import io.micrometer.core.instrument.MeterRegistry;
35 import java.util.ArrayList;
36 import java.util.Comparator;
37 import java.util.List;
38 import java.util.Objects;
39 import java.util.stream.Collectors;
40 import org.junit.Before;
41 import org.mockito.ArgumentCaptor;
42 import org.mockito.Captor;
43 import org.mockito.Mock;
44 import org.mockito.MockitoAnnotations;
45 import org.onap.policy.common.utils.coder.Coder;
46 import org.onap.policy.common.utils.coder.CoderException;
47 import org.onap.policy.common.utils.coder.StandardCoder;
48 import org.onap.policy.common.utils.resources.ResourceUtils;
49 import org.onap.policy.common.utils.services.Registry;
50 import org.onap.policy.models.pap.concepts.PolicyNotification;
51 import org.onap.policy.models.pdp.concepts.PdpGroup;
52 import org.onap.policy.models.pdp.concepts.PdpGroups;
53 import org.onap.policy.models.pdp.concepts.PdpStateChange;
54 import org.onap.policy.models.pdp.concepts.PdpUpdate;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
56 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
57 import org.onap.policy.pap.main.PapConstants;
58 import org.onap.policy.pap.main.comm.PdpModifyRequestMap;
59 import org.onap.policy.pap.main.notification.PolicyNotifier;
60 import org.onap.policy.pap.main.service.PdpGroupService;
61 import org.onap.policy.pap.main.service.PolicyAuditService;
62 import org.onap.policy.pap.main.service.PolicyStatusService;
63 import org.onap.policy.pap.main.service.ToscaServiceTemplateService;
66 * Super class for TestPdpGroupDeployProviderXxx classes.
68 public class ProviderSuper {
69 private static final Coder coder = new StandardCoder();
70 public static final String DEFAULT_USER = "PAP_TEST";
73 protected PdpGroupService pdpGroupService;
76 protected PolicyStatusService policyStatusService;
79 protected PolicyAuditService policyAuditService;
82 protected ToscaServiceTemplateService toscaService;
85 protected PolicyNotifier notifier;
88 * Used to capture input to dao.updatePdpGroups() and dao.createPdpGroups().
91 private ArgumentCaptor<List<PdpGroup>> updateCaptor;
93 protected Object lockit;
94 protected PdpModifyRequestMap reqmap;
95 protected ToscaPolicy policy1;
96 protected MeterRegistry meterRegistry;
99 * Configures DAO, captors, and various mocks.
102 public void setUp() throws Exception {
104 Registry.newRegistry();
106 MockitoAnnotations.openMocks(this);
108 reqmap = mock(PdpModifyRequestMap.class);
110 lockit = new Object();
111 policy1 = loadPolicy("policy.json");
113 meterRegistry = mock(MeterRegistry.class);
115 List<PdpGroup> groups = loadGroups("groups.json");
117 when(pdpGroupService.getFilteredPdpGroups(any())).thenReturn(groups);
119 when(pdpGroupService.createPdpGroups(any())).thenAnswer(answer -> answer.getArgument(0, List.class));
120 when(pdpGroupService.updatePdpGroups(any())).thenAnswer(answer -> answer.getArgument(0, List.class));
122 Registry.register(PapConstants.REG_PDP_MODIFY_LOCK, lockit);
123 Registry.register(PapConstants.REG_PDP_MODIFY_MAP, reqmap);
124 Registry.register(PapConstants.REG_METER_REGISTRY, meterRegistry);
129 * Initialize services to the provider for tests.
131 * @param prov the provider
133 public void initialize(ProviderBase prov) {
134 prov.setPdpGroupService(pdpGroupService);
135 prov.setPolicyAuditService(policyAuditService);
136 prov.setPolicyStatusService(policyStatusService);
137 prov.setToscaService(toscaService);
138 prov.setPolicyNotifier(notifier);
142 protected void assertGroup(List<PdpGroup> groups, String name) {
143 PdpGroup group = groups.remove(0);
145 assertEquals(name, group.getName());
148 protected void assertUpdateIgnorePolicy(List<PdpUpdate> updates, String groupName, String pdpType, String pdpName) {
150 PdpUpdate update = updates.remove(0);
152 assertEquals(groupName, update.getPdpGroup());
153 assertEquals(pdpType, update.getPdpSubgroup());
154 assertEquals(pdpName, update.getName());
158 * Gets the input to the create() method.
160 * @return the input that was passed to the dao.updatePdpGroups() method
162 protected List<PdpGroup> getGroupCreates() {
163 verify(pdpGroupService).createPdpGroups(updateCaptor.capture());
165 return copyList(updateCaptor.getValue());
169 * Gets the input to the update() method.
171 * @return the input that was passed to the dao.updatePdpGroups() method
173 protected List<PdpGroup> getGroupUpdates() {
174 verify(pdpGroupService).updatePdpGroups(updateCaptor.capture());
176 return copyList(updateCaptor.getValue());
180 * Gets the state-changes that were added to the request map.
182 * @param count the number of times the method is expected to have been called
183 * @return the state-changes that were added to the request map
185 protected List<PdpStateChange> getStateChangeRequests(int count) {
186 ArgumentCaptor<PdpStateChange> captor = ArgumentCaptor.forClass(PdpStateChange.class);
188 verify(reqmap, times(count)).addRequest(any(), captor.capture());
190 return captor.getAllValues().stream().filter(Objects::nonNull).collect(Collectors.toList());
194 * Gets the updates that were added to the request map.
196 * @param count the number of times the method is expected to have been called
197 * @return the updates that were added to the request map
199 protected List<PdpUpdate> getUpdateRequests(int count) {
200 ArgumentCaptor<PdpUpdate> captor = ArgumentCaptor.forClass(PdpUpdate.class);
202 verify(reqmap, times(count)).addRequest(captor.capture(), any());
204 return captor.getAllValues().stream().filter(Objects::nonNull).collect(Collectors.toList());
208 * Copies a list and sorts it by group name.
210 * @param source source list to copy
211 * @return a copy of the source list
213 private List<PdpGroup> copyList(List<PdpGroup> source) {
214 List<PdpGroup> newlst = new ArrayList<>(source);
215 newlst.sort(Comparator.comparing(PdpGroup::getName));
220 * Loads a list of groups.
222 * @param fileName name of the file from which to load
223 * @return a list of groups
225 protected List<PdpGroup> loadGroups(String fileName) {
226 return loadPdpGroups(fileName).getGroups();
232 * @param fileName name of the file from which to load
233 * @return a PdpGroups
235 protected PdpGroups loadPdpGroups(String fileName) {
236 return loadFile(fileName, PdpGroups.class);
242 * @param fileName name of the file from which to load
245 protected PdpGroup loadGroup(String fileName) {
246 return loadFile(fileName, PdpGroup.class);
250 * Loads a list of policies.
252 * @param fileName name of the file from which to load
253 * @return a list of policies
255 protected List<ToscaPolicy> loadPolicies(String fileName) {
256 return loadFile(fileName, PolicyList.class).policies;
262 * @param fileName name of the file from which to load
265 protected ToscaPolicy loadPolicy(String fileName) {
266 return loadFile(fileName, ToscaPolicy.class);
270 * Loads a policy type.
272 * @param fileName name of the file from which to load
273 * @return a policy type
275 protected ToscaPolicyType loadPolicyType(String fileName) {
276 return loadFile(fileName, ToscaPolicyType.class);
280 * Loads an object from a JSON file.
282 * @param fileName name of the file from which to load
283 * @param clazz the class of the object to be loaded
284 * @return the object that was loaded from the file
286 protected <T> T loadFile(String fileName, Class<T> clazz) {
287 File propFile = new File(ResourceUtils.getFilePath4Resource("simpleDeploy/" + fileName));
289 return coder.decode(propFile, clazz);
291 } catch (CoderException e) {
292 throw new RuntimeException(e);
297 * Verifies that an empty notification was published.
299 protected void checkEmptyNotification() {
300 ArgumentCaptor<PolicyNotification> captor = ArgumentCaptor.forClass(PolicyNotification.class);
301 verify(notifier).publish(captor.capture());
302 assertThat(captor.getValue().isEmpty()).isTrue();
306 * Wraps a list of policies. The decoder doesn't work with generic lists, so we wrap
307 * the list and decode it into the wrapper before extracting the list contents.
309 private static class PolicyList {
310 private List<ToscaPolicy> policies;