diff options
author | Jim Hahn <jrh3@att.com> | 2021-06-10 17:38:58 -0400 |
---|---|---|
committer | Jim Hahn <jrh3@att.com> | 2021-06-14 08:21:04 -0400 |
commit | 8cbad257df2d5f5c585ce37e61cfd16e402be738 (patch) | |
tree | 18027d19e9c6c231e41e4321d5938c8b0a5d5632 /models-pap | |
parent | 1b460194bc034443ba57fa690d294131836a3e9e (diff) |
Fix sonars in policy-models
Fixed:
- too many parameters in method call
Fixed it by introducing PfFilterParameters. However, classes having the
Builder annotation are not easily subclassed, so introduced an
interface, too.
Issue-ID: POLICY-3094
Change-Id: Ida99522a542b1296b367c55b7e8f8e83783c2e4f
Signed-off-by: Jim Hahn <jrh3@att.com>
Diffstat (limited to 'models-pap')
2 files changed, 83 insertions, 19 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 8923fb23f..ca8f18dc6 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 @@ -1,6 +1,7 @@ /*- * ============LICENSE_START======================================================= * Copyright (C) 2021 Nordix Foundation. + * Modifications Copyright (C) 2021 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -33,6 +34,7 @@ import org.apache.commons.lang3.StringUtils; import org.onap.policy.common.parameters.BeanValidationResult; import org.onap.policy.models.base.PfModelRuntimeException; import org.onap.policy.models.dao.PfDao; +import org.onap.policy.models.dao.PfFilterParametersIntfc; import org.onap.policy.models.pap.concepts.PolicyAudit; import org.onap.policy.models.pap.concepts.PolicyAudit.AuditAction; import org.onap.policy.models.pap.persistence.concepts.JpaPolicyAudit; @@ -46,7 +48,6 @@ import org.onap.policy.models.pap.persistence.concepts.JpaPolicyAudit; public class PolicyAuditProvider { private static final Integer DEFAULT_MAX_RECORDS = 100; - private static final String DESCENDING_ORDER = "DESC"; /** * Create audit records. @@ -96,24 +97,22 @@ public class PolicyAuditProvider { */ public List<PolicyAudit> getAuditRecords(@NonNull PfDao dao, @NonNull AuditFilter auditFilter, @NonNull Integer numRecords) { - numRecords = numRecords > DEFAULT_MAX_RECORDS ? DEFAULT_MAX_RECORDS : numRecords; - Map<String, Object> filter = new HashMap<>(); - if (StringUtils.isNotBlank(auditFilter.getPdpGroup())) { - filter.put("pdpGroup", auditFilter.getPdpGroup()); - } + auditFilter.setRecordNum(Math.min(numRecords, DEFAULT_MAX_RECORDS)); - if (auditFilter.getAction() != null) { - filter.put("action", auditFilter.getAction()); - } + return getAuditRecords(dao, auditFilter); + } - // @formatter:off - return dao.getFiltered(JpaPolicyAudit.class, - auditFilter.getName(), auditFilter.getVersion(), - auditFilter.getFromDate(), auditFilter.getToDate(), - filter, DESCENDING_ORDER, 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 + * @return list of {@link PolicyAudit} records + */ + public List<PolicyAudit> getAuditRecords(@NonNull PfDao dao, @NonNull AuditFilter auditFilter) { + + return dao.getFiltered(JpaPolicyAudit.class, auditFilter) + .stream().map(JpaPolicyAudit::toAuthorative).collect(Collectors.toList()); } /** @@ -126,13 +125,19 @@ public class PolicyAuditProvider { */ @Data @Builder - public static class AuditFilter { + public static class AuditFilter implements PfFilterParametersIntfc { private String name; private String version; private AuditAction action; private String pdpGroup; private Instant fromDate; private Instant toDate; + private int recordNum; + @Builder.Default + private String sortOrder = "DESC"; + + // 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. @@ -143,5 +148,34 @@ public class PolicyAuditProvider { return StringUtils.isAllEmpty(name, version, pdpGroup) && action == null && fromDate == null && toDate == null; } + + @Override + public Instant getStartTime() { + return fromDate; + } + + @Override + public Instant getEndTime() { + return toDate; + } + + @Override + public Map<String, Object> getFilterMap() { + if (filterMap != null) { + return filterMap; + } + + filterMap = new HashMap<>(); + + if (StringUtils.isNotBlank(pdpGroup)) { + filterMap.put("pdpGroup", pdpGroup); + } + + if (action != null) { + filterMap.put("action", action); + } + + return filterMap; + } } } 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 228a3fc01..0540d4844 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 @@ -134,7 +134,37 @@ public class PolicyAuditProviderTest { PolicyAuditProvider provider = new PolicyAuditProvider(); Instant date = Instant.now().truncatedTo(ChronoUnit.SECONDS); - System.out.println(date); + 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()).recordNum(NUMBER_RECORDS).build()); + assertThat(records).hasSize(6); + + 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)); @@ -202,7 +232,7 @@ public class PolicyAuditProviderTest { }).hasMessageMatching(String.format(FIELD_IS_NULL, "dao")); assertThatThrownBy(() -> { - provider.getAuditRecords(pfDao, null); + provider.getAuditRecords(pfDao, (Integer) null); }).hasMessageMatching(String.format(FIELD_IS_NULL, "numRecords")); assertThatThrownBy(() -> { |