2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2021 Bell Canada. All rights reserved.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 * ============LICENSE_END=========================================================
19 package org.onap.policy.pap.main.rest.e2e;
21 import static org.assertj.core.api.Assertions.assertThat;
23 import java.time.Instant;
24 import java.util.ArrayList;
25 import java.util.List;
26 import javax.ws.rs.client.Invocation;
27 import javax.ws.rs.core.GenericType;
28 import javax.ws.rs.core.Response;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.onap.policy.common.utils.services.Registry;
32 import org.onap.policy.models.base.PfModelException;
33 import org.onap.policy.models.base.PfModelRuntimeException;
34 import org.onap.policy.models.pap.concepts.PolicyAudit;
35 import org.onap.policy.models.pap.concepts.PolicyAudit.AuditAction;
36 import org.onap.policy.models.provider.PolicyModelsProvider;
37 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
38 import org.onap.policy.pap.main.PapConstants;
39 import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper;
40 import org.onap.policy.pap.main.rest.PolicyAuditControllerV1;
42 public class PolicyAuditTest extends End2EndBase {
43 private static final String TEST_GROUP = "testGroup";
44 private static final String TEST_PDP_TYPE = "testPdpType";
45 private static final ToscaConceptIdentifier POLICY_A = new ToscaConceptIdentifier("PolicyA", "1.0.0");
46 private static final ToscaConceptIdentifier POLICY_B = new ToscaConceptIdentifier("PolicyB", "2.0.0");
47 private static final String DEFAULT_USER = "TEST";
48 private static final String POLICY_AUDIT_ENDPOINT = "policies/audit";
49 private static final String URI_SEPERATOR = "/";
50 private static final String QUERY_PARAMS_INVALID = "?recordCount=5&startTime=2021-07-25T01:25:15";
51 private static final String QUERY_PARAMS_CORRECT = "?recordCount=5&startTime=1627219515&endTime=1627478715";
52 private static final String QUERY_PARAMS_INCORRECT = "?recordCount=5&startTime=1627478715&endTime=1627565115";
53 private static int NOT_FOUND_STATUS_CODE = 404;
54 private static int BAD_REQUEST_STATUS_CODE = 400;
55 private static final String BAD_REQUEST_MSG = "NumberFormatException For";
59 public void setUp() throws Exception {
64 private void setupEnv() {
65 List<PolicyAudit> recordList = new ArrayList<>();
66 Instant auditRecordTime = Instant.ofEpochSecond(1627392315L);
67 PolicyModelsProviderFactoryWrapper modelProviderWrapper =
68 Registry.get(PapConstants.REG_PAP_DAO_FACTORY, PolicyModelsProviderFactoryWrapper.class);
70 try (PolicyModelsProvider databaseProvider = modelProviderWrapper.create()) {
71 PolicyAudit audit1 = PolicyAudit.builder().auditId(123L).pdpGroup(TEST_GROUP).pdpType(TEST_PDP_TYPE)
72 .policy(POLICY_A).action(AuditAction.DEPLOYMENT)
73 .timestamp(auditRecordTime).user(DEFAULT_USER).build();
74 PolicyAudit audit2 = PolicyAudit.builder().auditId(456L).pdpGroup(TEST_GROUP).pdpType(TEST_PDP_TYPE)
75 .policy(POLICY_B).action(AuditAction.UNDEPLOYMENT)
76 .timestamp(auditRecordTime).user(DEFAULT_USER).build();
77 recordList.add(audit1);
78 recordList.add(audit2);
79 databaseProvider.createAuditRecords(recordList);
80 } catch (final PfModelException exp) {
81 throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, exp.getMessage());
86 public void testGetAllAuditRecords() throws Exception {
87 String uri = POLICY_AUDIT_ENDPOINT;
88 sendAndValidateSuccess(uri, 2);
92 public void testGetAllAuditRecordsWithParams() throws Exception {
93 // try with correct dates in query, should result in 2 records
94 String uri = POLICY_AUDIT_ENDPOINT + QUERY_PARAMS_CORRECT;
95 sendAndValidateSuccess(uri, 2);
97 // try with incorrect dates in query, should result in 0 record
98 uri = POLICY_AUDIT_ENDPOINT + QUERY_PARAMS_INCORRECT;
99 sendAndValidateSuccess(uri, 0);
101 // try with invalid date format, should result in error
102 uri = POLICY_AUDIT_ENDPOINT + QUERY_PARAMS_INVALID;
103 sendAndValidateError(uri, BAD_REQUEST_MSG, BAD_REQUEST_STATUS_CODE);
107 public void testGetAuditRecordsByGroup() throws Exception {
108 String uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP;
109 sendAndValidateSuccess(uri, 2);
113 public void testGetAuditRecordsByGroupWithParams() throws Exception {
114 // try with correct dates in query, should result in 2 records
115 String uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP + QUERY_PARAMS_CORRECT;
116 sendAndValidateSuccess(uri, 2);
118 // try with incorrect dates in query, should result in error
119 uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP + QUERY_PARAMS_INCORRECT;
120 sendAndValidateError(uri, PolicyAuditControllerV1.NO_AUDIT_RECORD_FOUND, NOT_FOUND_STATUS_CODE);
122 // try with invalid date format, should result in error
123 uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP + QUERY_PARAMS_INVALID;
124 sendAndValidateError(uri, BAD_REQUEST_MSG, BAD_REQUEST_STATUS_CODE);
128 public void testGetAuditRecordsOfPolicyWithGroup() throws Exception {
129 String uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP + URI_SEPERATOR + POLICY_A.getName()
130 + URI_SEPERATOR + POLICY_A.getVersion();
131 sendAndValidateSuccess(uri, 1);
135 public void testGetAuditRecordsOfPolicyWithGroupWithParams() throws Exception {
136 // try with correct dates in query, should result in 1 record
137 String uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP + URI_SEPERATOR + POLICY_A.getName()
138 + URI_SEPERATOR + POLICY_A.getVersion() + QUERY_PARAMS_CORRECT;
139 sendAndValidateSuccess(uri, 1);
141 // try with incorrect dates in query, should result in error
142 uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP + URI_SEPERATOR + POLICY_A.getName()
143 + URI_SEPERATOR + POLICY_A.getVersion() + QUERY_PARAMS_INCORRECT;
144 sendAndValidateError(uri, PolicyAuditControllerV1.NO_AUDIT_RECORD_FOUND, NOT_FOUND_STATUS_CODE);
146 // try with invalid date format, should result in error
147 uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP + URI_SEPERATOR + POLICY_A.getName() + URI_SEPERATOR
148 + POLICY_A.getVersion() + QUERY_PARAMS_INVALID;
149 sendAndValidateError(uri, BAD_REQUEST_MSG, BAD_REQUEST_STATUS_CODE);
153 public void testGetAuditRecordsOfPolicyWithoutGroup() throws Exception {
154 String uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + POLICY_A.getName() + URI_SEPERATOR + POLICY_A.getVersion();
155 sendAndValidateSuccess(uri, 1);
159 public void testGetAuditRecordsOfPolicyWithoutGroupWithParams() throws Exception {
160 // try with correct dates in query, should result in 1 record
161 String uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + POLICY_A.getName() + URI_SEPERATOR + POLICY_A.getVersion()
162 + QUERY_PARAMS_CORRECT;
163 sendAndValidateSuccess(uri, 1);
165 // try with incorrect dates in query, should result in error
166 uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + POLICY_A.getName() + URI_SEPERATOR + POLICY_A.getVersion()
167 + QUERY_PARAMS_INCORRECT;
168 sendAndValidateError(uri, PolicyAuditControllerV1.NO_AUDIT_RECORD_FOUND, NOT_FOUND_STATUS_CODE);
170 // try with invalid date format, should result in error
171 uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + POLICY_A.getName() + URI_SEPERATOR
172 + POLICY_A.getVersion() + QUERY_PARAMS_INVALID;
173 sendAndValidateError(uri, BAD_REQUEST_MSG, BAD_REQUEST_STATUS_CODE);
176 private void sendAndValidateSuccess(String uri, int count) throws Exception {
177 Invocation.Builder invocationBuilder = sendRequest(uri);
178 Response rawresp = invocationBuilder.get();
179 assertThat(rawresp.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
180 List<PolicyAudit> resp = rawresp.readEntity(new GenericType<List<PolicyAudit>>() {});
181 assertThat(resp).hasSize(count);
182 for (PolicyAudit audit : resp) {
183 if (audit.getAuditId() == 123L) {
184 assertThat(audit.getPdpGroup()).isEqualTo(TEST_GROUP);
185 assertThat(audit.getPdpType()).isEqualTo(TEST_PDP_TYPE);
186 assertThat(audit.getPolicy()).isEqualTo(POLICY_A);
187 assertThat(audit.getAction()).isEqualTo(AuditAction.DEPLOYMENT);
188 assertThat(audit.getUser()).isEqualTo(DEFAULT_USER);
189 } else if (audit.getAuditId() == 456L) {
190 assertThat(audit.getPdpGroup()).isEqualTo(TEST_GROUP);
191 assertThat(audit.getPdpType()).isEqualTo(TEST_PDP_TYPE);
192 assertThat(audit.getPolicy()).isEqualTo(POLICY_B);
193 assertThat(audit.getAction()).isEqualTo(AuditAction.UNDEPLOYMENT);
194 assertThat(audit.getUser()).isEqualTo(DEFAULT_USER);
199 private void sendAndValidateError(String uri, String errorMessage, int statusCode) throws Exception {
200 Invocation.Builder invocationBuilder = sendRequest(uri);
201 Response rawresp = invocationBuilder.get();
202 assertThat(rawresp.getStatus()).isEqualTo(statusCode);
203 String resp = rawresp.readEntity(String.class);
204 assertThat(resp).contains(errorMessage);