diff --git a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/AuditType.java b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/AuditType.java index adc58c4dee56..4d4d3722cbfb 100644 --- a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/AuditType.java +++ b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/AuditType.java @@ -33,19 +33,9 @@ * @author Halvdan Hoem Grelland */ public enum AuditType { - CREATE("create"), - UPDATE("update"), - DELETE("delete"), - READ("read"), - SEARCH("search"); - - private final String value; - - AuditType(String value) { - this.value = value; - } - - public String getValue() { - return value; - } + CREATE, + UPDATE, + DELETE, + READ, + SEARCH } diff --git a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceAuditStore.java b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceAuditStore.java index c2a0171f916c..d34651cc5266 100644 --- a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceAuditStore.java +++ b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/trackedentity/hibernate/HibernateTrackedEntityInstanceAuditStore.java @@ -97,7 +97,7 @@ public void addTrackedEntityInstanceAudit( sb.append(singleQuote(audit.getTrackedEntityInstance())).append(","); sb.append("now()").append(","); sb.append(singleQuote(audit.getAccessedBy())).append(","); - sb.append(singleQuote(audit.getAuditType().getValue())).append(","); + sb.append(singleQuote(audit.getAuditType().name())).append(","); sb.append( StringUtils.isNotEmpty(audit.getComment()) ? statementBuilder.encode(audit.getComment()) diff --git a/dhis-2/dhis-support/dhis-support-db-migration/src/main/resources/org/hisp/dhis/db/migration/2.40/V2_40_28__Fix_Audit_Type_Case_Sensitive.sql.sql b/dhis-2/dhis-support/dhis-support-db-migration/src/main/resources/org/hisp/dhis/db/migration/2.40/V2_40_28__Fix_Audit_Type_Case_Sensitive.sql.sql new file mode 100644 index 000000000000..880d071e845a --- /dev/null +++ b/dhis-2/dhis-support/dhis-support-db-migration/src/main/resources/org/hisp/dhis/db/migration/2.40/V2_40_28__Fix_Audit_Type_Case_Sensitive.sql.sql @@ -0,0 +1,25 @@ +drop table if exists metadataaudit; + +update trackedentityinstanceaudit set audittype = 'CREATE' where audittype = 'create'; +update trackedentityinstanceaudit set audittype = 'UPDATE' where audittype = 'update'; +update trackedentityinstanceaudit set audittype = 'DELETE' where audittype = 'delete'; +update trackedentityinstanceaudit set audittype = 'READ' where audittype = 'read'; +update trackedentityinstanceaudit set audittype = 'SEARCH' where audittype = 'search'; + +update datavalueaudit set audittype = 'CREATE' where audittype = 'create'; +update datavalueaudit set audittype = 'UPDATE' where audittype = 'update'; +update datavalueaudit set audittype = 'DELETE' where audittype = 'delete'; +update datavalueaudit set audittype = 'READ' where audittype = 'read'; +update datavalueaudit set audittype = 'SEARCH' where audittype = 'search'; + +update trackedentityattributevalueaudit set audittype = 'CREATE' where audittype = 'create'; +update trackedentityattributevalueaudit set audittype = 'UPDATE' where audittype = 'update'; +update trackedentityattributevalueaudit set audittype = 'DELETE' where audittype = 'delete'; +update trackedentityattributevalueaudit set audittype = 'READ' where audittype = 'read'; +update trackedentityattributevalueaudit set audittype = 'SEARCH' where audittype = 'search'; + +update trackedentitydatavalueaudit set audittype = 'CREATE' where audittype = 'create'; +update trackedentitydatavalueaudit set audittype = 'UPDATE' where audittype = 'update'; +update trackedentitydatavalueaudit set audittype = 'DELETE' where audittype = 'delete'; +update trackedentitydatavalueaudit set audittype = 'READ' where audittype = 'read'; +update trackedentitydatavalueaudit set audittype = 'SEARCH' where audittype = 'search'; \ No newline at end of file diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceAuditStoreTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceAuditStoreTest.java index 53ada4f80792..8b4223f0dbea 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceAuditStoreTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/trackedentity/TrackedEntityInstanceAuditStoreTest.java @@ -28,6 +28,8 @@ package org.hisp.dhis.trackedentity; import static org.hisp.dhis.utils.Assertions.assertContainsOnly; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNotNull; import java.util.Date; import java.util.List; @@ -45,47 +47,83 @@ class TrackedEntityInstanceAuditStoreTest extends SingleSetupIntegrationTestBase @Autowired private TrackedEntityInstanceAuditStore store; + private final TrackedEntityInstanceAudit auditA = + new TrackedEntityInstanceAudit("WGW7UnVcIIb", "Access", CREATED, "userA", AuditType.CREATE); + private final TrackedEntityInstanceAudit auditB = + new TrackedEntityInstanceAudit("WGW7UnVcIIb", "Access", CREATED, "userB", AuditType.UPDATE); + private final TrackedEntityInstanceAudit auditC = + new TrackedEntityInstanceAudit("zIAwTY3Drrn", "Access", CREATED, "userA", AuditType.UPDATE); + private final TrackedEntityInstanceAudit auditD = + new TrackedEntityInstanceAudit("zIAwTY3Drrn", "Access", CREATED, "userB", AuditType.DELETE); + + @Test + void shouldAuditTrackedEntity_whenAddAuditList() { + List trackedEntityAuditInput = List.of(auditA, auditB); + + store.addTrackedEntityInstanceAudit(trackedEntityAuditInput); + + TrackedEntityInstanceAuditQueryParams params = + new TrackedEntityInstanceAuditQueryParams() + .setTrackedEntityInstances(List.of("WGW7UnVcIIb")); + + List trackedEntityAudits = + store.getTrackedEntityInstanceAudits(params); + + assertEquals(trackedEntityAuditInput.size(), trackedEntityAudits.size()); + TrackedEntityInstanceAudit entityAudit = + filterByAuditType(trackedEntityAudits, AuditType.CREATE); + + assertNotNull(entityAudit); + assertEquals("userA", entityAudit.getAccessedBy()); + assertEquals("WGW7UnVcIIb", entityAudit.getTrackedEntityInstance()); + + entityAudit = filterByAuditType(trackedEntityAudits, AuditType.UPDATE); + + assertNotNull(entityAudit); + assertEquals("userB", entityAudit.getAccessedBy()); + assertEquals("WGW7UnVcIIb", entityAudit.getTrackedEntityInstance()); + } + + private static TrackedEntityInstanceAudit filterByAuditType( + List trackedEntityAuditsStore, AuditType auditType) { + return trackedEntityAuditsStore.stream() + .filter(a -> a.getAuditType() == auditType) + .findFirst() + .orElse(null); + } + @Test void testGetAuditsByParams() { - TrackedEntityInstanceAudit teiaA = - new TrackedEntityInstanceAudit("WGW7UnVcIIb", "Access", CREATED, "userA", AuditType.CREATE); - TrackedEntityInstanceAudit teiaB = - new TrackedEntityInstanceAudit("WGW7UnVcIIb", "Access", CREATED, "userB", AuditType.UPDATE); - TrackedEntityInstanceAudit teiaC = - new TrackedEntityInstanceAudit("zIAwTY3Drrn", "Access", CREATED, "userA", AuditType.UPDATE); - TrackedEntityInstanceAudit teiaD = - new TrackedEntityInstanceAudit("zIAwTY3Drrn", "Access", CREATED, "userB", AuditType.DELETE); - - store.addTrackedEntityInstanceAudit(teiaA); - store.addTrackedEntityInstanceAudit(teiaB); - store.addTrackedEntityInstanceAudit(teiaC); - store.addTrackedEntityInstanceAudit(teiaD); + store.addTrackedEntityInstanceAudit(auditA); + store.addTrackedEntityInstanceAudit(auditB); + store.addTrackedEntityInstanceAudit(auditC); + store.addTrackedEntityInstanceAudit(auditD); TrackedEntityInstanceAuditQueryParams params = new TrackedEntityInstanceAuditQueryParams() .setTrackedEntityInstances(List.of("WGW7UnVcIIb")); - assertContainsOnly(List.of(teiaA, teiaB), store.getTrackedEntityInstanceAudits(params)); + assertContainsOnly(List.of(auditA, auditB), store.getTrackedEntityInstanceAudits(params)); params = new TrackedEntityInstanceAuditQueryParams().setUsers(List.of("userA")); - assertContainsOnly(List.of(teiaA, teiaC), store.getTrackedEntityInstanceAudits(params)); + assertContainsOnly(List.of(auditA, auditC), store.getTrackedEntityInstanceAudits(params)); params = new TrackedEntityInstanceAuditQueryParams().setAuditTypes(List.of(AuditType.UPDATE)); - assertContainsOnly(List.of(teiaB, teiaC), store.getTrackedEntityInstanceAudits(params)); + assertContainsOnly(List.of(auditB, auditC), store.getTrackedEntityInstanceAudits(params)); params = new TrackedEntityInstanceAuditQueryParams() .setAuditTypes(List.of(AuditType.CREATE, AuditType.DELETE)); - assertContainsOnly(List.of(teiaA, teiaD), store.getTrackedEntityInstanceAudits(params)); + assertContainsOnly(List.of(auditA, auditD), store.getTrackedEntityInstanceAudits(params)); params = new TrackedEntityInstanceAuditQueryParams() .setTrackedEntityInstances(List.of("WGW7UnVcIIb")) .setUsers(List.of("userA")); - assertContainsOnly(List.of(teiaA), store.getTrackedEntityInstanceAudits(params)); + assertContainsOnly(List.of(auditA), store.getTrackedEntityInstanceAudits(params)); } }