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