2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 AT&T Intellectual Property. 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=========================================================
22 package org.onap.policy.pdpx.main.rest;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26 import static org.junit.Assert.fail;
28 import java.io.IOException;
29 import javax.ws.rs.client.Client;
30 import javax.ws.rs.client.ClientBuilder;
31 import javax.ws.rs.client.Invocation;
32 import javax.ws.rs.client.WebTarget;
33 import javax.ws.rs.core.MediaType;
34 import org.glassfish.jersey.client.ClientConfig;
35 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
36 import org.junit.Test;
37 import org.onap.policy.common.endpoints.report.HealthCheckReport;
38 import org.onap.policy.common.utils.network.NetworkUtil;
39 import org.onap.policy.pdpx.main.PolicyXacmlPdpException;
40 import org.onap.policy.pdpx.main.parameters.CommonTestData;
41 import org.onap.policy.pdpx.main.parameters.RestServerParameters;
42 import org.onap.policy.pdpx.main.startstop.Main;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
48 * Class to perform unit test of HealthCheckMonitor.
51 public class TestXacmlPdpRestServer {
53 private static final Logger LOGGER = LoggerFactory.getLogger(TestXacmlPdpRestServer.class);
54 private static final String NOT_ALIVE = "not alive";
55 private static final String ALIVE = "alive";
56 private static final String SELF = "self";
57 private static final String NAME = "Policy Xacml PDP";
60 public void testHealthCheckSuccess() throws PolicyXacmlPdpException, InterruptedException {
61 final String reportString = "Report [name=Policy Xacml PDP, url=self, healthy=true, code=200, message=alive]";
63 final Main main = startXacmlPdpService();
64 final HealthCheckReport report = performHealthCheck();
65 validateReport(NAME, SELF, true, 200, ALIVE, reportString, report);
66 stopXacmlPdpService(main);
67 } catch (final Exception e) {
68 LOGGER.error("testHealthCheckSuccess failed", e);
69 fail("Test should not throw an exception");
75 public void testHealthCheckFailure() throws InterruptedException {
76 final String reportString =
77 "Report [name=Policy Xacml PDP, url=self, healthy=false, code=500, message=not alive]";
78 final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false);
79 restServerParams.setName(CommonTestData.PDPX_GROUP_NAME);
80 final XacmlPdpRestServer restServer = new XacmlPdpRestServer(restServerParams);
84 final HealthCheckReport report = performHealthCheck();
85 validateReport(NAME, SELF, false, 500, NOT_ALIVE, reportString, report);
86 assertTrue(restServer.isAlive());
87 assertTrue(restServer.toString().startsWith("XacmlPdpRestServer [servers="));
88 restServer.shutdown();
89 } catch (final Exception e) {
90 LOGGER.error("testHealthCheckSuccess failed", e);
91 fail("Test should not throw an exception");
95 private Main startXacmlPdpService() {
96 final String[] xacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters.json"};
97 return new Main(xacmlPdpConfigParameters);
100 private void stopXacmlPdpService(final Main main) throws PolicyXacmlPdpException {
104 private HealthCheckReport performHealthCheck() throws InterruptedException, IOException {
106 final ClientConfig clientConfig = new ClientConfig();
108 final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
109 clientConfig.register(feature);
111 final Client client = ClientBuilder.newClient(clientConfig);
112 final WebTarget webTarget = client.target("http://localhost:6969/healthcheck");
114 final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
116 if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) {
117 throw new IllegalStateException("Cannot connect to port 6969");
120 return invocationBuilder.get(HealthCheckReport.class);
123 private void validateReport(final String name, final String url, final boolean healthy, final int code,
124 final String message, final String reportString, final HealthCheckReport report) {
125 assertEquals(name, report.getName());
126 assertEquals(url, report.getUrl());
127 assertEquals(healthy, report.isHealthy());
128 assertEquals(code, report.getCode());
129 assertEquals(message, report.getMessage());
130 assertEquals(reportString, report.toString());