b0a135d2934f8e8a1973dc23ac766dda6045fa99
[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  * ================================================================================
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.After;
30 import org.junit.Before;
31 import org.junit.Test;
32 import org.onap.policy.models.base.PfModelRuntimeException;
33 import org.onap.policy.models.pap.concepts.PolicyAudit;
34 import org.onap.policy.models.pap.concepts.PolicyAudit.AuditAction;
35 import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
36 import org.onap.policy.pap.main.repository.PolicyAuditRepository;
37 import org.onap.policy.pap.main.rest.PolicyAuditControllerV1;
38 import org.onap.policy.pap.main.service.PolicyAuditService;
39 import org.springframework.beans.factory.annotation.Autowired;
40
41 public class PolicyAuditTest extends End2EndBase {
42     private static final String TEST_GROUP = "testGroup";
43     private static final String TEST_PDP_TYPE = "testPdpType";
44     private static final ToscaConceptIdentifier POLICY_A = new ToscaConceptIdentifier("PolicyA", "1.0.0");
45     private static final ToscaConceptIdentifier POLICY_B = new ToscaConceptIdentifier("PolicyB", "2.0.0");
46     private static final String DEFAULT_USER = "TEST";
47     private static final String POLICY_AUDIT_ENDPOINT = "policies/audit";
48     private static final String URI_SEPERATOR = "/";
49     private static final String QUERY_PARAMS_INVALID = "?recordCount=5&startTime=2021-07-25T01:25:15";
50     private static final String QUERY_PARAMS_CORRECT = "?recordCount=5&startTime=1627219515&endTime=1627478715";
51     private static final String QUERY_PARAMS_INCORRECT = "?recordCount=5&startTime=1627478715&endTime=1627565115";
52     private static int NOT_FOUND_STATUS_CODE = 404;
53     private static int BAD_REQUEST_STATUS_CODE = 400;
54     private static final String BAD_REQUEST_MSG = "NumberFormatException For";
55
56     @Autowired
57     private PolicyAuditService policyAuditService;
58
59     @Autowired
60     private PolicyAuditRepository policyAuditRepository;
61
62     @Override
63     @Before
64     public void setUp() throws Exception {
65         super.setUp();
66         setupEnv();
67     }
68
69     /**
70      * Teardown after tests.
71      */
72     @Override
73     @After
74     public void tearDown() {
75         policyAuditRepository.deleteAll();
76         super.tearDown();
77     }
78
79     private void setupEnv() {
80         List<PolicyAudit> recordList = new ArrayList<>();
81         Instant auditRecordTime = Instant.ofEpochSecond(1627392315L);
82
83         try {
84             PolicyAudit audit1 = PolicyAudit.builder().pdpGroup(TEST_GROUP).pdpType(TEST_PDP_TYPE)
85                             .policy(POLICY_A).action(AuditAction.DEPLOYMENT)
86                             .timestamp(auditRecordTime).user(DEFAULT_USER).build();
87             PolicyAudit audit2 = PolicyAudit.builder().pdpGroup(TEST_GROUP).pdpType(TEST_PDP_TYPE)
88                             .policy(POLICY_B).action(AuditAction.UNDEPLOYMENT)
89                             .timestamp(auditRecordTime).user(DEFAULT_USER).build();
90             recordList.add(audit1);
91             recordList.add(audit2);
92             policyAuditService.createAuditRecords(recordList);
93         } catch (Exception exp) {
94             throw new PfModelRuntimeException(Response.Status.BAD_REQUEST, exp.getMessage());
95         }
96     }
97
98     @Test
99     public void testGetAllAuditRecords() throws Exception {
100         String uri = POLICY_AUDIT_ENDPOINT;
101         sendAndValidateSuccess(uri, 2);
102     }
103
104     @Test
105     public void testGetAllAuditRecordsWithParams() throws Exception {
106         // try with correct dates in query, should result in 2 records
107         String uri = POLICY_AUDIT_ENDPOINT + QUERY_PARAMS_CORRECT;
108         sendAndValidateSuccess(uri, 2);
109
110         // try with incorrect dates in query, should result in 0 record
111         uri = POLICY_AUDIT_ENDPOINT + QUERY_PARAMS_INCORRECT;
112         sendAndValidateSuccess(uri, 0);
113
114         // try with invalid date format, should result in error
115         uri = POLICY_AUDIT_ENDPOINT + QUERY_PARAMS_INVALID;
116         sendAndValidateError(uri, BAD_REQUEST_MSG, BAD_REQUEST_STATUS_CODE);
117     }
118
119     @Test
120     public void testGetAuditRecordsByGroup() throws Exception {
121         String uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP;
122         sendAndValidateSuccess(uri, 2);
123     }
124
125     @Test
126     public void testGetAuditRecordsByGroupWithParams() throws Exception {
127         // try with correct dates in query, should result in 2 records
128         String uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP + QUERY_PARAMS_CORRECT;
129         sendAndValidateSuccess(uri, 2);
130
131         // try with incorrect dates in query, should result in error
132         uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP + QUERY_PARAMS_INCORRECT;
133         sendAndValidateError(uri, PolicyAuditControllerV1.NO_AUDIT_RECORD_FOUND, NOT_FOUND_STATUS_CODE);
134
135         // try with invalid date format, should result in error
136         uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP + QUERY_PARAMS_INVALID;
137         sendAndValidateError(uri, BAD_REQUEST_MSG, BAD_REQUEST_STATUS_CODE);
138     }
139
140     @Test
141     public void testGetAuditRecordsOfPolicyWithGroup() throws Exception {
142         String uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP + URI_SEPERATOR + POLICY_A.getName()
143                         + URI_SEPERATOR + POLICY_A.getVersion();
144         sendAndValidateSuccess(uri, 1);
145     }
146
147     @Test
148     public void testGetAuditRecordsOfPolicyWithGroupWithParams() throws Exception {
149         // try with correct dates in query, should result in 1 record
150         String uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP + URI_SEPERATOR + POLICY_A.getName()
151                         + URI_SEPERATOR + POLICY_A.getVersion() + QUERY_PARAMS_CORRECT;
152         sendAndValidateSuccess(uri, 1);
153
154         // try with incorrect dates in query, should result in error
155         uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP + URI_SEPERATOR + POLICY_A.getName()
156                         + URI_SEPERATOR + POLICY_A.getVersion() + QUERY_PARAMS_INCORRECT;
157         sendAndValidateError(uri, PolicyAuditControllerV1.NO_AUDIT_RECORD_FOUND, NOT_FOUND_STATUS_CODE);
158
159         // try with invalid date format, should result in error
160         uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP + URI_SEPERATOR + POLICY_A.getName() + URI_SEPERATOR
161                         + POLICY_A.getVersion() + QUERY_PARAMS_INVALID;
162         sendAndValidateError(uri, BAD_REQUEST_MSG, BAD_REQUEST_STATUS_CODE);
163     }
164
165     @Test
166     public void testGetAuditRecordsOfPolicyWithoutGroup() throws Exception {
167         String uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + POLICY_A.getName() + URI_SEPERATOR + POLICY_A.getVersion();
168         sendAndValidateSuccess(uri, 1);
169     }
170
171     @Test
172     public void testGetAuditRecordsOfPolicyWithoutGroupWithParams() throws Exception {
173         // try with correct dates in query, should result in 1 record
174         String uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + POLICY_A.getName() + URI_SEPERATOR + POLICY_A.getVersion()
175                         + QUERY_PARAMS_CORRECT;
176         sendAndValidateSuccess(uri, 1);
177
178         // try with incorrect dates in query, should result in error
179         uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + POLICY_A.getName() + URI_SEPERATOR + POLICY_A.getVersion()
180                         + QUERY_PARAMS_INCORRECT;
181         sendAndValidateError(uri, PolicyAuditControllerV1.NO_AUDIT_RECORD_FOUND, NOT_FOUND_STATUS_CODE);
182
183         // try with invalid date format, should result in error
184         uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + POLICY_A.getName() + URI_SEPERATOR
185                         + POLICY_A.getVersion() + QUERY_PARAMS_INVALID;
186         sendAndValidateError(uri, BAD_REQUEST_MSG, BAD_REQUEST_STATUS_CODE);
187     }
188
189     private void sendAndValidateSuccess(String uri, int count) throws Exception {
190         Invocation.Builder invocationBuilder = sendRequest(uri);
191         Response rawresp = invocationBuilder.get();
192         assertThat(rawresp.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
193         List<PolicyAudit> resp = rawresp.readEntity(new GenericType<List<PolicyAudit>>() {});
194         assertThat(resp).hasSize(count);
195         for (PolicyAudit audit : resp) {
196             if (audit.getAuditId() == 123L) {
197                 assertThat(audit.getPdpGroup()).isEqualTo(TEST_GROUP);
198                 assertThat(audit.getPdpType()).isEqualTo(TEST_PDP_TYPE);
199                 assertThat(audit.getPolicy()).isEqualTo(POLICY_A);
200                 assertThat(audit.getAction()).isEqualTo(AuditAction.DEPLOYMENT);
201                 assertThat(audit.getUser()).isEqualTo(DEFAULT_USER);
202             } else if (audit.getAuditId() == 456L) {
203                 assertThat(audit.getPdpGroup()).isEqualTo(TEST_GROUP);
204                 assertThat(audit.getPdpType()).isEqualTo(TEST_PDP_TYPE);
205                 assertThat(audit.getPolicy()).isEqualTo(POLICY_B);
206                 assertThat(audit.getAction()).isEqualTo(AuditAction.UNDEPLOYMENT);
207                 assertThat(audit.getUser()).isEqualTo(DEFAULT_USER);
208             }
209         }
210     }
211
212     private void sendAndValidateError(String uri, String errorMessage, int statusCode) throws Exception {
213         Invocation.Builder invocationBuilder = sendRequest(uri);
214         Response rawresp = invocationBuilder.get();
215         assertThat(rawresp.getStatus()).isEqualTo(statusCode);
216         String resp = rawresp.readEntity(String.class);
217         assertThat(resp).contains(errorMessage);
218     }
219 }