2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020, 2022-2023 Nordix Foundation.
4 * Modifications Copyright (C) 2020-2021 AT&T Corp.
5 * Modifications Copyright (C) 2020-2022 Bell Canada. All rights reserved.
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.pap.main.rest;
25 import static org.junit.jupiter.api.Assertions.assertEquals;
26 import static org.junit.jupiter.api.Assertions.assertFalse;
27 import static org.junit.jupiter.api.Assertions.assertTrue;
28 import static org.mockito.Mockito.when;
30 import jakarta.ws.rs.core.Response;
32 import java.net.HttpURLConnection;
33 import java.util.ArrayList;
34 import java.util.List;
36 import org.apache.commons.lang3.tuple.Pair;
37 import org.junit.jupiter.api.AfterEach;
38 import org.junit.jupiter.api.BeforeEach;
39 import org.junit.jupiter.api.Test;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.onap.policy.common.endpoints.http.client.HttpClient;
43 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
44 import org.onap.policy.common.endpoints.report.HealthCheckReport;
45 import org.onap.policy.common.parameters.ParameterService;
46 import org.onap.policy.common.utils.coder.Coder;
47 import org.onap.policy.common.utils.coder.CoderException;
48 import org.onap.policy.common.utils.coder.StandardCoder;
49 import org.onap.policy.common.utils.resources.ResourceUtils;
50 import org.onap.policy.common.utils.services.Registry;
51 import org.onap.policy.models.pdp.concepts.Pdp;
52 import org.onap.policy.models.pdp.concepts.PdpGroup;
53 import org.onap.policy.models.pdp.concepts.PdpGroups;
54 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
55 import org.onap.policy.pap.main.PapConstants;
56 import org.onap.policy.pap.main.parameters.CommonTestData;
57 import org.onap.policy.pap.main.parameters.PapParameterGroup;
58 import org.onap.policy.pap.main.service.PdpGroupService;
59 import org.onap.policy.pap.main.startstop.PapActivator;
60 import org.springframework.http.HttpStatus;
61 import org.springframework.test.util.ReflectionTestUtils;
63 class TestPolicyComponentsHealthCheckProvider {
65 private static final String CLIENT_1 = "client1";
66 private static final String PDP_GROUP_DATA_FILE = "rest/pdpGroup.json";
67 private static final String PAP_GROUP_PARAMS_NAME = "PapGroup";
68 private static final String HEALTHY = "healthy";
71 private PdpGroupService pdpGroupService;
74 private HttpClientFactory clientFactory;
77 PapActivator papActivator;
80 private HttpClient client1;
83 private HttpClient client2;
86 private HttpClient client3;
89 private Response response1;
92 private Response response2;
95 private Response response3;
97 private List<PdpGroup> groups;
99 private PapParameterGroup savedPapParameterGroup;
101 private PolicyComponentsHealthCheckProvider provider;
103 AutoCloseable autoCloseable;
106 * Configures mocks and objects.
108 * @throws Exception if an error occurs
111 public void setUp() throws Exception {
112 autoCloseable = MockitoAnnotations.openMocks(this);
113 groups = loadPdpGroupsFromFile().getGroups();
114 when(pdpGroupService.getPdpGroups()).thenReturn(groups);
116 Registry.newRegistry();
118 when(papActivator.isAlive()).thenReturn(true);
119 Registry.register(PapConstants.REG_PAP_ACTIVATOR, papActivator);
121 if (ParameterService.contains(PAP_GROUP_PARAMS_NAME)) {
122 savedPapParameterGroup = ParameterService.get(PAP_GROUP_PARAMS_NAME);
124 CommonTestData testData = new CommonTestData();
125 ParameterService.register(testData.getPapParameterGroup(0), true);
127 when(client1.getName()).thenReturn(CLIENT_1);
128 when(client1.getBaseUrl()).thenReturn("url1");
129 when(response1.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
130 when(response1.readEntity(HealthCheckReport.class)).thenReturn(createReport(HttpURLConnection.HTTP_OK, true));
131 when(client1.get()).thenReturn(response1);
133 when(client2.getName()).thenReturn("client2");
134 when(client2.getBaseUrl()).thenReturn("url2");
135 when(response2.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
136 when(response2.readEntity(HealthCheckReport.class)).thenReturn(createReport(HttpURLConnection.HTTP_OK, true));
137 when(client2.get()).thenReturn(response2);
139 when(client3.getName()).thenReturn("dmaap");
140 when(client3.getBaseUrl()).thenReturn("message-router");
141 when(response3.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
142 when(response3.readEntity(DmaapGetTopicResponse.class)).thenReturn(createDmaapResponse());
143 when(client3.get()).thenReturn(response3);
144 List<HttpClient> clients = new ArrayList<>();
145 clients.add(client1);
146 clients.add(client2);
147 clients.add(client3);
148 PapParameterGroup papParameterGroup = ParameterService.get(PAP_GROUP_PARAMS_NAME);
149 provider = new PolicyComponentsHealthCheckProvider(papParameterGroup, pdpGroupService);
150 ReflectionTestUtils.setField(provider, "papParameterGroup", papParameterGroup);
151 provider.initializeClientHealthCheckExecutorService();
152 ReflectionTestUtils.setField(provider, "clients", clients);
153 ReflectionTestUtils.setField(provider, "topicPolicyPdpPap", "POLICY-PDP-PAP");
160 public void tearDown() throws Exception {
161 if (savedPapParameterGroup != null) {
162 ParameterService.register(savedPapParameterGroup, true);
164 ParameterService.deregister(PAP_GROUP_PARAMS_NAME);
167 autoCloseable.close();
172 void testFetchPolicyComponentsHealthStatus_allHealthy() {
173 Pair<HttpStatus, Map<String, Object>> ret = provider.fetchPolicyComponentsHealthStatus();
174 assertEquals(HttpStatus.OK, ret.getLeft());
175 assertTrue((Boolean) ret.getRight().get(HEALTHY));
179 void testFetchPolicyComponentsHealthStatus_unhealthyClient() {
180 when(response1.getStatus()).thenReturn(HttpURLConnection.HTTP_INTERNAL_ERROR);
181 when(response1.readEntity(HealthCheckReport.class))
182 .thenReturn(createReport(HttpURLConnection.HTTP_INTERNAL_ERROR, false));
183 Map<String, Object> result = callFetchPolicyComponentsHealthStatus();
184 assertFalse((Boolean) result.get(HEALTHY));
185 HealthCheckReport report = (HealthCheckReport) result.get(CLIENT_1);
186 assertFalse(report.isHealthy());
188 when(response1.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
189 when(response1.readEntity(HealthCheckReport.class)).thenReturn(createReport(HttpURLConnection.HTTP_OK, false));
190 Map<String, Object> result2 = callFetchPolicyComponentsHealthStatus();
191 assertFalse((Boolean) result2.get(HEALTHY));
192 HealthCheckReport report2 = (HealthCheckReport) result.get(CLIENT_1);
193 assertFalse(report2.isHealthy());
195 when(response3.getStatus()).thenReturn(HttpURLConnection.HTTP_INTERNAL_ERROR);
196 when(response3.readEntity(DmaapGetTopicResponse.class)).thenReturn(null);
197 Map<String, Object> result3 = callFetchPolicyComponentsHealthStatus();
198 assertFalse((Boolean) result3.get(HEALTHY));
199 HealthCheckReport report3 = (HealthCheckReport) result3.get("dmaap");
200 assertFalse(report3.isHealthy());
202 when(response3.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
203 when(response3.readEntity(DmaapGetTopicResponse.class)).thenReturn(new DmaapGetTopicResponse());
204 Map<String, Object> result4 = callFetchPolicyComponentsHealthStatus();
205 assertFalse((Boolean) result4.get(HEALTHY));
206 HealthCheckReport report4 = (HealthCheckReport) result4.get("dmaap");
207 assertFalse(report4.isHealthy());
210 @SuppressWarnings("unchecked")
212 void testFetchPolicyComponentsHealthStatus_unhealthyPdps() {
213 // Get a PDP and set it unhealthy
214 groups.get(0).getPdpSubgroups().get(0).getPdpInstances().get(0).setHealthy(PdpHealthStatus.NOT_HEALTHY);
215 Map<String, Object> result = callFetchPolicyComponentsHealthStatus();
216 Map<String, List<Pdp>> pdpListWithType = (Map<String, List<Pdp>>) result.get(PapConstants.POLICY_PDPS);
217 assertEquals(2, pdpListWithType.size());
218 assertFalse((Boolean) result.get(HEALTHY));
222 void testFetchPolicyComponentsHealthStatus_PdpDown() {
223 // Set currentInstanceCount as 0 to simulate PDP down
224 groups.get(0).getPdpSubgroups().get(0).setCurrentInstanceCount(0);
225 Map<String, Object> result = callFetchPolicyComponentsHealthStatus();
226 assertFalse((Boolean) result.get(HEALTHY));
230 void testFetchPolicyComponentsHealthStatus_unhealthyPap() {
231 when(papActivator.isAlive()).thenReturn(false);
232 Map<String, Object> result = callFetchPolicyComponentsHealthStatus();
233 assertFalse((Boolean) result.get(HEALTHY));
234 HealthCheckReport report = (HealthCheckReport) result.get(PapConstants.POLICY_PAP);
235 assertFalse(report.isHealthy());
238 private Map<String, Object> callFetchPolicyComponentsHealthStatus() {
240 return provider.fetchPolicyComponentsHealthStatus().getRight();
243 private HealthCheckReport createReport(int code, boolean healthy) {
244 HealthCheckReport report = new HealthCheckReport();
245 report.setName("name");
246 report.setUrl("url");
247 report.setCode(code);
248 report.setHealthy(healthy);
249 report.setMessage("message");
253 private static PdpGroups loadPdpGroupsFromFile() {
254 final File propFile = new File(ResourceUtils.getFilePath4Resource(PDP_GROUP_DATA_FILE));
256 Coder coder = new StandardCoder();
257 return coder.decode(propFile, PdpGroups.class);
258 } catch (final CoderException e) {
259 throw new RuntimeException(e);
263 private DmaapGetTopicResponse createDmaapResponse() {
264 DmaapGetTopicResponse response = new DmaapGetTopicResponse();
265 response.setTopics(List.of("POLICY-PDP-PAP"));