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 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");
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());
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");
114 private Main startXacmlPdpService() throws TopicSinkClientException {
115 final String[] XacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters.json", "-p",
116 "parameters/topic.properties"};
117 return new Main(XacmlPdpConfigParameters);
120 private void stopXacmlPdpService(final Main main) throws PolicyXacmlPdpException {
124 private StatisticsReport getXacmlPdpStatistics() throws InterruptedException, IOException {
126 final ClientConfig clientConfig = new ClientConfig();
128 final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
129 clientConfig.register(feature);
131 final Client client = ClientBuilder.newClient(clientConfig);
132 final WebTarget webTarget = client.target("http://localhost:6969/policy/pdpx/v1/statistics");
134 final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
136 if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) {
137 throw new IllegalStateException("Cannot connect to port 6969");
140 return invocationBuilder.get(StatisticsReport.class);
143 private void updateXacmlPdpStatistics() {
144 XacmlPdpStatisticsManager.updateTotalPoliciesCount();
145 XacmlPdpStatisticsManager.updatePermitDecisionsCount();
146 XacmlPdpStatisticsManager.updateDenyDecisionsCount();
147 XacmlPdpStatisticsManager.updateIndeterminantDecisionsCount();
148 XacmlPdpStatisticsManager.updateNotApplicableDecisionsCount();
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());