2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020, 2022 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.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.Mockito.when;
31 import java.net.HttpURLConnection;
32 import java.util.ArrayList;
33 import java.util.List;
35 import javax.ws.rs.core.Response;
36 import org.apache.commons.lang3.tuple.Pair;
37 import org.junit.After;
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mock;
42 import org.mockito.junit.MockitoJUnitRunner;
43 import org.onap.policy.common.endpoints.http.client.HttpClient;
44 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
45 import org.onap.policy.common.endpoints.report.HealthCheckReport;
46 import org.onap.policy.common.parameters.ParameterService;
47 import org.onap.policy.common.utils.coder.Coder;
48 import org.onap.policy.common.utils.coder.CoderException;
49 import org.onap.policy.common.utils.coder.StandardCoder;
50 import org.onap.policy.common.utils.resources.ResourceUtils;
51 import org.onap.policy.common.utils.services.Registry;
52 import org.onap.policy.models.pdp.concepts.Pdp;
53 import org.onap.policy.models.pdp.concepts.PdpGroup;
54 import org.onap.policy.models.pdp.concepts.PdpGroups;
55 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
56 import org.onap.policy.pap.main.PapConstants;
57 import org.onap.policy.pap.main.parameters.CommonTestData;
58 import org.onap.policy.pap.main.parameters.PapParameterGroup;
59 import org.onap.policy.pap.main.service.PdpGroupService;
60 import org.onap.policy.pap.main.startstop.PapActivator;
61 import org.springframework.http.HttpStatus;
62 import org.springframework.test.util.ReflectionTestUtils;
64 @RunWith(MockitoJUnitRunner.class)
65 public class TestPolicyComponentsHealthCheckProvider {
67 private static final String CLIENT_1 = "client1";
68 private static final String PDP_GROUP_DATA_FILE = "rest/pdpGroup.json";
69 private static final String PAP_GROUP_PARAMS_NAME = "PapGroup";
70 private static final String HEALTHY = "healthy";
73 private PdpGroupService pdpGroupService;
76 private HttpClientFactory clientFactory;
79 PapActivator papActivator;
82 private HttpClient client1;
85 private HttpClient client2;
88 private HttpClient client3;
91 private Response response1;
94 private Response response2;
97 private Response response3;
99 private List<PdpGroup> groups;
101 private PapParameterGroup savedPapParameterGroup;
103 private PolicyComponentsHealthCheckProvider provider;
106 * Configures mocks and objects.
108 * @throws Exception if an error occurs
111 public void setUp() throws Exception {
112 groups = loadPdpGroupsFromFile().getGroups();
113 when(pdpGroupService.getPdpGroups()).thenReturn(groups);
115 Registry.newRegistry();
117 when(papActivator.isAlive()).thenReturn(true);
118 Registry.register(PapConstants.REG_PAP_ACTIVATOR, papActivator);
120 if (ParameterService.contains(PAP_GROUP_PARAMS_NAME)) {
121 savedPapParameterGroup = ParameterService.get(PAP_GROUP_PARAMS_NAME);
123 CommonTestData testData = new CommonTestData();
124 ParameterService.register(testData.getPapParameterGroup(0), true);
126 when(client1.getName()).thenReturn(CLIENT_1);
127 when(client1.getBaseUrl()).thenReturn("url1");
128 when(response1.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
129 when(response1.readEntity(HealthCheckReport.class)).thenReturn(createReport(HttpURLConnection.HTTP_OK, true));
130 when(client1.get()).thenReturn(response1);
132 when(client2.getName()).thenReturn("client2");
133 when(client2.getBaseUrl()).thenReturn("url2");
134 when(response2.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
135 when(response2.readEntity(HealthCheckReport.class)).thenReturn(createReport(HttpURLConnection.HTTP_OK, true));
136 when(client2.get()).thenReturn(response2);
138 when(client3.getName()).thenReturn("dmaap");
139 when(client3.getBaseUrl()).thenReturn("message-router");
140 when(response3.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
141 when(response3.readEntity(DmaapGetTopicResponse.class)).thenReturn(createDmaapResponse());
142 when(client3.get()).thenReturn(response3);
143 List<HttpClient> clients = new ArrayList<>();
144 clients.add(client1);
145 clients.add(client2);
146 clients.add(client3);
147 PapParameterGroup papParameterGroup = ParameterService.get(PAP_GROUP_PARAMS_NAME);
148 provider = new PolicyComponentsHealthCheckProvider(papParameterGroup, pdpGroupService);
149 ReflectionTestUtils.setField(provider, "papParameterGroup", papParameterGroup);
150 provider.initializeClientHealthCheckExecutorService();
151 ReflectionTestUtils.setField(provider, "clients", clients);
152 ReflectionTestUtils.setField(provider, "topicPolicyPdpPap", "POLICY-PDP-PAP");
159 public void tearDown() {
160 if (savedPapParameterGroup != null) {
161 ParameterService.register(savedPapParameterGroup, true);
163 ParameterService.deregister(PAP_GROUP_PARAMS_NAME);
170 public void testFetchPolicyComponentsHealthStatus_allHealthy() {
171 Pair<HttpStatus, Map<String, Object>> ret = provider.fetchPolicyComponentsHealthStatus();
172 assertEquals(HttpStatus.OK, ret.getLeft());
173 assertTrue((Boolean) ret.getRight().get(HEALTHY));
177 public void testFetchPolicyComponentsHealthStatus_unhealthyClient() {
178 when(response1.getStatus()).thenReturn(HttpURLConnection.HTTP_INTERNAL_ERROR);
179 when(response1.readEntity(HealthCheckReport.class))
180 .thenReturn(createReport(HttpURLConnection.HTTP_INTERNAL_ERROR, false));
181 Map<String, Object> result = callFetchPolicyComponentsHealthStatus();
182 assertFalse((Boolean) result.get(HEALTHY));
183 HealthCheckReport report = (HealthCheckReport) result.get(CLIENT_1);
184 assertFalse(report.isHealthy());
186 when(response1.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
187 when(response1.readEntity(HealthCheckReport.class)).thenReturn(createReport(HttpURLConnection.HTTP_OK, false));
188 Map<String, Object> result2 = callFetchPolicyComponentsHealthStatus();
189 assertFalse((Boolean) result2.get(HEALTHY));
190 HealthCheckReport report2 = (HealthCheckReport) result.get(CLIENT_1);
191 assertFalse(report2.isHealthy());
193 when(response3.getStatus()).thenReturn(HttpURLConnection.HTTP_INTERNAL_ERROR);
194 when(response3.readEntity(DmaapGetTopicResponse.class)).thenReturn(null);
195 Map<String, Object> result3 = callFetchPolicyComponentsHealthStatus();
196 assertFalse((Boolean) result3.get(HEALTHY));
197 HealthCheckReport report3 = (HealthCheckReport) result3.get("dmaap");
198 assertFalse(report3.isHealthy());
200 when(response3.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
201 when(response3.readEntity(DmaapGetTopicResponse.class)).thenReturn(new DmaapGetTopicResponse());
202 Map<String, Object> result4 = callFetchPolicyComponentsHealthStatus();
203 assertFalse((Boolean) result4.get(HEALTHY));
204 HealthCheckReport report4 = (HealthCheckReport) result4.get("dmaap");
205 assertFalse(report4.isHealthy());
208 @SuppressWarnings("unchecked")
210 public void testFetchPolicyComponentsHealthStatus_unhealthyPdps() {
211 // Get a PDP and set it unhealthy
212 groups.get(0).getPdpSubgroups().get(0).getPdpInstances().get(0).setHealthy(PdpHealthStatus.NOT_HEALTHY);
213 Map<String, Object> result = callFetchPolicyComponentsHealthStatus();
214 Map<String, List<Pdp>> pdpListWithType = (Map<String, List<Pdp>>) result.get(PapConstants.POLICY_PDPS);
215 assertEquals(2, pdpListWithType.size());
216 assertFalse((Boolean) result.get(HEALTHY));
220 public void testFetchPolicyComponentsHealthStatus_PdpDown() {
221 // Set currentInstanceCount as 0 to simulate PDP down
222 groups.get(0).getPdpSubgroups().get(0).setCurrentInstanceCount(0);
223 Map<String, Object> result = callFetchPolicyComponentsHealthStatus();
224 assertFalse((Boolean) result.get(HEALTHY));
228 public void testFetchPolicyComponentsHealthStatus_unhealthyPap() {
229 when(papActivator.isAlive()).thenReturn(false);
230 Map<String, Object> result = callFetchPolicyComponentsHealthStatus();
231 assertFalse((Boolean) result.get(HEALTHY));
232 HealthCheckReport report = (HealthCheckReport) result.get(PapConstants.POLICY_PAP);
233 assertFalse(report.isHealthy());
236 private Map<String, Object> callFetchPolicyComponentsHealthStatus() {
238 return provider.fetchPolicyComponentsHealthStatus().getRight();
241 private HealthCheckReport createReport(int code, boolean healthy) {
242 HealthCheckReport report = new HealthCheckReport();
243 report.setName("name");
244 report.setUrl("url");
245 report.setCode(code);
246 report.setHealthy(healthy);
247 report.setMessage("message");
251 private static PdpGroups loadPdpGroupsFromFile() {
252 final File propFile = new File(ResourceUtils.getFilePath4Resource(PDP_GROUP_DATA_FILE));
254 Coder coder = new StandardCoder();
255 return coder.decode(propFile, PdpGroups.class);
256 } catch (final CoderException e) {
257 throw new RuntimeException(e);
261 private DmaapGetTopicResponse createDmaapResponse() {
262 DmaapGetTopicResponse response = new DmaapGetTopicResponse();
263 response.setTopics(List.of("POLICY-PDP-PAP"));