6a762924baefc3d5070eea0dd2d6b1f6e7ab0941
[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.BeforeClass;
38 import org.junit.ClassRule;
39 import org.junit.Test;
40 import org.junit.rules.TemporaryFolder;
41 import org.onap.policy.common.endpoints.event.comm.client.TopicSinkClientException;
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             LOGGER.info("*************************** Running testXacmlPdpStatistics_200 ***************************");
80             final Main main = startXacmlPdpService();
81             StatisticsReport report = getXacmlPdpStatistics();
82             validateReport(report, 0, 200);
83             assertThat(report.getTotalPolicyTypesCount()).isGreaterThan(0);
84             updateXacmlPdpStatistics();
85             report = getXacmlPdpStatistics();
86             validateReport(report, 1, 200);
87             stopXacmlPdpService(main);
88             XacmlPdpStatisticsManager.resetAllStatistics();
89         } catch (final Exception e) {
90             LOGGER.error("testApiStatistics_200 failed", e);
91             fail("Test should not throw an exception");
92         }
93     }
94
95     @Test
96     public void testXacmlPdpStatistics_500() throws InterruptedException {
97         LOGGER.info("***************************** Running testXacmlPdpStatistics_500 *****************************");
98         final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false);
99         restServerParams.setName(CommonTestData.PDPX_GROUP_NAME);
100         final XacmlPdpRestServer restServer = new XacmlPdpRestServer(restServerParams,
101                 applicationPath.getAbsolutePath());
102
103         try {
104             restServer.start();
105             final StatisticsReport report = getXacmlPdpStatistics();
106             validateReport(report, 0, 500);
107             restServer.shutdown();
108             XacmlPdpStatisticsManager.resetAllStatistics();
109         } catch (final Exception e) {
110             LOGGER.error("testApiStatistics_500 failed", e);
111             fail("Test should not throw an exception");
112         }
113     }
114
115
116     private Main startXacmlPdpService() throws TopicSinkClientException {
117         final String[] XacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters.json", "-p",
118             "parameters/topic.properties"};
119         return new Main(XacmlPdpConfigParameters);
120     }
121
122     private void stopXacmlPdpService(final Main main) throws PolicyXacmlPdpException {
123         main.shutdown();
124     }
125
126     private StatisticsReport getXacmlPdpStatistics() throws InterruptedException, IOException {
127
128         final ClientConfig clientConfig = new ClientConfig();
129
130         final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
131         clientConfig.register(feature);
132
133         final Client client = ClientBuilder.newClient(clientConfig);
134         final WebTarget webTarget = client.target("http://localhost:6969/policy/pdpx/v1/statistics");
135
136         final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
137
138         if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 20, 1000L)) {
139             throw new IllegalStateException("Cannot connect to port 6969");
140         }
141
142         return invocationBuilder.get(StatisticsReport.class);
143     }
144
145     private void updateXacmlPdpStatistics() {
146         XacmlPdpStatisticsManager.updateTotalPoliciesCount();
147         XacmlPdpStatisticsManager.updatePermitDecisionsCount();
148         XacmlPdpStatisticsManager.updateDenyDecisionsCount();
149         XacmlPdpStatisticsManager.updateIndeterminantDecisionsCount();
150         XacmlPdpStatisticsManager.updateNotApplicableDecisionsCount();
151     }
152
153     private void validateReport(final StatisticsReport report, final int count, final int code) {
154         assertEquals(code, report.getCode());
155         assertEquals(count, report.getTotalPoliciesCount());
156         assertEquals(count, report.getPermitDecisionsCount());
157         assertEquals(count, report.getDenyDecisionsCount());
158         assertEquals(count, report.getIndeterminantDecisionsCount());
159         assertEquals(count, report.getNotApplicableDecisionsCount());
160     }
161 }