Xacml PDP Register/Unregister Changes
[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             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() throws TopicSinkClientException {
115         final String[] XacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters.json", "-p",
116             "parameters/topic.properties"};
117         return new Main(XacmlPdpConfigParameters);
118     }
119
120     private void stopXacmlPdpService(final Main main) throws PolicyXacmlPdpException {
121         main.shutdown();
122     }
123
124     private StatisticsReport getXacmlPdpStatistics() throws InterruptedException, IOException {
125
126         final ClientConfig clientConfig = new ClientConfig();
127
128         final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
129         clientConfig.register(feature);
130
131         final Client client = ClientBuilder.newClient(clientConfig);
132         final WebTarget webTarget = client.target("http://localhost:6969/policy/pdpx/v1/statistics");
133
134         final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
135
136         if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) {
137             throw new IllegalStateException("Cannot connect to port 6969");
138         }
139
140         return invocationBuilder.get(StatisticsReport.class);
141     }
142
143     private void updateXacmlPdpStatistics() {
144         XacmlPdpStatisticsManager.updateTotalPoliciesCount();
145         XacmlPdpStatisticsManager.updatePermitDecisionsCount();
146         XacmlPdpStatisticsManager.updateDenyDecisionsCount();
147         XacmlPdpStatisticsManager.updateIndeterminantDecisionsCount();
148         XacmlPdpStatisticsManager.updateNotApplicableDecisionsCount();
149     }
150
151     private void validateReport(final StatisticsReport report, final int count, final int code) {
152         assertEquals(code, report.getCode());
153         assertEquals(count, report.getTotalPoliciesCount());
154         assertEquals(count, report.getPermitDecisionsCount());
155         assertEquals(count, report.getDenyDecisionsCount());
156         assertEquals(count, report.getIndeterminantDecisionsCount());
157         assertEquals(count, report.getNotApplicableDecisionsCount());
158     }
159 }