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;
28 import java.io.IOException;
29 import javax.ws.rs.client.Client;
30 import javax.ws.rs.client.ClientBuilder;
31 import javax.ws.rs.client.Invocation;
32 import javax.ws.rs.client.WebTarget;
33 import javax.ws.rs.core.MediaType;
34 import org.glassfish.jersey.client.ClientConfig;
35 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.onap.policy.common.utils.network.NetworkUtil;
39 import org.onap.policy.pdpx.main.PolicyXacmlPdpException;
40 import org.onap.policy.pdpx.main.parameters.CommonTestData;
41 import org.onap.policy.pdpx.main.parameters.RestServerParameters;
42 import org.onap.policy.pdpx.main.rest.XacmlPdpStatisticsManager;
43 import org.onap.policy.pdpx.main.rest.model.StatisticsReport;
44 import org.onap.policy.pdpx.main.startstop.Main;
45 import org.slf4j.Logger;
46 import org.slf4j.LoggerFactory;
49 * Class to perform unit test of {@link XacmlPdpRestController}.
52 public class TestXacmlPdpStatistics {
54 private static final Logger LOGGER = LoggerFactory.getLogger(TestXacmlPdpStatistics.class);
57 public static void beforeClass() {
58 System.setProperty("org.eclipse.jetty.util.log.class", "org.eclipse.jetty.util.log.StdErrLog");
59 System.setProperty("org.eclipse.jetty.LEVEL", "OFF");
63 public void testXacmlPdpStatistics_200() throws PolicyXacmlPdpException, InterruptedException {
65 final Main main = startXacmlPdpService();
66 StatisticsReport report = getXacmlPdpStatistics();
67 validateReport(report, 0, 200);
68 assertThat(report.getTotalPolicyTypesCount()).isGreaterThan(0);
69 updateXacmlPdpStatistics();
70 report = getXacmlPdpStatistics();
71 validateReport(report, 1, 200);
72 stopXacmlPdpService(main);
73 XacmlPdpStatisticsManager.resetAllStatistics();
74 } catch (final Exception e) {
75 LOGGER.error("testApiStatistics_200 failed", e);
76 fail("Test should not throw an exception");
81 public void testXacmlPdpStatistics_500() throws InterruptedException {
82 final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false);
83 restServerParams.setName(CommonTestData.PDPX_GROUP_NAME);
84 final XacmlPdpRestServer restServer = new XacmlPdpRestServer(restServerParams);
88 final StatisticsReport report = getXacmlPdpStatistics();
89 validateReport(report, 0, 500);
90 restServer.shutdown();
91 XacmlPdpStatisticsManager.resetAllStatistics();
92 } catch (final Exception e) {
93 LOGGER.error("testApiStatistics_500 failed", e);
94 fail("Test should not throw an exception");
99 private Main startXacmlPdpService() {
100 final String[] XacmlPdpConfigParameters = {"-c", "parameters/XacmlPdpConfigParameters.json"};
101 return new Main(XacmlPdpConfigParameters);
104 private void stopXacmlPdpService(final Main main) throws PolicyXacmlPdpException {
108 private StatisticsReport getXacmlPdpStatistics() throws InterruptedException, IOException {
110 final ClientConfig clientConfig = new ClientConfig();
112 final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
113 clientConfig.register(feature);
115 final Client client = ClientBuilder.newClient(clientConfig);
116 final WebTarget webTarget = client.target("http://localhost:6969/policy/pdpx/v1/statistics");
118 final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
120 if (!NetworkUtil.isTcpPortOpen("localhost", 6969, 6, 10000L)) {
121 throw new IllegalStateException("Cannot connect to port 6969");
124 return invocationBuilder.get(StatisticsReport.class);
127 private void updateXacmlPdpStatistics() {
128 XacmlPdpStatisticsManager.updateTotalPoliciesCount();
129 XacmlPdpStatisticsManager.updatePermitDecisionsCount();
130 XacmlPdpStatisticsManager.updateDenyDecisionsCount();
131 XacmlPdpStatisticsManager.updateIndeterminantDecisionsCount();
132 XacmlPdpStatisticsManager.updateNotApplicableDecisionsCount();
135 private void validateReport(final StatisticsReport report, final int count, final int code) {
136 assertEquals(code, report.getCode());
137 assertEquals(count, report.getTotalPoliciesCount());
138 assertEquals(count, report.getPermitDecisionsCount());
139 assertEquals(count, report.getDenyDecisionsCount());
140 assertEquals(count, report.getIndeterminantDecisionsCount());
141 assertEquals(count, report.getNotApplicableDecisionsCount());