2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2021 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pap.main.rest;
24 import static org.junit.Assert.assertEquals;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.Mockito.never;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
30 import java.util.Arrays;
31 import java.util.Collections;
32 import java.util.LinkedList;
33 import java.util.List;
35 import org.junit.AfterClass;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.ArgumentCaptor;
39 import org.mockito.Captor;
40 import org.mockito.Mock;
41 import org.onap.policy.common.utils.services.Registry;
42 import org.onap.policy.models.base.PfModelException;
43 import org.onap.policy.models.pdp.concepts.Pdp;
44 import org.onap.policy.models.pdp.concepts.PdpGroup;
45 import org.onap.policy.models.pdp.concepts.PdpSubGroup;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
48 public class TestPolicyUndeployerImpl extends ProviderSuper {
49 private static final String MY_GROUP = "my-group";
50 private static final String MY_SUBGROUP = "my-subgroup";
51 private static final String MY_SUBGROUP0 = "my-subgroup-0";
52 private static final String PDP1 = "my-pdp-a";
55 private SessionData session;
58 private ArgumentCaptor<Set<String>> pdpCaptor;
60 private ToscaConceptIdentifier ident1;
61 private ToscaConceptIdentifier ident2;
62 private ToscaConceptIdentifier ident3;
63 private ToscaConceptIdentifier ident4;
64 private PdpGroup group;
65 private PdpSubGroup subgroup;
66 private MyProvider prov;
70 public static void tearDownAfterClass() {
71 Registry.newRegistry();
75 * Configures mocks and objects.
77 * @throws Exception if an error occurs
81 public void setUp() throws Exception {
85 ident1 = new ToscaConceptIdentifier("ident-a", "2.3.1");
86 ident2 = new ToscaConceptIdentifier("ident-b", "2.3.2");
87 ident3 = new ToscaConceptIdentifier("ident-c", "2.3.3");
88 ident4 = new ToscaConceptIdentifier("ident-d", "2.3.4");
90 group = new PdpGroup();
91 group.setName(MY_GROUP);
93 subgroup = new PdpSubGroup();
94 subgroup.setPdpType(MY_SUBGROUP);
97 pdp1.setInstanceId(PDP1);
99 subgroup.setPdpInstances(Arrays.asList(pdp1));
101 // this subgroup should never be touched
102 PdpSubGroup subgroup0 = new PdpSubGroup();
103 subgroup0.setPdpType(MY_SUBGROUP0);
104 subgroup0.setPolicies(Collections.unmodifiableList(Arrays.asList(ident1, ident2, ident3, ident4)));
105 subgroup.setPdpInstances(Arrays.asList(pdp1));
107 group.setPdpSubgroups(Arrays.asList(subgroup0, subgroup));
109 when(session.getGroup(MY_GROUP)).thenReturn(group);
110 when(session.getPolicy(any())).thenReturn(policy1);
112 prov = new MyProvider();
116 public void testUndeployPolicies() throws PfModelException {
117 subgroup.setPolicies(new LinkedList<>(Arrays.asList(ident1, ident2, ident3, ident4)));
119 prov.undeploy(MY_GROUP, MY_SUBGROUP, Arrays.asList(ident1, ident2));
121 // group should have been updated
122 verify(session).update(group);
124 // subgroup should only have remaining policies
125 assertEquals(Arrays.asList(ident3, ident4).toString(), subgroup.getPolicies().toString());
127 // should have generated PDP-UPDATE for the PDP
128 verify(session).addUpdate(any());
132 * Tests undeployPolicies() when the policies do not exist in the subgroup.
135 public void testUndeployPoliciesUnchanged() throws PfModelException {
136 List<ToscaConceptIdentifier> origlist = Arrays.asList(ident3, ident4);
137 subgroup.setPolicies(new LinkedList<>(origlist));
139 prov.undeploy(MY_GROUP, MY_SUBGROUP, Arrays.asList(ident1, ident2));
141 // group NOT should have been updated
142 verify(session, never()).update(group);
144 // subgroup's policies should be unchanged
145 assertEquals(origlist.toString(), subgroup.getPolicies().toString());
147 // should NOT have generated PDP-UPDATE for the PDP
148 verify(session, never()).addUpdate(any());
152 * Tests undeployPolicies() when the group is not found.
155 public void testUndeployPoliciesGroupNotFound() throws PfModelException {
156 // force exception to be thrown if the list is changed
157 subgroup.setPolicies(Collections.unmodifiableList(Arrays.asList(ident1, ident2, ident3, ident4)));
159 when(session.getGroup(any())).thenReturn(null);
161 prov.undeploy(MY_GROUP, MY_SUBGROUP, Arrays.asList(ident1, ident2));
163 // group should have been updated
164 verify(session, never()).update(group);
166 // should have generated PDP-UPDATE for the PDP
167 verify(session, never()).addUpdate(any());
171 * Tests undeployPolicies() when the subgroup is not found.
174 public void testUndeployPoliciesSubGroupNotFound() throws PfModelException {
175 // force exception to be thrown if the list is changed
176 subgroup.setPolicies(Collections.unmodifiableList(Arrays.asList(ident1, ident2, ident3, ident4)));
178 subgroup.setPdpType(MY_SUBGROUP + "X");
180 prov.undeploy(MY_GROUP, MY_SUBGROUP, Arrays.asList(ident1, ident2));
182 // group should have been updated
183 verify(session, never()).update(group);
185 // should have generated PDP-UPDATE for the PDP
186 verify(session, never()).addUpdate(any());
189 @Test(expected = UnsupportedOperationException.class)
190 public void testMakeUpdater() {
191 prov.makeUpdater(null, null, null);
195 private class MyProvider extends PolicyUndeployerImpl {
198 protected <T> void process(T request, BiConsumerWithEx<SessionData, T> processor) throws PfModelException {
199 processor.accept(session, request);