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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
22 package org.onap.policy.pdpx.main.rest;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.fail;
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;
53 * Class to perform unit test of {@link XacmlPdpRestController}.
56 public class TestXacmlPdpStatistics {
58 private static final Logger LOGGER = LoggerFactory.getLogger(TestXacmlPdpStatistics.class);
59 private static File applicationPath;
62 public static final TemporaryFolder applicationFolder = new TemporaryFolder();
65 * Turn off some debugging and create temporary folder for applications.
67 * @throws IOException If temporary folder fails
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();
77 public void testXacmlPdpStatistics_200() throws PolicyXacmlPdpException, InterruptedException {
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");
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());
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");
116 private Main startXacmlPdpService() throws TopicSinkClientException {
117 final String[] XacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters.json", "-p",
118 "parameters/topic.properties"};
119 return new Main(XacmlPdpConfigParameters);
122 private void stopXacmlPdpService(final Main main) throws PolicyXacmlPdpException {
126 private StatisticsReport getXacmlPdpStatistics() throws InterruptedException, IOException {
128 final ClientConfig clientConfig = new ClientConfig();
130 final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
131 clientConfig.register(feature);
133 final Client client = ClientBuilder.newClient(clientConfig);
134 final WebTarget webTarget = client.target("http://localhost:6969/policy/pdpx/v1/statistics");
136 final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
138 if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 20, 1000L)) {
139 throw new IllegalStateException("Cannot connect to port 6969");
142 return invocationBuilder.get(StatisticsReport.class);
145 private void updateXacmlPdpStatistics() {
146 XacmlPdpStatisticsManager.updateTotalPoliciesCount();
147 XacmlPdpStatisticsManager.updatePermitDecisionsCount();
148 XacmlPdpStatisticsManager.updateDenyDecisionsCount();
149 XacmlPdpStatisticsManager.updateIndeterminantDecisionsCount();
150 XacmlPdpStatisticsManager.updateNotApplicableDecisionsCount();
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());