Add junit coverage to xacml-pdp
[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.when;
28
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;
46
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;
52
53     @Mock
54     private XacmlPdpApplicationManager appmgr;
55
56     @Mock
57     private XacmlPdpActivator act;
58
59     private ToscaPolicyTypeIdentifier ident1;
60     private ToscaPolicyTypeIdentifier ident2;
61
62     private String hostName;
63
64     private XacmlState state;
65
66     /**
67      * Initializes objects, including the state.
68      */
69     @Before
70     public void setUp() {
71         MockitoAnnotations.initMocks(this);
72
73         hostName = NetworkUtil.getHostname();
74
75         ident1 = new ToscaPolicyTypeIdentifier("nameA", "typeA");
76         ident2 = new ToscaPolicyTypeIdentifier("nameB", "typeB");
77
78         when(appmgr.getToscaPolicyTypeIdents()).thenReturn(Arrays.asList(ident1, ident2));
79
80         XacmlPdpActivator.setCurrent(act);
81
82         state = new XacmlState(appmgr);
83     }
84
85     @AfterClass
86     public static void tearDownAfterClass() {
87         XacmlPdpActivator.setCurrent(null);
88     }
89
90     @Test
91     public void testShouldHandle() {
92         PdpUpdate msg = new PdpUpdate();
93         assertFalse(state.shouldHandle(msg));
94
95         msg.setName(NetworkUtil.getHostname());
96         assertTrue(state.shouldHandle(msg));
97     }
98
99     @Test
100     public void testGenHeartbeat() {
101         // not healthy
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());
111
112         // healthy
113         when(act.isAlive()).thenReturn(true);
114
115         status = state.genHeartbeat();
116         assertEquals(PdpHealthStatus.HEALTHY, status.getHealthy());
117     }
118
119     @Test
120     public void testUpdateInternalStatePdpStateChange() {
121         PdpStateChange req = new PdpStateChange();
122         req.setName(hostName);
123         req.setPdpGroup(GROUP);
124         req.setPdpSubgroup(SUBGROUP);
125         req.setState(STATE);
126
127         PdpStatus status = state.updateInternalState(req);
128         assertEquals(PdpState.SAFE, status.getState());
129
130         PdpResponseDetails resp = status.getResponse();
131         assertNotNull(resp);
132         assertEquals(req.getRequestId(), resp.getResponseTo());
133         assertEquals(PdpResponseStatus.SUCCESS, resp.getResponseStatus());
134
135         // ensure info was saved
136         status = state.genHeartbeat();
137         assertEquals(PdpState.SAFE, status.getState());
138     }
139
140     @Test
141     public void testUpdateInternalStatePdpUpdate() {
142         PdpUpdate req = new PdpUpdate();
143         req.setPdpGroup(GROUP);
144         req.setPdpSubgroup(SUBGROUP);
145
146         PdpStatus status = state.updateInternalState(req);
147
148         PdpResponseDetails resp = status.getResponse();
149         assertNotNull(resp);
150         assertEquals(req.getRequestId(), resp.getResponseTo());
151         assertEquals(PdpResponseStatus.SUCCESS, resp.getResponseStatus());
152
153         // ensure info was saved
154         status = state.genHeartbeat();
155         assertEquals(GROUP, status.getPdpGroup());
156         assertEquals(SUBGROUP, status.getPdpSubgroup());
157     }
158
159     @Test
160     public void testTerminatePdpMessage() {
161         PdpStatus status = state.terminatePdpMessage();
162         assertEquals(PdpState.TERMINATED, status.getState());
163     }
164 }