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;
27 import javax.ws.rs.client.Client;
28 import javax.ws.rs.client.ClientBuilder;
29 import javax.ws.rs.client.Invocation;
30 import javax.ws.rs.client.WebTarget;
31 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.common.endpoints.report.HealthCheckReport;
36 import org.onap.policy.pdpx.main.PolicyXacmlPdpException;
37 import org.onap.policy.pdpx.main.parameters.CommonTestData;
38 import org.onap.policy.pdpx.main.parameters.RestServerParameters;
39 import org.onap.policy.pdpx.main.startstop.Main;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
45 * Class to perform unit test of HealthCheckMonitor.
48 public class TestXacmlPdpRestServer {
50 private static final Logger LOGGER = LoggerFactory.getLogger(TestXacmlPdpRestServer.class);
51 private static final String NOT_ALIVE = "not alive";
52 private static final String ALIVE = "alive";
53 private static final String SELF = "self";
54 private static final String NAME = "Policy Xacml PDP";
57 public void testHealthCheckSuccess() throws PolicyXacmlPdpException, InterruptedException {
58 final String reportString = "Report [name=Policy Xacml PDP, url=self, healthy=true, code=200, message=alive]";
59 final Main main = startXacmlPdpService();
60 final HealthCheckReport report = performHealthCheck();
61 validateReport(NAME, SELF, true, 200, ALIVE, reportString, report);
62 stopXacmlPdpService(main);
66 public void testHealthCheckFailure() throws InterruptedException {
67 final String reportString =
68 "Report [name=Policy Xacml PDP, url=self, healthy=false, code=500, message=not alive]";
69 final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false);
70 restServerParams.setName(CommonTestData.PDPX_GROUP_NAME);
71 final XacmlPdpRestServer restServer = new XacmlPdpRestServer(restServerParams);
73 final HealthCheckReport report = performHealthCheck();
74 validateReport(NAME, SELF, false, 500, NOT_ALIVE, reportString, report);
75 assertTrue(restServer.isAlive());
76 assertTrue(restServer.toString().startsWith("XacmlPdpRestServer [servers="));
77 restServer.shutdown();
80 private Main startXacmlPdpService() {
81 final String[] xacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters.json"};
82 return new Main(xacmlPdpConfigParameters);
85 private void stopXacmlPdpService(final Main main) throws PolicyXacmlPdpException {
89 private HealthCheckReport performHealthCheck() throws InterruptedException {
90 HealthCheckReport response = null;
91 final ClientConfig clientConfig = new ClientConfig();
93 final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
94 clientConfig.register(feature);
96 final Client client = ClientBuilder.newClient(clientConfig);
97 final WebTarget webTarget = client.target("http://localhost:6969/healthcheck");
99 final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
101 final long startTime = System.currentTimeMillis();
102 while (response == null && (System.currentTimeMillis() - startTime) < 120000) {
104 response = invocationBuilder.get(HealthCheckReport.class);
105 } catch (final Exception exp) {
106 LOGGER.info("the server is not started yet. We will retry again");
112 private void validateReport(final String name, final String url, final boolean healthy, final int code,
113 final String message, final String reportString, final HealthCheckReport report) {
114 assertEquals(name, report.getName());
115 assertEquals(url, report.getUrl());
116 assertEquals(healthy, report.isHealthy());
117 assertEquals(code, report.getCode());
118 assertEquals(message, report.getMessage());
119 assertEquals(reportString, report.toString());