2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019, 2021-2022 AT&T Intellectual Property. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pdpx.main.comm;
23 import static org.junit.Assert.assertEquals;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.ArgumentMatchers.startsWith;
26 import static org.mockito.Mockito.doThrow;
27 import static org.mockito.Mockito.never;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
31 import java.util.HashMap;
32 import java.util.List;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.junit.runner.RunWith;
37 import org.mockito.Mock;
38 import org.mockito.junit.MockitoJUnitRunner;
39 import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClient;
40 import org.onap.policy.models.pdp.concepts.PdpStatus;
41 import org.onap.policy.models.pdp.concepts.PdpUpdate;
42 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
44 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationException;
45 import org.onap.policy.pdp.xacml.application.common.XacmlApplicationServiceProvider;
46 import org.onap.policy.pdpx.main.XacmlState;
47 import org.onap.policy.pdpx.main.rest.XacmlPdpApplicationManager;
48 import org.onap.policy.pdpx.main.rest.XacmlPdpStatisticsManager;
52 * Initializes objects, including the publisher.
54 @RunWith(MockitoJUnitRunner.class)
55 public class XacmlPdpUpdatePublisherTest {
57 private static final int NEW_COUNT = 5;
60 private TopicSinkClient client;
63 private XacmlState state;
66 private PdpStatus status;
69 private XacmlPdpApplicationManager appmgr;
72 private ToscaPolicy deployed1;
75 private ToscaPolicy deployed2;
78 private ToscaPolicy deployed3;
81 private ToscaPolicy deployed4;
84 private ToscaPolicy deployed5;
87 private ToscaPolicy added1;
90 private ToscaPolicy added2;
93 private ToscaPolicy failPolicy1;
96 private ToscaPolicy failPolicy2;
99 private PdpUpdate update;
102 private PdpUpdate failurePdpUpdate;
104 private XacmlPdpUpdatePublisher publisher;
105 private XacmlPdpStatisticsManager statmgr;
109 * Initializes objects, including the publisher.
112 public void setUp() {
113 ToscaConceptIdentifier deployedId1 = new ToscaConceptIdentifier("deployed-1", "1.0.0");
114 ToscaConceptIdentifier deployedId2 = new ToscaConceptIdentifier("deployed-2", "1.0.0");
115 ToscaConceptIdentifier deployedId4 = new ToscaConceptIdentifier("deployed-4", "1.0.0");
116 ToscaConceptIdentifier deployedId5 = new ToscaConceptIdentifier("deployed-5", "1.0.0");
117 ToscaConceptIdentifier addedId1 = new ToscaConceptIdentifier("added-1", "1.0.0");
118 ToscaConceptIdentifier addedId2 = new ToscaConceptIdentifier("added-2", "1.0.0");
120 when(deployed1.getIdentifier()).thenReturn(deployedId1);
121 when(deployed2.getIdentifier()).thenReturn(deployedId2);
122 when(deployed3.getIdentifier()).thenReturn(new ToscaConceptIdentifier("deployed-3", "1.0.0"));
123 when(deployed4.getIdentifier()).thenReturn(deployedId4);
124 when(deployed5.getIdentifier()).thenReturn(deployedId5);
125 when(added1.getIdentifier()).thenReturn(addedId1);
126 when(added2.getIdentifier()).thenReturn(addedId2);
127 when(failPolicy1.getIdentifier()).thenReturn(new ToscaConceptIdentifier("failPolicy-1", "1.0.0"));
128 when(failPolicy2.getIdentifier()).thenReturn(new ToscaConceptIdentifier("failPolicy-2", "1.0.0"));
130 Map<ToscaPolicy, XacmlApplicationServiceProvider> deployedPolicies = new HashMap<>();
131 deployedPolicies.put(deployed1, null);
132 deployedPolicies.put(deployed2, null);
133 deployedPolicies.put(deployed3, null);
134 deployedPolicies.put(deployed4, null);
135 deployedPolicies.put(deployed5, null);
136 when(appmgr.getToscaPolicies()).thenReturn(deployedPolicies);
138 // update includes one overlap with existing and one overlap between the two
139 when(update.getPoliciesToBeDeployed()).thenReturn(List.of(added1, deployed2, deployed5, added2));
140 when(update.getPoliciesToBeUndeployed()).thenReturn(List.of(addedId1, deployedId1, deployedId5, deployedId4));
142 when(failurePdpUpdate.getPoliciesToBeDeployed())
143 .thenReturn(List.of(added1, deployed2, deployed5, failPolicy1, failPolicy2));
144 when(failurePdpUpdate.getPoliciesToBeUndeployed())
145 .thenReturn(List.of(addedId1, deployedId1, deployedId5, deployedId4));
147 when(appmgr.getPolicyCount()).thenReturn(NEW_COUNT);
149 when(state.updateInternalState(any(), any())).thenReturn(status);
151 when(client.send(any())).thenReturn(true);
153 publisher = new XacmlPdpUpdatePublisher(client, state, appmgr);
155 statmgr = new XacmlPdpStatisticsManager();
156 XacmlPdpStatisticsManager.setCurrent(statmgr);
160 public void testHandlePdpUpdate() throws XacmlApplicationException {
161 publisher.handlePdpUpdate(update);
164 verify(appmgr).removeUndeployedPolicy(deployed1);
165 verify(appmgr).removeUndeployedPolicy(deployed4);
168 verify(appmgr).loadDeployedPolicy(added1);
169 verify(appmgr).loadDeployedPolicy(added2);
172 verify(appmgr, never()).removeUndeployedPolicy(deployed2);
173 verify(appmgr, never()).removeUndeployedPolicy(deployed3);
174 verify(appmgr, never()).removeUndeployedPolicy(deployed5);
175 verify(appmgr, never()).loadDeployedPolicy(deployed2);
176 verify(appmgr, never()).loadDeployedPolicy(deployed3);
177 verify(appmgr, never()).loadDeployedPolicy(deployed5);
179 assertEquals(NEW_COUNT, statmgr.getTotalPoliciesCount());
181 verify(client).send(status);
185 public void testHandlePdpUpdate_Deploy() throws XacmlApplicationException {
186 when(update.getPoliciesToBeUndeployed()).thenReturn(null);
188 publisher.handlePdpUpdate(update);
191 verify(appmgr, never()).removeUndeployedPolicy(any());
194 verify(appmgr).loadDeployedPolicy(added1);
195 verify(appmgr).loadDeployedPolicy(added2);
198 verify(appmgr, never()).loadDeployedPolicy(deployed2);
199 verify(appmgr, never()).loadDeployedPolicy(deployed3);
200 verify(appmgr, never()).loadDeployedPolicy(deployed5);
204 public void testHandlePdpUpdate_Undeploy() throws XacmlApplicationException {
205 when(update.getPoliciesToBeDeployed()).thenReturn(null);
207 publisher.handlePdpUpdate(update);
210 verify(appmgr).removeUndeployedPolicy(deployed1);
211 verify(appmgr).removeUndeployedPolicy(deployed4);
212 verify(appmgr).removeUndeployedPolicy(deployed5);
215 verify(appmgr, never()).loadDeployedPolicy(any());
218 verify(appmgr, never()).removeUndeployedPolicy(deployed2);
219 verify(appmgr, never()).removeUndeployedPolicy(deployed3);
223 public void testHandlePdpUpdate_LoadPolicyFailed() throws XacmlApplicationException {
224 // Set loadPolicy to fail
225 doThrow(new XacmlApplicationException()).when(appmgr).loadDeployedPolicy(failPolicy1);
226 doThrow(new XacmlApplicationException()).when(appmgr).loadDeployedPolicy(failPolicy2);
228 publisher.handlePdpUpdate(failurePdpUpdate);
231 verify(appmgr).removeUndeployedPolicy(deployed1);
232 verify(appmgr).removeUndeployedPolicy(deployed4);
235 verify(appmgr, never()).removeUndeployedPolicy(deployed5);
237 verify(state).updateInternalState(any(), startsWith("Failed to load policy"));
238 verify(client).send(status);
242 public void testHandlePdpUpdate_NullPolicies() throws XacmlApplicationException {
243 when(update.getPoliciesToBeDeployed()).thenReturn(null);
244 when(update.getPoliciesToBeUndeployed()).thenReturn(null);
246 publisher.handlePdpUpdate(update);
249 verify(appmgr, never()).removeUndeployedPolicy(any());
252 verify(appmgr, never()).loadDeployedPolicy(any());
254 verify(client).send(status);
258 public void testHandlePdpUpdate_SendFail() {
260 when(client.send(any())).thenReturn(false);
262 publisher.handlePdpUpdate(update);
264 verify(client).send(status);