diff options
10 files changed, 133 insertions, 2 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 c117beefa..8923fb23f 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 @@ -59,7 +59,7 @@ public class PolicyAuditProvider { BeanValidationResult result = new BeanValidationResult("createAuditRecords", jpaAudits); int count = 0; - for (JpaPolicyAudit jpaAudit: jpaAudits) { + for (JpaPolicyAudit jpaAudit : jpaAudits) { result.addResult(jpaAudit.validate(String.valueOf(count++))); } @@ -126,12 +126,22 @@ public class PolicyAuditProvider { */ @Data @Builder - protected static class AuditFilter { + public static class AuditFilter { private String name; private String version; private AuditAction action; private String pdpGroup; private Instant fromDate; private Instant toDate; + + /** + * 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; + } } } 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 da231dc7b..228a3fc01 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,6 +23,7 @@ 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; @@ -217,6 +218,25 @@ public class PolicyAuditProviderTest { }).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() diff --git a/models-provider/pom.xml b/models-provider/pom.xml index 76e21c578..31157ca50 100644 --- a/models-provider/pom.xml +++ b/models-provider/pom.xml @@ -63,6 +63,12 @@ </dependency> <dependency> + <groupId>org.onap.policy.models</groupId> + <artifactId>policy-models-pap</artifactId> + <version>${project.version}</version> + </dependency> + + <dependency> <groupId>com.h2database</groupId> <artifactId>h2</artifactId> <scope>test</scope> diff --git a/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProvider.java b/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProvider.java index 3af069e24..5321e8597 100644 --- a/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProvider.java +++ b/models-provider/src/main/java/org/onap/policy/models/provider/PolicyModelsProvider.java @@ -27,6 +27,8 @@ import java.util.Collection; import java.util.List; import lombok.NonNull; import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.pap.concepts.PolicyAudit; +import org.onap.policy.models.pap.persistence.provider.PolicyAuditProvider.AuditFilter; import org.onap.policy.models.pdp.concepts.Pdp; import org.onap.policy.models.pdp.concepts.PdpGroup; import org.onap.policy.models.pdp.concepts.PdpGroupFilter; @@ -408,4 +410,19 @@ public interface PolicyModelsProvider extends AutoCloseable { */ public void cudPolicyStatus(Collection<PdpPolicyStatus> createObjs, Collection<PdpPolicyStatus> updateObjs, Collection<PdpPolicyStatus> deleteObjs); + + /** + * Creates records for audit actions on policies. + * + * @param auditRecords the objects to create + */ + public void createAuditRecords(@NonNull List<PolicyAudit> auditRecords); + + /** + * Collect the audit records. + * @param auditFilter filter for search + * @param numRecords max number of records to be collected + * @return list of {@link PolicyAudit} or empty if none or not match with filter + */ + public List<PolicyAudit> getAuditRecords(AuditFilter auditFilter, @NonNull Integer numRecords); } diff --git a/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java b/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java index a90dc895c..3bba1518a 100644 --- a/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java +++ b/models-provider/src/main/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderImpl.java @@ -29,6 +29,9 @@ import javax.ws.rs.core.Response; import lombok.NonNull; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfModelRuntimeException; +import org.onap.policy.models.pap.concepts.PolicyAudit; +import org.onap.policy.models.pap.persistence.provider.PolicyAuditProvider; +import org.onap.policy.models.pap.persistence.provider.PolicyAuditProvider.AuditFilter; import org.onap.policy.models.pdp.concepts.Pdp; import org.onap.policy.models.pdp.concepts.PdpGroup; import org.onap.policy.models.pdp.concepts.PdpGroupFilter; @@ -309,6 +312,22 @@ public class DatabasePolicyModelsProviderImpl extends AbstractModelsProvider imp new PdpProvider().cudPolicyStatus(getPfDao(), createObjs, updateObjs, deleteObjs); } + @Override + public void createAuditRecords(List<PolicyAudit> auditRecords) { + assertInitialized(); + new PolicyAuditProvider().createAuditRecords(getPfDao(), auditRecords); + } + + @Override + public List<PolicyAudit> getAuditRecords(AuditFilter auditFilter, @NonNull Integer numRecords) { + assertInitialized(); + if (auditFilter == null || auditFilter.isEmpty()) { + return new PolicyAuditProvider().getAuditRecords(getPfDao(), numRecords); + } else { + return new PolicyAuditProvider().getAuditRecords(getPfDao(), auditFilter, numRecords); + } + } + /** * Check if the model provider is initialized. */ diff --git a/models-provider/src/main/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderImpl.java b/models-provider/src/main/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderImpl.java index f47f4d464..dbeccfaeb 100644 --- a/models-provider/src/main/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderImpl.java +++ b/models-provider/src/main/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderImpl.java @@ -32,6 +32,8 @@ import org.onap.policy.common.utils.coder.StandardCoder; import org.onap.policy.common.utils.resources.ResourceUtils; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfModelRuntimeException; +import org.onap.policy.models.pap.concepts.PolicyAudit; +import org.onap.policy.models.pap.persistence.provider.PolicyAuditProvider.AuditFilter; import org.onap.policy.models.pdp.concepts.Pdp; import org.onap.policy.models.pdp.concepts.PdpGroup; import org.onap.policy.models.pdp.concepts.PdpGroupFilter; @@ -267,6 +269,17 @@ public class DummyPolicyModelsProviderImpl implements PolicyModelsProvider { // Not implemented } + @Override + public void createAuditRecords(List<PolicyAudit> auditRecords) { + // Not implemented + } + + @Override + public List<PolicyAudit> getAuditRecords(AuditFilter auditFilter, @NonNull Integer numRecords) { + // Not implemented + return new ArrayList<>(); + } + /** * Return a ToscaServicetemplate dummy response. * diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java index 9e8ba0bbc..4356e4b1d 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DatabasePolicyModelsProviderTest.java @@ -36,6 +36,9 @@ import org.junit.After; import org.junit.Before; import org.junit.Test; import org.onap.policy.models.base.PfModelException; +import org.onap.policy.models.pap.concepts.PolicyAudit; +import org.onap.policy.models.pap.concepts.PolicyAudit.AuditAction; +import org.onap.policy.models.pap.persistence.provider.PolicyAuditProvider.AuditFilter; import org.onap.policy.models.pdp.concepts.Pdp; import org.onap.policy.models.pdp.concepts.PdpGroup; import org.onap.policy.models.pdp.concepts.PdpGroupFilter; @@ -538,6 +541,33 @@ public class DatabasePolicyModelsProviderTest { databaseProvider.close(); } + @Test + public void testCreateAuditRecords() throws PfModelException { + PolicyAudit audit = PolicyAudit.builder().action(AuditAction.DEPLOYMENT).pdpGroup(GROUP).pdpType(GROUP) + .policy(new ToscaConceptIdentifier(NAME, VERSION_100)).user("user").build(); + + databaseProvider = new PolicyModelsProviderFactory().createPolicyModelsProvider(parameters); + + databaseProvider.createAuditRecords(List.of(audit)); + List<PolicyAudit> createdAudits = databaseProvider.getAuditRecords(null, 10); + assertThat(createdAudits).hasSize(1); + + createdAudits = databaseProvider.getAuditRecords(AuditFilter.builder().build(), 10); + assertThat(createdAudits).hasSize(1); + + List<PolicyAudit> emptyList = + databaseProvider.getAuditRecords(AuditFilter.builder().action(AuditAction.UNDEPLOYMENT).build(), 10); + assertThat(emptyList).isEmpty(); + + assertThatThrownBy(() -> databaseProvider.createAuditRecords(null)) + .hasMessageContaining("audits is marked non-null but is null"); + + assertThatThrownBy(() -> databaseProvider.getAuditRecords(null, null)) + .hasMessageContaining("numRecords is marked non-null but is null"); + + databaseProvider.close(); + } + private List<PdpStatistics> makePdpStatisticsList() { PdpStatistics pdpStatistics = new PdpStatistics(); pdpStatistics.setPdpInstanceId(NAME); diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyBadProviderImpl.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyBadProviderImpl.java index 4daeaba4c..34cdbff0f 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyBadProviderImpl.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyBadProviderImpl.java @@ -31,6 +31,8 @@ import javax.ws.rs.core.Response; import lombok.NonNull; import org.onap.policy.models.base.PfModelException; import org.onap.policy.models.base.PfModelRuntimeException; +import org.onap.policy.models.pap.concepts.PolicyAudit; +import org.onap.policy.models.pap.persistence.provider.PolicyAuditProvider.AuditFilter; import org.onap.policy.models.pdp.concepts.Pdp; import org.onap.policy.models.pdp.concepts.PdpGroup; import org.onap.policy.models.pdp.concepts.PdpGroupFilter; @@ -264,4 +266,15 @@ public class DummyBadProviderImpl implements PolicyModelsProvider { // Not implemented return null; } + + @Override + public void createAuditRecords(List<PolicyAudit> auditRecords) { + // Not implemented + } + + @Override + public List<PolicyAudit> getAuditRecords(AuditFilter auditFilter, @NonNull Integer numRecords) { + // Not implemented + return null; + } } diff --git a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderTest.java b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderTest.java index 67a1449f6..de320724a 100644 --- a/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderTest.java +++ b/models-provider/src/test/java/org/onap/policy/models/provider/impl/DummyPolicyModelsProviderTest.java @@ -126,6 +126,8 @@ public class DummyPolicyModelsProviderTest { assertThat(dummyProvider.getGroupPolicyStatus("name")).isEmpty(); assertThatCode(() -> dummyProvider.cudPolicyStatus(null, null, null)).doesNotThrowAnyException(); + assertThatCode(() -> dummyProvider.createAuditRecords(null)).doesNotThrowAnyException(); + assertThat(dummyProvider.getAuditRecords(null, 10)).isEmpty(); } @Test diff --git a/models-provider/src/test/resources/META-INF/persistence.xml b/models-provider/src/test/resources/META-INF/persistence.xml index 249b29aae..3b4d95656 100644 --- a/models-provider/src/test/resources/META-INF/persistence.xml +++ b/models-provider/src/test/resources/META-INF/persistence.xml @@ -27,6 +27,7 @@ <class>org.onap.policy.models.base.PfConceptKey</class> <class>org.onap.policy.models.dao.converters.CDataConditioner</class> <class>org.onap.policy.models.dao.converters.Uuid2String</class> + <class>org.onap.policy.models.pap.persistence.concepts.JpaPolicyAudit</class> <class>org.onap.policy.models.pdp.persistence.concepts.JpaPdp</class> <class>org.onap.policy.models.pdp.persistence.concepts.JpaPdpPolicyStatus</class> <class>org.onap.policy.models.pdp.persistence.concepts.JpaPdpGroup</class> |