PDPX Healthcheck/Statistic RESTful API entry point
[policy/xacml-pdp.git] / main / src / test / java / org / onap / policy / pdpx / main / rest / TestXacmlPdpStatistics.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
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;
31
32 import org.glassfish.jersey.client.ClientConfig;
33 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
34 import org.junit.Test;
35
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.rest.XacmlPdpStatisticsManager;
40 import org.onap.policy.pdpx.main.startstop.Main;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
43
44 /**
45  * Class to perform unit test of {@link XacmlPdpRestController}.
46  *
47  */
48 public class TestXacmlPdpStatistics {
49
50     private static final Logger LOGGER = LoggerFactory.getLogger(TestXacmlPdpStatistics.class);
51
52
53     @Test
54     public void testXacmlPdpStatistics_200() throws PolicyXacmlPdpException, InterruptedException {
55         final Main main = startXacmlPdpService();
56         StatisticsReport report = getXacmlPdpStatistics();
57
58         validateReport(report, 0, 200);
59         updateXacmlPdpStatistics();
60         report = getXacmlPdpStatistics();
61         validateReport(report, 1, 200);
62         stopXacmlPdpService(main);
63         XacmlPdpStatisticsManager.resetAllStatistics();
64     }
65
66     @Test
67     public void testXacmlPdpStatistics_500() throws InterruptedException {
68         final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false);
69         restServerParams.setName(CommonTestData.PDPX_GROUP_NAME);
70
71         final XacmlPdpRestServer restServer = new XacmlPdpRestServer(restServerParams);
72         restServer.start();
73         final StatisticsReport report = getXacmlPdpStatistics();
74
75         validateReport(report, 0, 500);
76         restServer.shutdown();
77         XacmlPdpStatisticsManager.resetAllStatistics();
78     }
79
80
81     private Main startXacmlPdpService() {
82         final String[] XacmlPdpConfigParameters =
83             { "-c", "parameters/XacmlPdpConfigParameters.json" };
84         return new Main(XacmlPdpConfigParameters);
85     }
86
87     private void stopXacmlPdpService(final Main main) throws PolicyXacmlPdpException {
88         main.shutdown();
89     }
90
91     private StatisticsReport getXacmlPdpStatistics() throws InterruptedException {
92         StatisticsReport response = null;
93         final ClientConfig clientConfig = new ClientConfig();
94
95         final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
96         clientConfig.register(feature);
97
98         final Client client = ClientBuilder.newClient(clientConfig);
99         final WebTarget webTarget = client.target("http://localhost:6969/statistics");
100
101         final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
102         final long startTime = System.currentTimeMillis();
103         while (response == null && (System.currentTimeMillis() - startTime) < 120000) {
104             try {
105                 response = invocationBuilder.get(StatisticsReport.class);
106             } catch (final Exception exp) {
107                 LOGGER.info("the server is not started yet. We will retry again");
108             }
109         }
110         return response;
111     }
112
113     private void updateXacmlPdpStatistics() {
114         XacmlPdpStatisticsManager.updateTotalPoliciesCount();
115         XacmlPdpStatisticsManager.updatePermitDecisionsCount();
116         XacmlPdpStatisticsManager.updateDenyDecisionsCount();
117         XacmlPdpStatisticsManager.updateIndeterminantDecisionsCount();
118         XacmlPdpStatisticsManager.updateNotApplicableDecisionsCount();
119     }
120
121     private void validateReport(final StatisticsReport report, final int count, final int code) {
122         assertEquals(code, report.getCode());
123         assertEquals(count, report.getTotalPoliciesCount());
124         assertEquals(count, report.getPermitDecisionsCount());
125         assertEquals(count, report.getDenyDecisionsCount());
126         assertEquals(count, report.getIndeterminantDecisionsCount());
127         assertEquals(count, report.getNotApplicableDecisionsCount());
128     }
129 }