2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.pdpx.main;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
31 import java.util.Arrays;
32 import org.junit.AfterClass;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
37 import org.onap.policy.common.utils.network.NetworkUtil;
38 import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
39 import org.onap.policy.models.pdp.concepts.PdpStateChange;
40 import org.onap.policy.models.pdp.concepts.PdpStatus;
41 import org.onap.policy.models.pdp.concepts.PdpUpdate;
42 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
43 import org.onap.policy.models.pdp.enums.PdpResponseStatus;
44 import org.onap.policy.models.pdp.enums.PdpState;
45 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
46 import org.onap.policy.pdpx.main.rest.XacmlPdpApplicationManager;
47 import org.onap.policy.pdpx.main.startstop.XacmlPdpActivator;
49 public class XacmlStateTest {
50 private static final String PDP_TYPE = "xacml";
51 private static final String GROUP = "my-group";
52 private static final String SUBGROUP = "my-subgroup";
53 private static final PdpState STATE = PdpState.SAFE;
56 private XacmlPdpApplicationManager appmgr;
59 private XacmlPdpActivator act;
61 private ToscaPolicyTypeIdentifier ident1;
62 private ToscaPolicyTypeIdentifier ident2;
64 private String hostName;
66 private XacmlState state;
69 * Initializes objects, including the state.
73 MockitoAnnotations.initMocks(this);
75 hostName = NetworkUtil.getHostname();
77 ident1 = new ToscaPolicyTypeIdentifier("nameA", "typeA");
78 ident2 = new ToscaPolicyTypeIdentifier("nameB", "typeB");
80 when(appmgr.getToscaPolicyTypeIdents()).thenReturn(Arrays.asList(ident1, ident2));
82 XacmlPdpActivator.setCurrent(act);
84 state = new XacmlState(appmgr, GROUP);
88 public static void tearDownAfterClass() {
89 XacmlPdpActivator.setCurrent(null);
93 public void testShouldHandle() {
94 PdpUpdate msg = new PdpUpdate();
95 assertFalse(state.shouldHandle(msg));
97 msg.setName(NetworkUtil.getHostname());
98 assertTrue(state.shouldHandle(msg));
102 public void testGenHeartbeat() {
104 PdpStatus status = state.genHeartbeat();
105 assertEquals(PdpHealthStatus.NOT_HEALTHY, status.getHealthy());
106 assertEquals(hostName, status.getName());
107 assertEquals(GROUP, status.getPdpGroup());
108 assertEquals(PDP_TYPE, status.getPdpType());
109 assertEquals(PdpState.PASSIVE, status.getState());
110 assertEquals("[ToscaPolicyTypeIdentifier(name=nameA, version=typeA), "
111 + "ToscaPolicyTypeIdentifier(name=nameB, version=typeB)]",
112 status.getSupportedPolicyTypes().toString());
113 assertTrue(status.getPolicies().isEmpty());
116 when(act.isAlive()).thenReturn(true);
118 status = state.genHeartbeat();
119 assertEquals(PdpHealthStatus.HEALTHY, status.getHealthy());
123 public void testUpdateInternalStatePdpStateChange() {
124 PdpStateChange req = new PdpStateChange();
125 req.setName(hostName);
126 req.setPdpGroup("wrong-pdp-group");
127 req.setPdpSubgroup(SUBGROUP);
130 PdpStatus status = state.updateInternalState(req);
131 assertEquals(PdpState.SAFE, status.getState());
132 assertEquals(GROUP, status.getPdpGroup());
134 PdpResponseDetails resp = status.getResponse();
136 assertEquals(req.getRequestId(), resp.getResponseTo());
137 assertEquals(PdpResponseStatus.SUCCESS, resp.getResponseStatus());
139 // ensure info was saved
140 status = state.genHeartbeat();
141 assertEquals(PdpState.SAFE, status.getState());
143 req.setState(PdpState.ACTIVE);
144 status = state.updateInternalState(req);
145 assertEquals(PdpState.ACTIVE, status.getState());
146 verify(act).startXacmlRestController();
148 req.setState(PdpState.PASSIVE);
149 status = state.updateInternalState(req);
150 assertEquals(PdpState.PASSIVE, status.getState());
151 verify(act).stopXacmlRestController();
155 public void testUpdateInternalStatePdpUpdate() {
156 PdpUpdate req = new PdpUpdate();
157 req.setPdpGroup("wrong-pdp-group");
158 req.setPdpSubgroup(SUBGROUP);
160 PdpStatus status = state.updateInternalState(req, "");
162 PdpResponseDetails resp = status.getResponse();
164 assertEquals(req.getRequestId(), resp.getResponseTo());
165 assertEquals(PdpResponseStatus.SUCCESS, resp.getResponseStatus());
166 assertNull(resp.getResponseMessage());
168 // ensure info was saved
169 status = state.genHeartbeat();
170 assertEquals(GROUP, status.getPdpGroup());
171 assertEquals(SUBGROUP, status.getPdpSubgroup());
173 status = state.updateInternalState(req, "Failed to load policy: failLoadPolicy1: null");
174 assertEquals(status.getResponse().getResponseMessage(), "Failed to load policy: failLoadPolicy1: null");
175 assertEquals(status.getResponse().getResponseStatus(), PdpResponseStatus.FAIL);
176 assertEquals(GROUP, status.getPdpGroup());
180 public void testTerminatePdpMessage() {
181 PdpStatus status = state.terminatePdpMessage();
182 assertEquals(PdpState.TERMINATED, status.getState());