Add PDP Group Deploy and Delete REST API stubs
[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         // verify it fails when no authorization info is included
71         checkUnauthRequest(STATISTICS_ENDPOINT, req -> req.get());
72     }
73
74     @Test
75     public void testPapStatistics_500() throws Exception {
76
77         markActivatorDead();
78
79         PapStatisticsManager.getInstance().resetAllStatistics();
80
81         Invocation.Builder invocationBuilder = sendRequest(STATISTICS_ENDPOINT);
82         StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
83         validateStatisticsReport(report, 0, 500);
84     }
85
86     @Test
87     public void testPapStatisticsConstructorIsProtected() throws Exception {
88         final Constructor<PapStatisticsManager> constructor = PapStatisticsManager.class.getDeclaredConstructor();
89         assertTrue(Modifier.isProtected(constructor.getModifiers()));
90     }
91
92     private void updateDistributionStatistics() {
93         PapStatisticsManager mgr = PapStatisticsManager.getInstance();
94
95         mgr.updateTotalPdpCount();
96         mgr.updateTotalPdpGroupCount();
97         mgr.updateTotalPolicyDeployCount();
98         mgr.updatePolicyDeploySuccessCount();
99         mgr.updatePolicyDeployFailureCount();
100         mgr.updateTotalPolicyDownloadCount();
101         mgr.updatePolicyDownloadSuccessCount();
102         mgr.updatePolicyDownloadFailureCount();
103     }
104
105     private void validateStatisticsReport(final StatisticsReport report, final int count, final int code) {
106         assertEquals(code, report.getCode());
107         assertEquals(count, report.getTotalPdpCount());
108         assertEquals(count, report.getTotalPdpGroupCount());
109         assertEquals(count, report.getTotalPolicyDeployCount());
110         assertEquals(count, report.getPolicyDeploySuccessCount());
111         assertEquals(count, report.getPolicyDeployFailureCount());
112         assertEquals(count, report.getTotalPolicyDownloadCount());
113         assertEquals(count, report.getPolicyDeploySuccessCount());
114         assertEquals(count, report.getPolicyDeployFailureCount());
115     }
116 }