de142b5153e9a091ff739e7bb730d849fb4ae764
[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 import static org.junit.Assert.assertTrue;
26
27 import java.lang.reflect.Constructor;
28 import java.lang.reflect.Modifier;
29 import javax.ws.rs.client.Invocation;
30 import org.junit.Before;
31 import org.junit.Test;
32
33 /**
34  * Class to perform unit test of {@link PapRestServer}.
35  *
36  * @author Ram Krishna Verma (ram.krishna.verma@est.tech)
37  */
38 public class TestStatisticsRestControllerV1 extends CommonPapRestServer {
39
40     private static final String STATISTICS_ENDPOINT = "statistics";
41
42     /**
43      * Set up.
44      *
45      * @throws Exception if an error occurs
46      */
47     @Before
48     public void setUp() throws Exception {
49         super.setUp();
50
51         PapStatisticsManager.getInstance().resetAllStatistics();
52     }
53
54     @Test
55     public void testSwagger() throws Exception {
56         super.testSwagger(STATISTICS_ENDPOINT);
57     }
58
59     @Test
60     public void testPapStatistics_200() throws Exception {
61         Invocation.Builder invocationBuilder = sendRequest(STATISTICS_ENDPOINT);
62         StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
63         validateStatisticsReport(report, 0, 200);
64         updateDistributionStatistics();
65
66         invocationBuilder = sendRequest(STATISTICS_ENDPOINT);
67         report = invocationBuilder.get(StatisticsReport.class);
68         validateStatisticsReport(report, 1, 200);
69     }
70
71     @Test
72     public void testPapStatistics_500() throws Exception {
73
74         markActivatorDead();
75
76         PapStatisticsManager.getInstance().resetAllStatistics();
77
78         Invocation.Builder invocationBuilder = sendRequest(STATISTICS_ENDPOINT);
79         StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
80         validateStatisticsReport(report, 0, 500);
81     }
82
83     @Test
84     public void testPapStatisticsConstructorIsProtected() throws Exception {
85         final Constructor<PapStatisticsManager> constructor = PapStatisticsManager.class.getDeclaredConstructor();
86         assertTrue(Modifier.isProtected(constructor.getModifiers()));
87     }
88
89     private void updateDistributionStatistics() {
90         PapStatisticsManager mgr = PapStatisticsManager.getInstance();
91
92         mgr.updateTotalPdpCount();
93         mgr.updateTotalPdpGroupCount();
94         mgr.updateTotalPolicyDeployCount();
95         mgr.updatePolicyDeploySuccessCount();
96         mgr.updatePolicyDeployFailureCount();
97         mgr.updateTotalPolicyDownloadCount();
98         mgr.updatePolicyDownloadSuccessCount();
99         mgr.updatePolicyDownloadFailureCount();
100     }
101
102     private void validateStatisticsReport(final StatisticsReport report, final int count, final int code) {
103         assertEquals(code, report.getCode());
104         assertEquals(count, report.getTotalPdpCount());
105         assertEquals(count, report.getTotalPdpGroupCount());
106         assertEquals(count, report.getTotalPolicyDeployCount());
107         assertEquals(count, report.getPolicyDeploySuccessCount());
108         assertEquals(count, report.getPolicyDeployFailureCount());
109         assertEquals(count, report.getTotalPolicyDownloadCount());
110         assertEquals(count, report.getPolicyDeploySuccessCount());
111         assertEquals(count, report.getPolicyDeployFailureCount());
112     }
113 }