2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2020 Nordix Foundation.
4 * Modifications Copyright (C) 2020 AT&T Corp.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pap.main.rest;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertFalse;
26 import static org.junit.Assert.assertTrue;
27 import static org.mockito.ArgumentMatchers.any;
28 import static org.mockito.Mockito.when;
31 import java.net.HttpURLConnection;
32 import java.util.List;
34 import javax.ws.rs.core.Response;
35 import javax.ws.rs.core.Response.Status;
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.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
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.models.provider.PolicyModelsProvider;
57 import org.onap.policy.pap.main.PapConstants;
58 import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper;
59 import org.onap.policy.pap.main.parameters.CommonTestData;
60 import org.onap.policy.pap.main.parameters.PapParameterGroup;
61 import org.onap.policy.pap.main.startstop.PapActivator;
63 public 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";
70 private PolicyModelsProvider dao;
73 private PolicyModelsProviderFactoryWrapper daofact;
76 private HttpClientFactory clientFactory;
79 PapActivator papActivator;
82 private HttpClient client1;
85 private HttpClient client2;
88 private Response response1;
91 private Response response2;
93 private List<PdpGroup> groups;
95 private PapParameterGroup savedPapParameterGroup;
98 * Configures mocks and objects.
100 * @throws Exception if an error occurs
103 public void setUp() throws Exception {
104 MockitoAnnotations.initMocks(this);
106 groups = loadPdpGroupsFromFile().getGroups();
107 when(dao.getPdpGroups(any())).thenReturn(groups);
109 when(daofact.create()).thenReturn(dao);
110 Registry.newRegistry();
111 Registry.register(PapConstants.REG_PAP_DAO_FACTORY, daofact);
113 when(papActivator.isAlive()).thenReturn(true);
114 Registry.register(PapConstants.REG_PAP_ACTIVATOR, papActivator);
116 if (ParameterService.contains(PAP_GROUP_PARAMS_NAME)) {
117 savedPapParameterGroup = ParameterService.get(PAP_GROUP_PARAMS_NAME);
119 CommonTestData testData = new CommonTestData();
120 ParameterService.register(testData.getPapParameterGroup(0), true);
122 when(client1.getName()).thenReturn(CLIENT_1);
123 when(client1.getBaseUrl()).thenReturn("url1");
124 when(response1.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
125 when(response1.readEntity(HealthCheckReport.class)).thenReturn(createReport(HttpURLConnection.HTTP_OK, true));
126 when(client1.get()).thenReturn(response1);
128 when(client2.getName()).thenReturn("client2");
129 when(client2.getBaseUrl()).thenReturn("url2");
130 when(response2.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
131 when(response2.readEntity(HealthCheckReport.class)).thenReturn(createReport(HttpURLConnection.HTTP_OK, true));
132 when(client2.get()).thenReturn(response2);
134 PapParameterGroup papParameterGroup = ParameterService.get(PAP_GROUP_PARAMS_NAME);
135 List<BusTopicParams> params = papParameterGroup.getHealthCheckRestClientParameters();
136 when(clientFactory.build(params.get(0))).thenReturn(client1);
137 when(clientFactory.build(params.get(1))).thenReturn(client2);
144 public void tearDown() {
145 if (savedPapParameterGroup != null) {
146 ParameterService.register(savedPapParameterGroup, true);
148 ParameterService.deregister(PAP_GROUP_PARAMS_NAME);
153 public void testFetchPolicyComponentsHealthStatus_allHealthy() throws Exception {
154 PolicyComponentsHealthCheckProvider provider = new PolicyComponentsHealthCheckProvider(clientFactory);
155 Pair<Status, Map<String, Object>> ret = provider.fetchPolicyComponentsHealthStatus();
156 assertEquals(Response.Status.OK, ret.getLeft());
157 assertTrue((Boolean) ret.getRight().get("healthy"));
161 public void testFetchPolicyComponentsHealthStatus_unhealthyClient() throws Exception {
162 when(response1.getStatus()).thenReturn(HttpURLConnection.HTTP_INTERNAL_ERROR);
163 when(response1.readEntity(HealthCheckReport.class))
164 .thenReturn(createReport(HttpURLConnection.HTTP_INTERNAL_ERROR, false));
165 Map<String, Object> result = callFetchPolicyComponentsHealthStatus();
166 assertFalse((Boolean) result.get("healthy"));
167 HealthCheckReport report = (HealthCheckReport) result.get(CLIENT_1);
168 assertFalse(report.isHealthy());
170 when(response1.getStatus()).thenReturn(HttpURLConnection.HTTP_OK);
171 when(response1.readEntity(HealthCheckReport.class)).thenReturn(createReport(HttpURLConnection.HTTP_OK, false));
172 Map<String, Object> result2 = callFetchPolicyComponentsHealthStatus();
173 assertFalse((Boolean) result2.get("healthy"));
174 HealthCheckReport report2 = (HealthCheckReport) result.get(CLIENT_1);
175 assertFalse(report2.isHealthy());
178 @SuppressWarnings("unchecked")
180 public void testFetchPolicyComponentsHealthStatus_unhealthyPdps() throws Exception {
181 // Get a PDP and set it unhealthy
182 groups.get(0).getPdpSubgroups().get(0).getPdpInstances().get(0).setHealthy(PdpHealthStatus.NOT_HEALTHY);
183 Map<String, Object> result = callFetchPolicyComponentsHealthStatus();
184 Map<String, List<Pdp>> pdpListWithType = (Map<String, List<Pdp>>) result.get(PapConstants.POLICY_PDPS);
185 assertEquals(2, pdpListWithType.size());
186 assertFalse((Boolean) result.get("healthy"));
190 public void testFetchPolicyComponentsHealthStatus_unhealthyPap() throws Exception {
191 when(papActivator.isAlive()).thenReturn(false);
192 Map<String, Object> result = callFetchPolicyComponentsHealthStatus();
193 assertFalse((Boolean) result.get("healthy"));
194 HealthCheckReport report = (HealthCheckReport) result.get(PapConstants.POLICY_PAP);
195 assertFalse(report.isHealthy());
198 private Map<String, Object> callFetchPolicyComponentsHealthStatus() throws Exception {
199 PolicyComponentsHealthCheckProvider provider = new PolicyComponentsHealthCheckProvider(clientFactory);
200 return provider.fetchPolicyComponentsHealthStatus().getRight();
203 private HealthCheckReport createReport(int code, boolean healthy) {
204 HealthCheckReport report = new HealthCheckReport();
205 report.setName("name");
206 report.setUrl("url");
207 report.setCode(code);
208 report.setHealthy(healthy);
209 report.setMessage("message");
213 private static PdpGroups loadPdpGroupsFromFile() {
214 final File propFile = new File(ResourceUtils.getFilePath4Resource(PDP_GROUP_DATA_FILE));
216 Coder coder = new StandardCoder();
217 return coder.decode(propFile, PdpGroups.class);
218 } catch (final CoderException e) {
219 throw new RuntimeException(e);