Adding statistics to PDP Heartbeat Messages
[policy/xacml-pdp.git] / main / src / test / java / org / onap / policy / pdpx / main / comm / XacmlPdpUpdatePublisherTest.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pdpx.main.comm;
22
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;
30
31 import java.util.HashMap;
32 import java.util.List;
33 import java.util.Map;
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;
49
50
51 /**
52  * Initializes objects, including the publisher.
53  */
54 @RunWith(MockitoJUnitRunner.class)
55 public class XacmlPdpUpdatePublisherTest {
56
57     private static final int NEW_COUNT = 5;
58
59     @Mock
60     private TopicSinkClient client;
61
62     @Mock
63     private XacmlState state;
64
65     @Mock
66     private PdpStatus status;
67
68     @Mock
69     private XacmlPdpApplicationManager appmgr;
70
71     @Mock
72     private ToscaPolicy deployed1;
73
74     @Mock
75     private ToscaPolicy deployed2;
76
77     @Mock
78     private ToscaPolicy deployed3;
79
80     @Mock
81     private ToscaPolicy deployed4;
82
83     @Mock
84     private ToscaPolicy deployed5;
85
86     @Mock
87     private ToscaPolicy added1;
88
89     @Mock
90     private ToscaPolicy added2;
91
92     @Mock
93     private ToscaPolicy failPolicy1;
94
95     @Mock
96     private ToscaPolicy failPolicy2;
97
98     @Mock
99     private PdpUpdate update;
100
101     @Mock
102     private PdpUpdate failurePdpUpdate;
103
104     private XacmlPdpUpdatePublisher publisher;
105     private XacmlPdpStatisticsManager statmgr;
106
107
108     /**
109      * Initializes objects, including the publisher.
110      */
111     @Before
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");
119
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"));
129
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);
137
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));
141
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));
146
147         when(appmgr.getPolicyCount()).thenReturn(NEW_COUNT);
148
149         when(state.updateInternalState(any(), any())).thenReturn(status);
150
151         when(client.send(any())).thenReturn(true);
152
153         publisher = new XacmlPdpUpdatePublisher(client, state, appmgr);
154
155         statmgr = new XacmlPdpStatisticsManager();
156         XacmlPdpStatisticsManager.setCurrent(statmgr);
157     }
158
159     @Test
160     public void testHandlePdpUpdate() throws XacmlApplicationException {
161         publisher.handlePdpUpdate(update);
162
163         // two removed
164         verify(appmgr).removeUndeployedPolicy(deployed1);
165         verify(appmgr).removeUndeployedPolicy(deployed4);
166
167         // two added
168         verify(appmgr).loadDeployedPolicy(added1);
169         verify(appmgr).loadDeployedPolicy(added2);
170
171         // three untouched
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);
178
179         assertEquals(NEW_COUNT, statmgr.getTotalPoliciesCount());
180
181         verify(client).send(status);
182     }
183
184     @Test
185     public void testHandlePdpUpdate_Deploy() throws XacmlApplicationException {
186         when(update.getPoliciesToBeUndeployed()).thenReturn(null);
187
188         publisher.handlePdpUpdate(update);
189
190         // none removed
191         verify(appmgr, never()).removeUndeployedPolicy(any());
192
193         // two added
194         verify(appmgr).loadDeployedPolicy(added1);
195         verify(appmgr).loadDeployedPolicy(added2);
196
197         // three untouched
198         verify(appmgr, never()).loadDeployedPolicy(deployed2);
199         verify(appmgr, never()).loadDeployedPolicy(deployed3);
200         verify(appmgr, never()).loadDeployedPolicy(deployed5);
201     }
202
203     @Test
204     public void testHandlePdpUpdate_Undeploy() throws XacmlApplicationException {
205         when(update.getPoliciesToBeDeployed()).thenReturn(null);
206
207         publisher.handlePdpUpdate(update);
208
209         // three removed
210         verify(appmgr).removeUndeployedPolicy(deployed1);
211         verify(appmgr).removeUndeployedPolicy(deployed4);
212         verify(appmgr).removeUndeployedPolicy(deployed5);
213
214         // none added
215         verify(appmgr, never()).loadDeployedPolicy(any());
216
217         // two untouched
218         verify(appmgr, never()).removeUndeployedPolicy(deployed2);
219         verify(appmgr, never()).removeUndeployedPolicy(deployed3);
220     }
221
222     @Test
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);
227
228         publisher.handlePdpUpdate(failurePdpUpdate);
229
230         // two removed
231         verify(appmgr).removeUndeployedPolicy(deployed1);
232         verify(appmgr).removeUndeployedPolicy(deployed4);
233
234         // one untouched
235         verify(appmgr, never()).removeUndeployedPolicy(deployed5);
236
237         verify(state).updateInternalState(any(), startsWith("Failed to load policy"));
238         verify(client).send(status);
239     }
240
241     @Test
242     public void testHandlePdpUpdate_NullPolicies() throws XacmlApplicationException {
243         when(update.getPoliciesToBeDeployed()).thenReturn(null);
244         when(update.getPoliciesToBeUndeployed()).thenReturn(null);
245
246         publisher.handlePdpUpdate(update);
247
248         // none removed
249         verify(appmgr, never()).removeUndeployedPolicy(any());
250
251         // none added
252         verify(appmgr, never()).loadDeployedPolicy(any());
253
254         verify(client).send(status);
255     }
256
257     @Test
258     public void testHandlePdpUpdate_SendFail() {
259
260         when(client.send(any())).thenReturn(false);
261
262         publisher.handlePdpUpdate(update);
263
264         verify(client).send(status);
265     }
266
267 }