Changed Xacml-pdp to report pdp group defined in XacmlPdpParameters config file
[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.assertNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.Mockito.verify;
29 import static org.mockito.Mockito.when;
30
31 import java.util.Arrays;
32 import org.junit.AfterClass;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.mockito.Mock;
36 import org.mockito.MockitoAnnotations;
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.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
46 import org.onap.policy.pdpx.main.rest.XacmlPdpApplicationManager;
47 import org.onap.policy.pdpx.main.startstop.XacmlPdpActivator;
48
49 public class XacmlStateTest {
50     private static final String PDP_TYPE = "xacml";
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 ToscaPolicyTypeIdentifier ident1;
62     private ToscaPolicyTypeIdentifier ident2;
63
64     private String hostName;
65
66     private XacmlState state;
67
68     /**
69      * Initializes objects, including the state.
70      */
71     @Before
72     public void setUp() {
73         MockitoAnnotations.initMocks(this);
74
75         hostName = NetworkUtil.getHostname();
76
77         ident1 = new ToscaPolicyTypeIdentifier("nameA", "typeA");
78         ident2 = new ToscaPolicyTypeIdentifier("nameB", "typeB");
79
80         when(appmgr.getToscaPolicyTypeIdents()).thenReturn(Arrays.asList(ident1, ident2));
81
82         XacmlPdpActivator.setCurrent(act);
83
84         state = new XacmlState(appmgr, GROUP);
85     }
86
87     @AfterClass
88     public static void tearDownAfterClass() {
89         XacmlPdpActivator.setCurrent(null);
90     }
91
92     @Test
93     public void testShouldHandle() {
94         PdpUpdate msg = new PdpUpdate();
95         assertFalse(state.shouldHandle(msg));
96
97         msg.setName(NetworkUtil.getHostname());
98         assertTrue(state.shouldHandle(msg));
99     }
100
101     @Test
102     public void testGenHeartbeat() {
103         // not healthy
104         PdpStatus status = state.genHeartbeat();
105         assertEquals(PdpHealthStatus.NOT_HEALTHY, status.getHealthy());
106         assertEquals(hostName, status.getName());
107         assertEquals(GROUP, status.getPdpGroup());
108         assertEquals(PDP_TYPE, status.getPdpType());
109         assertEquals(PdpState.PASSIVE, status.getState());
110         assertEquals("[ToscaPolicyTypeIdentifier(name=nameA, version=typeA), "
111                         + "ToscaPolicyTypeIdentifier(name=nameB, version=typeB)]",
112                         status.getSupportedPolicyTypes().toString());
113         assertTrue(status.getPolicies().isEmpty());
114
115         // healthy
116         when(act.isAlive()).thenReturn(true);
117
118         status = state.genHeartbeat();
119         assertEquals(PdpHealthStatus.HEALTHY, status.getHealthy());
120     }
121
122     @Test
123     public void testUpdateInternalStatePdpStateChange() {
124         PdpStateChange req = new PdpStateChange();
125         req.setName(hostName);
126         req.setPdpGroup("wrong-pdp-group");
127         req.setPdpSubgroup(SUBGROUP);
128         req.setState(STATE);
129
130         PdpStatus status = state.updateInternalState(req);
131         assertEquals(PdpState.SAFE, status.getState());
132         assertEquals(GROUP, status.getPdpGroup());
133
134         PdpResponseDetails resp = status.getResponse();
135         assertNotNull(resp);
136         assertEquals(req.getRequestId(), resp.getResponseTo());
137         assertEquals(PdpResponseStatus.SUCCESS, resp.getResponseStatus());
138
139         // ensure info was saved
140         status = state.genHeartbeat();
141         assertEquals(PdpState.SAFE, status.getState());
142
143         req.setState(PdpState.ACTIVE);
144         status = state.updateInternalState(req);
145         assertEquals(PdpState.ACTIVE, status.getState());
146         verify(act).startXacmlRestController();
147
148         req.setState(PdpState.PASSIVE);
149         status = state.updateInternalState(req);
150         assertEquals(PdpState.PASSIVE, status.getState());
151         verify(act).stopXacmlRestController();
152     }
153
154     @Test
155     public void testUpdateInternalStatePdpUpdate() {
156         PdpUpdate req = new PdpUpdate();
157         req.setPdpGroup("wrong-pdp-group");
158         req.setPdpSubgroup(SUBGROUP);
159
160         PdpStatus status = state.updateInternalState(req, "");
161
162         PdpResponseDetails resp = status.getResponse();
163         assertNotNull(resp);
164         assertEquals(req.getRequestId(), resp.getResponseTo());
165         assertEquals(PdpResponseStatus.SUCCESS, resp.getResponseStatus());
166         assertNull(resp.getResponseMessage());
167
168         // ensure info was saved
169         status = state.genHeartbeat();
170         assertEquals(GROUP, status.getPdpGroup());
171         assertEquals(SUBGROUP, status.getPdpSubgroup());
172
173         status = state.updateInternalState(req, "Failed to load policy: failLoadPolicy1: null");
174         assertEquals(status.getResponse().getResponseMessage(), "Failed to load policy: failLoadPolicy1: null");
175         assertEquals(status.getResponse().getResponseStatus(), PdpResponseStatus.FAIL);
176         assertEquals(GROUP, status.getPdpGroup());
177     }
178
179     @Test
180     public void testTerminatePdpMessage() {
181         PdpStatus status = state.terminatePdpMessage();
182         assertEquals(PdpState.TERMINATED, status.getState());
183     }
184 }