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.assertTrue;
27 import static org.mockito.Mockito.verify;
28 import static org.mockito.Mockito.when;
30 import java.util.Arrays;
31 import org.junit.AfterClass;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.mockito.Mock;
35 import org.mockito.MockitoAnnotations;
36 import org.onap.policy.common.utils.network.NetworkUtil;
37 import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
38 import org.onap.policy.models.pdp.concepts.PdpStateChange;
39 import org.onap.policy.models.pdp.concepts.PdpStatus;
40 import org.onap.policy.models.pdp.concepts.PdpUpdate;
41 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
42 import org.onap.policy.models.pdp.enums.PdpResponseStatus;
43 import org.onap.policy.models.pdp.enums.PdpState;
44 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
45 import org.onap.policy.pdpx.main.rest.XacmlPdpApplicationManager;
46 import org.onap.policy.pdpx.main.startstop.XacmlPdpActivator;
48 public class XacmlStateTest {
49 private static final String PDP_TYPE = "xacml";
50 private static final String GROUP = "my-group";
51 private static final String SUBGROUP = "my-subgroup";
52 private static final PdpState STATE = PdpState.SAFE;
55 private XacmlPdpApplicationManager appmgr;
58 private XacmlPdpActivator act;
60 private ToscaPolicyTypeIdentifier ident1;
61 private ToscaPolicyTypeIdentifier ident2;
63 private String hostName;
65 private XacmlState state;
68 * Initializes objects, including the state.
72 MockitoAnnotations.initMocks(this);
74 hostName = NetworkUtil.getHostname();
76 ident1 = new ToscaPolicyTypeIdentifier("nameA", "typeA");
77 ident2 = new ToscaPolicyTypeIdentifier("nameB", "typeB");
79 when(appmgr.getToscaPolicyTypeIdents()).thenReturn(Arrays.asList(ident1, ident2));
81 XacmlPdpActivator.setCurrent(act);
83 state = new XacmlState(appmgr);
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(NetworkUtil.getHostname());
97 assertTrue(state.shouldHandle(msg));
101 public void testGenHeartbeat() {
103 PdpStatus status = state.genHeartbeat();
104 assertEquals(PdpHealthStatus.NOT_HEALTHY, status.getHealthy());
105 assertEquals(hostName, status.getName());
106 assertEquals(PDP_TYPE, status.getPdpType());
107 assertEquals(PdpState.PASSIVE, status.getState());
108 assertEquals("[ToscaPolicyTypeIdentifier(name=nameA, version=typeA), "
109 + "ToscaPolicyTypeIdentifier(name=nameB, version=typeB)]",
110 status.getSupportedPolicyTypes().toString());
111 assertTrue(status.getPolicies().isEmpty());
114 when(act.isAlive()).thenReturn(true);
116 status = state.genHeartbeat();
117 assertEquals(PdpHealthStatus.HEALTHY, status.getHealthy());
121 public void testUpdateInternalStatePdpStateChange() {
122 PdpStateChange req = new PdpStateChange();
123 req.setName(hostName);
124 req.setPdpGroup(GROUP);
125 req.setPdpSubgroup(SUBGROUP);
128 PdpStatus status = state.updateInternalState(req);
129 assertEquals(PdpState.SAFE, status.getState());
131 PdpResponseDetails resp = status.getResponse();
133 assertEquals(req.getRequestId(), resp.getResponseTo());
134 assertEquals(PdpResponseStatus.SUCCESS, resp.getResponseStatus());
136 // ensure info was saved
137 status = state.genHeartbeat();
138 assertEquals(PdpState.SAFE, status.getState());
140 req.setState(PdpState.ACTIVE);
141 status = state.updateInternalState(req);
142 assertEquals(PdpState.ACTIVE, status.getState());
143 verify(act).startXacmlRestController();
145 req.setState(PdpState.PASSIVE);
146 status = state.updateInternalState(req);
147 assertEquals(PdpState.PASSIVE, status.getState());
148 verify(act).stopXacmlRestController();
152 public void testUpdateInternalStatePdpUpdate() {
153 PdpUpdate req = new PdpUpdate();
154 req.setPdpGroup(GROUP);
155 req.setPdpSubgroup(SUBGROUP);
157 PdpStatus status = state.updateInternalState(req);
159 PdpResponseDetails resp = status.getResponse();
161 assertEquals(req.getRequestId(), resp.getResponseTo());
162 assertEquals(PdpResponseStatus.SUCCESS, resp.getResponseStatus());
164 // ensure info was saved
165 status = state.genHeartbeat();
166 assertEquals(GROUP, status.getPdpGroup());
167 assertEquals(SUBGROUP, status.getPdpSubgroup());
171 public void testTerminatePdpMessage() {
172 PdpStatus status = state.terminatePdpMessage();
173 assertEquals(PdpState.TERMINATED, status.getState());