Added support for 'Active' and 'Passive' states.
[policy/xacml-pdp.git] / main / src / test / java / org / onap / policy / pdpx / main / XacmlStateTest.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pdpx.main;
22
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;
29
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;
47
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;
53
54     @Mock
55     private XacmlPdpApplicationManager appmgr;
56
57     @Mock
58     private XacmlPdpActivator act;
59
60     private ToscaPolicyTypeIdentifier ident1;
61     private ToscaPolicyTypeIdentifier ident2;
62
63     private String hostName;
64
65     private XacmlState state;
66
67     /**
68      * Initializes objects, including the state.
69      */
70     @Before
71     public void setUp() {
72         MockitoAnnotations.initMocks(this);
73
74         hostName = NetworkUtil.getHostname();
75
76         ident1 = new ToscaPolicyTypeIdentifier("nameA", "typeA");
77         ident2 = new ToscaPolicyTypeIdentifier("nameB", "typeB");
78
79         when(appmgr.getToscaPolicyTypeIdents()).thenReturn(Arrays.asList(ident1, ident2));
80
81         XacmlPdpActivator.setCurrent(act);
82
83         state = new XacmlState(appmgr);
84     }
85
86     @AfterClass
87     public static void tearDownAfterClass() {
88         XacmlPdpActivator.setCurrent(null);
89     }
90
91     @Test
92     public void testShouldHandle() {
93         PdpUpdate msg = new PdpUpdate();
94         assertFalse(state.shouldHandle(msg));
95
96         msg.setName(NetworkUtil.getHostname());
97         assertTrue(state.shouldHandle(msg));
98     }
99
100     @Test
101     public void testGenHeartbeat() {
102         // not healthy
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());
112
113         // healthy
114         when(act.isAlive()).thenReturn(true);
115
116         status = state.genHeartbeat();
117         assertEquals(PdpHealthStatus.HEALTHY, status.getHealthy());
118     }
119
120     @Test
121     public void testUpdateInternalStatePdpStateChange() {
122         PdpStateChange req = new PdpStateChange();
123         req.setName(hostName);
124         req.setPdpGroup(GROUP);
125         req.setPdpSubgroup(SUBGROUP);
126         req.setState(STATE);
127
128         PdpStatus status = state.updateInternalState(req);
129         assertEquals(PdpState.SAFE, status.getState());
130
131         PdpResponseDetails resp = status.getResponse();
132         assertNotNull(resp);
133         assertEquals(req.getRequestId(), resp.getResponseTo());
134         assertEquals(PdpResponseStatus.SUCCESS, resp.getResponseStatus());
135
136         // ensure info was saved
137         status = state.genHeartbeat();
138         assertEquals(PdpState.SAFE, status.getState());
139
140         req.setState(PdpState.ACTIVE);
141         status = state.updateInternalState(req);
142         assertEquals(PdpState.ACTIVE, status.getState());
143         verify(act).startXacmlRestController();
144
145         req.setState(PdpState.PASSIVE);
146         status = state.updateInternalState(req);
147         assertEquals(PdpState.PASSIVE, status.getState());
148         verify(act).stopXacmlRestController();
149     }
150
151     @Test
152     public void testUpdateInternalStatePdpUpdate() {
153         PdpUpdate req = new PdpUpdate();
154         req.setPdpGroup(GROUP);
155         req.setPdpSubgroup(SUBGROUP);
156
157         PdpStatus status = state.updateInternalState(req);
158
159         PdpResponseDetails resp = status.getResponse();
160         assertNotNull(resp);
161         assertEquals(req.getRequestId(), resp.getResponseTo());
162         assertEquals(PdpResponseStatus.SUCCESS, resp.getResponseStatus());
163
164         // ensure info was saved
165         status = state.genHeartbeat();
166         assertEquals(GROUP, status.getPdpGroup());
167         assertEquals(SUBGROUP, status.getPdpSubgroup());
168     }
169
170     @Test
171     public void testTerminatePdpMessage() {
172         PdpStatus status = state.terminatePdpMessage();
173         assertEquals(PdpState.TERMINATED, status.getState());
174     }
175 }