2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019, 2021-2022 AT&T Intellectual Property. All rights reserved.
4 * Modifications Copyright (C) 2021 Nordix Foundation.
5 * Modifications Copyright (C) 2023 Bell Canada.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
19 * SPDX-License-Identifier: Apache-2.0
20 * ============LICENSE_END=========================================================
23 package org.onap.policy.pdpx.main;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertTrue;
30 import static org.mockito.Mockito.mock;
31 import static org.mockito.Mockito.verify;
32 import static org.mockito.Mockito.when;
34 import java.util.Arrays;
35 import org.junit.AfterClass;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mock;
40 import org.mockito.Mockito;
41 import org.mockito.junit.MockitoJUnitRunner;
42 import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClient;
43 import org.onap.policy.models.pdp.concepts.PdpResponseDetails;
44 import org.onap.policy.models.pdp.concepts.PdpStateChange;
45 import org.onap.policy.models.pdp.concepts.PdpStatus;
46 import org.onap.policy.models.pdp.concepts.PdpUpdate;
47 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
48 import org.onap.policy.models.pdp.enums.PdpResponseStatus;
49 import org.onap.policy.models.pdp.enums.PdpState;
50 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
51 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicy;
52 import org.onap.policy.pdpx.main.comm.XacmlPdpUpdatePublisher;
53 import org.onap.policy.pdpx.main.rest.XacmlPdpApplicationManager;
54 import org.onap.policy.pdpx.main.rest.XacmlPdpStatisticsManager;
55 import org.onap.policy.pdpx.main.startstop.XacmlPdpActivator;
57 @RunWith(MockitoJUnitRunner.class)
58 public class XacmlStateTest {
59 private static final String PDP_TYPE = "xacml-flavor";
60 private static final String GROUP = "my-group";
61 private static final String SUBGROUP = "my-subgroup";
62 private static final PdpState STATE = PdpState.SAFE;
65 private XacmlPdpApplicationManager appmgr;
68 private XacmlPdpActivator act;
70 private String pdpName;
72 private XacmlState state;
75 * Initializes objects, including the state.
79 pdpName = XacmlState.PDP_NAME;
81 XacmlPdpActivator.setCurrent(act);
82 state = new XacmlState(appmgr, GROUP, PDP_TYPE);
86 public static void tearDownAfterClass() {
87 XacmlPdpActivator.setCurrent(null);
91 public void testShouldHandle() {
92 PdpUpdate msg = new PdpUpdate();
93 assertFalse(state.shouldHandle(msg));
95 msg.setName(XacmlState.PDP_NAME);
96 assertTrue(state.shouldHandle(msg));
100 public void testGenHeartbeat() {
102 PdpStatus status = state.genHeartbeat();
103 assertEquals(PdpHealthStatus.NOT_HEALTHY, status.getHealthy());
104 assertEquals(pdpName, status.getName());
105 assertEquals(GROUP, status.getPdpGroup());
106 assertEquals(PDP_TYPE, status.getPdpType());
107 assertEquals(PdpState.PASSIVE, status.getState());
108 assertTrue(status.getPolicies().isEmpty());
111 when(act.isAlive()).thenReturn(true);
113 status = state.genHeartbeat();
114 assertEquals(PdpHealthStatus.HEALTHY, status.getHealthy());
118 public void testGetStatistics() {
119 XacmlPdpStatisticsManager statmgr = new XacmlPdpStatisticsManager();
120 XacmlPdpStatisticsManager.setCurrent(statmgr);
122 ToscaPolicy policy1 = mock(ToscaPolicy.class);
123 ToscaPolicy policy2 = mock(ToscaPolicy.class);
124 ToscaConceptIdentifier ident = new ToscaConceptIdentifier("undeployed", "2.3.4");
125 when(policy2.getIdentifier()).thenReturn(ident);
127 PdpUpdate message = new PdpUpdate();
128 message.setPoliciesToBeDeployed(Arrays.asList(policy1));
129 message.setPoliciesToBeUndeployed(Arrays.asList(policy2.getIdentifier()));
131 TopicSinkClient client = Mockito.mock(TopicSinkClient.class);
132 XacmlPdpUpdatePublisher publisher = new XacmlPdpUpdatePublisher(client, state, appmgr);
133 publisher.handlePdpUpdate(message);
137 public void testUpdateInternalStatePdpStateChange() {
138 PdpStateChange req = new PdpStateChange();
139 req.setName(pdpName);
140 req.setPdpGroup("wrong-pdp-group");
141 req.setPdpSubgroup(SUBGROUP);
144 PdpStatus status = state.updateInternalState(req);
145 assertEquals(PdpState.SAFE, status.getState());
146 assertEquals(GROUP, status.getPdpGroup());
148 PdpResponseDetails resp = status.getResponse();
150 assertEquals(req.getRequestId(), resp.getResponseTo());
151 assertEquals(PdpResponseStatus.SUCCESS, resp.getResponseStatus());
153 // ensure info was saved
154 status = state.genHeartbeat();
155 assertEquals(PdpState.SAFE, status.getState());
157 req.setState(PdpState.ACTIVE);
158 status = state.updateInternalState(req);
159 assertEquals(PdpState.ACTIVE, status.getState());
160 verify(act).enableApi();
162 req.setState(PdpState.PASSIVE);
163 status = state.updateInternalState(req);
164 assertEquals(PdpState.PASSIVE, status.getState());
165 verify(act).disableApi();
169 public void testUpdateInternalStatePdpUpdate() {
170 PdpUpdate req = new PdpUpdate();
171 req.setPdpGroup("wrong-pdp-group");
172 req.setPdpSubgroup(SUBGROUP);
174 PdpStatus status = state.updateInternalState(req, "");
176 PdpResponseDetails resp = status.getResponse();
178 assertEquals(req.getRequestId(), resp.getResponseTo());
179 assertEquals(PdpResponseStatus.SUCCESS, resp.getResponseStatus());
180 assertNull(resp.getResponseMessage());
182 // ensure info was saved
183 status = state.genHeartbeat();
184 assertEquals(GROUP, status.getPdpGroup());
185 assertEquals(SUBGROUP, status.getPdpSubgroup());
187 status = state.updateInternalState(req, "Failed to load policy: failLoadPolicy1: null");
188 assertEquals("Failed to load policy: failLoadPolicy1: null", status.getResponse().getResponseMessage());
189 assertEquals(PdpResponseStatus.FAIL, status.getResponse().getResponseStatus());
190 assertEquals(GROUP, status.getPdpGroup());
194 public void testTerminatePdpMessage() {
195 PdpStatus status = state.terminatePdpMessage();
196 assertEquals(PdpState.TERMINATED, status.getState());