2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2018 Samsung Electronics Co., Ltd. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.policy.api.main.rest;
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
26 import javax.ws.rs.client.Client;
27 import javax.ws.rs.client.ClientBuilder;
28 import javax.ws.rs.client.Invocation;
29 import javax.ws.rs.client.WebTarget;
30 import javax.ws.rs.core.MediaType;
32 import org.glassfish.jersey.client.ClientConfig;
33 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
34 import org.junit.Test;
35 import org.onap.policy.api.main.PolicyApiException;
36 import org.onap.policy.api.main.parameters.CommonTestData;
37 import org.onap.policy.api.main.parameters.RestServerParameters;
38 import org.onap.policy.api.main.startstop.Main;
39 import org.onap.policy.common.endpoints.report.HealthCheckReport;
40 import org.onap.policy.common.logging.flexlogger.FlexLogger;
41 import org.onap.policy.common.logging.flexlogger.Logger;
44 * Class to perform unit test of HealthCheckMonitor.
47 public class TestApiRestServer {
49 private static final Logger LOGGER = FlexLogger.getLogger(TestApiRestServer.class);
50 private static final String NOT_ALIVE = "not alive";
51 private static final String ALIVE = "alive";
52 private static final String SELF = "self";
53 private static final String NAME = "Policy API";
56 public void testHealthCheckSuccess() throws PolicyApiException, InterruptedException {
57 final String reportString = "Report [name=Policy API, url=self, healthy=true, code=200, message=alive]";
58 final Main main = startApiService();
59 final HealthCheckReport report = performHealthCheck();
60 validateReport(NAME, SELF, true, 200, ALIVE, reportString, report);
65 public void testHealthCheckFailure() throws InterruptedException {
66 final String reportString = "Report [name=Policy API, url=self, healthy=false, code=500, message=not alive]";
67 final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false);
68 restServerParams.setName(CommonTestData.API_GROUP_NAME);
69 final ApiRestServer restServer = new ApiRestServer(restServerParams);
71 final HealthCheckReport report = performHealthCheck();
72 validateReport(NAME, SELF, false, 500, NOT_ALIVE, reportString, report);
73 assertTrue(restServer.isAlive());
74 assertTrue(restServer.toString().startsWith("ApiRestServer [servers="));
75 restServer.shutdown();
78 private Main startApiService() {
79 final String[] apiConfigParameters = { "-c", "parameters/ApiConfigParameters.json" };
80 return new Main(apiConfigParameters);
83 private void stopApiService(final Main main) throws PolicyApiException {
87 private HealthCheckReport performHealthCheck() throws InterruptedException {
88 HealthCheckReport response = null;
89 final ClientConfig clientConfig = new ClientConfig();
91 final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
92 clientConfig.register(feature);
94 final Client client = ClientBuilder.newClient(clientConfig);
95 final WebTarget webTarget = client.target("http://localhost:6969/healthcheck");
97 final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
98 while (response == null) {
100 response = invocationBuilder.get(HealthCheckReport.class);
101 } catch (final Exception exp) {
102 LOGGER.info("the server is not started yet. We will retry again");
108 private void validateReport(final String name, final String url, final boolean healthy, final int code,
109 final String message, final String reportString, final HealthCheckReport report) {
110 assertEquals(name, report.getName());
111 assertEquals(url, report.getUrl());
112 assertEquals(healthy, report.isHealthy());
113 assertEquals(code, report.getCode());
114 assertEquals(message, report.getMessage());
115 assertEquals(reportString, report.toString());