Migrate pap startup & controllers to spring boot
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / e2e / StatisticsTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
4  * ================================================================================
5  * Copyright (C) 2019-2021 AT&T Intellectual Property. All rights reserved.
6  * Modifications Copyright (C) 2020-2021 Nordix Foundation.
7  * Modifications Copyright (C) 2021 Bell Canada. All rights reserved.
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.pap.main.rest.e2e;
24
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.junit.Assert.assertEquals;
27
28 import java.net.HttpURLConnection;
29 import java.time.Instant;
30 import java.util.List;
31 import java.util.Map;
32 import javax.ws.rs.client.Invocation;
33 import javax.ws.rs.core.GenericType;
34 import javax.ws.rs.core.Response;
35 import org.junit.BeforeClass;
36 import org.junit.Test;
37 import org.onap.policy.common.utils.services.Registry;
38 import org.onap.policy.models.base.PfModelException;
39 import org.onap.policy.models.base.PfModelRuntimeException;
40 import org.onap.policy.models.pdp.concepts.PdpStatistics;
41 import org.onap.policy.models.provider.PolicyModelsProvider;
42 import org.onap.policy.models.provider.PolicyModelsProviderFactory;
43 import org.onap.policy.pap.main.PapConstants;
44 import org.onap.policy.pap.main.parameters.CommonTestData;
45 import org.onap.policy.pap.main.parameters.PapParameterGroup;
46 import org.onap.policy.pap.main.rest.PapStatisticsManager;
47 import org.onap.policy.pap.main.rest.StatisticsReport;
48
49 public class StatisticsTest extends End2EndBase {
50     private static final String STATISTICS_ENDPOINT = "statistics";
51     private static final String END_TIME_NAME = "endTime";
52     private static final String START_TIME_NAME = "startTime";
53     private static final long TIMESTAMP_SEC = 1562494272;
54
55
56     /**
57      * Adds a record to the DB.
58      */
59     @BeforeClass
60     public static void setUpBeforeClass() throws Exception {
61         End2EndBase.setUpBeforeClass();
62
63         PolicyModelsProviderFactory modelProviderWrapper = new PolicyModelsProviderFactory();
64         PapParameterGroup parameterGroup = new CommonTestData().getPapParameterGroup(6969);
65         try (PolicyModelsProvider databaseProvider =
66             modelProviderWrapper.createPolicyModelsProvider(parameterGroup.getDatabaseProviderParameters())) {
67             PdpStatistics pdpStatisticsRecord = new PdpStatistics();
68             pdpStatisticsRecord.setPdpGroupName("defaultGroup");
69             pdpStatisticsRecord.setPdpSubGroupName("apex");
70             pdpStatisticsRecord.setPdpInstanceId("pdp1");
71             pdpStatisticsRecord.setTimeStamp(Instant.ofEpochSecond(TIMESTAMP_SEC));
72             pdpStatisticsRecord.setPolicyDeployCount(1);
73             pdpStatisticsRecord.setPolicyDeployFailCount(0);
74             pdpStatisticsRecord.setPolicyDeploySuccessCount(1);
75             pdpStatisticsRecord.setPolicyExecutedCount(1);
76             pdpStatisticsRecord.setPolicyExecutedFailCount(0);
77             pdpStatisticsRecord.setPolicyExecutedSuccessCount(1);
78             databaseProvider.createPdpStatistics(List.of(pdpStatisticsRecord));
79         } catch (final PfModelException exp) {
80             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, exp.getMessage());
81         }
82     }
83
84     @Test
85     public void test() throws Exception {
86         Invocation.Builder invocationBuilder = sendRequest(STATISTICS_ENDPOINT);
87         StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
88         assertEquals(HttpURLConnection.HTTP_OK, report.getCode());
89
90         updateDistributionStatistics();
91
92         invocationBuilder = sendRequest(STATISTICS_ENDPOINT);
93         StatisticsReport report2 = invocationBuilder.get(StatisticsReport.class);
94
95         assertEquals(HttpURLConnection.HTTP_OK, report.getCode());
96         assertEquals(report.getTotalPdpCount() + 1, report2.getTotalPdpCount());
97         assertEquals(report.getTotalPdpGroupCount() + 1, report2.getTotalPdpGroupCount());
98         assertEquals(report.getTotalPolicyDeployCount() + 1, report2.getTotalPolicyDeployCount());
99         assertEquals(report.getPolicyDeploySuccessCount() + 1, report2.getPolicyDeploySuccessCount());
100         assertEquals(report.getPolicyDeployFailureCount() + 1, report2.getPolicyDeployFailureCount());
101         assertEquals(report.getTotalPolicyDownloadCount() + 1, report2.getTotalPolicyDownloadCount());
102         assertEquals(report.getPolicyDeploySuccessCount() + 1, report2.getPolicyDeploySuccessCount());
103         assertEquals(report.getPolicyDeployFailureCount() + 1, report2.getPolicyDeployFailureCount());
104     }
105
106     @Test
107     public void testDb() throws Exception {
108         verifyResponse("pdps/statistics");
109     }
110
111     @Test
112     public void testDbWithGroup() throws Exception {
113         verifyResponse("pdps/statistics/defaultGroup");
114
115     }
116
117     @Test
118     public void testDbWithSubGroup() throws Exception {
119         verifyResponse("pdps/statistics/defaultGroup/apex");
120     }
121
122     @Test
123     public void testDbWithPdp() throws Exception {
124         verifyResponse("pdps/statistics/defaultGroup/apex/pdp1");
125     }
126
127     @Test
128     public void testDbWithPdpLatest() throws Exception {
129         verifyResponse("pdps/statistics/defaultGroup/apex/pdp1?recordCount=5");
130     }
131
132     private void updateDistributionStatistics() {
133         PapStatisticsManager mgr = Registry.get(PapConstants.REG_STATISTICS_MANAGER, PapStatisticsManager.class);
134
135         mgr.updateTotalPdpCount();
136         mgr.updateTotalPdpGroupCount();
137         mgr.updateTotalPolicyDeployCount();
138         mgr.updatePolicyDeploySuccessCount();
139         mgr.updatePolicyDeployFailureCount();
140         mgr.updateTotalPolicyDownloadCount();
141         mgr.updatePolicyDownloadSuccessCount();
142         mgr.updatePolicyDownloadFailureCount();
143     }
144
145     private void verifyResponse(String endpoint) throws Exception {
146         Invocation.Builder invocationBuilder = sendRequest(endpoint);
147         verifyResponse(invocationBuilder.get());
148
149         // repeat with "start", in range
150         invocationBuilder = sendRequest(addTimeParam(endpoint, START_TIME_NAME, TIMESTAMP_SEC));
151         verifyResponse(invocationBuilder.get());
152
153         // repeat with "end", in range
154         invocationBuilder = sendRequest(addTimeParam(endpoint, END_TIME_NAME, TIMESTAMP_SEC));
155         verifyResponse(invocationBuilder.get());
156
157         // repeat with "start" and "end", in range
158         invocationBuilder = sendRequest(addTimeParam(endpoint, START_TIME_NAME, TIMESTAMP_SEC - 1)
159                         + "&" + END_TIME_NAME + "=" + TIMESTAMP_SEC + 1);
160         verifyResponse(invocationBuilder.get());
161
162         // repeat with "start", out of range
163         invocationBuilder = sendRequest(addTimeParam(endpoint, START_TIME_NAME, TIMESTAMP_SEC + 1));
164         verifyEmptyResponse(invocationBuilder.get());
165
166         // repeat with "end", out of range
167         invocationBuilder = sendRequest(addTimeParam(endpoint, END_TIME_NAME, TIMESTAMP_SEC - 1));
168         verifyEmptyResponse(invocationBuilder.get());
169     }
170
171     private void verifyResponse(Response testResponse) {
172         assertEquals(Response.Status.OK.getStatusCode(), testResponse.getStatus());
173         Map<String, Map<String, List<PdpStatistics>>> map =
174                 testResponse.readEntity(new GenericType<Map<String, Map<String, List<PdpStatistics>>>>() {});
175         Map<String, List<PdpStatistics>> subMap = map.get("defaultGroup");
176         List<PdpStatistics> resRecord = subMap.get("apex");
177         assertEquals("pdp1", resRecord.get(0).getPdpInstanceId());
178         assertEquals("apex", resRecord.get(0).getPdpSubGroupName());
179         assertEquals("defaultGroup", resRecord.get(0).getPdpGroupName());
180     }
181
182     private void verifyEmptyResponse(Response testResponse) {
183         assertEquals(Response.Status.OK.getStatusCode(), testResponse.getStatus());
184         Map<String, Map<String, List<PdpStatistics>>> map =
185                 testResponse.readEntity(new GenericType<Map<String, Map<String, List<PdpStatistics>>>>() {});
186         assertThat(map).isEmpty();
187     }
188
189     /**
190      * Adds a timestamp parameter to an endpoint string.
191      * @param endpoint endpoint to which it should be added
192      * @param paramName parameter name
193      * @param timeSec time, in seconds
194      * @return the new endpoint, with the added parameter
195      */
196     private String addTimeParam(String endpoint, String paramName, long timeSec) {
197         StringBuilder builder = new StringBuilder(endpoint);
198         builder.append(endpoint.contains("?") ? '&' : '?');
199         builder.append(paramName);
200         builder.append('=');
201         builder.append(timeSec);
202         return builder.toString();
203     }
204 }