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=========================================================
21 package org.onap.policy.pdpx.main.rest;
23 import static org.junit.Assert.assertEquals;
25 import javax.ws.rs.client.Invocation;
26 import org.junit.Test;
27 import org.onap.policy.common.endpoints.report.HealthCheckReport;
28 import org.onap.policy.pdpx.main.CommonRest;
29 import org.onap.policy.pdpx.main.rest.model.StatisticsReport;
30 import org.slf4j.Logger;
31 import org.slf4j.LoggerFactory;
34 * Class to perform unit test of {@link XacmlPdpRestServer}.
37 public class TestXacmlPdpRestServer extends CommonRest {
39 private static final Logger LOGGER = LoggerFactory.getLogger(TestXacmlPdpRestServer.class);
40 private static final String NOT_ALIVE = "not alive";
41 private static final String ALIVE = "alive";
42 private static final String SELF = "self";
43 private static final String NAME = "Policy Xacml PDP";
44 private static final String HEALTHCHECK_ENDPOINT = "healthcheck";
45 private static final String STATISTICS_ENDPOINT = "statistics";
48 public void testHealthCheckSuccess() throws Exception {
49 LOGGER.info("***************************** Running testHealthCheckSuccess *****************************");
50 final Invocation.Builder invocationBuilder = sendHttpsRequest(HEALTHCHECK_ENDPOINT);
51 final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class);
52 LOGGER.info("test1HealthCheckSuccess health report {}", report);
53 validateHealthCheckReport(NAME, SELF, true, 200, ALIVE, report);
57 public void testHealthCheckFailure() throws Exception {
58 LOGGER.info("***************************** Running testHealthCheckFailure *****************************");
62 final Invocation.Builder invocationBuilder = sendHttpsRequest(HEALTHCHECK_ENDPOINT);
63 final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class);
64 LOGGER.info("testHealthCheckFailure health report {}", report);
65 validateHealthCheckReport(NAME, SELF, false, 500, NOT_ALIVE, report);
69 public void testHttpsHealthCheckSuccess() throws Exception {
70 LOGGER.info("***************************** Running testHttpsHealthCheckSuccess *****************************");
71 final Invocation.Builder invocationBuilder = sendHttpsRequest(HEALTHCHECK_ENDPOINT);
72 final HealthCheckReport report = invocationBuilder.get(HealthCheckReport.class);
73 LOGGER.info("testHttpsHealthCheckSuccess health report {}", report);
74 validateHealthCheckReport(NAME, SELF, true, 200, ALIVE, report);
78 public void testStatistics_200() throws Exception {
79 LOGGER.info("***************************** Running testStatistics_200 *****************************");
80 Invocation.Builder invocationBuilder = sendHttpsRequest(STATISTICS_ENDPOINT);
81 StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
82 LOGGER.info("testStatistics_200 health report {}", report);
83 validateStatisticsReport(report, 0, 200);
84 updateXacmlPdpStatistics();
85 invocationBuilder = sendHttpsRequest(STATISTICS_ENDPOINT);
86 report = invocationBuilder.get(StatisticsReport.class);
87 LOGGER.info("testStatistics_200 health report {}", report);
88 validateStatisticsReport(report, 1, 200);
92 public void testStatistics_500() throws Exception {
93 LOGGER.info("***************************** Running testStatistics_500 *****************************");
97 final Invocation.Builder invocationBuilder = sendHttpsRequest(STATISTICS_ENDPOINT);
98 final StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
99 LOGGER.info("testStatistics_500 health report {}", report);
100 validateStatisticsReport(report, 0, 500);
104 public void testHttpsStatistic() throws Exception {
105 LOGGER.info("***************************** Running testHttpsStatistic *****************************");
106 final Invocation.Builder invocationBuilder = sendHttpsRequest(STATISTICS_ENDPOINT);
107 final StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
108 LOGGER.info("testHttpsStatistic health report {}", report);
109 validateStatisticsReport(report, 0, 200);
112 private void updateXacmlPdpStatistics() {
113 XacmlPdpStatisticsManager stats = XacmlPdpStatisticsManager.getCurrent();
114 stats.updateTotalPoliciesCount();
115 stats.updatePermitDecisionsCount();
116 stats.updateDenyDecisionsCount();
117 stats.updateIndeterminantDecisionsCount();
118 stats.updateNotApplicableDecisionsCount();
121 private void validateStatisticsReport(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());
130 private void validateHealthCheckReport(final String name, final String url, final boolean healthy, final int code,
131 final String message, final HealthCheckReport report) {
132 assertEquals(name, report.getName());
133 assertEquals(url, report.getUrl());
134 assertEquals(healthy, report.isHealthy());
135 assertEquals(code, report.getCode());
136 assertEquals(message, report.getMessage());