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