Remove db based statistics from xacml-pdp
[policy/xacml-pdp.git] / main / src / test / java / org / onap / policy / pdpx / main / XacmlStateTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2019, 2021-2022 AT&T Intellectual Property. All rights reserved.
4  * Modifications Copyright (C) 2021 Nordix Foundation.
5  * Modifications Copyright (C) 2023 Bell Canada.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  * SPDX-License-Identifier: Apache-2.0
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pdpx.main;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertTrue;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
33
34 import java.util.Arrays;
35 import org.junit.AfterClass;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mock;
40 import org.mockito.Mockito;
41 import org.mockito.junit.MockitoJUnitRunner;
42 import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClient;
43 import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
44 import org.onap.policy.models.pdp.concepts.PdpStateChange;
45 import org.onap.policy.models.pdp.concepts.PdpStatus;
46 import org.onap.policy.models.pdp.concepts.PdpUpdate;
47 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
48 import org.onap.policy.models.pdp.enums.PdpResponseStatus;
49 import org.onap.policy.models.pdp.enums.PdpState;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
52 import org.onap.policy.pdpx.main.comm.XacmlPdpUpdatePublisher;
53 import org.onap.policy.pdpx.main.rest.XacmlPdpApplicationManager;
54 import org.onap.policy.pdpx.main.rest.XacmlPdpStatisticsManager;
55 import org.onap.policy.pdpx.main.startstop.XacmlPdpActivator;
56
57 @RunWith(MockitoJUnitRunner.class)
58 public class XacmlStateTest {
59     private static final String PDP_TYPE = "xacml-flavor";
60     private static final String GROUP = "my-group";
61     private static final String SUBGROUP = "my-subgroup";
62     private static final PdpState STATE = PdpState.SAFE;
63
64     @Mock
65     private XacmlPdpApplicationManager appmgr;
66
67     @Mock
68     private XacmlPdpActivator act;
69
70     private String pdpName;
71
72     private XacmlState state;
73
74     /**
75      * Initializes objects, including the state.
76      */
77     @Before
78     public void setUp() {
79         pdpName = XacmlState.PDP_NAME;
80
81         XacmlPdpActivator.setCurrent(act);
82         state = new XacmlState(appmgr, GROUP, PDP_TYPE);
83     }
84
85     @AfterClass
86     public static void tearDownAfterClass() {
87         XacmlPdpActivator.setCurrent(null);
88     }
89
90     @Test
91     public void testShouldHandle() {
92         PdpUpdate msg = new PdpUpdate();
93         assertFalse(state.shouldHandle(msg));
94
95         msg.setName(XacmlState.PDP_NAME);
96         assertTrue(state.shouldHandle(msg));
97     }
98
99     @Test
100     public void testGenHeartbeat() {
101         // not healthy
102         PdpStatus status = state.genHeartbeat();
103         assertEquals(PdpHealthStatus.NOT_HEALTHY, status.getHealthy());
104         assertEquals(pdpName, status.getName());
105         assertEquals(GROUP, status.getPdpGroup());
106         assertEquals(PDP_TYPE, status.getPdpType());
107         assertEquals(PdpState.PASSIVE, status.getState());
108         assertTrue(status.getPolicies().isEmpty());
109
110         // healthy
111         when(act.isAlive()).thenReturn(true);
112
113         status = state.genHeartbeat();
114         assertEquals(PdpHealthStatus.HEALTHY, status.getHealthy());
115     }
116
117     @Test
118     public void testGetStatistics() {
119         XacmlPdpStatisticsManager statmgr = new XacmlPdpStatisticsManager();
120         XacmlPdpStatisticsManager.setCurrent(statmgr);
121
122         ToscaPolicy policy1 = mock(ToscaPolicy.class);
123         ToscaPolicy policy2 = mock(ToscaPolicy.class);
124         ToscaConceptIdentifier ident = new ToscaConceptIdentifier("undeployed", "2.3.4");
125         when(policy2.getIdentifier()).thenReturn(ident);
126
127         PdpUpdate message = new PdpUpdate();
128         message.setPoliciesToBeDeployed(Arrays.asList(policy1));
129         message.setPoliciesToBeUndeployed(Arrays.asList(policy2.getIdentifier()));
130
131         TopicSinkClient client = Mockito.mock(TopicSinkClient.class);
132         XacmlPdpUpdatePublisher publisher = new XacmlPdpUpdatePublisher(client, state, appmgr);
133         publisher.handlePdpUpdate(message);
134     }
135
136     @Test
137     public void testUpdateInternalStatePdpStateChange() {
138         PdpStateChange req = new PdpStateChange();
139         req.setName(pdpName);
140         req.setPdpGroup("wrong-pdp-group");
141         req.setPdpSubgroup(SUBGROUP);
142         req.setState(STATE);
143
144         PdpStatus status = state.updateInternalState(req);
145         assertEquals(PdpState.SAFE, status.getState());
146         assertEquals(GROUP, status.getPdpGroup());
147
148         PdpResponseDetails resp = status.getResponse();
149         assertNotNull(resp);
150         assertEquals(req.getRequestId(), resp.getResponseTo());
151         assertEquals(PdpResponseStatus.SUCCESS, resp.getResponseStatus());
152
153         // ensure info was saved
154         status = state.genHeartbeat();
155         assertEquals(PdpState.SAFE, status.getState());
156
157         req.setState(PdpState.ACTIVE);
158         status = state.updateInternalState(req);
159         assertEquals(PdpState.ACTIVE, status.getState());
160         verify(act).enableApi();
161
162         req.setState(PdpState.PASSIVE);
163         status = state.updateInternalState(req);
164         assertEquals(PdpState.PASSIVE, status.getState());
165         verify(act).disableApi();
166     }
167
168     @Test
169     public void testUpdateInternalStatePdpUpdate() {
170         PdpUpdate req = new PdpUpdate();
171         req.setPdpGroup("wrong-pdp-group");
172         req.setPdpSubgroup(SUBGROUP);
173
174         PdpStatus status = state.updateInternalState(req, "");
175
176         PdpResponseDetails resp = status.getResponse();
177         assertNotNull(resp);
178         assertEquals(req.getRequestId(), resp.getResponseTo());
179         assertEquals(PdpResponseStatus.SUCCESS, resp.getResponseStatus());
180         assertNull(resp.getResponseMessage());
181
182         // ensure info was saved
183         status = state.genHeartbeat();
184         assertEquals(GROUP, status.getPdpGroup());
185         assertEquals(SUBGROUP, status.getPdpSubgroup());
186
187         status = state.updateInternalState(req, "Failed to load policy: failLoadPolicy1: null");
188         assertEquals("Failed to load policy: failLoadPolicy1: null", status.getResponse().getResponseMessage());
189         assertEquals(PdpResponseStatus.FAIL, status.getResponse().getResponseStatus());
190         assertEquals(GROUP, status.getPdpGroup());
191     }
192
193     @Test
194     public void testTerminatePdpMessage() {
195         PdpStatus status = state.terminatePdpMessage();
196         assertEquals(PdpState.TERMINATED, status.getState());
197     }
198 }