Leave xacml-pdp REST server always running
[policy/xacml-pdp.git] / main / src / test / java / org / onap / policy / pdpx / main / XacmlStateTest.java
1 /*-
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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pdpx.main;
23
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.when;
30
31 import org.junit.AfterClass;
32 import org.junit.Before;
33 import org.junit.Test;
34 import org.junit.runner.RunWith;
35 import org.mockito.Mock;
36 import org.mockito.junit.MockitoJUnitRunner;
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.pdpx.main.rest.XacmlPdpApplicationManager;
46 import org.onap.policy.pdpx.main.startstop.XacmlPdpActivator;
47
48 @RunWith(MockitoJUnitRunner.class)
49 public class XacmlStateTest {
50     private static final String PDP_TYPE = "xacml-flavor";
51     private static final String GROUP = "my-group";
52     private static final String SUBGROUP = "my-subgroup";
53     private static final PdpState STATE = PdpState.SAFE;
54
55     @Mock
56     private XacmlPdpApplicationManager appmgr;
57
58     @Mock
59     private XacmlPdpActivator act;
60
61     private String hostName;
62
63     private XacmlState state;
64
65     /**
66      * Initializes objects, including the state.
67      */
68     @Before
69     public void setUp() {
70         hostName = NetworkUtil.getHostname();
71
72         XacmlPdpActivator.setCurrent(act);
73
74         state = new XacmlState(appmgr, GROUP, PDP_TYPE);
75     }
76
77     @AfterClass
78     public static void tearDownAfterClass() {
79         XacmlPdpActivator.setCurrent(null);
80     }
81
82     @Test
83     public void testShouldHandle() {
84         PdpUpdate msg = new PdpUpdate();
85         assertFalse(state.shouldHandle(msg));
86
87         msg.setName(NetworkUtil.getHostname());
88         assertTrue(state.shouldHandle(msg));
89     }
90
91     @Test
92     public void testGenHeartbeat() {
93         // not healthy
94         PdpStatus status = state.genHeartbeat();
95         assertEquals(PdpHealthStatus.NOT_HEALTHY, status.getHealthy());
96         assertEquals(hostName, status.getName());
97         assertEquals(GROUP, status.getPdpGroup());
98         assertEquals(PDP_TYPE, status.getPdpType());
99         assertEquals(PdpState.PASSIVE, status.getState());
100         assertTrue(status.getPolicies().isEmpty());
101
102         // healthy
103         when(act.isAlive()).thenReturn(true);
104
105         status = state.genHeartbeat();
106         assertEquals(PdpHealthStatus.HEALTHY, status.getHealthy());
107     }
108
109     @Test
110     public void testUpdateInternalStatePdpStateChange() {
111         PdpStateChange req = new PdpStateChange();
112         req.setName(hostName);
113         req.setPdpGroup("wrong-pdp-group");
114         req.setPdpSubgroup(SUBGROUP);
115         req.setState(STATE);
116
117         PdpStatus status = state.updateInternalState(req);
118         assertEquals(PdpState.SAFE, status.getState());
119         assertEquals(GROUP, status.getPdpGroup());
120
121         PdpResponseDetails resp = status.getResponse();
122         assertNotNull(resp);
123         assertEquals(req.getRequestId(), resp.getResponseTo());
124         assertEquals(PdpResponseStatus.SUCCESS, resp.getResponseStatus());
125
126         // ensure info was saved
127         status = state.genHeartbeat();
128         assertEquals(PdpState.SAFE, status.getState());
129
130         req.setState(PdpState.ACTIVE);
131         status = state.updateInternalState(req);
132         assertEquals(PdpState.ACTIVE, status.getState());
133
134         req.setState(PdpState.PASSIVE);
135         status = state.updateInternalState(req);
136         assertEquals(PdpState.PASSIVE, status.getState());
137     }
138
139     @Test
140     public void testUpdateInternalStatePdpUpdate() {
141         PdpUpdate req = new PdpUpdate();
142         req.setPdpGroup("wrong-pdp-group");
143         req.setPdpSubgroup(SUBGROUP);
144
145         PdpStatus status = state.updateInternalState(req, "");
146
147         PdpResponseDetails resp = status.getResponse();
148         assertNotNull(resp);
149         assertEquals(req.getRequestId(), resp.getResponseTo());
150         assertEquals(PdpResponseStatus.SUCCESS, resp.getResponseStatus());
151         assertNull(resp.getResponseMessage());
152
153         // ensure info was saved
154         status = state.genHeartbeat();
155         assertEquals(GROUP, status.getPdpGroup());
156         assertEquals(SUBGROUP, status.getPdpSubgroup());
157
158         status = state.updateInternalState(req, "Failed to load policy: failLoadPolicy1: null");
159         assertEquals("Failed to load policy: failLoadPolicy1: null", status.getResponse().getResponseMessage());
160         assertEquals(PdpResponseStatus.FAIL, status.getResponse().getResponseStatus());
161         assertEquals(GROUP, status.getPdpGroup());
162     }
163
164     @Test
165     public void testTerminatePdpMessage() {
166         PdpStatus status = state.terminatePdpMessage();
167         assertEquals(PdpState.TERMINATED, status.getState());
168     }
169 }