2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019-2022 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2021-2023 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.jupiter.api.Assertions.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.jupiter.api.AfterEach;
41 import org.junit.jupiter.api.BeforeEach;
42 import org.mockito.ArgumentCaptor;
43 import org.mockito.Captor;
44 import org.mockito.Mock;
45 import org.mockito.MockitoAnnotations;
46 import org.onap.policy.common.utils.coder.Coder;
47 import org.onap.policy.common.utils.coder.CoderException;
48 import org.onap.policy.common.utils.coder.StandardCoder;
49 import org.onap.policy.common.utils.resources.ResourceUtils;
50 import org.onap.policy.common.utils.services.Registry;
51 import org.onap.policy.models.pap.concepts.PolicyNotification;
52 import org.onap.policy.models.pdp.concepts.PdpGroup;
53 import org.onap.policy.models.pdp.concepts.PdpGroups;
54 import org.onap.policy.models.pdp.concepts.PdpStateChange;
55 import org.onap.policy.models.pdp.concepts.PdpUpdate;
56 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
57 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyType;
58 import org.onap.policy.pap.main.PapConstants;
59 import org.onap.policy.pap.main.comm.PdpModifyRequestMap;
60 import org.onap.policy.pap.main.notification.PolicyNotifier;
61 import org.onap.policy.pap.main.service.PdpGroupService;
62 import org.onap.policy.pap.main.service.PolicyAuditService;
63 import org.onap.policy.pap.main.service.PolicyStatusService;
64 import org.onap.policy.pap.main.service.ToscaServiceTemplateService;
67 * Super class for TestPdpGroupDeployProviderXxx classes.
69 public class ProviderSuper {
70 private static final Coder coder = new StandardCoder();
71 public static final String DEFAULT_USER = "PAP_TEST";
74 protected PdpGroupService pdpGroupService;
77 protected PolicyStatusService policyStatusService;
80 protected PolicyAuditService policyAuditService;
83 protected ToscaServiceTemplateService toscaService;
86 protected PolicyNotifier notifier;
89 * Used to capture input to dao.updatePdpGroups() and dao.createPdpGroups().
92 private ArgumentCaptor<List<PdpGroup>> updateCaptor;
94 protected Object lockit;
95 protected PdpModifyRequestMap reqmap;
96 protected ToscaPolicy policy1;
97 protected MeterRegistry meterRegistry;
99 AutoCloseable closeable;
102 * Configures DAO, captors, and various mocks.
105 public void setUp() throws Exception {
107 Registry.newRegistry();
109 closeable = MockitoAnnotations.openMocks(this);
111 reqmap = mock(PdpModifyRequestMap.class);
113 lockit = new Object();
114 policy1 = loadPolicy("policy.json");
116 meterRegistry = mock(MeterRegistry.class);
118 List<PdpGroup> groups = loadGroups("groups.json");
120 when(pdpGroupService.getFilteredPdpGroups(any())).thenReturn(groups);
122 when(pdpGroupService.createPdpGroups(any())).thenAnswer(answer -> answer.getArgument(0, List.class));
123 when(pdpGroupService.updatePdpGroups(any())).thenAnswer(answer -> answer.getArgument(0, List.class));
125 Registry.register(PapConstants.REG_PDP_MODIFY_LOCK, lockit);
126 Registry.register(PapConstants.REG_PDP_MODIFY_MAP, reqmap);
127 Registry.register(PapConstants.REG_METER_REGISTRY, meterRegistry);
132 public void tearDown() throws Exception {
137 * Initialize services to the provider for tests.
139 * @param prov the provider
141 public void initialize(ProviderBase prov) {
142 prov.setPdpGroupService(pdpGroupService);
143 prov.setPolicyAuditService(policyAuditService);
144 prov.setPolicyStatusService(policyStatusService);
145 prov.setToscaService(toscaService);
146 prov.setPolicyNotifier(notifier);
150 protected void assertGroup(List<PdpGroup> groups, String name) {
151 PdpGroup group = groups.remove(0);
153 assertEquals(name, group.getName());
156 protected void assertUpdateIgnorePolicy(List<PdpUpdate> updates, String groupName, String pdpType, String pdpName) {
158 PdpUpdate update = updates.remove(0);
160 assertEquals(groupName, update.getPdpGroup());
161 assertEquals(pdpType, update.getPdpSubgroup());
162 assertEquals(pdpName, update.getName());
166 * Gets the input to the create() method.
168 * @return the input that was passed to the dao.updatePdpGroups() method
170 protected List<PdpGroup> getGroupCreates() {
171 verify(pdpGroupService).createPdpGroups(updateCaptor.capture());
173 return copyList(updateCaptor.getValue());
177 * Gets the input to the update() method.
179 * @return the input that was passed to the dao.updatePdpGroups() method
181 protected List<PdpGroup> getGroupUpdates() {
182 verify(pdpGroupService).updatePdpGroups(updateCaptor.capture());
184 return copyList(updateCaptor.getValue());
188 * Gets the state-changes that were added to the request map.
190 * @param count the number of times the method is expected to have been called
191 * @return the state-changes that were added to the request map
193 protected List<PdpStateChange> getStateChangeRequests(int count) {
194 ArgumentCaptor<PdpStateChange> captor = ArgumentCaptor.forClass(PdpStateChange.class);
196 verify(reqmap, times(count)).addRequest(any(), captor.capture());
198 return captor.getAllValues().stream().filter(Objects::nonNull).collect(Collectors.toList());
202 * Gets the updates that were added to the request map.
204 * @param count the number of times the method is expected to have been called
205 * @return the updates that were added to the request map
207 protected List<PdpUpdate> getUpdateRequests(int count) {
208 ArgumentCaptor<PdpUpdate> captor = ArgumentCaptor.forClass(PdpUpdate.class);
210 verify(reqmap, times(count)).addRequest(captor.capture(), any());
212 return captor.getAllValues().stream().filter(Objects::nonNull).collect(Collectors.toList());
216 * Copies a list and sorts it by group name.
218 * @param source source list to copy
219 * @return a copy of the source list
221 private List<PdpGroup> copyList(List<PdpGroup> source) {
222 List<PdpGroup> newlst = new ArrayList<>(source);
223 newlst.sort(Comparator.comparing(PdpGroup::getName));
228 * Loads a list of groups.
230 * @param fileName name of the file from which to load
231 * @return a list of groups
233 protected List<PdpGroup> loadGroups(String fileName) {
234 return loadPdpGroups(fileName).getGroups();
240 * @param fileName name of the file from which to load
241 * @return a PdpGroups
243 protected PdpGroups loadPdpGroups(String fileName) {
244 return loadFile(fileName, PdpGroups.class);
250 * @param fileName name of the file from which to load
253 protected PdpGroup loadGroup(String fileName) {
254 return loadFile(fileName, PdpGroup.class);
258 * Loads a list of policies.
260 * @param fileName name of the file from which to load
261 * @return a list of policies
263 protected List<ToscaPolicy> loadPolicies(String fileName) {
264 return loadFile(fileName, PolicyList.class).policies;
270 * @param fileName name of the file from which to load
273 protected ToscaPolicy loadPolicy(String fileName) {
274 return loadFile(fileName, ToscaPolicy.class);
278 * Loads a policy type.
280 * @param fileName name of the file from which to load
281 * @return a policy type
283 protected ToscaPolicyType loadPolicyType(String fileName) {
284 return loadFile(fileName, ToscaPolicyType.class);
288 * Loads an object from a JSON file.
290 * @param fileName name of the file from which to load
291 * @param clazz the class of the object to be loaded
292 * @return the object that was loaded from the file
294 protected <T> T loadFile(String fileName, Class<T> clazz) {
295 File propFile = new File(ResourceUtils.getFilePath4Resource("simpleDeploy/" + fileName));
297 return coder.decode(propFile, clazz);
299 } catch (CoderException e) {
300 throw new RuntimeException(e);
305 * Verifies that an empty notification was published.
307 protected void checkEmptyNotification() {
308 ArgumentCaptor<PolicyNotification> captor = ArgumentCaptor.forClass(PolicyNotification.class);
309 verify(notifier).publish(captor.capture());
310 assertThat(captor.getValue().isEmpty()).isTrue();
314 * Wraps a list of policies. The decoder doesn't work with generic lists, so we wrap
315 * the list and decode it into the wrapper before extracting the list contents.
317 private static class PolicyList {
318 private List<ToscaPolicy> policies;