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: [DHIS2-18599] Respect dryRun in old TEI Import (2.39) #19545

Merged
merged 1 commit into from
Dec 20, 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
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
Loading