ee05ff06ee5b37a4e51b3597507a8666605232ad
[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 import static org.junit.Assert.fail;
26
27 import java.io.IOException;
28 import javax.ws.rs.client.Client;
29 import javax.ws.rs.client.ClientBuilder;
30 import javax.ws.rs.client.Invocation;
31 import javax.ws.rs.client.WebTarget;
32 import javax.ws.rs.core.MediaType;
33 import org.glassfish.jersey.client.ClientConfig;
34 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
35 import org.junit.Test;
36 import org.onap.policy.common.utils.network.NetworkUtil;
37 import org.onap.policy.pdpx.main.PolicyXacmlPdpException;
38 import org.onap.policy.pdpx.main.parameters.CommonTestData;
39 import org.onap.policy.pdpx.main.parameters.RestServerParameters;
40 import org.onap.policy.pdpx.main.rest.XacmlPdpStatisticsManager;
41 import org.onap.policy.pdpx.main.rest.model.StatisticsReport;
42 import org.onap.policy.pdpx.main.startstop.Main;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 /**
47  * Class to perform unit test of {@link XacmlPdpRestController}.
48  *
49  */
50 public class TestXacmlPdpStatistics {
51
52     private static final Logger LOGGER = LoggerFactory.getLogger(TestXacmlPdpStatistics.class);
53
54     @Test
55     public void testXacmlPdpStatistics_200() throws PolicyXacmlPdpException, InterruptedException {
56         try {
57             final Main main = startXacmlPdpService();
58             StatisticsReport report = getXacmlPdpStatistics();
59             validateReport(report, 0, 200);
60             updateXacmlPdpStatistics();
61             report = getXacmlPdpStatistics();
62             validateReport(report, 1, 200);
63             stopXacmlPdpService(main);
64             XacmlPdpStatisticsManager.resetAllStatistics();
65         } catch (final Exception e) {
66             LOGGER.error("testApiStatistics_200 failed", e);
67             fail("Test should not throw an exception");
68         }
69     }
70
71     @Test
72     public void testXacmlPdpStatistics_500() throws InterruptedException {
73         final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false);
74         restServerParams.setName(CommonTestData.PDPX_GROUP_NAME);
75         final XacmlPdpRestServer restServer = new XacmlPdpRestServer(restServerParams);
76
77         try {
78             restServer.start();
79             final StatisticsReport report = getXacmlPdpStatistics();
80             validateReport(report, 0, 500);
81             restServer.shutdown();
82             XacmlPdpStatisticsManager.resetAllStatistics();
83         } catch (final Exception e) {
84             LOGGER.error("testApiStatistics_500 failed", e);
85             fail("Test should not throw an exception");
86         }
87     }
88
89
90     private Main startXacmlPdpService() {
91         final String[] XacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters.json"};
92         return new Main(XacmlPdpConfigParameters);
93     }
94
95     private void stopXacmlPdpService(final Main main) throws PolicyXacmlPdpException {
96         main.shutdown();
97     }
98
99     private StatisticsReport getXacmlPdpStatistics() throws InterruptedException, IOException {
100
101         final ClientConfig clientConfig = new ClientConfig();
102
103         final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
104         clientConfig.register(feature);
105
106         final Client client = ClientBuilder.newClient(clientConfig);
107         final WebTarget webTarget = client.target("http://localhost:6969/policy/pdpx/v1/statistics");
108
109         final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
110
111         if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) {
112             throw new IllegalStateException("Cannot connect to port 6969");
113         }
114
115         return invocationBuilder.get(StatisticsReport.class);
116     }
117
118     private void updateXacmlPdpStatistics() {
119         XacmlPdpStatisticsManager.updateTotalPoliciesCount();
120         XacmlPdpStatisticsManager.updatePermitDecisionsCount();
121         XacmlPdpStatisticsManager.updateDenyDecisionsCount();
122         XacmlPdpStatisticsManager.updateIndeterminantDecisionsCount();
123         XacmlPdpStatisticsManager.updateNotApplicableDecisionsCount();
124     }
125
126     private void validateReport(final StatisticsReport report, final int count, final int code) {
127         assertEquals(code, report.getCode());
128         assertEquals(count, report.getTotalPoliciesCount());
129         assertEquals(count, report.getPermitDecisionsCount());
130         assertEquals(count, report.getDenyDecisionsCount());
131         assertEquals(count, report.getIndeterminantDecisionsCount());
132         assertEquals(count, report.getNotApplicableDecisionsCount());
133     }
134 }