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
11 * http://www.apache.org/licenses/LICENSE-2.0
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.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.pdpx.main;
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;
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.PdpStatistics;
46 import org.onap.policy.models.pdp.concepts.PdpStatus;
47 import org.onap.policy.models.pdp.concepts.PdpUpdate;
48 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
49 import org.onap.policy.models.pdp.enums.PdpResponseStatus;
50 import org.onap.policy.models.pdp.enums.PdpState;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
52 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
53 import org.onap.policy.pdpx.main.comm.XacmlPdpUpdatePublisher;
54 import org.onap.policy.pdpx.main.rest.XacmlPdpApplicationManager;
55 import org.onap.policy.pdpx.main.rest.XacmlPdpStatisticsManager;
56 import org.onap.policy.pdpx.main.startstop.XacmlPdpActivator;
58 @RunWith(MockitoJUnitRunner.class)
59 public class XacmlStateTest {
60 private static final String PDP_TYPE = "xacml-flavor";
61 private static final String GROUP = "my-group";
62 private static final String SUBGROUP = "my-subgroup";
63 private static final PdpState STATE = PdpState.SAFE;
66 private XacmlPdpApplicationManager appmgr;
69 private XacmlPdpActivator act;
71 private String pdpName;
73 private XacmlState state;
76 * Initializes objects, including the state.
80 pdpName = XacmlState.PDP_NAME;
82 XacmlPdpActivator.setCurrent(act);
83 state = new XacmlState(appmgr, GROUP, PDP_TYPE);
87 public static void tearDownAfterClass() {
88 XacmlPdpActivator.setCurrent(null);
92 public void testShouldHandle() {
93 PdpUpdate msg = new PdpUpdate();
94 assertFalse(state.shouldHandle(msg));
96 msg.setName(XacmlState.PDP_NAME);
97 assertTrue(state.shouldHandle(msg));
101 public void testGenHeartbeat() {
103 PdpStatus status = state.genHeartbeat();
104 assertEquals(PdpHealthStatus.NOT_HEALTHY, status.getHealthy());
105 assertEquals(pdpName, status.getName());
106 assertEquals(GROUP, status.getPdpGroup());
107 assertEquals(PDP_TYPE, status.getPdpType());
108 assertEquals(PdpState.PASSIVE, status.getState());
109 assertTrue(status.getPolicies().isEmpty());
112 when(act.isAlive()).thenReturn(true);
114 status = state.genHeartbeat();
115 assertEquals(PdpHealthStatus.HEALTHY, status.getHealthy());
119 public void testGetStatistics() {
120 XacmlPdpStatisticsManager statmgr = new XacmlPdpStatisticsManager();
121 XacmlPdpStatisticsManager.setCurrent(statmgr);
123 ToscaPolicy policy1 = mock(ToscaPolicy.class);
124 ToscaPolicy policy2 = mock(ToscaPolicy.class);
125 ToscaConceptIdentifier ident = new ToscaConceptIdentifier("undeployed", "2.3.4");
126 when(policy2.getIdentifier()).thenReturn(ident);
128 PdpUpdate message = new PdpUpdate();
129 message.setPoliciesToBeDeployed(Arrays.asList(policy1));
130 message.setPoliciesToBeUndeployed(Arrays.asList(policy2.getIdentifier()));
132 TopicSinkClient client = Mockito.mock(TopicSinkClient.class);
133 XacmlPdpUpdatePublisher publisher = new XacmlPdpUpdatePublisher(client, state, appmgr);
134 publisher.handlePdpUpdate(message);
136 PdpStatistics stats = state.getStatistics();
137 assertNotNull(stats);
138 assertEquals(GROUP, stats.getPdpGroupName());
139 assertEquals(1, stats.getPolicyDeployCount());
140 assertEquals(1, stats.getPolicyDeploySuccessCount());
141 assertEquals(0, stats.getPolicyDeployFailCount());
142 assertEquals(1, stats.getPolicyUndeployCount());
143 assertEquals(1, stats.getPolicyUndeployFailCount());
144 assertEquals(0, stats.getPolicyUndeploySuccessCount());
146 PdpStatistics test = new PdpStatistics();
147 test.setTimeStamp(stats.getTimeStamp());
148 test.setPdpGroupName(GROUP);
149 test.setPolicyDeployCount(1);
150 test.setPolicyDeploySuccessCount(1);
151 test.setPolicyUndeployCount(1);
152 test.setPolicyUndeployFailCount(1);
154 assertEquals(stats.toString(), test.toString());
158 public void testUpdateInternalStatePdpStateChange() {
159 PdpStateChange req = new PdpStateChange();
160 req.setName(pdpName);
161 req.setPdpGroup("wrong-pdp-group");
162 req.setPdpSubgroup(SUBGROUP);
165 PdpStatus status = state.updateInternalState(req);
166 assertEquals(PdpState.SAFE, status.getState());
167 assertEquals(GROUP, status.getPdpGroup());
169 PdpResponseDetails resp = status.getResponse();
171 assertEquals(req.getRequestId(), resp.getResponseTo());
172 assertEquals(PdpResponseStatus.SUCCESS, resp.getResponseStatus());
174 // ensure info was saved
175 status = state.genHeartbeat();
176 assertEquals(PdpState.SAFE, status.getState());
178 req.setState(PdpState.ACTIVE);
179 status = state.updateInternalState(req);
180 assertEquals(PdpState.ACTIVE, status.getState());
181 verify(act).enableApi();
183 req.setState(PdpState.PASSIVE);
184 status = state.updateInternalState(req);
185 assertEquals(PdpState.PASSIVE, status.getState());
186 verify(act).disableApi();
190 public void testUpdateInternalStatePdpUpdate() {
191 PdpUpdate req = new PdpUpdate();
192 req.setPdpGroup("wrong-pdp-group");
193 req.setPdpSubgroup(SUBGROUP);
195 PdpStatus status = state.updateInternalState(req, "");
197 PdpResponseDetails resp = status.getResponse();
199 assertEquals(req.getRequestId(), resp.getResponseTo());
200 assertEquals(PdpResponseStatus.SUCCESS, resp.getResponseStatus());
201 assertNull(resp.getResponseMessage());
203 // ensure info was saved
204 status = state.genHeartbeat();
205 assertEquals(GROUP, status.getPdpGroup());
206 assertEquals(SUBGROUP, status.getPdpSubgroup());
208 status = state.updateInternalState(req, "Failed to load policy: failLoadPolicy1: null");
209 assertEquals("Failed to load policy: failLoadPolicy1: null", status.getResponse().getResponseMessage());
210 assertEquals(PdpResponseStatus.FAIL, status.getResponse().getResponseStatus());
211 assertEquals(GROUP, status.getPdpGroup());
215 public void testTerminatePdpMessage() {
216 PdpStatus status = state.terminatePdpMessage();
217 assertEquals(PdpState.TERMINATED, status.getState());