Changes to PAP infrastructure to support PDP
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / TestStatisticsRestControllerV1.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 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
9  *
10  *      http://www.apache.org/licenses/LICENSE-2.0
11  *
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.
17  *
18  * SPDX-License-Identifier: Apache-2.0
19  * ============LICENSE_END=========================================================
20  */
21
22 package org.onap.policy.pap.main.rest;
23
24 import static org.junit.Assert.assertEquals;
25
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
31 /**
32  * Class to perform unit test of {@link PapRestServer}.
33  *
34  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
35  */
36 public class TestStatisticsRestControllerV1 extends CommonPapRestServer {
37
38     private static final String STATISTICS_ENDPOINT = "statistics";
39
40     @Test
41     public void testSwagger() throws Exception {
42         super.testSwagger(STATISTICS_ENDPOINT);
43     }
44
45     @Test
46     public void testPapStatistics_200() throws Exception {
47         Invocation.Builder invocationBuilder = sendRequest(STATISTICS_ENDPOINT);
48         StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
49         validateStatisticsReport(report, 0, 200);
50         updateDistributionStatistics();
51
52         invocationBuilder = sendRequest(STATISTICS_ENDPOINT);
53         report = invocationBuilder.get(StatisticsReport.class);
54         validateStatisticsReport(report, 1, 200);
55
56         // verify it fails when no authorization info is included
57         checkUnauthRequest(STATISTICS_ENDPOINT, req -> req.get());
58     }
59
60     @Test
61     public void testPapStatistics_500() throws Exception {
62
63         markActivatorDead();
64
65         Registry.get(PapConstants.REG_STATISTICS_MANAGER, PapStatisticsManager.class).resetAllStatistics();
66
67         Invocation.Builder invocationBuilder = sendRequest(STATISTICS_ENDPOINT);
68         StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
69         validateStatisticsReport(report, 0, 500);
70     }
71
72     private void updateDistributionStatistics() {
73         PapStatisticsManager mgr = Registry.get(PapConstants.REG_STATISTICS_MANAGER, PapStatisticsManager.class);
74
75         mgr.updateTotalPdpCount();
76         mgr.updateTotalPdpGroupCount();
77         mgr.updateTotalPolicyDeployCount();
78         mgr.updatePolicyDeploySuccessCount();
79         mgr.updatePolicyDeployFailureCount();
80         mgr.updateTotalPolicyDownloadCount();
81         mgr.updatePolicyDownloadSuccessCount();
82         mgr.updatePolicyDownloadFailureCount();
83     }
84
85     private void validateStatisticsReport(final StatisticsReport report, final int count, final int code) {
86         assertEquals(code, report.getCode());
87         assertEquals(count, report.getTotalPdpCount());
88         assertEquals(count, report.getTotalPdpGroupCount());
89         assertEquals(count, report.getTotalPolicyDeployCount());
90         assertEquals(count, report.getPolicyDeploySuccessCount());
91         assertEquals(count, report.getPolicyDeployFailureCount());
92         assertEquals(count, report.getTotalPolicyDownloadCount());
93         assertEquals(count, report.getPolicyDeploySuccessCount());
94         assertEquals(count, report.getPolicyDeployFailureCount());
95     }
96 }