Add datetime format to audit api's
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / rest / e2e / PolicyAuditTest.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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=========================================================
17  */
18
19 package org.onap.policy.pap.main.rest.e2e;
20
21 import static org.assertj.core.api.Assertions.assertThat;
22
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;
41
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&fromDate=2021-07-25T01:25:15";
51     private static final String QUERY_PARAMS_CORRECT = "?recordCount=5&fromDate=1627219515&toDate=1627478715";
52     private static final String QUERY_PARAMS_INCORRECT = "?recordCount=5&fromDate=1627478715&toDate=1627565115";
53
54     @Override
55     @Before
56     public void setUp() throws Exception {
57         super.setUp();
58         setupEnv();
59     }
60
61     private void setupEnv() {
62         List<PolicyAudit> recordList = new ArrayList<>();
63         Instant auditRecordTime = Instant.ofEpochSecond(1627392315L);
64         PolicyModelsProviderFactoryWrapper modelProviderWrapper =
65                         Registry.get(PapConstants.REG_PAP_DAO_FACTORY, PolicyModelsProviderFactoryWrapper.class);
66
67         try (PolicyModelsProvider databaseProvider = modelProviderWrapper.create()) {
68             PolicyAudit audit1 = PolicyAudit.builder().auditId(123L).pdpGroup(TEST_GROUP).pdpType(TEST_PDP_TYPE)
69                             .policy(POLICY_A).action(AuditAction.DEPLOYMENT)
70                             .timestamp(auditRecordTime).user(DEFAULT_USER).build();
71             PolicyAudit audit2 = PolicyAudit.builder().auditId(456L).pdpGroup(TEST_GROUP).pdpType(TEST_PDP_TYPE)
72                             .policy(POLICY_B).action(AuditAction.UNDEPLOYMENT)
73                             .timestamp(auditRecordTime).user(DEFAULT_USER).build();
74             recordList.add(audit1);
75             recordList.add(audit2);
76             databaseProvider.createAuditRecords(recordList);
77         } catch (final PfModelException exp) {
78             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, exp.getMessage());
79         }
80     }
81
82     @Test
83     public void testGetAllAuditRecords() throws Exception {
84         String uri = POLICY_AUDIT_ENDPOINT;
85         sendAndValidateSuccess(uri, 2);
86     }
87
88     @Test
89     public void testGetAllAuditRecordsWithParams() throws Exception {
90         // try with correct dates in query, should result in 2 records
91         String uri = POLICY_AUDIT_ENDPOINT + QUERY_PARAMS_CORRECT;
92         sendAndValidateSuccess(uri, 2);
93
94         // try with incorrect dates in query, should result in 0 record
95         uri = POLICY_AUDIT_ENDPOINT + QUERY_PARAMS_INCORRECT;
96         sendAndValidateSuccess(uri, 0);
97
98         // try with invalid date format, should result in error
99         uri = POLICY_AUDIT_ENDPOINT + QUERY_PARAMS_INVALID;
100         sendAndValidateError(uri, Response.Status.NOT_FOUND.toString());
101     }
102
103     @Test
104     public void testGetAuditRecordsByGroup() throws Exception {
105         String uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP;
106         sendAndValidateSuccess(uri, 2);
107     }
108
109     @Test
110     public void testGetAuditRecordsByGroupWithParams() throws Exception {
111         // try with correct dates in query, should result in 2 records
112         String uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP + QUERY_PARAMS_CORRECT;
113         sendAndValidateSuccess(uri, 2);
114
115         // try with incorrect dates in query, should result in error
116         uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP + QUERY_PARAMS_INCORRECT;
117         sendAndValidateError(uri, PolicyAuditControllerV1.NO_AUDIT_RECORD_FOUND);
118
119         // try with invalid date format, should result in error
120         uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP + QUERY_PARAMS_INVALID;
121         sendAndValidateError(uri, Response.Status.NOT_FOUND.toString());
122     }
123
124     @Test
125     public void testGetAuditRecordsOfPolicyWithGroup() throws Exception {
126         String uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP + URI_SEPERATOR + POLICY_A.getName()
127                         + URI_SEPERATOR + POLICY_A.getVersion();
128         sendAndValidateSuccess(uri, 1);
129     }
130
131     @Test
132     public void testGetAuditRecordsOfPolicyWithGroupWithParams() throws Exception {
133         // try with correct dates in query, should result in 1 record
134         String uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP + URI_SEPERATOR + POLICY_A.getName()
135                         + URI_SEPERATOR + POLICY_A.getVersion() + QUERY_PARAMS_CORRECT;
136         sendAndValidateSuccess(uri, 1);
137
138         // try with incorrect dates in query, should result in error
139         uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP + URI_SEPERATOR + POLICY_A.getName()
140                         + URI_SEPERATOR + POLICY_A.getVersion() + QUERY_PARAMS_INCORRECT;
141         sendAndValidateError(uri, PolicyAuditControllerV1.NO_AUDIT_RECORD_FOUND);
142
143         // try with invalid date format, should result in error
144         uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP + URI_SEPERATOR + POLICY_A.getName() + URI_SEPERATOR
145                         + POLICY_A.getVersion() + QUERY_PARAMS_INVALID;
146         sendAndValidateError(uri, Response.Status.NOT_FOUND.toString());
147     }
148
149     @Test
150     public void testGetAuditRecordsOfPolicyWithoutGroup() throws Exception {
151         String uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + POLICY_A.getName() + URI_SEPERATOR + POLICY_A.getVersion();
152         sendAndValidateSuccess(uri, 1);
153     }
154
155     @Test
156     public void testGetAuditRecordsOfPolicyWithoutGroupWithParams() throws Exception {
157         // try with correct dates in query, should result in 1 record
158         String uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + POLICY_A.getName() + URI_SEPERATOR + POLICY_A.getVersion()
159                         + QUERY_PARAMS_CORRECT;
160         sendAndValidateSuccess(uri, 1);
161
162         // try with incorrect dates in query, should result in error
163         uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + POLICY_A.getName() + URI_SEPERATOR + POLICY_A.getVersion()
164                         + QUERY_PARAMS_INCORRECT;
165         sendAndValidateError(uri, PolicyAuditControllerV1.NO_AUDIT_RECORD_FOUND);
166
167         // try with invalid date format, should result in error
168         uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + POLICY_A.getName() + URI_SEPERATOR
169                         + POLICY_A.getVersion() + QUERY_PARAMS_INVALID;
170         sendAndValidateError(uri, Response.Status.NOT_FOUND.toString());
171     }
172
173     private void sendAndValidateSuccess(String uri, int count) throws Exception {
174         Invocation.Builder invocationBuilder = sendRequest(uri);
175         Response rawresp = invocationBuilder.get();
176         assertThat(rawresp.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
177         List<PolicyAudit> resp = rawresp.readEntity(new GenericType<List<PolicyAudit>>() {});
178         assertThat(resp).hasSize(count);
179         for (PolicyAudit audit : resp) {
180             if (audit.getAuditId() == 123L) {
181                 assertThat(audit.getPdpGroup()).isEqualTo(TEST_GROUP);
182                 assertThat(audit.getPdpType()).isEqualTo(TEST_PDP_TYPE);
183                 assertThat(audit.getPolicy()).isEqualTo(POLICY_A);
184                 assertThat(audit.getAction()).isEqualTo(AuditAction.DEPLOYMENT);
185                 assertThat(audit.getUser()).isEqualTo(DEFAULT_USER);
186             } else if (audit.getAuditId() == 456L) {
187                 assertThat(audit.getPdpGroup()).isEqualTo(TEST_GROUP);
188                 assertThat(audit.getPdpType()).isEqualTo(TEST_PDP_TYPE);
189                 assertThat(audit.getPolicy()).isEqualTo(POLICY_B);
190                 assertThat(audit.getAction()).isEqualTo(AuditAction.UNDEPLOYMENT);
191                 assertThat(audit.getUser()).isEqualTo(DEFAULT_USER);
192             }
193         }
194     }
195
196     private void sendAndValidateError(String uri, String errorMessage) throws Exception {
197         Invocation.Builder invocationBuilder = sendRequest(uri);
198         Response rawresp = invocationBuilder.get();
199         assertThat(rawresp.getStatus()).isEqualTo(Response.Status.NOT_FOUND.getStatusCode());
200         String resp = rawresp.readEntity(String.class);
201         assertThat(resp).contains(errorMessage);
202     }
203 }