Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: AuditType failing enum conversion when get tracked entity audits [DHIS2-15805] [ 2.40 ] #16102

Merged
merged 2 commits into from
Jan 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 5 additions & 15 deletions dhis-2/dhis-api/src/main/java/org/hisp/dhis/common/AuditType.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
Original file line number Diff line number Diff line change
@@ -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';
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<TrackedEntityInstanceAudit> trackedEntityAuditInput = List.of(auditA, auditB);

store.addTrackedEntityInstanceAudit(trackedEntityAuditInput);

TrackedEntityInstanceAuditQueryParams params =
new TrackedEntityInstanceAuditQueryParams()
.setTrackedEntityInstances(List.of("WGW7UnVcIIb"));

List<TrackedEntityInstanceAudit> 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<TrackedEntityInstanceAudit> 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));
}
}