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.annotation.DirtiesContext;
31 import org.springframework.test.context.ActiveProfiles;
34 * Class to perform unit test of {@link StatisticsRestControllerV1}.
36 * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
38 @ActiveProfiles("test")
39 public class TestStatisticsRestControllerV1 extends CommonPapRestServer {
41 private static final String STATISTICS_ENDPOINT = "statistics";
42 private static final String STATISTICS_DB_ENDPOINT = "pdps/statistics";
45 public void testSwagger() throws Exception {
46 super.testSwagger(STATISTICS_ENDPOINT);
47 super.testSwagger(STATISTICS_DB_ENDPOINT);
48 super.testSwagger(STATISTICS_DB_ENDPOINT + "/{group}");
49 super.testSwagger(STATISTICS_DB_ENDPOINT + "/{group}" + "/{type}");
50 super.testSwagger(STATISTICS_DB_ENDPOINT + "/{group}" + "/{type}" + "/{pdp}");
54 public void testPapStatistics_200() throws Exception {
55 Invocation.Builder invocationBuilder = sendRequest(STATISTICS_ENDPOINT);
56 StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
57 validateStatisticsReport(report, 0, 200);
58 updateDistributionStatistics();
60 invocationBuilder = sendRequest(STATISTICS_ENDPOINT);
61 report = invocationBuilder.get(StatisticsReport.class);
62 validateStatisticsReport(report, 1, 200);
64 // verify it fails when no authorization info is included
65 checkUnauthRequest(STATISTICS_ENDPOINT, req -> req.get());
69 public void testPapStatistics_500() throws Exception {
73 Registry.get(PapConstants.REG_STATISTICS_MANAGER, PapStatisticsManager.class).resetAllStatistics();
75 Invocation.Builder invocationBuilder = sendRequest(STATISTICS_ENDPOINT);
76 StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
77 validateStatisticsReport(report, 0, 500);
80 private void updateDistributionStatistics() {
81 PapStatisticsManager mgr = Registry.get(PapConstants.REG_STATISTICS_MANAGER, PapStatisticsManager.class);
83 mgr.updateTotalPdpCount();
84 mgr.updateTotalPdpGroupCount();
85 mgr.updateTotalPolicyDeployCount();
86 mgr.updatePolicyDeploySuccessCount();
87 mgr.updatePolicyDeployFailureCount();
88 mgr.updateTotalPolicyDownloadCount();
89 mgr.updatePolicyDownloadSuccessCount();
90 mgr.updatePolicyDownloadFailureCount();
93 private void validateStatisticsReport(final StatisticsReport report, final int count, final int code) {
94 assertEquals(code, report.getCode());
95 assertEquals(count, report.getTotalPdpCount());
96 assertEquals(count, report.getTotalPdpGroupCount());
97 assertEquals(count, report.getTotalPolicyDeployCount());
98 assertEquals(count, report.getPolicyDeploySuccessCount());
99 assertEquals(count, report.getPolicyDeployFailureCount());
100 assertEquals(count, report.getTotalPolicyDownloadCount());
101 assertEquals(count, report.getPolicyDeploySuccessCount());
102 assertEquals(count, report.getPolicyDeployFailureCount());