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.when;
29 import java.util.Arrays;
30 import org.junit.AfterClass;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.mockito.Mock;
34 import org.mockito.MockitoAnnotations;
35 import org.onap.policy.common.utils.network.NetworkUtil;
36 import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
37 import org.onap.policy.models.pdp.concepts.PdpStateChange;
38 import org.onap.policy.models.pdp.concepts.PdpStatus;
39 import org.onap.policy.models.pdp.concepts.PdpUpdate;
40 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
41 import org.onap.policy.models.pdp.enums.PdpResponseStatus;
42 import org.onap.policy.models.pdp.enums.PdpState;
43 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
44 import org.onap.policy.pdpx.main.rest.XacmlPdpApplicationManager;
45 import org.onap.policy.pdpx.main.startstop.XacmlPdpActivator;
47 public class XacmlStateTest {
48 private static final String PDP_TYPE = "xacml";
49 private static final String GROUP = "my-group";
50 private static final String SUBGROUP = "my-subgroup";
51 private static final PdpState STATE = PdpState.SAFE;
54 private XacmlPdpApplicationManager appmgr;
57 private XacmlPdpActivator act;
59 private ToscaPolicyTypeIdentifier ident1;
60 private ToscaPolicyTypeIdentifier ident2;
62 private String hostName;
64 private XacmlState state;
67 * Initializes objects, including the state.
71 MockitoAnnotations.initMocks(this);
73 hostName = NetworkUtil.getHostname();
75 ident1 = new ToscaPolicyTypeIdentifier("nameA", "typeA");
76 ident2 = new ToscaPolicyTypeIdentifier("nameB", "typeB");
78 when(appmgr.getToscaPolicyTypeIdents()).thenReturn(Arrays.asList(ident1, ident2));
80 XacmlPdpActivator.setCurrent(act);
82 state = new XacmlState(appmgr);
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(NetworkUtil.getHostname());
96 assertTrue(state.shouldHandle(msg));
100 public void testGenHeartbeat() {
102 PdpStatus status = state.genHeartbeat();
103 assertEquals(PdpHealthStatus.NOT_HEALTHY, status.getHealthy());
104 assertEquals(hostName, status.getName());
105 assertEquals(PDP_TYPE, status.getPdpType());
106 assertEquals(PdpState.PASSIVE, status.getState());
107 assertEquals("[ToscaPolicyTypeIdentifier(name=nameA, version=typeA), "
108 + "ToscaPolicyTypeIdentifier(name=nameB, version=typeB)]",
109 status.getSupportedPolicyTypes().toString());
110 assertTrue(status.getPolicies().isEmpty());
113 when(act.isAlive()).thenReturn(true);
115 status = state.genHeartbeat();
116 assertEquals(PdpHealthStatus.HEALTHY, status.getHealthy());
120 public void testUpdateInternalStatePdpStateChange() {
121 PdpStateChange req = new PdpStateChange();
122 req.setName(hostName);
123 req.setPdpGroup(GROUP);
124 req.setPdpSubgroup(SUBGROUP);
127 PdpStatus status = state.updateInternalState(req);
128 assertEquals(PdpState.SAFE, status.getState());
130 PdpResponseDetails resp = status.getResponse();
132 assertEquals(req.getRequestId(), resp.getResponseTo());
133 assertEquals(PdpResponseStatus.SUCCESS, resp.getResponseStatus());
135 // ensure info was saved
136 status = state.genHeartbeat();
137 assertEquals(PdpState.SAFE, status.getState());
141 public void testUpdateInternalStatePdpUpdate() {
142 PdpUpdate req = new PdpUpdate();
143 req.setPdpGroup(GROUP);
144 req.setPdpSubgroup(SUBGROUP);
146 PdpStatus status = state.updateInternalState(req);
148 PdpResponseDetails resp = status.getResponse();
150 assertEquals(req.getRequestId(), resp.getResponseTo());
151 assertEquals(PdpResponseStatus.SUCCESS, resp.getResponseStatus());
153 // ensure info was saved
154 status = state.genHeartbeat();
155 assertEquals(GROUP, status.getPdpGroup());
156 assertEquals(SUBGROUP, status.getPdpSubgroup());
160 public void testTerminatePdpMessage() {
161 PdpStatus status = state.terminatePdpMessage();
162 assertEquals(PdpState.TERMINATED, status.getState());