2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019-2020, 2022 Nordix Foundation.
4 * Modifications Copyright (C) 2019 AT&T Intellectual Property.
5 * ================================================================================
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
18 * SPDX-License-Identifier: Apache-2.0
19 * ============LICENSE_END=========================================================
22 package org.onap.policy.pap.main.rest;
24 import static org.junit.Assert.assertEquals;
26 import javax.ws.rs.client.Invocation;
27 import org.junit.Test;
28 import org.onap.policy.common.utils.services.Registry;
29 import org.onap.policy.pap.main.PapConstants;
30 import org.springframework.test.context.ActiveProfiles;
33 * Class to perform unit test of {@link StatisticsRestControllerV1}.
35 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
37 @ActiveProfiles("test")
38 public class TestStatisticsRestControllerV1 extends CommonPapRestServer {
40 private static final String STATISTICS_ENDPOINT = "statistics";
41 private static final String STATISTICS_DB_ENDPOINT = "pdps/statistics";
44 public void testSwagger() throws Exception {
45 super.testSwagger(STATISTICS_ENDPOINT);
46 super.testSwagger(STATISTICS_DB_ENDPOINT);
47 super.testSwagger(STATISTICS_DB_ENDPOINT + "/{group}");
48 super.testSwagger(STATISTICS_DB_ENDPOINT + "/{group}" + "/{type}");
49 super.testSwagger(STATISTICS_DB_ENDPOINT + "/{group}" + "/{type}" + "/{pdp}");
53 public void testPapStatistics_200() throws Exception {
54 Invocation.Builder invocationBuilder = sendRequest(STATISTICS_ENDPOINT);
55 StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
56 validateStatisticsReport(report, 0, 200);
57 updateDistributionStatistics();
59 invocationBuilder = sendRequest(STATISTICS_ENDPOINT);
60 report = invocationBuilder.get(StatisticsReport.class);
61 validateStatisticsReport(report, 1, 200);
63 // verify it fails when no authorization info is included
64 checkUnauthRequest(STATISTICS_ENDPOINT, req -> req.get());
68 public void testPapStatistics_500() throws Exception {
72 Registry.get(PapConstants.REG_STATISTICS_MANAGER, PapStatisticsManager.class).resetAllStatistics();
74 Invocation.Builder invocationBuilder = sendRequest(STATISTICS_ENDPOINT);
75 StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
76 validateStatisticsReport(report, 0, 500);
79 private void updateDistributionStatistics() {
80 PapStatisticsManager mgr = Registry.get(PapConstants.REG_STATISTICS_MANAGER, PapStatisticsManager.class);
82 mgr.updateTotalPdpCount();
83 mgr.updateTotalPdpGroupCount();
84 mgr.updateTotalPolicyDeployCount();
85 mgr.updatePolicyDeploySuccessCount();
86 mgr.updatePolicyDeployFailureCount();
87 mgr.updateTotalPolicyDownloadCount();
88 mgr.updatePolicyDownloadSuccessCount();
89 mgr.updatePolicyDownloadFailureCount();
92 private void validateStatisticsReport(final StatisticsReport report, final int count, final int code) {
93 assertEquals(code, report.getCode());
94 assertEquals(count, report.getTotalPdpCount());
95 assertEquals(count, report.getTotalPdpGroupCount());
96 assertEquals(count, report.getTotalPolicyDeployCount());
97 assertEquals(count, report.getPolicyDeploySuccessCount());
98 assertEquals(count, report.getPolicyDeployFailureCount());
99 assertEquals(count, report.getTotalPolicyDownloadCount());
100 assertEquals(count, report.getPolicyDeploySuccessCount());
101 assertEquals(count, report.getPolicyDeployFailureCount());