Refactor xacml-pdp to remove various statics
[policy/xacml-pdp.git] / main / src / test / java / org / onap / policy / pdpx / main / rest / TestXacmlPdpRestServer.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.policy.pdpx.main.rest;
22
23 import static org.junit.Assert.assertEquals;
24
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;
32
33 /**
34  * Class to perform unit test of {@link XacmlPdpRestServer}.
35  *
36  */
37 public class TestXacmlPdpRestServer extends CommonRest {
38
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";
46
47     @Test
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);
54     }
55
56     @Test
57     public void testHealthCheckFailure() throws Exception {
58         LOGGER.info("***************************** Running testHealthCheckFailure *****************************");
59
60         markActivatorDead();
61
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);
66     }
67
68     @Test
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);
75     }
76
77     @Test
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);
89     }
90
91     @Test
92     public void testStatistics_500() throws Exception {
93         LOGGER.info("***************************** Running testStatistics_500 *****************************");
94
95         markActivatorDead();
96
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);
101     }
102
103     @Test
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);
110     }
111
112     private void updateXacmlPdpStatistics() {
113         XacmlPdpStatisticsManager stats = XacmlPdpStatisticsManager.getCurrent();
114         stats.updateTotalPoliciesCount();
115         stats.updatePermitDecisionsCount();
116         stats.updateDenyDecisionsCount();
117         stats.updateIndeterminantDecisionsCount();
118         stats.updateNotApplicableDecisionsCount();
119     }
120
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());
128     }
129
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());
137     }
138 }