2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2022 AT&T Intellectual Property. All rights reserved.
6 * Modifications Copyright (C) 2024 Nordix Foundation.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.drools.healthcheck;
24 import static org.junit.jupiter.api.Assertions.assertEquals;
25 import static org.junit.jupiter.api.Assertions.assertTrue;
26 import static org.mockito.Mockito.mock;
27 import static org.mockito.Mockito.when;
29 import jakarta.ws.rs.core.Response;
30 import org.junit.jupiter.api.AfterAll;
31 import org.junit.jupiter.api.BeforeAll;
32 import org.junit.jupiter.api.Test;
33 import org.onap.policy.common.endpoints.event.comm.bus.internal.BusTopicParams;
34 import org.onap.policy.common.endpoints.http.client.HttpClient;
35 import org.onap.policy.common.endpoints.http.client.HttpClientFactory;
36 import org.onap.policy.common.endpoints.http.client.HttpClientFactoryInstance;
37 import org.onap.policy.common.endpoints.http.server.HttpServletServer;
38 import org.onap.policy.common.endpoints.http.server.HttpServletServerFactoryInstance;
39 import org.onap.policy.common.endpoints.http.server.YamlJacksonHandler;
40 import org.onap.policy.common.gson.JacksonHandler;
41 import org.onap.policy.common.utils.logging.LoggerUtils;
42 import org.onap.policy.common.utils.network.NetworkUtil;
43 import org.onap.policy.drools.system.PolicyController;
44 import org.onap.policy.drools.system.PolicyControllerFactory;
47 * REST Healthcheck Tests.
49 public class RestHealthCheckTest {
51 private static HttpClientFactory clientFactory;
52 private static PolicyControllerFactory controllerFactory;
53 private static HealthCheckManager healthcheckManager;
54 private static PolicyController controller1;
55 private static HttpClient client1;
57 private static HealthCheck.Reports summary;
58 private static HttpClient client;
65 public static void setUp() throws Exception {
66 LoggerUtils.setLevel("org.onap.policy.common.endpoints", "WARN");
67 LoggerUtils.setLevel("org.eclipse", "ERROR");
68 LoggerUtils.setLevel("org.onap.policy.drools.healthcheck", "DEBUG");
69 LoggerUtils.setLevel("ROOT", "INFO");
71 clientFactory = mock(HttpClientFactory.class);
72 controllerFactory = mock(PolicyControllerFactory.class);
73 healthcheckManager = mock(HealthCheckManager.class);
74 controller1 = mock(PolicyController.class);
75 client1 = mock(HttpClient.class);
77 summary = new HealthCheck.Reports();
79 client = HttpClientFactoryInstance.getClientFactory().build(
80 BusTopicParams.builder()
81 .clientName("healthcheck")
82 .hostname("localhost")
84 .basePath("healthcheck")
88 HttpServletServer server =
89 HttpServletServerFactoryInstance
91 .build("lifecycle", "localhost", 8768, "/",
94 server.setSerializationProvider(
95 String.join(",", JacksonHandler.class.getName(),
96 YamlJacksonHandler.class.getName()));
97 server.addServletClass("/*", RestMockHealthcheck.class.getName());
98 server.waitedStart(5000L);
100 assertTrue(NetworkUtil.isTcpPortOpen("localhost", 8768, 5, 10000L));
108 public static void tearDown() {
109 HttpClientFactoryInstance.getClientFactory().destroy();
110 HttpServletServerFactoryInstance.getServerFactory().destroy();
115 when(healthcheckManager.healthCheck()).thenReturn(summary);
121 when(healthcheckManager.engineHealthcheck()).thenReturn(summary);
122 assertHttp("engine");
127 when(healthcheckManager.controllerHealthcheck()).thenReturn(summary);
128 assertHttp("controllers");
130 when(controllerFactory.get("controller1")).thenReturn(controller1);
131 when(healthcheckManager.controllerHealthcheck(controller1)).thenReturn(summary);
132 assertHttp("controllers/controller1");
134 when(controllerFactory.get("controller1")).thenThrow(new IllegalArgumentException("expected"));
135 Response resp = client.get("controllers/controller1");
136 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), resp.getStatus());
138 when(controllerFactory.get("controller2")).thenThrow(new IllegalStateException("expected"));
139 resp = client.get("controllers/controller2");
140 assertEquals(Response.Status.NOT_ACCEPTABLE.getStatusCode(), resp.getStatus());
145 when(healthcheckManager.clientHealthcheck()).thenReturn(summary);
146 assertHttp("clients");
148 when(clientFactory.get("client1")).thenReturn(client1);
149 when(healthcheckManager.clientHealthcheck(client1)).thenReturn(summary);
150 assertHttp("clients/client1");
152 when(clientFactory.get("client2")).thenThrow(new IllegalArgumentException("expected"));
153 Response resp = client.get("clients/client2");
154 assertEquals(Response.Status.NOT_FOUND.getStatusCode(), resp.getStatus());
157 private void assertHttp(String url) {
158 summary.setHealthy(true);
159 var resp = client.get(url);
160 assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
162 summary.setHealthy(false);
163 resp = client.get(url);
164 assertEquals(Response.Status.SERVICE_UNAVAILABLE.getStatusCode(), resp.getStatus());
167 public static class RestMockHealthcheck extends RestHealthCheck {
169 protected PolicyControllerFactory getControllerFactory() {
170 return controllerFactory;
174 protected HttpClientFactory getClientFactory() {
175 return clientFactory;
179 protected HealthCheck getHealthcheckManager() {
180 return healthcheckManager;