ccd083cc09238772f3f778f2a3e111ce8941b492
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / TestPolicyUndeployerImpl.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2021, 2023 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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.pap.main.rest;
23
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertThrows;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.Mockito.never;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30
31 import java.util.Collections;
32 import java.util.LinkedList;
33 import java.util.List;
34 import java.util.Set;
35 import org.junit.jupiter.api.AfterAll;
36 import org.junit.jupiter.api.BeforeEach;
37 import org.junit.jupiter.api.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;
47
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";
53
54     @Mock
55     private SessionData session;
56
57     @Captor
58     private ArgumentCaptor<Set<String>> pdpCaptor;
59
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;
67
68
69     @AfterAll
70     public static void tearDownAfterClass() {
71         Registry.newRegistry();
72     }
73
74     /**
75      * Configures mocks and objects.
76      *
77      * @throws Exception if an error occurs
78      */
79     @Override
80     @BeforeEach
81     public void setUp() throws Exception {
82
83         super.setUp();
84
85         ident1 = new ToscaConceptIdentifier("ident-a", "2.3.1+1");
86         ident2 = new ToscaConceptIdentifier("ident-b", "2.3.2-1");
87         ident3 = new ToscaConceptIdentifier("ident-c", "2.3.3");
88         ident4 = new ToscaConceptIdentifier("ident-d", "2.3.4");
89
90         group = new PdpGroup();
91         group.setName(MY_GROUP);
92
93         subgroup = new PdpSubGroup();
94         subgroup.setPdpType(MY_SUBGROUP);
95
96         Pdp pdp1 = new Pdp();
97         pdp1.setInstanceId(PDP1);
98
99         subgroup.setPdpInstances(List.of(pdp1));
100
101         // this subgroup should never be touched
102         PdpSubGroup subgroup0 = new PdpSubGroup();
103         subgroup0.setPdpType(MY_SUBGROUP0);
104         subgroup0.setPolicies(List.of(ident1, ident2, ident3, ident4));
105         subgroup.setPdpInstances(List.of(pdp1));
106
107         group.setPdpSubgroups(List.of(subgroup0, subgroup));
108
109         when(session.getGroup(MY_GROUP)).thenReturn(group);
110         when(session.getPolicy(any())).thenReturn(policy1);
111
112         prov = new MyProvider();
113     }
114
115     @Test
116     void testUndeployPolicies() throws PfModelException {
117         subgroup.setPolicies(new LinkedList<>(List.of(ident1, ident2, ident3, ident4)));
118
119         prov.undeploy(MY_GROUP, MY_SUBGROUP, List.of(ident1, ident2));
120
121         // group should have been updated
122         verify(session).update(group);
123
124         // subgroup should only have remaining policies
125         assertEquals(List.of(ident3, ident4).toString(), subgroup.getPolicies().toString());
126
127         // should have generated PDP-UPDATE for the PDP
128         verify(session).addUpdate(any());
129     }
130
131     /**
132      * Tests undeployPolicies() when the policies do not exist in the subgroup.
133      */
134     @Test
135     void testUndeployPoliciesUnchanged() throws PfModelException {
136         List<ToscaConceptIdentifier> origlist = List.of(ident3, ident4);
137         subgroup.setPolicies(new LinkedList<>(origlist));
138
139         prov.undeploy(MY_GROUP, MY_SUBGROUP, List.of(ident1, ident2));
140
141         // group NOT should have been updated
142         verify(session, never()).update(group);
143
144         // subgroup's policies should be unchanged
145         assertEquals(origlist.toString(), subgroup.getPolicies().toString());
146
147         // should NOT have generated PDP-UPDATE for the PDP
148         verify(session, never()).addUpdate(any());
149     }
150
151     /**
152      * Tests undeployPolicies() when the group is not found.
153      */
154     @Test
155     void testUndeployPoliciesGroupNotFound() throws PfModelException {
156         // force exception to be thrown if the list is changed
157         subgroup.setPolicies(List.of(ident1, ident2, ident3, ident4));
158
159         when(session.getGroup(any())).thenReturn(null);
160
161         prov.undeploy(MY_GROUP, MY_SUBGROUP, List.of(ident1, ident2));
162
163         // group should have been updated
164         verify(session, never()).update(group);
165
166         // should have generated PDP-UPDATE for the PDP
167         verify(session, never()).addUpdate(any());
168     }
169
170     /**
171      * Tests undeployPolicies() when the subgroup is not found.
172      */
173     @Test
174     void testUndeployPoliciesSubGroupNotFound() throws PfModelException {
175         // force exception to be thrown if the list is changed
176         subgroup.setPolicies(Collections.unmodifiableList(List.of(ident1, ident2, ident3, ident4)));
177
178         subgroup.setPdpType(MY_SUBGROUP + "X");
179
180         prov.undeploy(MY_GROUP, MY_SUBGROUP, List.of(ident1, ident2));
181
182         // group should have been updated
183         verify(session, never()).update(group);
184
185         // should have generated PDP-UPDATE for the PDP
186         verify(session, never()).addUpdate(any());
187     }
188
189     @Test
190     void testMakeUpdater() {
191         assertThrows(UnsupportedOperationException.class, () -> prov.makeUpdater(null, null, null));
192     }
193
194     private class MyProvider extends PolicyUndeployerImpl {
195
196         @Override
197         protected <T> void process(T request, BiConsumerWithEx<SessionData, T> processor) throws PfModelException {
198             processor.accept(session, request);
199         }
200     }
201 }