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 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 PapStatisticsManager statsmanager;
97 protected MeterRegistry meterRegistry;
100 * Configures DAO, captors, and various mocks.
103 public void setUp() throws Exception {
105 Registry.newRegistry();
107 MockitoAnnotations.openMocks(this);
109 reqmap = mock(PdpModifyRequestMap.class);
111 lockit = new Object();
112 policy1 = loadPolicy("policy.json");
113 statsmanager = mock(PapStatisticsManager.class);
115 meterRegistry = mock(MeterRegistry.class);
117 List<PdpGroup> groups = loadGroups("groups.json");
119 when(pdpGroupService.getFilteredPdpGroups(any())).thenReturn(groups);
121 when(pdpGroupService.createPdpGroups(any())).thenAnswer(answer -> answer.getArgument(0, List.class));
122 when(pdpGroupService.updatePdpGroups(any())).thenAnswer(answer -> answer.getArgument(0, List.class));
124 Registry.register(PapConstants.REG_PDP_MODIFY_LOCK, lockit);
125 Registry.register(PapConstants.REG_PDP_MODIFY_MAP, reqmap);
126 Registry.register(PapConstants.REG_STATISTICS_MANAGER, statsmanager);
127 Registry.register(PapConstants.REG_METER_REGISTRY, meterRegistry);
132 * Initialize services to the provider for tests.
134 * @param prov the provider
136 public void initialize(ProviderBase prov) {
137 prov.setPdpGroupService(pdpGroupService);
138 prov.setPolicyAuditService(policyAuditService);
139 prov.setPolicyStatusService(policyStatusService);
140 prov.setToscaService(toscaService);
141 prov.setPolicyNotifier(notifier);
145 protected void assertGroup(List<PdpGroup> groups, String name) {
146 PdpGroup group = groups.remove(0);
148 assertEquals(name, group.getName());
151 protected void assertUpdateIgnorePolicy(List<PdpUpdate> updates, String groupName, String pdpType, String pdpName) {
153 PdpUpdate update = updates.remove(0);
155 assertEquals(groupName, update.getPdpGroup());
156 assertEquals(pdpType, update.getPdpSubgroup());
157 assertEquals(pdpName, update.getName());
161 * Gets the input to the create() method.
163 * @return the input that was passed to the dao.updatePdpGroups() method
165 protected List<PdpGroup> getGroupCreates() {
166 verify(pdpGroupService).createPdpGroups(updateCaptor.capture());
168 return copyList(updateCaptor.getValue());
172 * Gets the input to the update() method.
174 * @return the input that was passed to the dao.updatePdpGroups() method
176 protected List<PdpGroup> getGroupUpdates() {
177 verify(pdpGroupService).updatePdpGroups(updateCaptor.capture());
179 return copyList(updateCaptor.getValue());
183 * Gets the state-changes that were added to the request map.
185 * @param count the number of times the method is expected to have been called
186 * @return the state-changes that were added to the request map
188 protected List<PdpStateChange> getStateChangeRequests(int count) {
189 ArgumentCaptor<PdpStateChange> captor = ArgumentCaptor.forClass(PdpStateChange.class);
191 verify(reqmap, times(count)).addRequest(any(), captor.capture());
193 return captor.getAllValues().stream().filter(Objects::nonNull).collect(Collectors.toList());
197 * Gets the updates that were added to the request map.
199 * @param count the number of times the method is expected to have been called
200 * @return the updates that were added to the request map
202 protected List<PdpUpdate> getUpdateRequests(int count) {
203 ArgumentCaptor<PdpUpdate> captor = ArgumentCaptor.forClass(PdpUpdate.class);
205 verify(reqmap, times(count)).addRequest(captor.capture(), any());
207 return captor.getAllValues().stream().filter(Objects::nonNull).collect(Collectors.toList());
211 * Copies a list and sorts it by group name.
213 * @param source source list to copy
214 * @return a copy of the source list
216 private List<PdpGroup> copyList(List<PdpGroup> source) {
217 List<PdpGroup> newlst = new ArrayList<>(source);
218 newlst.sort(Comparator.comparing(PdpGroup::getName));
223 * Loads a list of groups.
225 * @param fileName name of the file from which to load
226 * @return a list of groups
228 protected List<PdpGroup> loadGroups(String fileName) {
229 return loadPdpGroups(fileName).getGroups();
235 * @param fileName name of the file from which to load
236 * @return a PdpGroups
238 protected PdpGroups loadPdpGroups(String fileName) {
239 return loadFile(fileName, PdpGroups.class);
245 * @param fileName name of the file from which to load
248 protected PdpGroup loadGroup(String fileName) {
249 return loadFile(fileName, PdpGroup.class);
253 * Loads a list of policies.
255 * @param fileName name of the file from which to load
256 * @return a list of policies
258 protected List<ToscaPolicy> loadPolicies(String fileName) {
259 return loadFile(fileName, PolicyList.class).policies;
265 * @param fileName name of the file from which to load
268 protected ToscaPolicy loadPolicy(String fileName) {
269 return loadFile(fileName, ToscaPolicy.class);
273 * Loads a policy type.
275 * @param fileName name of the file from which to load
276 * @return a policy type
278 protected ToscaPolicyType loadPolicyType(String fileName) {
279 return loadFile(fileName, ToscaPolicyType.class);
283 * Loads an object from a JSON file.
285 * @param fileName name of the file from which to load
286 * @param clazz the class of the object to be loaded
287 * @return the object that was loaded from the file
289 protected <T> T loadFile(String fileName, Class<T> clazz) {
290 File propFile = new File(ResourceUtils.getFilePath4Resource("simpleDeploy/" + fileName));
292 return coder.decode(propFile, clazz);
294 } catch (CoderException e) {
295 throw new RuntimeException(e);
300 * Verifies that an empty notification was published.
302 protected void checkEmptyNotification() {
303 ArgumentCaptor<PolicyNotification> captor = ArgumentCaptor.forClass(PolicyNotification.class);
304 verify(notifier).publish(captor.capture());
305 assertThat(captor.getValue().isEmpty()).isTrue();
309 * Wraps a list of policies. The decoder doesn't work with generic lists, so we wrap
310 * the list and decode it into the wrapper before extracting the list contents.
312 private static class PolicyList {
313 private List<ToscaPolicy> policies;