aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--main/src/main/java/org/onap/policy/pap/main/rest/PolicyAuditControllerV1.java86
-rw-r--r--main/src/test/java/org/onap/policy/pap/main/rest/e2e/PolicyAuditTest.java120
2 files changed, 138 insertions, 68 deletions
diff --git a/main/src/main/java/org/onap/policy/pap/main/rest/PolicyAuditControllerV1.java b/main/src/main/java/org/onap/policy/pap/main/rest/PolicyAuditControllerV1.java
index e669594a..7848cf38 100644
--- a/main/src/main/java/org/onap/policy/pap/main/rest/PolicyAuditControllerV1.java
+++ b/main/src/main/java/org/onap/policy/pap/main/rest/PolicyAuditControllerV1.java
@@ -26,8 +26,8 @@ import io.swagger.annotations.Authorization;
import io.swagger.annotations.Extension;
import io.swagger.annotations.ExtensionProperty;
import io.swagger.annotations.ResponseHeader;
+import java.time.Instant;
import java.util.Collection;
-import java.util.Date;
import java.util.UUID;
import javax.ws.rs.GET;
import javax.ws.rs.HeaderParam;
@@ -47,9 +47,10 @@ import org.slf4j.LoggerFactory;
* various operations on policies.
*/
public class PolicyAuditControllerV1 extends PapRestControllerV1 {
- private static final String GET_AUDIT_RECORD_FAILED = "get audit records failed";
private static final Logger logger = LoggerFactory.getLogger(PolicyAuditControllerV1.class);
+ private static final String GET_AUDIT_RECORD_FAILED = "get audit records failed";
+ public static final String NO_AUDIT_RECORD_FOUND = "No records found matching the input parameters";
private final PolicyAuditProvider provider = new PolicyAuditProvider();
@@ -58,8 +59,8 @@ public class PolicyAuditControllerV1 extends PapRestControllerV1 {
*
* @param requestId request ID used in ONAP logging
* @param recordCount number of records to fetch
- * @param fromDate the starting date for the query
- * @param toDate the ending date for the query
+ * @param fromDate the starting date for the query in epoch timestamp
+ * @param toDate the ending date for the query in epoch timestamp
* @return a response
*/
// @formatter:off
@@ -96,16 +97,18 @@ public class PolicyAuditControllerV1 extends PapRestControllerV1 {
public Response getAllAuditRecords(
@HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId,
- @ApiParam(value = "Record Count",
+ @ApiParam(value = "Record count between 1-100",
required = false) @QueryParam("recordCount") final int recordCount,
- @ApiParam(value = "From Date", required = false) @QueryParam("fromDate") final Date fromDate,
- @ApiParam(value = "To Date", required = false) @QueryParam("toDate") final Date toDate) {
+ @ApiParam(value = "From date in epoch timestamp",
+ required = false) @QueryParam("fromDate") final Long fromDate,
+ @ApiParam(value = "To date in epoch timestamp",
+ required = false) @QueryParam("toDate") final Long toDate) {
try {
return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
.entity(provider.getAuditRecords(AuditFilter.builder().recordNum(recordCount)
- .fromDate((fromDate == null ? null : fromDate.toInstant()))
- .toDate((toDate == null ? null : toDate.toInstant())).build()))
+ .fromDate(convertEpochtoInstant(fromDate))
+ .toDate(convertEpochtoInstant(toDate)).build()))
.build();
} catch (PfModelException | PfModelRuntimeException exp) {
@@ -121,8 +124,8 @@ public class PolicyAuditControllerV1 extends PapRestControllerV1 {
*
* @param requestId request ID used in ONAP logging
* @param recordCount number of records to fetch
- * @param fromDate the starting date for the query
- * @param toDate the ending date for the query
+ * @param fromDate the starting date for the query in epoch timestamp
+ * @param toDate the ending date for the query in epoch timestamp
* @param pdpGroupName the pdp group name for the query
* @return a response
*/
@@ -160,17 +163,20 @@ public class PolicyAuditControllerV1 extends PapRestControllerV1 {
public Response getAuditRecordsByGroup(
@HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId,
- @ApiParam(value = "Record Count",
+ @ApiParam(value = "Record count between 1-100",
required = false) @QueryParam("recordCount") final int recordCount,
- @ApiParam(value = "From Date", required = false) @QueryParam("fromDate") final Date fromDate,
- @ApiParam(value = "To Date", required = false) @QueryParam("toDate") final Date toDate,
+ @ApiParam(value = "From date in epoch timestamp",
+ required = false) @QueryParam("fromDate") final Long fromDate,
+ @ApiParam(value = "To date in epoch timestamp",
+ required = false) @QueryParam("toDate") final Long toDate,
@ApiParam(value = "PDP Group Name",
required = true) @PathParam("pdpGroupName") String pdpGroupName) {
try {
- return makeOkOrNotFoundResponse(requestId, provider.getAuditRecords(AuditFilter.builder()
- .recordNum(recordCount).fromDate((fromDate == null ? null : fromDate.toInstant()))
- .toDate((toDate == null ? null : toDate.toInstant())).pdpGroup(pdpGroupName).build()));
+ return makeOkOrNotFoundResponse(requestId,
+ provider.getAuditRecords(AuditFilter.builder().recordNum(recordCount)
+ .fromDate((convertEpochtoInstant(fromDate)))
+ .toDate(convertEpochtoInstant(toDate)).pdpGroup(pdpGroupName).build()));
} catch (PfModelException | PfModelRuntimeException exp) {
logger.warn(GET_AUDIT_RECORD_FAILED, exp);
@@ -185,8 +191,8 @@ public class PolicyAuditControllerV1 extends PapRestControllerV1 {
*
* @param requestId request ID used in ONAP logging
* @param recordCount number of records to fetch
- * @param fromDate the starting date for the query
- * @param toDate the ending date for the query
+ * @param fromDate the starting date for the query in epoch timestamp
+ * @param toDate the ending date for the query in epoch timestamp
* @param pdpGroupName the pdp group name for the query
* @param policyName name of the Policy
* @param policyVersion version of the Policy
@@ -226,20 +232,22 @@ public class PolicyAuditControllerV1 extends PapRestControllerV1 {
public Response getAuditRecordsOfPolicy(
@HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId,
- @ApiParam(value = "Record Count",
+ @ApiParam(value = "Record count between 1-100",
required = false) @QueryParam("recordCount") final int recordCount,
- @ApiParam(value = "From Date", required = false) @QueryParam("fromDate") final Date fromDate,
- @ApiParam(value = "To Date", required = false) @QueryParam("toDate") final Date toDate,
+ @ApiParam(value = "From date in epoch timestamp",
+ required = false) @QueryParam("fromDate") final Long fromDate,
+ @ApiParam(value = "To date in epoch timestamp",
+ required = false) @QueryParam("toDate") final Long toDate,
@ApiParam(value = "PDP Group Name", required = true) @PathParam("pdpGroupName") String pdpGroupName,
- @ApiParam(value = "Policy Id", required = true) @PathParam("policyName") String policyName,
+ @ApiParam(value = "Policy Name", required = true) @PathParam("policyName") String policyName,
@ApiParam(value = "Policy Version",
required = true) @PathParam("policyVersion") String policyVersion) {
try {
return makeOkOrNotFoundResponse(requestId,
provider.getAuditRecords(AuditFilter.builder().recordNum(recordCount)
- .fromDate((fromDate == null ? null : fromDate.toInstant()))
- .toDate((toDate == null ? null : toDate.toInstant())).pdpGroup(pdpGroupName)
+ .fromDate(convertEpochtoInstant(fromDate))
+ .toDate(convertEpochtoInstant(toDate)).pdpGroup(pdpGroupName)
.name(policyName).version(policyVersion).build()));
} catch (PfModelException | PfModelRuntimeException exp) {
@@ -255,8 +263,8 @@ public class PolicyAuditControllerV1 extends PapRestControllerV1 {
*
* @param requestId request ID used in ONAP logging
* @param recordCount number of records to fetch
- * @param fromDate the starting date for the query
- * @param toDate the ending date for the query
+ * @param fromDate the starting date for the query in epoch timestamp
+ * @param toDate the ending date for the query in epoch timestamp
* @param policyName name of the Policy
* @param policyVersion version of the Policy
* @return a response
@@ -295,20 +303,20 @@ public class PolicyAuditControllerV1 extends PapRestControllerV1 {
public Response getAuditRecordsOfPolicy(
@HeaderParam(REQUEST_ID_NAME) @ApiParam(REQUEST_ID_PARAM_DESCRIPTION) final UUID requestId,
- @ApiParam(value = "Record Count",
+ @ApiParam(value = "Record count between 1-100",
required = false) @QueryParam("recordCount") final int recordCount,
- @ApiParam(value = "From Date", required = false) @QueryParam("fromDate") final Date fromDate,
- @ApiParam(value = "To Date", required = false) @QueryParam("toDate") final Date toDate,
- @ApiParam(value = "Policy Id", required = true) @PathParam("policyName") String policyName,
+ @ApiParam(value = "From date in epoch timestamp",
+ required = false) @QueryParam("fromDate") final Long fromDate,
+ @ApiParam(value = "To date in epoch timestamp",
+ required = false) @QueryParam("toDate") final Long toDate,
+ @ApiParam(value = "Policy Name", required = true) @PathParam("policyName") String policyName,
@ApiParam(value = "Policy Version",
required = true) @PathParam("policyVersion") String policyVersion) {
try {
- return makeOkOrNotFoundResponse(requestId,
- provider.getAuditRecords(AuditFilter.builder().recordNum(recordCount)
- .fromDate((fromDate == null ? null : fromDate.toInstant()))
- .toDate((toDate == null ? null : toDate.toInstant())).name(policyName)
- .version(policyVersion).build()));
+ return makeOkOrNotFoundResponse(requestId, provider.getAuditRecords(AuditFilter.builder()
+ .recordNum(recordCount).fromDate(convertEpochtoInstant(fromDate))
+ .toDate(convertEpochtoInstant(toDate)).name(policyName).version(policyVersion).build()));
} catch (PfModelException | PfModelRuntimeException exp) {
logger.warn(GET_AUDIT_RECORD_FAILED, exp);
@@ -321,10 +329,14 @@ public class PolicyAuditControllerV1 extends PapRestControllerV1 {
private Response makeOkOrNotFoundResponse(UUID requestId, Collection<PolicyAudit> result) {
if (result.isEmpty()) {
return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.NOT_FOUND)), requestId)
- .build();
+ .entity(NO_AUDIT_RECORD_FOUND).build();
} else {
return addLoggingHeaders(addVersionControlHeaders(Response.status(Response.Status.OK)), requestId)
.entity(result).build();
}
}
+
+ private Instant convertEpochtoInstant(Long epochSecond) {
+ return (epochSecond == null ? null : Instant.ofEpochSecond(epochSecond));
+ }
}
diff --git a/main/src/test/java/org/onap/policy/pap/main/rest/e2e/PolicyAuditTest.java b/main/src/test/java/org/onap/policy/pap/main/rest/e2e/PolicyAuditTest.java
index c4de1f4a..e1211f34 100644
--- a/main/src/test/java/org/onap/policy/pap/main/rest/e2e/PolicyAuditTest.java
+++ b/main/src/test/java/org/onap/policy/pap/main/rest/e2e/PolicyAuditTest.java
@@ -19,7 +19,6 @@
package org.onap.policy.pap.main.rest.e2e;
import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.Assert.assertEquals;
import java.time.Instant;
import java.util.ArrayList;
@@ -38,6 +37,7 @@ import org.onap.policy.models.provider.PolicyModelsProvider;
import org.onap.policy.models.tosca.authorative.concepts.ToscaConceptIdentifier;
import org.onap.policy.pap.main.PapConstants;
import org.onap.policy.pap.main.PolicyModelsProviderFactoryWrapper;
+import org.onap.policy.pap.main.rest.PolicyAuditControllerV1;
public class PolicyAuditTest extends End2EndBase {
private static final String TEST_GROUP = "testGroup";
@@ -47,6 +47,9 @@ public class PolicyAuditTest extends End2EndBase {
private static final String DEFAULT_USER = "TEST";
private static final String POLICY_AUDIT_ENDPOINT = "policies/audit";
private static final String URI_SEPERATOR = "/";
+ private static final String QUERY_PARAMS_INVALID = "?recordCount=5&fromDate=2021-07-25T01:25:15";
+ private static final String QUERY_PARAMS_CORRECT = "?recordCount=5&fromDate=1627219515&toDate=1627478715";
+ private static final String QUERY_PARAMS_INCORRECT = "?recordCount=5&fromDate=1627478715&toDate=1627565115";
@Override
@Before
@@ -57,16 +60,17 @@ public class PolicyAuditTest extends End2EndBase {
private void setupEnv() {
List<PolicyAudit> recordList = new ArrayList<>();
+ Instant auditRecordTime = Instant.ofEpochSecond(1627392315L);
PolicyModelsProviderFactoryWrapper modelProviderWrapper =
Registry.get(PapConstants.REG_PAP_DAO_FACTORY, PolicyModelsProviderFactoryWrapper.class);
try (PolicyModelsProvider databaseProvider = modelProviderWrapper.create()) {
PolicyAudit audit1 = PolicyAudit.builder().auditId(123L).pdpGroup(TEST_GROUP).pdpType(TEST_PDP_TYPE)
- .policy(POLICY_A).action(AuditAction.DEPLOYMENT).timestamp(Instant.now()).user(DEFAULT_USER)
- .build();
+ .policy(POLICY_A).action(AuditAction.DEPLOYMENT)
+ .timestamp(auditRecordTime).user(DEFAULT_USER).build();
PolicyAudit audit2 = PolicyAudit.builder().auditId(456L).pdpGroup(TEST_GROUP).pdpType(TEST_PDP_TYPE)
- .policy(POLICY_B).action(AuditAction.UNDEPLOYMENT).timestamp(Instant.now())
- .user(DEFAULT_USER).build();
+ .policy(POLICY_B).action(AuditAction.UNDEPLOYMENT)
+ .timestamp(auditRecordTime).user(DEFAULT_USER).build();
recordList.add(audit1);
recordList.add(audit2);
databaseProvider.createAuditRecords(recordList);
@@ -78,55 +82,101 @@ public class PolicyAuditTest extends End2EndBase {
@Test
public void testGetAllAuditRecords() throws Exception {
String uri = POLICY_AUDIT_ENDPOINT;
+ sendAndValidateSuccess(uri, 2);
+ }
- Invocation.Builder invocationBuilder = sendRequest(uri);
- Response rawresp = invocationBuilder.get();
- assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
-
- List<PolicyAudit> resp = rawresp.readEntity(new GenericType<List<PolicyAudit>>() {});
- validateAuditRecords(resp, 2);
+ @Test
+ public void testGetAllAuditRecordsWithParams() throws Exception {
+ // try with correct dates in query, should result in 2 records
+ String uri = POLICY_AUDIT_ENDPOINT + QUERY_PARAMS_CORRECT;
+ sendAndValidateSuccess(uri, 2);
+
+ // try with incorrect dates in query, should result in 0 record
+ uri = POLICY_AUDIT_ENDPOINT + QUERY_PARAMS_INCORRECT;
+ sendAndValidateSuccess(uri, 0);
+
+ // try with invalid date format, should result in error
+ uri = POLICY_AUDIT_ENDPOINT + QUERY_PARAMS_INVALID;
+ sendAndValidateError(uri, Response.Status.NOT_FOUND.toString());
}
@Test
public void testGetAuditRecordsByGroup() throws Exception {
String uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP;
+ sendAndValidateSuccess(uri, 2);
+ }
- Invocation.Builder invocationBuilder = sendRequest(uri);
- Response rawresp = invocationBuilder.get();
- assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
-
- List<PolicyAudit> resp = rawresp.readEntity(new GenericType<List<PolicyAudit>>() {});
- validateAuditRecords(resp, 2);
+ @Test
+ public void testGetAuditRecordsByGroupWithParams() throws Exception {
+ // try with correct dates in query, should result in 2 records
+ String uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP + QUERY_PARAMS_CORRECT;
+ sendAndValidateSuccess(uri, 2);
+
+ // try with incorrect dates in query, should result in error
+ uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP + QUERY_PARAMS_INCORRECT;
+ sendAndValidateError(uri, PolicyAuditControllerV1.NO_AUDIT_RECORD_FOUND);
+
+ // try with invalid date format, should result in error
+ uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP + QUERY_PARAMS_INVALID;
+ sendAndValidateError(uri, Response.Status.NOT_FOUND.toString());
}
@Test
public void testGetAuditRecordsOfPolicyWithGroup() throws Exception {
String uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP + URI_SEPERATOR + POLICY_A.getName()
+ URI_SEPERATOR + POLICY_A.getVersion();
+ sendAndValidateSuccess(uri, 1);
+ }
- Invocation.Builder invocationBuilder = sendRequest(uri);
- Response rawresp = invocationBuilder.get();
- assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
-
- List<PolicyAudit> resp = rawresp.readEntity(new GenericType<List<PolicyAudit>>() {});
- validateAuditRecords(resp, 1);
+ @Test
+ public void testGetAuditRecordsOfPolicyWithGroupWithParams() throws Exception {
+ // try with correct dates in query, should result in 1 record
+ String uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP + URI_SEPERATOR + POLICY_A.getName()
+ + URI_SEPERATOR + POLICY_A.getVersion() + QUERY_PARAMS_CORRECT;
+ sendAndValidateSuccess(uri, 1);
+
+ // try with incorrect dates in query, should result in error
+ uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP + URI_SEPERATOR + POLICY_A.getName()
+ + URI_SEPERATOR + POLICY_A.getVersion() + QUERY_PARAMS_INCORRECT;
+ sendAndValidateError(uri, PolicyAuditControllerV1.NO_AUDIT_RECORD_FOUND);
+
+ // try with invalid date format, should result in error
+ uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + TEST_GROUP + URI_SEPERATOR + POLICY_A.getName() + URI_SEPERATOR
+ + POLICY_A.getVersion() + QUERY_PARAMS_INVALID;
+ sendAndValidateError(uri, Response.Status.NOT_FOUND.toString());
}
@Test
public void testGetAuditRecordsOfPolicyWithoutGroup() throws Exception {
String uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + POLICY_A.getName() + URI_SEPERATOR + POLICY_A.getVersion();
+ sendAndValidateSuccess(uri, 1);
+ }
+
+ @Test
+ public void testGetAuditRecordsOfPolicyWithoutGroupWithParams() throws Exception {
+ // try with correct dates in query, should result in 1 record
+ String uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + POLICY_A.getName() + URI_SEPERATOR + POLICY_A.getVersion()
+ + QUERY_PARAMS_CORRECT;
+ sendAndValidateSuccess(uri, 1);
+
+ // try with incorrect dates in query, should result in error
+ uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + POLICY_A.getName() + URI_SEPERATOR + POLICY_A.getVersion()
+ + QUERY_PARAMS_INCORRECT;
+ sendAndValidateError(uri, PolicyAuditControllerV1.NO_AUDIT_RECORD_FOUND);
+
+ // try with invalid date format, should result in error
+ uri = POLICY_AUDIT_ENDPOINT + URI_SEPERATOR + POLICY_A.getName() + URI_SEPERATOR
+ + POLICY_A.getVersion() + QUERY_PARAMS_INVALID;
+ sendAndValidateError(uri, Response.Status.NOT_FOUND.toString());
+ }
+ private void sendAndValidateSuccess(String uri, int count) throws Exception {
Invocation.Builder invocationBuilder = sendRequest(uri);
Response rawresp = invocationBuilder.get();
- assertEquals(Response.Status.OK.getStatusCode(), rawresp.getStatus());
-
+ assertThat(rawresp.getStatus()).isEqualTo(Response.Status.OK.getStatusCode());
List<PolicyAudit> resp = rawresp.readEntity(new GenericType<List<PolicyAudit>>() {});
- validateAuditRecords(resp, 1);
- }
-
- private void validateAuditRecords(List<PolicyAudit> result, int count) {
- assertThat(result).hasSize(count);
- for (PolicyAudit audit : result) {
+ assertThat(resp).hasSize(count);
+ for (PolicyAudit audit : resp) {
if (audit.getAuditId() == 123L) {
assertThat(audit.getPdpGroup()).isEqualTo(TEST_GROUP);
assertThat(audit.getPdpType()).isEqualTo(TEST_PDP_TYPE);
@@ -142,4 +192,12 @@ public class PolicyAuditTest extends End2EndBase {
}
}
}
+
+ private void sendAndValidateError(String uri, String errorMessage) throws Exception {
+ Invocation.Builder invocationBuilder = sendRequest(uri);
+ Response rawresp = invocationBuilder.get();
+ assertThat(rawresp.getStatus()).isEqualTo(Response.Status.NOT_FOUND.getStatusCode());
+ String resp = rawresp.readEntity(String.class);
+ assertThat(resp).contains(errorMessage);
+ }
}