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.junit.Assert.assertEquals;
26 import javax.ws.rs.client.Client;
27 import javax.ws.rs.client.ClientBuilder;
28 import javax.ws.rs.client.Invocation;
29 import javax.ws.rs.client.WebTarget;
30 import javax.ws.rs.core.MediaType;
32 import org.glassfish.jersey.client.ClientConfig;
33 import org.glassfish.jersey.client.authentication.HttpAuthenticationFeature;
34 import org.junit.Test;
36 import org.onap.policy.pdpx.main.PolicyXacmlPdpException;
37 import org.onap.policy.pdpx.main.parameters.CommonTestData;
38 import org.onap.policy.pdpx.main.parameters.RestServerParameters;
39 import org.onap.policy.pdpx.main.rest.XacmlPdpStatisticsManager;
40 import org.onap.policy.pdpx.main.startstop.Main;
41 import org.slf4j.Logger;
42 import org.slf4j.LoggerFactory;
45 * Class to perform unit test of {@link XacmlPdpRestController}.
48 public class TestXacmlPdpStatistics {
50 private static final Logger LOGGER = LoggerFactory.getLogger(TestXacmlPdpStatistics.class);
54 public void testXacmlPdpStatistics_200() throws PolicyXacmlPdpException, InterruptedException {
55 final Main main = startXacmlPdpService();
56 StatisticsReport report = getXacmlPdpStatistics();
58 validateReport(report, 0, 200);
59 updateXacmlPdpStatistics();
60 report = getXacmlPdpStatistics();
61 validateReport(report, 1, 200);
62 stopXacmlPdpService(main);
63 XacmlPdpStatisticsManager.resetAllStatistics();
67 public void testXacmlPdpStatistics_500() throws InterruptedException {
68 final RestServerParameters restServerParams = new CommonTestData().getRestServerParameters(false);
69 restServerParams.setName(CommonTestData.PDPX_GROUP_NAME);
71 final XacmlPdpRestServer restServer = new XacmlPdpRestServer(restServerParams);
73 final StatisticsReport report = getXacmlPdpStatistics();
75 validateReport(report, 0, 500);
76 restServer.shutdown();
77 XacmlPdpStatisticsManager.resetAllStatistics();
81 private Main startXacmlPdpService() {
82 final String[] XacmlPdpConfigParameters =
83 { "-c", "parameters/XacmlPdpConfigParameters.json" };
84 return new Main(XacmlPdpConfigParameters);
87 private void stopXacmlPdpService(final Main main) throws PolicyXacmlPdpException {
91 private StatisticsReport getXacmlPdpStatistics() throws InterruptedException {
92 StatisticsReport response = null;
93 final ClientConfig clientConfig = new ClientConfig();
95 final HttpAuthenticationFeature feature = HttpAuthenticationFeature.basic("healthcheck", "zb!XztG34");
96 clientConfig.register(feature);
98 final Client client = ClientBuilder.newClient(clientConfig);
99 final WebTarget webTarget = client.target("http://localhost:6969/statistics");
101 final Invocation.Builder invocationBuilder = webTarget.request(MediaType.APPLICATION_JSON);
102 final long startTime = System.currentTimeMillis();
103 while (response == null && (System.currentTimeMillis() - startTime) < 120000) {
105 response = invocationBuilder.get(StatisticsReport.class);
106 } catch (final Exception exp) {
107 LOGGER.info("the server is not started yet. We will retry again");
113 private void updateXacmlPdpStatistics() {
114 XacmlPdpStatisticsManager.updateTotalPoliciesCount();
115 XacmlPdpStatisticsManager.updatePermitDecisionsCount();
116 XacmlPdpStatisticsManager.updateDenyDecisionsCount();
117 XacmlPdpStatisticsManager.updateIndeterminantDecisionsCount();
118 XacmlPdpStatisticsManager.updateNotApplicableDecisionsCount();
121 private void validateReport(final StatisticsReport report, final int count, final int code) {
122 assertEquals(code, report.getCode());
123 assertEquals(count, report.getTotalPoliciesCount());
124 assertEquals(count, report.getPermitDecisionsCount());
125 assertEquals(count, report.getDenyDecisionsCount());
126 assertEquals(count, report.getIndeterminantDecisionsCount());
127 assertEquals(count, report.getNotApplicableDecisionsCount());