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.Arrays;
34 import java.util.List;
36 import javax.ws.rs.core.Response;
37 import javax.ws.rs.core.Response.Status;
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.parameters.RestClientParameters;
48 import org.onap.policy.common.endpoints.report.HealthCheckReport;
49 import org.onap.policy.common.parameters.ParameterService;
50 import org.onap.policy.common.utils.coder.Coder;
51 import org.onap.policy.common.utils.coder.CoderException;
52 import org.onap.policy.common.utils.coder.StandardCoder;
53 import org.onap.policy.common.utils.resources.ResourceUtils;
54 import org.onap.policy.common.utils.services.Registry;
55 import org.onap.policy.models.pdp.concepts.Pdp;
56 import org.onap.policy.models.pdp.concepts.PdpGroup;
57 import org.onap.policy.models.pdp.concepts.PdpGroups;
58 import org.onap.policy.models.pdp.enums.PdpHealthStatus;
59 import org.onap.policy.models.provider.PolicyModelsProvider;
60 import org.onap.policy.pap.main.PapConstants;
61 import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper;
62 import org.onap.policy.pap.main.parameters.CommonTestData;
63 import org.onap.policy.pap.main.parameters.PapParameterGroup;
64 import org.onap.policy.pap.main.startstop.PapActivator;
66 @RunWith(MockitoJUnitRunner.class)
67 public class TestPolicyComponentsHealthCheckProvider {
69 private static final String CLIENT_1 = "client1";
70 private static final String PDP_GROUP_DATA_FILE = "rest/pdpGroup.json";
71 private static final String PAP_GROUP_PARAMS_NAME = "PapGroup";
72 private static final String HEALTHY = "healthy";
75 private PolicyModelsProvider dao;
78 private PolicyModelsProviderFactoryWrapper daofact;
81 private HttpClientFactory clientFactory;
84 PapActivator papActivator;
87 private HttpClient client1;
90 private HttpClient client2;
93 private HttpClient client3;
96 private Response response1;
99 private Response response2;
102 private Response response3;
104 private List<PdpGroup> groups;
106 private PapParameterGroup savedPapParameterGroup;
109 * Configures mocks and objects.
111 * @throws Exception if an error occurs
114 public void setUp() throws Exception {
115 groups = loadPdpGroupsFromFile().getGroups();
116 when(dao.getPdpGroups(any())).thenReturn(groups);
118 when(daofact.create()).thenReturn(dao);
119 Registry.newRegistry();
120 Registry.register(PapConstants.REG_PAP_DAO_FACTORY, daofact);
122 when(papActivator.isAlive()).thenReturn(true);
123 Registry.register(PapConstants.REG_PAP_ACTIVATOR, papActivator);
125 if (ParameterService.contains(PAP_GROUP_PARAMS_NAME)) {
126 savedPapParameterGroup = ParameterService.get(PAP_GROUP_PARAMS_NAME);
128 CommonTestData testData = new CommonTestData();
129 ParameterService.register(testData.getPapParameterGroup(0), true);
131 when(client1.getName()).thenReturn(CLIENT_1);
132 when(client1.getBaseUrl()).thenReturn("url1");
133 when(response1.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
134 when(response1.readEntity(HealthCheckReport.class)).thenReturn(createReport(HttpURLConnection.HTTP_OK, true));
135 when(client1.get()).thenReturn(response1);
137 when(client2.getName()).thenReturn("client2");
138 when(client2.getBaseUrl()).thenReturn("url2");
139 when(response2.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
140 when(response2.readEntity(HealthCheckReport.class)).thenReturn(createReport(HttpURLConnection.HTTP_OK, true));
141 when(client2.get()).thenReturn(response2);
143 when(client3.getName()).thenReturn("dmaap");
144 when(client3.getBaseUrl()).thenReturn("message-router");
145 when(response3.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
146 when(response3.readEntity(DmaapGetTopicResponse.class)).thenReturn(createDmaapResponse());
147 when(client3.get()).thenReturn(response3);
149 PapParameterGroup papParameterGroup = ParameterService.get(PAP_GROUP_PARAMS_NAME);
150 List<RestClientParameters> params = papParameterGroup.getHealthCheckRestClientParameters();
151 when(clientFactory.build(params.get(0))).thenReturn(client1);
152 when(clientFactory.build(params.get(1))).thenReturn(client2);
153 when(clientFactory.build(params.get(2))).thenReturn(client3);
155 PolicyComponentsHealthCheckProvider.initializeClientHealthCheckExecutorService(papParameterGroup,
163 public void tearDown() {
164 if (savedPapParameterGroup != null) {
165 ParameterService.register(savedPapParameterGroup, true);
167 ParameterService.deregister(PAP_GROUP_PARAMS_NAME);
169 PolicyComponentsHealthCheckProvider.cleanup();
174 public void testFetchPolicyComponentsHealthStatus_allHealthy() {
175 PolicyComponentsHealthCheckProvider provider = new PolicyComponentsHealthCheckProvider();
176 Pair<Status, Map<String, Object>> ret = provider.fetchPolicyComponentsHealthStatus();
177 assertEquals(Response.Status.OK, ret.getLeft());
178 assertTrue((Boolean) ret.getRight().get(HEALTHY));
182 public void testFetchPolicyComponentsHealthStatus_unhealthyClient() {
183 when(response1.getStatus()).thenReturn(HttpURLConnection.HTTP_INTERNAL_ERROR);
184 when(response1.readEntity(HealthCheckReport.class))
185 .thenReturn(createReport(HttpURLConnection.HTTP_INTERNAL_ERROR, false));
186 Map<String, Object> result = callFetchPolicyComponentsHealthStatus();
187 assertFalse((Boolean) result.get(HEALTHY));
188 HealthCheckReport report = (HealthCheckReport) result.get(CLIENT_1);
189 assertFalse(report.isHealthy());
191 when(response1.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
192 when(response1.readEntity(HealthCheckReport.class)).thenReturn(createReport(HttpURLConnection.HTTP_OK, false));
193 Map<String, Object> result2 = callFetchPolicyComponentsHealthStatus();
194 assertFalse((Boolean) result2.get(HEALTHY));
195 HealthCheckReport report2 = (HealthCheckReport) result.get(CLIENT_1);
196 assertFalse(report2.isHealthy());
198 when(response3.getStatus()).thenReturn(HttpURLConnection.HTTP_INTERNAL_ERROR);
199 when(response3.readEntity(DmaapGetTopicResponse.class)).thenReturn(null);
200 Map<String, Object> result3 = callFetchPolicyComponentsHealthStatus();
201 assertFalse((Boolean) result3.get(HEALTHY));
202 HealthCheckReport report3 = (HealthCheckReport) result3.get("dmaap");
203 assertFalse(report3.isHealthy());
205 when(response3.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
206 when(response3.readEntity(DmaapGetTopicResponse.class)).thenReturn(new DmaapGetTopicResponse());
207 Map<String, Object> result4 = callFetchPolicyComponentsHealthStatus();
208 assertFalse((Boolean) result4.get(HEALTHY));
209 HealthCheckReport report4 = (HealthCheckReport) result4.get("dmaap");
210 assertFalse(report4.isHealthy());
213 @SuppressWarnings("unchecked")
215 public void testFetchPolicyComponentsHealthStatus_unhealthyPdps() {
216 // Get a PDP and set it unhealthy
217 groups.get(0).getPdpSubgroups().get(0).getPdpInstances().get(0).setHealthy(PdpHealthStatus.NOT_HEALTHY);
218 Map<String, Object> result = callFetchPolicyComponentsHealthStatus();
219 Map<String, List<Pdp>> pdpListWithType = (Map<String, List<Pdp>>) result.get(PapConstants.POLICY_PDPS);
220 assertEquals(2, pdpListWithType.size());
221 assertFalse((Boolean) result.get(HEALTHY));
225 public void testFetchPolicyComponentsHealthStatus_PdpDown() {
226 // Set currentInstanceCount as 0 to simulate PDP down
227 groups.get(0).getPdpSubgroups().get(0).setCurrentInstanceCount(0);
228 Map<String, Object> result = callFetchPolicyComponentsHealthStatus();
229 assertFalse((Boolean) result.get(HEALTHY));
233 public void testFetchPolicyComponentsHealthStatus_unhealthyPap() {
234 when(papActivator.isAlive()).thenReturn(false);
235 Map<String, Object> result = callFetchPolicyComponentsHealthStatus();
236 assertFalse((Boolean) result.get(HEALTHY));
237 HealthCheckReport report = (HealthCheckReport) result.get(PapConstants.POLICY_PAP);
238 assertFalse(report.isHealthy());
241 private Map<String, Object> callFetchPolicyComponentsHealthStatus() {
242 PolicyComponentsHealthCheckProvider provider = new PolicyComponentsHealthCheckProvider();
243 return provider.fetchPolicyComponentsHealthStatus().getRight();
246 private HealthCheckReport createReport(int code, boolean healthy) {
247 HealthCheckReport report = new HealthCheckReport();
248 report.setName("name");
249 report.setUrl("url");
250 report.setCode(code);
251 report.setHealthy(healthy);
252 report.setMessage("message");
256 private static PdpGroups loadPdpGroupsFromFile() {
257 final File propFile = new File(ResourceUtils.getFilePath4Resource(PDP_GROUP_DATA_FILE));
259 Coder coder = new StandardCoder();
260 return coder.decode(propFile, PdpGroups.class);
261 } catch (final CoderException e) {
262 throw new RuntimeException(e);
266 private DmaapGetTopicResponse createDmaapResponse() {
267 DmaapGetTopicResponse response = new DmaapGetTopicResponse();
268 response.setTopics(Arrays.asList(PapConstants.TOPIC_POLICY_PDP_PAP));