2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019, 2021-2022 AT&T Intellectual Property. All rights reserved.
4 * Modifications Copyright (C) 2021 Nordix Foundation.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pdpx.main;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertNotNull;
27 import static org.junit.Assert.assertNull;
28 import static org.junit.Assert.assertTrue;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.verify;
31 import static org.mockito.Mockito.when;
33 import java.util.Arrays;
34 import org.junit.AfterClass;
35 import org.junit.Before;
36 import org.junit.Test;
37 import org.junit.runner.RunWith;
38 import org.mockito.Mock;
39 import org.mockito.Mockito;
40 import org.mockito.junit.MockitoJUnitRunner;
41 import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClient;
42 import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
43 import org.onap.policy.models.pdp.concepts.PdpStateChange;
44 import org.onap.policy.models.pdp.concepts.PdpStatistics;
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;
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;
65 private XacmlPdpApplicationManager appmgr;
68 private XacmlPdpActivator act;
70 private String pdpName;
72 private XacmlState state;
75 * Initializes objects, including the state.
79 pdpName = XacmlState.PDP_NAME;
81 XacmlPdpActivator.setCurrent(act);
82 state = new XacmlState(appmgr, GROUP, PDP_TYPE);
86 public static void tearDownAfterClass() {
87 XacmlPdpActivator.setCurrent(null);
91 public void testShouldHandle() {
92 PdpUpdate msg = new PdpUpdate();
93 assertFalse(state.shouldHandle(msg));
95 msg.setName(XacmlState.PDP_NAME);
96 assertTrue(state.shouldHandle(msg));
100 public void testGenHeartbeat() {
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());
111 when(act.isAlive()).thenReturn(true);
113 status = state.genHeartbeat();
114 assertEquals(PdpHealthStatus.HEALTHY, status.getHealthy());
118 public void testGetStatistics() {
119 XacmlPdpStatisticsManager statmgr = new XacmlPdpStatisticsManager();
120 XacmlPdpStatisticsManager.setCurrent(statmgr);
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);
127 PdpUpdate message = new PdpUpdate();
128 message.setPoliciesToBeDeployed(Arrays.asList(policy1));
129 message.setPoliciesToBeUndeployed(Arrays.asList(policy2.getIdentifier()));
131 TopicSinkClient client = Mockito.mock(TopicSinkClient.class);
132 XacmlPdpUpdatePublisher publisher = new XacmlPdpUpdatePublisher(client, state, appmgr);
133 publisher.handlePdpUpdate(message);
135 PdpStatistics stats = state.getStatistics();
136 assertTrue(stats != null);
137 assertEquals(GROUP, stats.getPdpGroupName());
138 assertEquals(stats.getPolicyDeployCount(), 1);
139 assertEquals(stats.getPolicyDeploySuccessCount(), 1);
140 assertEquals(stats.getPolicyDeployFailCount(), 0);
141 assertEquals(stats.getPolicyUndeployCount(), 1);
142 assertEquals(stats.getPolicyUndeployFailCount(), 1);
143 assertEquals(stats.getPolicyUndeploySuccessCount(), 0);
145 PdpStatistics test = new PdpStatistics();
146 test.setTimeStamp(stats.getTimeStamp());
147 test.setPdpGroupName(GROUP);
148 test.setPolicyDeployCount(1);
149 test.setPolicyDeploySuccessCount(1);
150 test.setPolicyUndeployCount(1);
151 test.setPolicyUndeployFailCount(1);
153 assertEquals(stats.toString(), test.toString());
157 public void testUpdateInternalStatePdpStateChange() {
158 PdpStateChange req = new PdpStateChange();
159 req.setName(pdpName);
160 req.setPdpGroup("wrong-pdp-group");
161 req.setPdpSubgroup(SUBGROUP);
164 PdpStatus status = state.updateInternalState(req);
165 assertEquals(PdpState.SAFE, status.getState());
166 assertEquals(GROUP, status.getPdpGroup());
168 PdpResponseDetails resp = status.getResponse();
170 assertEquals(req.getRequestId(), resp.getResponseTo());
171 assertEquals(PdpResponseStatus.SUCCESS, resp.getResponseStatus());
173 // ensure info was saved
174 status = state.genHeartbeat();
175 assertEquals(PdpState.SAFE, status.getState());
177 req.setState(PdpState.ACTIVE);
178 status = state.updateInternalState(req);
179 assertEquals(PdpState.ACTIVE, status.getState());
180 verify(act).enableApi();
182 req.setState(PdpState.PASSIVE);
183 status = state.updateInternalState(req);
184 assertEquals(PdpState.PASSIVE, status.getState());
185 verify(act).disableApi();
189 public void testUpdateInternalStatePdpUpdate() {
190 PdpUpdate req = new PdpUpdate();
191 req.setPdpGroup("wrong-pdp-group");
192 req.setPdpSubgroup(SUBGROUP);
194 PdpStatus status = state.updateInternalState(req, "");
196 PdpResponseDetails resp = status.getResponse();
198 assertEquals(req.getRequestId(), resp.getResponseTo());
199 assertEquals(PdpResponseStatus.SUCCESS, resp.getResponseStatus());
200 assertNull(resp.getResponseMessage());
202 // ensure info was saved
203 status = state.genHeartbeat();
204 assertEquals(GROUP, status.getPdpGroup());
205 assertEquals(SUBGROUP, status.getPdpSubgroup());
207 status = state.updateInternalState(req, "Failed to load policy: failLoadPolicy1: null");
208 assertEquals("Failed to load policy: failLoadPolicy1: null", status.getResponse().getResponseMessage());
209 assertEquals(PdpResponseStatus.FAIL, status.getResponse().getResponseStatus());
210 assertEquals(GROUP, status.getPdpGroup());
214 public void testTerminatePdpMessage() {
215 PdpStatus status = state.terminatePdpMessage();
216 assertEquals(PdpState.TERMINATED, status.getState());