Removing deprecated DMAAP library
[policy/drools-pdp.git] / feature-healthcheck / src / test / java / org / onap / policy / drools / healthcheck / RestHealthCheckTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP
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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.policy.drools.healthcheck;
23
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;
28
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;
45
46 /**
47  * REST Healthcheck Tests.
48  */
49 public class RestHealthCheckTest {
50
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;
56
57     private static HealthCheck.Reports summary;
58     private static HttpClient client;
59
60     /**
61      * Set up.
62      */
63
64     @BeforeAll
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");
70
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);
76
77         summary = new HealthCheck.Reports();
78
79         client = HttpClientFactoryInstance.getClientFactory().build(
80                     BusTopicParams.builder()
81                         .clientName("healthcheck")
82                         .hostname("localhost")
83                         .port(8768)
84                         .basePath("healthcheck")
85                         .managed(true)
86                         .build());
87
88         HttpServletServer server =
89             HttpServletServerFactoryInstance
90                 .getServerFactory()
91                 .build("lifecycle", "localhost", 8768, "/",
92                     true, true);
93
94         server.setSerializationProvider(
95                 String.join(",", JacksonHandler.class.getName(),
96                         YamlJacksonHandler.class.getName()));
97         server.addServletClass("/*", RestMockHealthcheck.class.getName());
98         server.waitedStart(5000L);
99
100         assertTrue(NetworkUtil.isTcpPortOpen("localhost", 8768, 5, 10000L));
101     }
102
103     /**
104      * Tear down.
105      */
106
107     @AfterAll
108     public static void tearDown() {
109         HttpClientFactoryInstance.getClientFactory().destroy();
110         HttpServletServerFactoryInstance.getServerFactory().destroy();
111     }
112
113     @Test
114     void healthcheck() {
115         when(healthcheckManager.healthCheck()).thenReturn(summary);
116         assertHttp("/");
117     }
118
119     @Test
120     void engine() {
121         when(healthcheckManager.engineHealthcheck()).thenReturn(summary);
122         assertHttp("engine");
123     }
124
125     @Test
126     void controllers() {
127         when(healthcheckManager.controllerHealthcheck()).thenReturn(summary);
128         assertHttp("controllers");
129
130         when(controllerFactory.get("controller1")).thenReturn(controller1);
131         when(healthcheckManager.controllerHealthcheck(controller1)).thenReturn(summary);
132         assertHttp("controllers/controller1");
133
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());
137
138         when(controllerFactory.get("controller2")).thenThrow(new IllegalStateException("expected"));
139         resp = client.get("controllers/controller2");
140         assertEquals(Response.Status.NOT_ACCEPTABLE.getStatusCode(), resp.getStatus());
141     }
142
143     @Test
144     void clients() {
145         when(healthcheckManager.clientHealthcheck()).thenReturn(summary);
146         assertHttp("clients");
147
148         when(clientFactory.get("client1")).thenReturn(client1);
149         when(healthcheckManager.clientHealthcheck(client1)).thenReturn(summary);
150         assertHttp("clients/client1");
151
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());
155     }
156
157     private void assertHttp(String url) {
158         summary.setHealthy(true);
159         var resp = client.get(url);
160         assertEquals(Response.Status.OK.getStatusCode(), resp.getStatus());
161
162         summary.setHealthy(false);
163         resp = client.get(url);
164         assertEquals(Response.Status.SERVICE_UNAVAILABLE.getStatusCode(), resp.getStatus());
165     }
166
167     public static class RestMockHealthcheck extends RestHealthCheck {
168         @Override
169         protected PolicyControllerFactory getControllerFactory() {
170             return controllerFactory;
171         }
172
173         @Override
174         protected HttpClientFactory getClientFactory() {
175             return clientFactory;
176         }
177
178         @Override
179         protected HealthCheck getHealthcheckManager() {
180             return healthcheckManager;
181         }
182
183     }
184 }