2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019, 2021 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.verify;
30 import static org.mockito.Mockito.when;
32 import org.junit.AfterClass;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.mockito.Mock;
37 import org.mockito.junit.MockitoJUnitRunner;
38 import org.onap.policy.common.utils.network.NetworkUtil;
39 import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
40 import org.onap.policy.models.pdp.concepts.PdpStateChange;
41 import org.onap.policy.models.pdp.concepts.PdpStatus;
42 import org.onap.policy.models.pdp.concepts.PdpUpdate;
43 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
44 import org.onap.policy.models.pdp.enums.PdpResponseStatus;
45 import org.onap.policy.models.pdp.enums.PdpState;
46 import org.onap.policy.pdpx.main.rest.XacmlPdpApplicationManager;
47 import org.onap.policy.pdpx.main.startstop.XacmlPdpActivator;
49 @RunWith(MockitoJUnitRunner.class)
50 public class XacmlStateTest {
51 private static final String PDP_TYPE = "xacml";
52 private static final String GROUP = "my-group";
53 private static final String SUBGROUP = "my-subgroup";
54 private static final PdpState STATE = PdpState.SAFE;
57 private XacmlPdpApplicationManager appmgr;
60 private XacmlPdpActivator act;
62 private String hostName;
64 private XacmlState state;
67 * Initializes objects, including the state.
71 hostName = NetworkUtil.getHostname();
73 XacmlPdpActivator.setCurrent(act);
75 state = new XacmlState(appmgr, GROUP);
79 public static void tearDownAfterClass() {
80 XacmlPdpActivator.setCurrent(null);
84 public void testShouldHandle() {
85 PdpUpdate msg = new PdpUpdate();
86 assertFalse(state.shouldHandle(msg));
88 msg.setName(NetworkUtil.getHostname());
89 assertTrue(state.shouldHandle(msg));
93 public void testGenHeartbeat() {
95 PdpStatus status = state.genHeartbeat();
96 assertEquals(PdpHealthStatus.NOT_HEALTHY, status.getHealthy());
97 assertEquals(hostName, status.getName());
98 assertEquals(GROUP, status.getPdpGroup());
99 assertEquals(PDP_TYPE, status.getPdpType());
100 assertEquals(PdpState.PASSIVE, status.getState());
101 assertTrue(status.getPolicies().isEmpty());
104 when(act.isAlive()).thenReturn(true);
106 status = state.genHeartbeat();
107 assertEquals(PdpHealthStatus.HEALTHY, status.getHealthy());
111 public void testUpdateInternalStatePdpStateChange() {
112 PdpStateChange req = new PdpStateChange();
113 req.setName(hostName);
114 req.setPdpGroup("wrong-pdp-group");
115 req.setPdpSubgroup(SUBGROUP);
118 PdpStatus status = state.updateInternalState(req);
119 assertEquals(PdpState.SAFE, status.getState());
120 assertEquals(GROUP, status.getPdpGroup());
122 PdpResponseDetails resp = status.getResponse();
124 assertEquals(req.getRequestId(), resp.getResponseTo());
125 assertEquals(PdpResponseStatus.SUCCESS, resp.getResponseStatus());
127 // ensure info was saved
128 status = state.genHeartbeat();
129 assertEquals(PdpState.SAFE, status.getState());
131 req.setState(PdpState.ACTIVE);
132 status = state.updateInternalState(req);
133 assertEquals(PdpState.ACTIVE, status.getState());
134 verify(act).startXacmlRestController();
136 req.setState(PdpState.PASSIVE);
137 status = state.updateInternalState(req);
138 assertEquals(PdpState.PASSIVE, status.getState());
139 verify(act).stopXacmlRestController();
143 public void testUpdateInternalStatePdpUpdate() {
144 PdpUpdate req = new PdpUpdate();
145 req.setPdpGroup("wrong-pdp-group");
146 req.setPdpSubgroup(SUBGROUP);
148 PdpStatus status = state.updateInternalState(req, "");
150 PdpResponseDetails resp = status.getResponse();
152 assertEquals(req.getRequestId(), resp.getResponseTo());
153 assertEquals(PdpResponseStatus.SUCCESS, resp.getResponseStatus());
154 assertNull(resp.getResponseMessage());
156 // ensure info was saved
157 status = state.genHeartbeat();
158 assertEquals(GROUP, status.getPdpGroup());
159 assertEquals(SUBGROUP, status.getPdpSubgroup());
161 status = state.updateInternalState(req, "Failed to load policy: failLoadPolicy1: null");
162 assertEquals("Failed to load policy: failLoadPolicy1: null", status.getResponse().getResponseMessage());
163 assertEquals(PdpResponseStatus.FAIL, status.getResponse().getResponseStatus());
164 assertEquals(GROUP, status.getPdpGroup());
168 public void testTerminatePdpMessage() {
169 PdpStatus status = state.terminatePdpMessage();
170 assertEquals(PdpState.TERMINATED, status.getState());