Skip to content

Commit

Permalink
fix: [DHIS2-18599] Respect dryRun in old TEI Import (#19545)
Browse files Browse the repository at this point in the history
  • Loading branch information
ameenhere authored Dec 20, 2024
1 parent 504c53a commit 4a2bdda
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,9 @@ private ImportSummary addEnrollment(
programInstance.setCreatedByUserInfo(UserInfoSnapshot.from(importOptions.getUser()));
programInstance.setLastUpdatedByUserInfo(UserInfoSnapshot.from(importOptions.getUser()));

programInstanceService.addProgramInstance(programInstance, importOptions.getUser());
if (!importOptions.isDryRun()) {
programInstanceService.addProgramInstance(programInstance, importOptions.getUser());
}

importSummary = validateProgramInstance(program, programInstance, enrollment, importSummary);

Expand All @@ -618,7 +620,9 @@ private ImportSummary addEnrollment(
programInstance.setFollowup(enrollment.getFollowup());
programInstance.setStoredBy(storedBy);

programInstanceService.updateProgramInstance(programInstance, importOptions.getUser());
if (!importOptions.isDryRun()) {
programInstanceService.updateProgramInstance(programInstance, importOptions.getUser());
}
trackerOwnershipAccessManager.assignOwnership(
daoTrackedEntityInstance, program, organisationUnit, true, true);
saveTrackedEntityComment(programInstance, enrollment, importOptions.getUser());
Expand Down Expand Up @@ -939,7 +943,9 @@ private ImportSummary updateEnrollment(

updateFeatureType(program, enrollment, programInstance);

if (EnrollmentStatus.fromProgramStatus(programInstance.getStatus()) != enrollment.getStatus()) {
if (!importOptions.isDryRun()
&& EnrollmentStatus.fromProgramStatus(programInstance.getStatus())
!= enrollment.getStatus()) {
Date endDate = enrollment.getCompletedDate();

String user = enrollment.getCompletedBy();
Expand Down Expand Up @@ -977,11 +983,12 @@ private ImportSummary updateEnrollment(

programInstance.setLastUpdatedByUserInfo(UserInfoSnapshot.from(importOptions.getUser()));

programInstanceService.updateProgramInstance(programInstance, importOptions.getUser());
teiService.updateTrackedEntityInstance(
programInstance.getEntityInstance(), importOptions.getUser());

saveTrackedEntityComment(programInstance, enrollment, importOptions.getUser());
if (!importOptions.isDryRun()) {
programInstanceService.updateProgramInstance(programInstance, importOptions.getUser());
teiService.updateTrackedEntityInstance(
programInstance.getEntityInstance(), importOptions.getUser());
saveTrackedEntityComment(programInstance, enrollment, importOptions.getUser());
}

importSummary = new ImportSummary(enrollment.getEnrollment()).incrementUpdated();
importSummary.setReference(enrollment.getEnrollment());
Expand Down Expand Up @@ -1061,13 +1068,15 @@ private ImportSummary deleteEnrollment(
}

programInstance.setLastUpdatedByUserInfo(UserInfoSnapshot.from(importOptions.getUser()));
programInstanceService.deleteProgramInstance(programInstance);

if (!importOptions.isDryRun()) {
programInstanceService.deleteProgramInstance(programInstance);
}
TrackedEntityInstance entity = programInstance.getEntityInstance();
entity.setLastUpdatedByUserInfo(UserInfoSnapshot.from(importOptions.getUser()));

teiService.updateTrackedEntityInstance(entity);

if (!importOptions.isDryRun()) {
teiService.updateTrackedEntityInstance(entity);
}
importSummary.setReference(uid);
importSummary.setStatus(ImportStatus.SUCCESS);
importSummary.setDescription("Deletion of enrollment " + uid + " was successful");
Expand Down Expand Up @@ -1423,9 +1432,10 @@ private void updateAttributeValues(Enrollment enrollment, ImportOptions importOp
value.setValue(newValue);
value.setStoredBy(getStoredBy(enrollmentAttribute, importOptions.getUser()));

trackedEntityAttributeValueService.updateTrackedEntityAttributeValue(
value, importOptions.getUser());

if (!importOptions.isDryRun()) {
trackedEntityAttributeValueService.updateTrackedEntityAttributeValue(
value, importOptions.getUser());
}
attributeValueMap.remove(value.getAttribute().getUid());
});

Expand All @@ -1440,9 +1450,10 @@ private void updateAttributeValues(Enrollment enrollment, ImportOptions importOp
value.setValue(enrollmentAttribute.getValue());
value.setAttribute(attribute);
value.setStoredBy(getStoredBy(enrollmentAttribute, importOptions.getUser()));

trackedEntityAttributeValueService.addTrackedEntityAttributeValue(value);
trackedEntityInstance.addAttributeValue(value);
if (!importOptions.isDryRun()) {
trackedEntityAttributeValueService.addTrackedEntityAttributeValue(value);
trackedEntityInstance.addAttributeValue(value);
}
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -695,9 +695,10 @@ private ImportSummary addTrackedEntityInstance(
return new ImportSummary(ImportStatus.ERROR, errors.toString()).incrementIgnored();
}

teiService.addTrackedEntityInstance(daoEntityInstance);

addAttributeValues(dtoEntityInstance, daoEntityInstance, importOptions.getUser());
if (!importOptions.isDryRun()) {
teiService.addTrackedEntityInstance(daoEntityInstance);
addAttributeValues(dtoEntityInstance, daoEntityInstance, importOptions.getUser());
}

importSummary.setReference(daoEntityInstance.getUid());
importSummary.getImportCount().incrementImported();
Expand Down Expand Up @@ -843,13 +844,17 @@ private ImportSummary updateTrackedEntityInstance(
daoEntityInstance.setGeometry(null);
}

if (!importOptions.isIgnoreEmptyCollection() || !dtoEntityInstance.getAttributes().isEmpty()) {
if (!importOptions.isDryRun()
&& (!importOptions.isIgnoreEmptyCollection()
|| !dtoEntityInstance.getAttributes().isEmpty())) {
updateAttributeValues(dtoEntityInstance, daoEntityInstance, program, importOptions.getUser());
}

updateDateFields(dtoEntityInstance, daoEntityInstance);

teiService.updateTrackedEntityInstance(daoEntityInstance);
if (!importOptions.isDryRun()) {
teiService.updateTrackedEntityInstance(daoEntityInstance);
}

importSummary.setReference(daoEntityInstance.getUid());
importSummary.getImportCount().incrementUpdated();
Expand Down Expand Up @@ -920,8 +925,9 @@ private ImportSummary deleteTrackedEntityInstance(
}

daoEntityInstance.setLastUpdatedByUserInfo(UserInfoSnapshot.from(importOptions.getUser()));
teiService.deleteTrackedEntityInstance(daoEntityInstance);

if (!importOptions.isDryRun()) {
teiService.deleteTrackedEntityInstance(daoEntityInstance);
}
importSummary.setStatus(ImportStatus.SUCCESS);
importSummary.setDescription(
"Deletion of tracked entity instance " + uid + " was successful");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -226,6 +226,63 @@ void shouldAddTrackedEntityWithCreateStrategy() {
verify(defaultTrackedEntityInstanceService, times(1)).addTrackedEntityInstance(any());
}

@Test
void shouldNotAddTrackedEntityWhenDryRun() {
List<TrackedEntityInstance> trackedEntityInstanceList =
Collections.singletonList(trackedEntityInstance);

when(importOptions.getImportStrategy()).thenReturn(ImportStrategy.CREATE);
when(importOptions.isDryRun()).thenReturn(true);

ImportSummaries importSummaries =
trackedEntityInstanceService.mergeOrDeleteTrackedEntityInstances(
trackedEntityInstanceList, importOptions, jobConfiguration);

assertFalse(
importSummaries.getImportSummaries().stream()
.anyMatch(is -> is.isStatus(ImportStatus.ERROR)));

verify(defaultTrackedEntityInstanceService, times(0)).addTrackedEntityInstance(any());
}

@Test
void shouldNotUpdateTrackedEntityWhenDryRun() {
List<TrackedEntityInstance> trackedEntityInstanceList =
Collections.singletonList(trackedEntityInstance);

when(importOptions.getImportStrategy()).thenReturn(ImportStrategy.UPDATE);
when(importOptions.isDryRun()).thenReturn(true);

ImportSummaries importSummaries =
trackedEntityInstanceService.mergeOrDeleteTrackedEntityInstances(
trackedEntityInstanceList, importOptions, jobConfiguration);

assertFalse(
importSummaries.getImportSummaries().stream()
.anyMatch(is -> is.isStatus(ImportStatus.ERROR)));

verify(defaultTrackedEntityInstanceService, times(0)).updateTrackedEntityInstance(any());
}

@Test
void shouldNotDeleteTrackedEntityWhenDryRun() {
List<TrackedEntityInstance> trackedEntityInstanceList =
Collections.singletonList(trackedEntityInstance);

when(importOptions.getImportStrategy()).thenReturn(ImportStrategy.DELETE);
when(importOptions.isDryRun()).thenReturn(true);

ImportSummaries importSummaries =
trackedEntityInstanceService.mergeOrDeleteTrackedEntityInstances(
trackedEntityInstanceList, importOptions, jobConfiguration);

assertFalse(
importSummaries.getImportSummaries().stream()
.anyMatch(is -> is.isStatus(ImportStatus.ERROR)));

verify(defaultTrackedEntityInstanceService, times(0)).deleteTrackedEntityInstance(any());
}

@Test
void shouldUpdateTrackedEntityWithUpdateStrategy() {
List<TrackedEntityInstance> trackedEntityInstanceList =
Expand Down

0 comments on commit 4a2bdda

Please sign in to comment.