2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019, 2021 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2020-2021 Nordix Foundation.
7 * Modifications Copyright (C) 2021-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.assertThatThrownBy;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertSame;
29 import static org.junit.Assert.assertTrue;
30 import static org.mockito.ArgumentMatchers.any;
31 import static org.mockito.ArgumentMatchers.eq;
32 import static org.mockito.Mockito.doThrow;
33 import static org.mockito.Mockito.never;
34 import static org.mockito.Mockito.spy;
35 import static org.mockito.Mockito.verify;
36 import static org.mockito.Mockito.when;
38 import java.util.Arrays;
39 import java.util.List;
41 import javax.ws.rs.core.Response.Status;
42 import org.junit.AfterClass;
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.mockito.ArgumentCaptor;
46 import org.mockito.Captor;
47 import org.mockito.Mock;
48 import org.onap.policy.common.utils.services.Registry;
49 import org.onap.policy.models.base.PfModelException;
50 import org.onap.policy.models.base.PfModelRuntimeException;
51 import org.onap.policy.models.pdp.concepts.PdpGroup;
52 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
53 import org.onap.policy.models.pdp.concepts.PdpUpdate;
54 import org.onap.policy.models.pdp.enums.PdpState;
55 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
56 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifierOptVersion;
57 import org.onap.policy.pap.main.rest.ProviderBase.Updater;
59 public class TestPdpGroupDeleteProvider extends ProviderSuper {
60 private static final String EXPECTED_EXCEPTION = "expected exception";
61 private static final String GROUP1_NAME = "groupA";
64 private SessionData session;
67 private ArgumentCaptor<Set<String>> pdpCaptor;
69 private MyProvider prov;
70 private ToscaConceptIdentifierOptVersion optIdent;
71 private ToscaConceptIdentifierOptVersion fullIdent;
72 private ToscaConceptIdentifier ident;
73 private Updater updater;
76 public static void tearDownAfterClass() {
77 Registry.newRegistry();
81 * Configures mocks and objects.
83 * @throws Exception if an error occurs
87 public void setUp() throws Exception {
89 prov = new MyProvider();
90 super.initialize(prov);
92 ident = policy1.getIdentifier();
93 optIdent = new ToscaConceptIdentifierOptVersion(ident.getName(), null);
94 fullIdent = new ToscaConceptIdentifierOptVersion(ident.getName(), ident.getVersion());
96 updater = prov.makeUpdater(session, policy1, fullIdent);
100 public void testDeleteGroup_Inctive() throws Exception {
101 PdpGroup group = loadGroup("deleteGroup.json");
103 when(session.getGroup(GROUP1_NAME)).thenReturn(group);
105 prov.deleteGroup(GROUP1_NAME);
107 verify(session).deleteGroupFromDb(group);
109 // should be no PDP requests
110 verify(session, never()).addRequests(any(), any());
114 public void testDeleteGroup_Active() throws Exception {
115 PdpGroup group = loadGroup("deleteGroup.json");
117 group.setPdpGroupState(PdpState.ACTIVE);
119 when(session.getGroup(GROUP1_NAME)).thenReturn(group);
121 assertThatThrownBy(() -> prov.deleteGroup(GROUP1_NAME)).isInstanceOf(PfModelException.class)
122 .hasMessage("group is still ACTIVE");
126 public void testDeleteGroup_NotFound() throws Exception {
127 assertThatThrownBy(() -> prov.deleteGroup(GROUP1_NAME)).isInstanceOf(PfModelException.class)
128 .hasMessage("group not found")
129 .extracting(ex -> ((PfModelException) ex).getErrorResponse().getResponseCode())
130 .isEqualTo(Status.NOT_FOUND);
134 public void testDeleteGroup_Inactive() throws Exception {
135 PdpGroup group = loadGroup("deleteGroup.json");
137 when(session.getGroup(GROUP1_NAME)).thenReturn(group);
139 prov.deleteGroup(GROUP1_NAME);
141 verify(session).deleteGroupFromDb(group);
143 // should done no requests for the PDPs
144 verify(session, never()).addRequests(any(), any());
148 public void testDeleteGroup_Ex() throws Exception {
149 PdpGroup group = loadGroup("deleteGroup.json");
151 when(session.getGroup(GROUP1_NAME)).thenReturn(group);
153 PfModelRuntimeException ex = new PfModelRuntimeException(Status.BAD_REQUEST, EXPECTED_EXCEPTION);
154 doThrow(ex).when(session).deleteGroupFromDb(group);
156 assertThatThrownBy(() -> prov.deleteGroup(GROUP1_NAME)).isSameAs(ex);
160 * Tests using a real provider, just to verify end-to-end functionality.
162 * @throws Exception if an error occurs
165 public void testUndeploy_Full() throws Exception {
166 when(toscaService.getFilteredPolicyList(any())).thenReturn(Arrays.asList(policy1));
168 PdpGroup group = loadGroup("undeploy.json");
170 when(pdpGroupService.getFilteredPdpGroups(any())).thenReturn(Arrays.asList(group));
171 when(toscaService.getFilteredPolicyList(any())).thenReturn(Arrays.asList(policy1));
173 PdpGroupDeleteProvider deleteProvider = new PdpGroupDeleteProvider();
174 super.initialize(deleteProvider);
175 deleteProvider.undeploy(fullIdent, DEFAULT_USER);
177 // should have updated the old group
178 List<PdpGroup> updates = getGroupUpdates();
179 assertEquals(1, updates.size());
180 assertSame(group, updates.get(0));
181 assertEquals(PdpState.ACTIVE, group.getPdpGroupState());
183 // should be one less item in the new subgroup
184 assertEquals(2, group.getPdpSubgroups().get(0).getPolicies().size());
186 // should have updated the PDPs
187 List<PdpUpdate> requests = getUpdateRequests(1);
188 assertEquals(1, requests.size());
189 PdpUpdate req = requests.get(0);
190 assertEquals("pdpA", req.getName());
191 assertEquals(GROUP1_NAME, req.getPdpGroup());
192 assertEquals("pdpTypeA", req.getPdpSubgroup());
193 assertEquals(Arrays.asList(policy1.getIdentifier()), req.getPoliciesToBeUndeployed());
197 public void testUndeployPolicy_NotFound() throws Exception {
198 when(session.isUnchanged()).thenReturn(true);
200 assertThatThrownBy(() -> prov.undeploy(optIdent, DEFAULT_USER)).isInstanceOf(PfModelException.class)
201 .hasMessage("policy does not appear in any PDP group: policyA null");
205 public void testUndeployPolicy_DaoEx() throws Exception {
206 PfModelException exc = new PfModelException(Status.BAD_REQUEST, EXPECTED_EXCEPTION);
209 doThrow(exc).when(prov).processPolicy(any(), any());
211 assertThatThrownBy(() -> prov.undeploy(optIdent, null)).isSameAs(exc);
215 public void testUndeployPolicy_RtEx() throws Exception {
216 RuntimeException exc = new RuntimeException(EXPECTED_EXCEPTION);
219 doThrow(exc).when(prov).processPolicy(any(), any());
221 // process method catches RuntimeException and re-throws as PfModelException
222 assertThatThrownBy(() -> prov.undeploy(fullIdent, null)).isInstanceOf(PfModelException.class)
223 .hasRootCauseMessage(EXPECTED_EXCEPTION);
227 public void testMakeUpdater_WithVersion() throws PfModelException {
229 * this group has two matching policies and one policy with a different name.
231 PdpGroup group = loadGroup("undeploy.json");
233 PdpSubGroup subgroup = group.getPdpSubgroups().get(0);
234 int origSize = subgroup.getPolicies().size();
236 // invoke updater - matching both name and version
237 assertTrue(updater.apply(group, subgroup));
239 // identified policy should have been removed
240 assertEquals(origSize - 1, subgroup.getPolicies().size());
241 assertFalse(subgroup.getPolicies().contains(ident));
243 verify(session).trackUndeploy(eq(ident), pdpCaptor.capture(), eq(group.getName()), eq(subgroup.getPdpType()));
244 assertEquals("[pdpA]", pdpCaptor.getValue().toString());
248 public void testMakeUpdater_NullVersion() throws PfModelException {
250 * this group has two matching policies and one policy with a different name.
252 PdpGroup group = loadGroup("undeploy.json");
254 PdpSubGroup subgroup = group.getPdpSubgroups().get(0);
255 int origSize = subgroup.getPolicies().size();
257 // invoke updater - matching the name, but with a null (i.e., wild-card) version
258 updater = prov.makeUpdater(session, policy1, optIdent);
259 assertTrue(updater.apply(group, subgroup));
261 // identified policy should have been removed
262 assertEquals(origSize - 2, subgroup.getPolicies().size());
263 assertFalse(subgroup.getPolicies().contains(ident));
267 public void testMakeUpdater_NotFound() throws PfModelException {
269 * this group has one policy with a different name and one with a different
270 * version, but not the policy of interest.
272 PdpGroup group = loadGroup("undeployMakeUpdaterGroupNotFound.json");
274 PdpSubGroup subgroup = group.getPdpSubgroups().get(0);
275 int origSize = subgroup.getPolicies().size();
278 assertFalse(updater.apply(group, subgroup));
280 // should be unchanged
281 assertEquals(origSize, subgroup.getPolicies().size());
284 private class MyProvider extends PdpGroupDeleteProvider {
285 private MyProvider() {
290 protected <T> void process(T request, BiConsumerWithEx<SessionData, T> processor) throws PfModelException {
291 processor.accept(session, request);
295 protected void processPolicy(SessionData data, ToscaConceptIdentifierOptVersion desiredPolicy)
296 throws PfModelException {