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 * Modifications Copyright (C) 2021-2022 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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.policy.pap.main.rest.e2e;
25 import static org.assertj.core.api.Assertions.assertThat;
26 import static org.junit.Assert.assertEquals;
28 import java.net.HttpURLConnection;
29 import java.time.Instant;
30 import java.util.List;
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.Before;
36 import org.junit.BeforeClass;
37 import org.junit.Test;
38 import org.onap.policy.common.utils.services.Registry;
39 import org.onap.policy.models.pdp.concepts.PdpStatistics;
40 import org.onap.policy.pap.main.PapConstants;
41 import org.onap.policy.pap.main.rest.PapStatisticsManager;
42 import org.onap.policy.pap.main.rest.StatisticsReport;
44 public class StatisticsTest extends End2EndBase {
45 private static final String STATISTICS_ENDPOINT = "statistics";
46 private static final String END_TIME_NAME = "endTime";
47 private static final String START_TIME_NAME = "startTime";
48 private static final long TIMESTAMP_SEC = 1562494272;
50 private static PdpStatistics pdpStatisticsRecord;
53 * Adds a record to the DB.
56 public static void setUpBeforeClass() throws Exception {
57 End2EndBase.setUpBeforeClass();
59 pdpStatisticsRecord = new PdpStatistics();
60 pdpStatisticsRecord.setPdpGroupName("defaultGroup");
61 pdpStatisticsRecord.setPdpSubGroupName("apex");
62 pdpStatisticsRecord.setPdpInstanceId("pdp1");
63 pdpStatisticsRecord.setTimeStamp(Instant.ofEpochSecond(TIMESTAMP_SEC));
64 pdpStatisticsRecord.setPolicyDeployCount(1);
65 pdpStatisticsRecord.setPolicyDeployFailCount(0);
66 pdpStatisticsRecord.setPolicyDeploySuccessCount(1);
67 pdpStatisticsRecord.setPolicyExecutedCount(1);
68 pdpStatisticsRecord.setPolicyExecutedFailCount(0);
69 pdpStatisticsRecord.setPolicyExecutedSuccessCount(1);
74 public void setUp() throws Exception {
76 pdpStatisticsService.createPdpStatistics(List.of(pdpStatisticsRecord));
80 public void test() throws Exception {
81 Invocation.Builder invocationBuilder = sendRequest(STATISTICS_ENDPOINT);
82 StatisticsReport report = invocationBuilder.get(StatisticsReport.class);
83 assertEquals(HttpURLConnection.HTTP_OK, report.getCode());
85 updateDistributionStatistics();
87 invocationBuilder = sendRequest(STATISTICS_ENDPOINT);
88 StatisticsReport report2 = invocationBuilder.get(StatisticsReport.class);
90 assertEquals(HttpURLConnection.HTTP_OK, report.getCode());
91 assertEquals(report.getTotalPdpCount() + 1, report2.getTotalPdpCount());
92 assertEquals(report.getTotalPdpGroupCount() + 1, report2.getTotalPdpGroupCount());
93 assertEquals(report.getTotalPolicyDeployCount() + 1, report2.getTotalPolicyDeployCount());
94 assertEquals(report.getPolicyDeploySuccessCount() + 1, report2.getPolicyDeploySuccessCount());
95 assertEquals(report.getPolicyDeployFailureCount() + 1, report2.getPolicyDeployFailureCount());
96 assertEquals(report.getTotalPolicyDownloadCount() + 1, report2.getTotalPolicyDownloadCount());
97 assertEquals(report.getPolicyDeploySuccessCount() + 1, report2.getPolicyDeploySuccessCount());
98 assertEquals(report.getPolicyDeployFailureCount() + 1, report2.getPolicyDeployFailureCount());
102 public void testDb() throws Exception {
103 verifyResponse("pdps/statistics");
107 public void testDbWithGroup() throws Exception {
108 verifyResponse("pdps/statistics/defaultGroup");
113 public void testDbWithSubGroup() throws Exception {
114 verifyResponse("pdps/statistics/defaultGroup/apex");
118 public void testDbWithPdp() throws Exception {
119 verifyResponse("pdps/statistics/defaultGroup/apex/pdp1");
123 public void testDbWithPdpLatest() throws Exception {
124 verifyResponse("pdps/statistics/defaultGroup/apex/pdp1?recordCount=5");
127 private void updateDistributionStatistics() {
128 PapStatisticsManager mgr = Registry.get(PapConstants.REG_STATISTICS_MANAGER, PapStatisticsManager.class);
130 mgr.updateTotalPdpCount();
131 mgr.updateTotalPdpGroupCount();
132 mgr.updateTotalPolicyDeployCount();
133 mgr.updatePolicyDeploySuccessCount();
134 mgr.updatePolicyDeployFailureCount();
135 mgr.updateTotalPolicyDownloadCount();
136 mgr.updatePolicyDownloadSuccessCount();
137 mgr.updatePolicyDownloadFailureCount();
140 private void verifyResponse(String endpoint) throws Exception {
141 Invocation.Builder invocationBuilder = sendRequest(endpoint);
142 verifyResponse(invocationBuilder.get());
144 // repeat with "start", in range
145 invocationBuilder = sendRequest(addTimeParam(endpoint, START_TIME_NAME, TIMESTAMP_SEC));
146 verifyResponse(invocationBuilder.get());
148 // repeat with "end", in range
149 invocationBuilder = sendRequest(addTimeParam(endpoint, END_TIME_NAME, TIMESTAMP_SEC));
150 verifyResponse(invocationBuilder.get());
152 // repeat with "start" and "end", in range
153 invocationBuilder = sendRequest(addTimeParam(endpoint, START_TIME_NAME, TIMESTAMP_SEC - 1)
154 + "&" + END_TIME_NAME + "=" + TIMESTAMP_SEC + 1);
155 verifyResponse(invocationBuilder.get());
157 // repeat with "start", out of range
158 invocationBuilder = sendRequest(addTimeParam(endpoint, START_TIME_NAME, TIMESTAMP_SEC + 1));
159 verifyEmptyResponse(invocationBuilder.get());
161 // repeat with "end", out of range
162 invocationBuilder = sendRequest(addTimeParam(endpoint, END_TIME_NAME, TIMESTAMP_SEC - 1));
163 verifyEmptyResponse(invocationBuilder.get());
166 private void verifyResponse(Response testResponse) {
167 assertEquals(Response.Status.OK.getStatusCode(), testResponse.getStatus());
168 Map<String, Map<String, List<PdpStatistics>>> map =
169 testResponse.readEntity(new GenericType<Map<String, Map<String, List<PdpStatistics>>>>() {});
170 Map<String, List<PdpStatistics>> subMap = map.get("defaultGroup");
171 List<PdpStatistics> resRecord = subMap.get("apex");
172 assertEquals("pdp1", resRecord.get(0).getPdpInstanceId());
173 assertEquals("apex", resRecord.get(0).getPdpSubGroupName());
174 assertEquals("defaultGroup", resRecord.get(0).getPdpGroupName());
177 private void verifyEmptyResponse(Response testResponse) {
178 assertEquals(Response.Status.OK.getStatusCode(), testResponse.getStatus());
179 Map<String, Map<String, List<PdpStatistics>>> map =
180 testResponse.readEntity(new GenericType<Map<String, Map<String, List<PdpStatistics>>>>() {});
181 assertThat(map).isEmpty();
185 * Adds a timestamp parameter to an endpoint string.
186 * @param endpoint endpoint to which it should be added
187 * @param paramName parameter name
188 * @param timeSec time, in seconds
189 * @return the new endpoint, with the added parameter
191 private String addTimeParam(String endpoint, String paramName, long timeSec) {
192 StringBuilder builder = new StringBuilder(endpoint);
193 builder.append(endpoint.contains("?") ? '&' : '?');
194 builder.append(paramName);
196 builder.append(timeSec);
197 return builder.toString();