2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Nordix Foundation.
4 * Modifications Copyright (C) 2020-2021 AT&T Corp.
5 * Modifications Copyright (C) 2020-2021 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.ArgumentMatchers.any;
29 import static org.mockito.Mockito.when;
32 import java.net.HttpURLConnection;
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.List;
37 import javax.ws.rs.core.Response;
38 import org.apache.commons.lang3.tuple.Pair;
39 import org.junit.After;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.junit.runner.RunWith;
43 import org.mockito.Mock;
44 import org.mockito.junit.MockitoJUnitRunner;
45 import org.onap.policy.common.endpoints.http.client.HttpClient;
46 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
47 import org.onap.policy.common.endpoints.report.HealthCheckReport;
48 import org.onap.policy.common.parameters.ParameterService;
49 import org.onap.policy.common.utils.coder.Coder;
50 import org.onap.policy.common.utils.coder.CoderException;
51 import org.onap.policy.common.utils.coder.StandardCoder;
52 import org.onap.policy.common.utils.resources.ResourceUtils;
53 import org.onap.policy.common.utils.services.Registry;
54 import org.onap.policy.models.pdp.concepts.Pdp;
55 import org.onap.policy.models.pdp.concepts.PdpGroup;
56 import org.onap.policy.models.pdp.concepts.PdpGroups;
57 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
58 import org.onap.policy.models.provider.PolicyModelsProvider;
59 import org.onap.policy.pap.main.PapConstants;
60 import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper;
61 import org.onap.policy.pap.main.parameters.CommonTestData;
62 import org.onap.policy.pap.main.parameters.PapParameterGroup;
63 import org.onap.policy.pap.main.startstop.PapActivator;
64 import org.springframework.http.HttpStatus;
65 import org.springframework.test.util.ReflectionTestUtils;
67 @RunWith(MockitoJUnitRunner.class)
68 public class TestPolicyComponentsHealthCheckProvider {
70 private static final String CLIENT_1 = "client1";
71 private static final String PDP_GROUP_DATA_FILE = "rest/pdpGroup.json";
72 private static final String PAP_GROUP_PARAMS_NAME = "PapGroup";
73 private static final String HEALTHY = "healthy";
76 private PolicyModelsProvider dao;
79 private PolicyModelsProviderFactoryWrapper daofact;
82 private HttpClientFactory clientFactory;
85 PapActivator papActivator;
88 private HttpClient client1;
91 private HttpClient client2;
94 private HttpClient client3;
97 private Response response1;
100 private Response response2;
103 private Response response3;
105 private List<PdpGroup> groups;
107 private PapParameterGroup savedPapParameterGroup;
109 private PolicyComponentsHealthCheckProvider provider;
112 * Configures mocks and objects.
114 * @throws Exception if an error occurs
117 public void setUp() throws Exception {
118 groups = loadPdpGroupsFromFile().getGroups();
119 when(dao.getPdpGroups(any())).thenReturn(groups);
121 when(daofact.create()).thenReturn(dao);
122 Registry.newRegistry();
123 Registry.register(PapConstants.REG_PAP_DAO_FACTORY, daofact);
125 when(papActivator.isAlive()).thenReturn(true);
126 Registry.register(PapConstants.REG_PAP_ACTIVATOR, papActivator);
128 if (ParameterService.contains(PAP_GROUP_PARAMS_NAME)) {
129 savedPapParameterGroup = ParameterService.get(PAP_GROUP_PARAMS_NAME);
131 CommonTestData testData = new CommonTestData();
132 ParameterService.register(testData.getPapParameterGroup(0), true);
134 when(client1.getName()).thenReturn(CLIENT_1);
135 when(client1.getBaseUrl()).thenReturn("url1");
136 when(response1.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
137 when(response1.readEntity(HealthCheckReport.class)).thenReturn(createReport(HttpURLConnection.HTTP_OK, true));
138 when(client1.get()).thenReturn(response1);
140 when(client2.getName()).thenReturn("client2");
141 when(client2.getBaseUrl()).thenReturn("url2");
142 when(response2.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
143 when(response2.readEntity(HealthCheckReport.class)).thenReturn(createReport(HttpURLConnection.HTTP_OK, true));
144 when(client2.get()).thenReturn(response2);
146 when(client3.getName()).thenReturn("dmaap");
147 when(client3.getBaseUrl()).thenReturn("message-router");
148 when(response3.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
149 when(response3.readEntity(DmaapGetTopicResponse.class)).thenReturn(createDmaapResponse());
150 when(client3.get()).thenReturn(response3);
151 List<HttpClient> clients = new ArrayList<>();
152 clients.add(client1);
153 clients.add(client2);
154 clients.add(client3);
155 PapParameterGroup papParameterGroup = ParameterService.get(PAP_GROUP_PARAMS_NAME);
156 provider = new PolicyComponentsHealthCheckProvider();
157 ReflectionTestUtils.setField(provider, "papParameterGroup", papParameterGroup);
158 provider.initializeClientHealthCheckExecutorService();
159 ReflectionTestUtils.setField(provider, "clients", clients);
166 public void tearDown() {
167 if (savedPapParameterGroup != null) {
168 ParameterService.register(savedPapParameterGroup, true);
170 ParameterService.deregister(PAP_GROUP_PARAMS_NAME);
177 public void testFetchPolicyComponentsHealthStatus_allHealthy() {
178 Pair<HttpStatus, Map<String, Object>> ret = provider.fetchPolicyComponentsHealthStatus();
179 assertEquals(HttpStatus.OK, ret.getLeft());
180 assertTrue((Boolean) ret.getRight().get(HEALTHY));
184 public void testFetchPolicyComponentsHealthStatus_unhealthyClient() {
185 when(response1.getStatus()).thenReturn(HttpURLConnection.HTTP_INTERNAL_ERROR);
186 when(response1.readEntity(HealthCheckReport.class))
187 .thenReturn(createReport(HttpURLConnection.HTTP_INTERNAL_ERROR, false));
188 Map<String, Object> result = callFetchPolicyComponentsHealthStatus();
189 assertFalse((Boolean) result.get(HEALTHY));
190 HealthCheckReport report = (HealthCheckReport) result.get(CLIENT_1);
191 assertFalse(report.isHealthy());
193 when(response1.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
194 when(response1.readEntity(HealthCheckReport.class)).thenReturn(createReport(HttpURLConnection.HTTP_OK, false));
195 Map<String, Object> result2 = callFetchPolicyComponentsHealthStatus();
196 assertFalse((Boolean) result2.get(HEALTHY));
197 HealthCheckReport report2 = (HealthCheckReport) result.get(CLIENT_1);
198 assertFalse(report2.isHealthy());
200 when(response3.getStatus()).thenReturn(HttpURLConnection.HTTP_INTERNAL_ERROR);
201 when(response3.readEntity(DmaapGetTopicResponse.class)).thenReturn(null);
202 Map<String, Object> result3 = callFetchPolicyComponentsHealthStatus();
203 assertFalse((Boolean) result3.get(HEALTHY));
204 HealthCheckReport report3 = (HealthCheckReport) result3.get("dmaap");
205 assertFalse(report3.isHealthy());
207 when(response3.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
208 when(response3.readEntity(DmaapGetTopicResponse.class)).thenReturn(new DmaapGetTopicResponse());
209 Map<String, Object> result4 = callFetchPolicyComponentsHealthStatus();
210 assertFalse((Boolean) result4.get(HEALTHY));
211 HealthCheckReport report4 = (HealthCheckReport) result4.get("dmaap");
212 assertFalse(report4.isHealthy());
215 @SuppressWarnings("unchecked")
217 public void testFetchPolicyComponentsHealthStatus_unhealthyPdps() {
218 // Get a PDP and set it unhealthy
219 groups.get(0).getPdpSubgroups().get(0).getPdpInstances().get(0).setHealthy(PdpHealthStatus.NOT_HEALTHY);
220 Map<String, Object> result = callFetchPolicyComponentsHealthStatus();
221 Map<String, List<Pdp>> pdpListWithType = (Map<String, List<Pdp>>) result.get(PapConstants.POLICY_PDPS);
222 assertEquals(2, pdpListWithType.size());
223 assertFalse((Boolean) result.get(HEALTHY));
227 public void testFetchPolicyComponentsHealthStatus_PdpDown() {
228 // Set currentInstanceCount as 0 to simulate PDP down
229 groups.get(0).getPdpSubgroups().get(0).setCurrentInstanceCount(0);
230 Map<String, Object> result = callFetchPolicyComponentsHealthStatus();
231 assertFalse((Boolean) result.get(HEALTHY));
235 public void testFetchPolicyComponentsHealthStatus_unhealthyPap() {
236 when(papActivator.isAlive()).thenReturn(false);
237 Map<String, Object> result = callFetchPolicyComponentsHealthStatus();
238 assertFalse((Boolean) result.get(HEALTHY));
239 HealthCheckReport report = (HealthCheckReport) result.get(PapConstants.POLICY_PAP);
240 assertFalse(report.isHealthy());
243 private Map<String, Object> callFetchPolicyComponentsHealthStatus() {
245 return provider.fetchPolicyComponentsHealthStatus().getRight();
248 private HealthCheckReport createReport(int code, boolean healthy) {
249 HealthCheckReport report = new HealthCheckReport();
250 report.setName("name");
251 report.setUrl("url");
252 report.setCode(code);
253 report.setHealthy(healthy);
254 report.setMessage("message");
258 private static PdpGroups loadPdpGroupsFromFile() {
259 final File propFile = new File(ResourceUtils.getFilePath4Resource(PDP_GROUP_DATA_FILE));
261 Coder coder = new StandardCoder();
262 return coder.decode(propFile, PdpGroups.class);
263 } catch (final CoderException e) {
264 throw new RuntimeException(e);
268 private DmaapGetTopicResponse createDmaapResponse() {
269 DmaapGetTopicResponse response = new DmaapGetTopicResponse();
270 response.setTopics(Arrays.asList(PapConstants.TOPIC_POLICY_PDP_PAP));