Changed identifiers to concept identifiers
[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  * 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.verify;
30 import static org.mockito.Mockito.when;
31
32 import java.util.Arrays;
33 import org.junit.AfterClass;
34 import org.junit.Before;
35 import org.junit.Test;
36 import org.mockito.Mock;
37 import org.mockito.MockitoAnnotations;
38 import org.onap.policy.common.utils.network.NetworkUtil;
39 import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
40 import org.onap.policy.models.pdp.concepts.PdpStateChange;
41 import org.onap.policy.models.pdp.concepts.PdpStatus;
42 import org.onap.policy.models.pdp.concepts.PdpUpdate;
43 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
44 import org.onap.policy.models.pdp.enums.PdpResponseStatus;
45 import org.onap.policy.models.pdp.enums.PdpState;
46 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
47 import org.onap.policy.pdpx.main.rest.XacmlPdpApplicationManager;
48 import org.onap.policy.pdpx.main.startstop.XacmlPdpActivator;
49
50 public class XacmlStateTest {
51     private static final String PDP_TYPE = "xacml";
52     private static final String GROUP = "my-group";
53     private static final String SUBGROUP = "my-subgroup";
54     private static final PdpState STATE = PdpState.SAFE;
55
56     @Mock
57     private XacmlPdpApplicationManager appmgr;
58
59     @Mock
60     private XacmlPdpActivator act;
61
62     private ToscaConceptIdentifier ident1;
63     private ToscaConceptIdentifier ident2;
64
65     private String hostName;
66
67     private XacmlState state;
68
69     /**
70      * Initializes objects, including the state.
71      */
72     @Before
73     public void setUp() {
74         MockitoAnnotations.initMocks(this);
75
76         hostName = NetworkUtil.getHostname();
77
78         ident1 = new ToscaConceptIdentifier("nameA", "typeA");
79         ident2 = new ToscaConceptIdentifier("nameB", "typeB");
80
81         when(appmgr.getToscaPolicyTypeIdents()).thenReturn(Arrays.asList(ident1, ident2));
82
83         XacmlPdpActivator.setCurrent(act);
84
85         state = new XacmlState(appmgr, GROUP);
86     }
87
88     @AfterClass
89     public static void tearDownAfterClass() {
90         XacmlPdpActivator.setCurrent(null);
91     }
92
93     @Test
94     public void testShouldHandle() {
95         PdpUpdate msg = new PdpUpdate();
96         assertFalse(state.shouldHandle(msg));
97
98         msg.setName(NetworkUtil.getHostname());
99         assertTrue(state.shouldHandle(msg));
100     }
101
102     @Test
103     public void testGenHeartbeat() {
104         // not healthy
105         PdpStatus status = state.genHeartbeat();
106         assertEquals(PdpHealthStatus.NOT_HEALTHY, status.getHealthy());
107         assertEquals(hostName, status.getName());
108         assertEquals(GROUP, status.getPdpGroup());
109         assertEquals(PDP_TYPE, status.getPdpType());
110         assertEquals(PdpState.PASSIVE, status.getState());
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("wrong-pdp-group");
125         req.setPdpSubgroup(SUBGROUP);
126         req.setState(STATE);
127
128         PdpStatus status = state.updateInternalState(req);
129         assertEquals(PdpState.SAFE, status.getState());
130         assertEquals(GROUP, status.getPdpGroup());
131
132         PdpResponseDetails resp = status.getResponse();
133         assertNotNull(resp);
134         assertEquals(req.getRequestId(), resp.getResponseTo());
135         assertEquals(PdpResponseStatus.SUCCESS, resp.getResponseStatus());
136
137         // ensure info was saved
138         status = state.genHeartbeat();
139         assertEquals(PdpState.SAFE, status.getState());
140
141         req.setState(PdpState.ACTIVE);
142         status = state.updateInternalState(req);
143         assertEquals(PdpState.ACTIVE, status.getState());
144         verify(act).startXacmlRestController();
145
146         req.setState(PdpState.PASSIVE);
147         status = state.updateInternalState(req);
148         assertEquals(PdpState.PASSIVE, status.getState());
149         verify(act).stopXacmlRestController();
150     }
151
152     @Test
153     public void testUpdateInternalStatePdpUpdate() {
154         PdpUpdate req = new PdpUpdate();
155         req.setPdpGroup("wrong-pdp-group");
156         req.setPdpSubgroup(SUBGROUP);
157
158         PdpStatus status = state.updateInternalState(req, "");
159
160         PdpResponseDetails resp = status.getResponse();
161         assertNotNull(resp);
162         assertEquals(req.getRequestId(), resp.getResponseTo());
163         assertEquals(PdpResponseStatus.SUCCESS, resp.getResponseStatus());
164         assertNull(resp.getResponseMessage());
165
166         // ensure info was saved
167         status = state.genHeartbeat();
168         assertEquals(GROUP, status.getPdpGroup());
169         assertEquals(SUBGROUP, status.getPdpSubgroup());
170
171         status = state.updateInternalState(req, "Failed to load policy: failLoadPolicy1: null");
172         assertEquals("Failed to load policy: failLoadPolicy1: null", status.getResponse().getResponseMessage());
173         assertEquals(PdpResponseStatus.FAIL, status.getResponse().getResponseStatus());
174         assertEquals(GROUP, status.getPdpGroup());
175     }
176
177     @Test
178     public void testTerminatePdpMessage() {
179         PdpStatus status = state.terminatePdpMessage();
180         assertEquals(PdpState.TERMINATED, status.getState());
181     }
182 }