Update totalPoliciesCount statistic
[policy/xacml-pdp.git] / main / src / test / java / org / onap / policy / pdpx / main / rest / TestXacmlPdpStatistics.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
22 package org.onap.policy.pdpx.main.rest;
23
24 import static org.junit.Assert.assertEquals;
25
26 import org.junit.Test;
27 import org.onap.policy.pdpx.main.CommonRest;
28 import org.onap.policy.pdpx.main.rest.model.StatisticsReport;
29 import org.slf4j.Logger;
30 import org.slf4j.LoggerFactory;
31
32 /**
33  * Class to perform unit test of {@link XacmlPdpRestController}.
34  *
35  */
36 public class TestXacmlPdpStatistics extends CommonRest {
37
38     private static final Logger LOGGER = LoggerFactory.getLogger(TestXacmlPdpStatistics.class);
39
40     private int nupdates = 0;
41
42     @Test
43     public void testXacmlPdpStatistics_200() throws Exception {
44         LOGGER.info("*************************** Running testXacmlPdpStatistics_200 ***************************");
45         StatisticsReport report = getXacmlPdpStatistics();
46         validateReport(report, 0, 200);
47         updateXacmlPdpStatistics();
48         report = getXacmlPdpStatistics();
49         validateReport(report, 1, 200);
50     }
51
52     @Test
53     public void testXacmlPdpStatistics_500() throws Exception {
54         LOGGER.info("***************************** Running testXacmlPdpStatistics_500 *****************************");
55
56         markActivatorDead();
57
58         final StatisticsReport report = getXacmlPdpStatistics();
59         validateReport(report, 0, 500);
60     }
61
62     private StatisticsReport getXacmlPdpStatistics() throws Exception {
63         return sendHttpsRequest("statistics").get(StatisticsReport.class);
64     }
65
66     private void updateXacmlPdpStatistics() {
67         XacmlPdpStatisticsManager stats = XacmlPdpStatisticsManager.getCurrent();
68
69         ++nupdates;
70         stats.setTotalPolicyCount(nupdates);
71         stats.setTotalPolicyTypesCount(nupdates);
72         stats.updatePermitDecisionsCount();
73         stats.updateDenyDecisionsCount();
74         stats.updateIndeterminantDecisionsCount();
75         stats.updateNotApplicableDecisionsCount();
76     }
77
78     private void validateReport(final StatisticsReport report, final int count, final int code) {
79         assertEquals(code, report.getCode());
80         assertEquals(count, report.getTotalPoliciesCount());
81         assertEquals(count, report.getTotalPolicyTypesCount());
82         assertEquals(count, report.getPermitDecisionsCount());
83         assertEquals(count, report.getDenyDecisionsCount());
84         assertEquals(count, report.getIndeterminantDecisionsCount());
85         assertEquals(count, report.getNotApplicableDecisionsCount());
86     }
87 }