Java 17 / Spring 6 / Spring Boot 3 Upgrade
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / e2e / PolicyAuditTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * Copyright (C) 2021-2022 Bell Canada. All rights reserved.
4  * Modifications Copyright (C) 2023 Nordix Foundation.
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  * ============LICENSE_END=========================================================
18  */
19
20 package org.onap.policy.pap.main.rest.e2e;
21
22 import static org.assertj.core.api.Assertions.assertThat;
23
24 import jakarta.ws.rs.client.Invocation;
25 import jakarta.ws.rs.core.GenericType;
26 import jakarta.ws.rs.core.Response;
27 import java.time.Instant;
28 import java.util.ArrayList;
29 import java.util.List;
30 import org.junit.jupiter.api.AfterEach;
31 import org.junit.jupiter.api.BeforeEach;
32 import org.junit.jupiter.api.Test;
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.tosca.authorative.concepts.ToscaConceptIdentifier;
37 import org.onap.policy.pap.main.repository.PolicyAuditRepository;
38 import org.onap.policy.pap.main.rest.PolicyAuditControllerV1;
39 import org.onap.policy.pap.main.service.PolicyAuditService;
40 import org.springframework.beans.factory.annotation.Autowired;
41
42 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 final int NOT_FOUND_STATUS_CODE = 404;
54     private static final int BAD_REQUEST_STATUS_CODE = 400;
55     private static final String BAD_REQUEST_MSG = "NumberFormatException For";
56
57     @Autowired
58     private PolicyAuditService policyAuditService;
59
60     @Autowired
61     private PolicyAuditRepository policyAuditRepository;
62
63     @Override
64     @BeforeEach
65     public void setUp() throws Exception {
66         super.setUp();
67         setupEnv();
68     }
69
70     /**
71      * Teardown after tests.
72      */
73     @Override
74     @AfterEach
75     public void tearDown() {
76         policyAuditRepository.deleteAll();
77         super.tearDown();
78     }
79
80     private void setupEnv() {
81         List<PolicyAudit> recordList = new ArrayList<>();
82         Instant auditRecordTime = Instant.ofEpochSecond(1627392315L);
83
84         try {
85             PolicyAudit audit1 = PolicyAudit.builder().pdpGroup(TEST_GROUP).pdpType(TEST_PDP_TYPE)
86                             .policy(POLICY_A).action(AuditAction.DEPLOYMENT)
87                             .timestamp(auditRecordTime).user(DEFAULT_USER).build();
88             PolicyAudit audit2 = PolicyAudit.builder().pdpGroup(TEST_GROUP).pdpType(TEST_PDP_TYPE)
89                             .policy(POLICY_B).action(AuditAction.UNDEPLOYMENT)
90                             .timestamp(auditRecordTime).user(DEFAULT_USER).build();
91             recordList.add(audit1);
92             recordList.add(audit2);
93             policyAuditService.createAuditRecords(recordList);
94         } catch (Exception exp) {
95             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, exp.getMessage());
96         }
97     }
98
99     @Test
100     void testGetAllAuditRecords() throws Exception {
101         String uri = POLICY_AUDIT_ENDPOINT;
102         sendAndValidateSuccess(uri, 2);
103     }
104
105     @Test
106     void testGetAllAuditRecordsWithParams() throws Exception {
107         // try with correct dates in query, should result in 2 records
108         String uri = POLICY_AUDIT_ENDPOINT + QUERY_PARAMS_CORRECT;
109         sendAndValidateSuccess(uri, 2);
110
111         // try with incorrect dates in query, should result in 0 record
112         uri = POLICY_AUDIT_ENDPOINT + QUERY_PARAMS_INCORRECT;
113         sendAndValidateSuccess(uri, 0);
114
115         // try with invalid date format, should result in error
116         uri = POLICY_AUDIT_ENDPOINT + QUERY_PARAMS_INVALID;
117         sendAndValidateError(uri, BAD_REQUEST_MSG, BAD_REQUEST_STATUS_CODE);
118     }
119
120     @Test
121     void testGetAuditRecordsByGroup() throws Exception {
122         String uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP;
123         sendAndValidateSuccess(uri, 2);
124     }
125
126     @Test
127     void testGetAuditRecordsByGroupWithParams() throws Exception {
128         // try with correct dates in query, should result in 2 records
129         String uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP + QUERY_PARAMS_CORRECT;
130         sendAndValidateSuccess(uri, 2);
131
132         // try with incorrect dates in query, should result in error
133         uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP + QUERY_PARAMS_INCORRECT;
134         sendAndValidateError(uri, PolicyAuditControllerV1.NO_AUDIT_RECORD_FOUND, NOT_FOUND_STATUS_CODE);
135
136         // try with invalid date format, should result in error
137         uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP + QUERY_PARAMS_INVALID;
138         sendAndValidateError(uri, BAD_REQUEST_MSG, BAD_REQUEST_STATUS_CODE);
139     }
140
141     @Test
142     void testGetAuditRecordsOfPolicyWithGroup() throws Exception {
143         String uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP + URI_SEPERATOR + POLICY_A.getName()
144                         + URI_SEPERATOR + POLICY_A.getVersion();
145         sendAndValidateSuccess(uri, 1);
146     }
147
148     @Test
149     void testGetAuditRecordsOfPolicyWithGroupWithParams() throws Exception {
150         // try with correct dates in query, should result in 1 record
151         String uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP + URI_SEPERATOR + POLICY_A.getName()
152                         + URI_SEPERATOR + POLICY_A.getVersion() + QUERY_PARAMS_CORRECT;
153         sendAndValidateSuccess(uri, 1);
154
155         // try with incorrect dates in query, should result in error
156         uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP + URI_SEPERATOR + POLICY_A.getName()
157                         + URI_SEPERATOR + POLICY_A.getVersion() + QUERY_PARAMS_INCORRECT;
158         sendAndValidateError(uri, PolicyAuditControllerV1.NO_AUDIT_RECORD_FOUND, NOT_FOUND_STATUS_CODE);
159
160         // try with invalid date format, should result in error
161         uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP + URI_SEPERATOR + POLICY_A.getName() + URI_SEPERATOR
162                         + POLICY_A.getVersion() + QUERY_PARAMS_INVALID;
163         sendAndValidateError(uri, BAD_REQUEST_MSG, BAD_REQUEST_STATUS_CODE);
164     }
165
166     @Test
167     void testGetAuditRecordsOfPolicyWithoutGroup() throws Exception {
168         String uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + POLICY_A.getName() + URI_SEPERATOR + POLICY_A.getVersion();
169         sendAndValidateSuccess(uri, 1);
170     }
171
172     @Test
173     void testGetAuditRecordsOfPolicyWithoutGroupWithParams() throws Exception {
174         // try with correct dates in query, should result in 1 record
175         String uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + POLICY_A.getName() + URI_SEPERATOR + POLICY_A.getVersion()
176                         + QUERY_PARAMS_CORRECT;
177         sendAndValidateSuccess(uri, 1);
178
179         // try with incorrect dates in query, should result in error
180         uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + POLICY_A.getName() + URI_SEPERATOR + POLICY_A.getVersion()
181                         + QUERY_PARAMS_INCORRECT;
182         sendAndValidateError(uri, PolicyAuditControllerV1.NO_AUDIT_RECORD_FOUND, NOT_FOUND_STATUS_CODE);
183
184         // try with invalid date format, should result in error
185         uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + POLICY_A.getName() + URI_SEPERATOR
186                         + POLICY_A.getVersion() + QUERY_PARAMS_INVALID;
187         sendAndValidateError(uri, BAD_REQUEST_MSG, BAD_REQUEST_STATUS_CODE);
188     }
189
190     private void sendAndValidateSuccess(String uri, int count) throws Exception {
191         Invocation.Builder invocationBuilder = sendRequest(uri);
192         Response rawresp = invocationBuilder.get();
193         assertThat(rawresp.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
194         List<PolicyAudit> resp = rawresp.readEntity(new GenericType<List<PolicyAudit>>() {});
195         assertThat(resp).hasSize(count);
196         for (PolicyAudit audit : resp) {
197             if (audit.getAuditId() == 123L) {
198                 assertThat(audit.getPdpGroup()).isEqualTo(TEST_GROUP);
199                 assertThat(audit.getPdpType()).isEqualTo(TEST_PDP_TYPE);
200                 assertThat(audit.getPolicy()).isEqualTo(POLICY_A);
201                 assertThat(audit.getAction()).isEqualTo(AuditAction.DEPLOYMENT);
202                 assertThat(audit.getUser()).isEqualTo(DEFAULT_USER);
203             } else if (audit.getAuditId() == 456L) {
204                 assertThat(audit.getPdpGroup()).isEqualTo(TEST_GROUP);
205                 assertThat(audit.getPdpType()).isEqualTo(TEST_PDP_TYPE);
206                 assertThat(audit.getPolicy()).isEqualTo(POLICY_B);
207                 assertThat(audit.getAction()).isEqualTo(AuditAction.UNDEPLOYMENT);
208                 assertThat(audit.getUser()).isEqualTo(DEFAULT_USER);
209             }
210         }
211     }
212
213     private void sendAndValidateError(String uri, String errorMessage, int statusCode) throws Exception {
214         Invocation.Builder invocationBuilder = sendRequest(uri);
215         Response rawresp = invocationBuilder.get();
216         assertThat(rawresp.getStatus()).isEqualTo(statusCode);
217         String resp = rawresp.readEntity(String.class);
218         assertThat(resp).contains(errorMessage);
219     }
220 }