PDPX Healthcheck/Statistic RESTful API entry point
[policy/xacml-pdp.git] / main / src / test / java / org / onap / policy / pdpx / main / rest / TestXacmlPdpRestServer.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21
22 package org.onap.policy.pdpx.main.rest;
23
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertTrue;
26
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;
42
43
44 /**
45  * Class to perform unit test of HealthCheckMonitor.
46  *
47  */
48 public class TestXacmlPdpRestServer {
49
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";
55
56     @Test
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);
63     }
64
65     @Test
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);
72         restServer.start();
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();
78     }
79
80     private Main startXacmlPdpService() {
81         final String[] xacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters.json"};
82         return new Main(xacmlPdpConfigParameters);
83     }
84
85     private void stopXacmlPdpService(final Main main) throws PolicyXacmlPdpException {
86         main.shutdown();
87     }
88
89     private HealthCheckReport performHealthCheck() throws InterruptedException {
90         HealthCheckReport response = null;
91         final ClientConfig clientConfig = new ClientConfig();
92
93         final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
94         clientConfig.register(feature);
95
96         final Client client = ClientBuilder.newClient(clientConfig);
97         final WebTarget webTarget = client.target("http://localhost:6969/healthcheck");
98
99         final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
100
101         final long startTime = System.currentTimeMillis();
102         while (response == null && (System.currentTimeMillis() - startTime) < 120000) {
103             try {
104                 response = invocationBuilder.get(HealthCheckReport.class);
105             } catch (final Exception exp) {
106                 LOGGER.info("the server is not started yet. We will retry again");
107             }
108         }
109         return response;
110     }
111
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());
120     }
121 }