diff options
author | adheli.tavares <adheli.tavares@est.tech> | 2021-07-09 18:28:28 +0100 |
---|---|---|
committer | adheli.tavares <adheli.tavares@est.tech> | 2021-07-12 16:50:04 +0100 |
commit | c9067692f47fc532a6306e3dd8f5df75d8828a7b (patch) | |
tree | a5ad4ef3cfefc47129a197c4a5c2eb4edbc97616 /models-pap | |
parent | 43ed6b71f24a06834ee9fd6f7ceb28eda2c351ba (diff) |
Handle numRecords default setting when built as 0.
Check the numRecord to be set as DEFAULT_MAX_RECORDS when not added to
the auditFilter (builder set it to 0).
Issue-ID: POLICY-3340
Change-Id: I25ad83bbea71ddd82064aec3069ffa4cbce5aac1
Signed-off-by: adheli.tavares <adheli.tavares@est.tech>
Diffstat (limited to 'models-pap')
2 files changed, 23 insertions, 119 deletions
diff --git a/models-pap/src/main/java/org/onap/policy/models/pap/persistence/provider/PolicyAuditProvider.java b/models-pap/src/main/java/org/onap/policy/models/pap/persistence/provider/PolicyAuditProvider.java index 432e4c85e..7368d29c7 100644 --- a/models-pap/src/main/java/org/onap/policy/models/pap/persistence/provider/PolicyAuditProvider.java +++ b/models-pap/src/main/java/org/onap/policy/models/pap/persistence/provider/PolicyAuditProvider.java @@ -73,47 +73,18 @@ public class PolicyAuditProvider { } /** - * Collect all audit records. - * - * @param numRecords number of records to be collected - * @return list of {@link PolicyAudit} records - */ - public List<PolicyAudit> getAuditRecords(@NonNull PfDao dao, @NonNull Integer numRecords) { - numRecords = numRecords > DEFAULT_MAX_RECORDS ? DEFAULT_MAX_RECORDS : numRecords; - - // @formatter:off - return dao.getAll(JpaPolicyAudit.class, "timeStamp DESC", numRecords) - .stream() - .map(JpaPolicyAudit::toAuthorative) - .collect(Collectors.toList()); - // @formatter:on - } - - /** - * Collect audit records based on filters at {@link AuditFilter}. - * - * @param auditFilter {@link AuditFilter} object with filters for search - * @param numRecords number of records to be collected - * @return list of {@link PolicyAudit} records - */ - public List<PolicyAudit> getAuditRecords(@NonNull PfDao dao, @NonNull AuditFilter auditFilter, - @NonNull Integer numRecords) { - - auditFilter.setRecordNum(Math.min(numRecords, DEFAULT_MAX_RECORDS)); - - return getAuditRecords(dao, auditFilter); - } - - /** * Collect audit records based on filters at {@link AuditFilter}. * * @param auditFilter {@link AuditFilter} object with filters for search * @return list of {@link PolicyAudit} records */ public List<PolicyAudit> getAuditRecords(@NonNull PfDao dao, @NonNull AuditFilter auditFilter) { + if (auditFilter.getRecordNum() < 1 || auditFilter.getRecordNum() > DEFAULT_MAX_RECORDS) { + auditFilter.setRecordNum(DEFAULT_MAX_RECORDS); + } - return dao.getFiltered(JpaPolicyAudit.class, auditFilter) - .stream().map(JpaPolicyAudit::toAuthorative).collect(Collectors.toList()); + return dao.getFiltered(JpaPolicyAudit.class, auditFilter).stream().map(JpaPolicyAudit::toAuthorative) + .collect(Collectors.toList()); } /** @@ -140,16 +111,6 @@ public class PolicyAuditProvider { // initialized lazily, if not set via the builder private Map<String, Object> filterMap; - /** - * Check if even still using build(), none of the params were provided. - * - * @return {@code true} if all empty/null; {@code false} otherwise. - */ - public boolean isEmpty() { - return StringUtils.isAllEmpty(name, version, pdpGroup) && action == null && fromDate == null - && toDate == null; - } - @Override public Instant getStartTime() { return fromDate; diff --git a/models-pap/src/test/java/org/onap/policy/models/pap/persistence/provider/PolicyAuditProviderTest.java b/models-pap/src/test/java/org/onap/policy/models/pap/persistence/provider/PolicyAuditProviderTest.java index 0540d4844..7c70371d3 100644 --- a/models-pap/src/test/java/org/onap/policy/models/pap/persistence/provider/PolicyAuditProviderTest.java +++ b/models-pap/src/test/java/org/onap/policy/models/pap/persistence/provider/PolicyAuditProviderTest.java @@ -23,7 +23,6 @@ package org.onap.policy.models.pap.persistence.provider; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertThrows; import static org.junit.Assert.assertTrue; @@ -107,12 +106,13 @@ public class PolicyAuditProviderTest { Instant date = Instant.now(); provider.createAuditRecords(pfDao, generatePolicyAudits(date, GROUP_A, MY_POLICY)); - List<PolicyAudit> records = provider.getAuditRecords(pfDao, NUMBER_RECORDS); + List<PolicyAudit> records = + provider.getAuditRecords(pfDao, AuditFilter.builder().recordNum(NUMBER_RECORDS).build()); assertThat(records).hasSize(2); // as the start date is 10 min ahead of first record, shouldn't return any records List<PolicyAudit> emptyList = provider.getAuditRecords(pfDao, - AuditFilter.builder().fromDate(Instant.now().plusSeconds(600)).build(), 600); + AuditFilter.builder().fromDate(Instant.now().plusSeconds(600)).recordNum(600).build()); assertThat(emptyList).isEmpty(); } @@ -125,7 +125,8 @@ public class PolicyAuditProviderTest { assertThrows(PfModelRuntimeException.class, () -> provider.createAuditRecords(pfDao, audits)); - List<PolicyAudit> records = provider.getAuditRecords(pfDao, NUMBER_RECORDS); + List<PolicyAudit> records = + provider.getAuditRecords(pfDao, AuditFilter.builder().recordNum(NUMBER_RECORDS).build()); assertThat(records).isEmpty(); } @@ -145,48 +146,16 @@ public class PolicyAuditProviderTest { AuditFilter.builder().fromDate(date).toDate(Instant.now()).recordNum(NUMBER_RECORDS).build()); assertThat(records).hasSize(6); - List<PolicyAudit> recordsWithGroupB = - provider.getAuditRecords(pfDao, - AuditFilter.builder().pdpGroup(GROUP_B).recordNum(NUMBER_RECORDS).build()); + List<PolicyAudit> recordsWithGroupB = provider.getAuditRecords(pfDao, + AuditFilter.builder().pdpGroup(GROUP_B).recordNum(NUMBER_RECORDS).build()); assertThat(recordsWithGroupB).hasSize(4); List<PolicyAudit> recordsWithActionDeploy = provider.getAuditRecords(pfDao, AuditFilter.builder().action(AuditAction.DEPLOYMENT).recordNum(NUMBER_RECORDS).build()); assertThat(recordsWithActionDeploy).hasSize(3); - List<PolicyAudit> recordsWithMyPolicy = provider.getAuditRecords(pfDao, - AuditFilter.builder().name(MY_POLICY.getName()).version(MY_POLICY.getVersion()) - .recordNum(NUMBER_RECORDS).build()); - assertThat(recordsWithMyPolicy).hasSize(4); - } - - @Test - public void testFiltersOld() { - PolicyAuditProvider provider = new PolicyAuditProvider(); - - Instant date = Instant.now().truncatedTo(ChronoUnit.SECONDS); - provider.createAuditRecords(pfDao, generatePolicyAudits(date, GROUP_A, MY_POLICY)); - provider.createAuditRecords(pfDao, generatePolicyAudits(date, GROUP_B, MY_POLICY)); - provider.createAuditRecords(pfDao, generatePolicyAudits(date, GROUP_B, MY_POLICY2)); - Awaitility.await().pollDelay(3, TimeUnit.SECONDS).until(() -> { - return true; - }); - - List<PolicyAudit> records = provider.getAuditRecords(pfDao, - AuditFilter.builder().fromDate(date).toDate(Instant.now()).build(), NUMBER_RECORDS); - assertThat(records).hasSize(6); - - List<PolicyAudit> recordsWithGroupB = - provider.getAuditRecords(pfDao, AuditFilter.builder().pdpGroup(GROUP_B).build(), NUMBER_RECORDS); - assertThat(recordsWithGroupB).hasSize(4); - - List<PolicyAudit> recordsWithActionDeploy = provider.getAuditRecords(pfDao, - AuditFilter.builder().action(AuditAction.DEPLOYMENT).build(), NUMBER_RECORDS); - assertThat(recordsWithActionDeploy).hasSize(3); - - List<PolicyAudit> recordsWithMyPolicy = provider.getAuditRecords(pfDao, - AuditFilter.builder().name(MY_POLICY.getName()).version(MY_POLICY.getVersion()).build(), - NUMBER_RECORDS); + List<PolicyAudit> recordsWithMyPolicy = provider.getAuditRecords(pfDao, AuditFilter.builder() + .name(MY_POLICY.getName()).version(MY_POLICY.getVersion()).recordNum(NUMBER_RECORDS).build()); assertThat(recordsWithMyPolicy).hasSize(4); } @@ -203,7 +172,8 @@ public class PolicyAuditProviderTest { provider.createAuditRecords(pfDao, loadAudits); - List<PolicyAudit> records = provider.getAuditRecords(pfDao, NUMBER_RECORDS); + List<PolicyAudit> records = + provider.getAuditRecords(pfDao, AuditFilter.builder().recordNum(NUMBER_RECORDS).build()); assertThat(records).hasSize(10); // check that is being ordered @@ -211,7 +181,11 @@ public class PolicyAuditProviderTest { assertEquals(loadAudits.get(loadAudits.size() - 1).getTimestamp(), records.get(0).getTimestamp()); // try to get 102 records should return 100 - records = provider.getAuditRecords(pfDao, 102); + records = provider.getAuditRecords(pfDao, AuditFilter.builder().recordNum(102).build()); + assertThat(records).hasSize(100); + + // try to get -1 records should return 100 + records = provider.getAuditRecords(pfDao, AuditFilter.builder().recordNum(-1).build()); assertThat(records).hasSize(100); } @@ -228,45 +202,14 @@ public class PolicyAuditProviderTest { }).hasMessageMatching(String.format(FIELD_IS_NULL, "audits")); assertThatThrownBy(() -> { - provider.getAuditRecords(null, NUMBER_RECORDS); + provider.getAuditRecords(null, AuditFilter.builder().build()); }).hasMessageMatching(String.format(FIELD_IS_NULL, "dao")); assertThatThrownBy(() -> { - provider.getAuditRecords(pfDao, (Integer) null); - }).hasMessageMatching(String.format(FIELD_IS_NULL, "numRecords")); - - assertThatThrownBy(() -> { - provider.getAuditRecords(null, AuditFilter.builder().build(), NUMBER_RECORDS); - }).hasMessageMatching(String.format(FIELD_IS_NULL, "dao")); - - assertThatThrownBy(() -> { - provider.getAuditRecords(pfDao, AuditFilter.builder().build(), null); - }).hasMessageMatching(String.format(FIELD_IS_NULL, "numRecords")); - - assertThatThrownBy(() -> { - provider.getAuditRecords(pfDao, null, NUMBER_RECORDS); + provider.getAuditRecords(pfDao, null); }).hasMessageMatching(String.format(FIELD_IS_NULL, "auditFilter")); } - @Test - public void testAuditFiltersIsEmpty() { - AuditFilter emptyFilter = AuditFilter.builder().build(); - assertTrue(emptyFilter.isEmpty()); - - AuditFilter stringsEmpty = - AuditFilter.builder().action(AuditAction.DEPLOYMENT).fromDate(Instant.MIN).toDate(Instant.MAX).build(); - assertFalse(stringsEmpty.isEmpty()); - - AuditFilter filter = AuditFilter.builder().fromDate(Instant.MIN).toDate(Instant.MAX).build(); - assertFalse(filter.isEmpty()); - filter = AuditFilter.builder().fromDate(Instant.MIN).build(); - assertFalse(filter.isEmpty()); - filter = AuditFilter.builder().toDate(Instant.MAX).build(); - assertFalse(filter.isEmpty()); - filter = AuditFilter.builder().name("myPolicy").toDate(Instant.MAX).build(); - assertFalse(filter.isEmpty()); - } - private List<PolicyAudit> generatePolicyAudits(Instant date, String group, ToscaConceptIdentifier policy) { // @formatter:off PolicyAudit deploy = PolicyAudit.builder() |