diff --git a/dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/AbstractEnrollmentService.java b/dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/AbstractEnrollmentService.java index 35c8db9bdae9..06d7c269f406 100644 --- a/dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/AbstractEnrollmentService.java +++ b/dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/enrollment/AbstractEnrollmentService.java @@ -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); @@ -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()); @@ -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(); @@ -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()); @@ -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"); @@ -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()); }); @@ -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); + } } } } diff --git a/dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/AbstractTrackedEntityInstanceService.java b/dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/AbstractTrackedEntityInstanceService.java index da0d3ee06e16..599cbc1fb223 100644 --- a/dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/AbstractTrackedEntityInstanceService.java +++ b/dhis-2/dhis-services/dhis-service-dxf2/src/main/java/org/hisp/dhis/dxf2/events/trackedentity/AbstractTrackedEntityInstanceService.java @@ -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(); @@ -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(); @@ -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"); diff --git a/dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/TrackerCrudTest.java b/dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/TrackerCrudTest.java index 92d2bbb36438..f682b80cedfd 100644 --- a/dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/TrackerCrudTest.java +++ b/dhis-2/dhis-services/dhis-service-dxf2/src/test/java/org/hisp/dhis/dxf2/TrackerCrudTest.java @@ -226,6 +226,63 @@ void shouldAddTrackedEntityWithCreateStrategy() { verify(defaultTrackedEntityInstanceService, times(1)).addTrackedEntityInstance(any()); } + @Test + void shouldNotAddTrackedEntityWhenDryRun() { + List 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 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 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 trackedEntityInstanceList =