diff --git a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/scheduling/JobType.java b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/scheduling/JobType.java index 7b51389db2d9..7357c130c46c 100644 --- a/dhis-2/dhis-api/src/main/java/org/hisp/dhis/scheduling/JobType.java +++ b/dhis-2/dhis-api/src/main/java/org/hisp/dhis/scheduling/JobType.java @@ -206,7 +206,6 @@ public boolean isUsingNotifications() { || this == DATAVALUE_IMPORT || this == COMPLETE_DATA_SET_REGISTRATION_IMPORT || this == METADATA_IMPORT - || this == TRACKER_IMPORT_JOB || this == GEOJSON_IMPORT; } @@ -232,7 +231,7 @@ public boolean isUsingErrorNotification() { * the ready jobs per type is attempted to start in a single loop cycle */ public boolean isUsingContinuousExecution() { - return this == METADATA_IMPORT || this == TRACKER_IMPORT_JOB; + return this == METADATA_IMPORT; } public boolean hasJobParameters() { diff --git a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/scheduling/HibernateJobConfigurationStore.java b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/scheduling/HibernateJobConfigurationStore.java index 44653469e464..be63df14bf74 100644 --- a/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/scheduling/HibernateJobConfigurationStore.java +++ b/dhis-2/dhis-services/dhis-service-core/src/main/java/org/hisp/dhis/scheduling/HibernateJobConfigurationStore.java @@ -29,7 +29,6 @@ import static java.lang.Math.max; import static java.util.stream.Collectors.toSet; -import static org.springframework.transaction.annotation.Propagation.REQUIRES_NEW; import java.util.List; import java.util.Set; @@ -46,7 +45,6 @@ import org.springframework.context.ApplicationEventPublisher; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.stereotype.Repository; -import org.springframework.transaction.annotation.Transactional; /** * @author Jan Bernitt @@ -244,7 +242,6 @@ public Stream getDueJobConfigurations(boolean includeWaiting) } @Override - @Transactional(propagation = REQUIRES_NEW) public boolean tryExecuteNow(@Nonnull String jobId) { // language=SQL String sql = diff --git a/dhis-2/dhis-services/dhis-service-tracker/pom.xml b/dhis-2/dhis-services/dhis-service-tracker/pom.xml index 542a8b642f0d..393e2d329c1a 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/pom.xml +++ b/dhis-2/dhis-services/dhis-service-tracker/pom.xml @@ -65,6 +65,10 @@ org.hisp.dhis.rules rule-engine + + org.springframework + spring-core + org.springframework spring-jdbc @@ -85,6 +89,10 @@ org.springframework spring-beans + + org.springframework.security + spring-security-core + org.projectlombok lombok diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/DefaultTrackerImportService.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/DefaultTrackerImportService.java index b203d58bbc68..ccf754014a01 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/DefaultTrackerImportService.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/DefaultTrackerImportService.java @@ -27,25 +27,35 @@ */ package org.hisp.dhis.tracker.imports; +import static org.hisp.dhis.tracker.imports.report.TimingsStats.COMMIT_OPS; +import static org.hisp.dhis.tracker.imports.report.TimingsStats.PREHEAT_OPS; +import static org.hisp.dhis.tracker.imports.report.TimingsStats.PREPROCESS_OPS; +import static org.hisp.dhis.tracker.imports.report.TimingsStats.PROGRAMRULE_OPS; +import static org.hisp.dhis.tracker.imports.report.TimingsStats.TOTAL_OPS; +import static org.hisp.dhis.tracker.imports.report.TimingsStats.VALIDATE_PROGRAMRULE_OPS; +import static org.hisp.dhis.tracker.imports.report.TimingsStats.VALIDATION_OPS; + import java.util.Collection; import java.util.Collections; import java.util.List; import java.util.Map; import java.util.Optional; +import java.util.stream.Collectors; import java.util.stream.Stream; import javax.annotation.Nonnull; import lombok.RequiredArgsConstructor; import lombok.extern.slf4j.Slf4j; -import org.hisp.dhis.scheduling.JobProgress; +import org.hisp.dhis.system.notification.NotificationLevel; +import org.hisp.dhis.system.notification.Notifier; import org.hisp.dhis.tracker.TrackerType; import org.hisp.dhis.tracker.imports.bundle.TrackerBundle; import org.hisp.dhis.tracker.imports.bundle.TrackerBundleService; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.job.TrackerSideEffectDataBundle; import org.hisp.dhis.tracker.imports.preprocess.TrackerPreprocessService; import org.hisp.dhis.tracker.imports.report.ImportReport; import org.hisp.dhis.tracker.imports.report.PersistenceReport; import org.hisp.dhis.tracker.imports.report.Status; +import org.hisp.dhis.tracker.imports.report.TimingsStats; import org.hisp.dhis.tracker.imports.report.TrackerTypeReport; import org.hisp.dhis.tracker.imports.report.ValidationReport; import org.hisp.dhis.tracker.imports.validation.ValidationResult; @@ -68,78 +78,144 @@ public class DefaultTrackerImportService implements TrackerImportService { @Nonnull private final TrackerUserService trackerUserService; - /* Import is not meant to be annotated with @Transactional. - * PreHeat and Commit phases are separated transactions, other - * phases do not need to be in a transaction. */ + @Nonnull private final Notifier notifier; + @Override - public ImportReport importTracker( - TrackerImportParams params, TrackerObjects trackerObjects, JobProgress jobProgress) { + public ImportReport importTracker(TrackerImportParams params) { User user = trackerUserService.getUser(params.getUserId()); + params.setUser(user); - jobProgress.startingStage("Running PreHeat"); - TrackerBundle trackerBundle = - jobProgress.runStage(() -> trackerBundleService.create(params, trackerObjects, user)); + TimingsStats opsTimer = new TimingsStats(); - jobProgress.startingStage("Calculating Payload Size"); - Map bundleSize = - jobProgress.runStage(() -> calculatePayloadSize(trackerBundle)); + startImport(params); - jobProgress.startingStage("Running PreProcess"); - jobProgress.runStage(() -> trackerPreprocessService.preprocess(trackerBundle)); + try { + TrackerBundle trackerBundle = preHeat(params, opsTimer); - jobProgress.startingStage("Running Validation"); - ValidationResult validationResult = jobProgress.runStage(() -> validateBundle(trackerBundle)); + Map bundleSize = calculatePayloadSize(trackerBundle); - ValidationReport validationReport = ValidationReport.fromResult(validationResult); + preProcess(opsTimer, trackerBundle); - if (!trackerBundle.isSkipRuleEngine() && !params.getImportStrategy().isDelete()) { - jobProgress.startingStage("Running Rule Engine"); - jobProgress.runStage(() -> trackerBundleService.runRuleEngine(trackerBundle)); - - jobProgress.startingStage("Running Rule Engine Validation"); - ValidationResult result = - jobProgress.runStage(() -> validationService.validateRuleEngine(trackerBundle)); - trackerBundle.setTrackedEntities(result.getTrackedEntities()); - trackerBundle.setEnrollments(result.getEnrollments()); - trackerBundle.setEvents(result.getEvents()); - trackerBundle.setRelationships(result.getRelationships()); - - validationReport = ValidationReport.merge(validationResult, result); - } + ValidationReport validationReport = validate(params, opsTimer, trackerBundle); + + if (exitOnError(validationReport, params)) { + return buildReportAndNotify(params, validationReport, opsTimer, bundleSize); + } + + PersistenceReport persistenceReport = commit(params, opsTimer, trackerBundle); + + postCommit(trackerBundle); + + ImportReport importReport = + ImportReport.withImportCompleted( + Status.OK, persistenceReport, validationReport, opsTimer.stopTimer(), bundleSize); - if (exitOnError(validationReport, params)) { - return ImportReport.withValidationErrors( - validationReport, bundleSize.values().stream().mapToInt(Integer::intValue).sum()); + endImport(params, importReport); + + return importReport; + } catch (Exception e) { + log.error("Exception thrown during import.", e); + + ImportReport report = + ImportReport.withError( + "Exception:" + e.getMessage(), ValidationReport.emptyReport(), opsTimer.stopTimer()); + + endImportWithError(params, report, e); + + return report; } + } + + private TrackerBundle preHeat(TrackerImportParams params, TimingsStats opsTimer) { + TrackerBundle trackerBundle = opsTimer.exec(PREHEAT_OPS, () -> preheatBundle(params)); + + notifyOps(params, PREHEAT_OPS, opsTimer); - jobProgress.startingStage("Commit Transaction"); - PersistenceReport persistenceReport = jobProgress.runStage(() -> commit(params, trackerBundle)); + return trackerBundle; + } + + private void preProcess(TimingsStats opsTimer, TrackerBundle trackerBundle) { + opsTimer.execVoid(PREPROCESS_OPS, () -> preProcessBundle(trackerBundle)); + } + + private ValidationReport validate( + TrackerImportParams params, TimingsStats opsTimer, TrackerBundle trackerBundle) { + ValidationResult validationResult = + opsTimer.exec(VALIDATION_OPS, () -> validateBundle(params, trackerBundle, opsTimer)); - jobProgress.startingStage("PostCommit"); - jobProgress.runStage(() -> trackerBundleService.postCommit(trackerBundle)); + if (!trackerBundle.isSkipRuleEngine() && !params.getImportStrategy().isDelete()) { + ValidationResult ruleEnginevalidationResult = execRuleEngine(params, opsTimer, trackerBundle); + + return ValidationReport.merge(validationResult, ruleEnginevalidationResult); + } - return ImportReport.withImportCompleted( - Status.OK, persistenceReport, validationReport, bundleSize); + return ValidationReport.fromResult(validationResult); } - private PersistenceReport commit(TrackerImportParams params, TrackerBundle trackerBundle) { + private PersistenceReport commit( + TrackerImportParams params, TimingsStats opsTimer, TrackerBundle trackerBundle) { + PersistenceReport persistenceReport; if (TrackerImportStrategy.DELETE == params.getImportStrategy()) { - return deleteBundle(trackerBundle); + persistenceReport = opsTimer.exec(COMMIT_OPS, () -> deleteBundle(trackerBundle)); } else { - return commitBundle(trackerBundle); + persistenceReport = opsTimer.exec(COMMIT_OPS, () -> commitBundle(trackerBundle)); } + + notifyOps(params, COMMIT_OPS, opsTimer); + return persistenceReport; + } + + private void postCommit(TrackerBundle trackerBundle) { + trackerBundleService.postCommit(trackerBundle); } - protected ValidationResult validateBundle(TrackerBundle bundle) { + protected ValidationResult validateBundle( + TrackerImportParams params, TrackerBundle bundle, TimingsStats opsTimer) { ValidationResult result = validationService.validate(bundle); bundle.setTrackedEntities(result.getTrackedEntities()); bundle.setEnrollments(result.getEnrollments()); bundle.setEvents(result.getEvents()); bundle.setRelationships(result.getRelationships()); + notifyOps(params, VALIDATION_OPS, opsTimer); + + return result; + } + + private ValidationResult execRuleEngine( + TrackerImportParams params, TimingsStats opsTimer, TrackerBundle bundle) { + opsTimer.execVoid(PROGRAMRULE_OPS, () -> trackerBundleService.runRuleEngine(bundle)); + + notifyOps(params, PROGRAMRULE_OPS, opsTimer); + + ValidationResult result = + opsTimer.exec(VALIDATE_PROGRAMRULE_OPS, () -> validationService.validateRuleEngine(bundle)); + bundle.setTrackedEntities(result.getTrackedEntities()); + bundle.setEnrollments(result.getEnrollments()); + bundle.setEvents(result.getEvents()); + bundle.setRelationships(result.getRelationships()); + + notifyOps(params, VALIDATE_PROGRAMRULE_OPS, opsTimer); + return result; } + private ImportReport buildReportAndNotify( + TrackerImportParams params, + ValidationReport validationReport, + TimingsStats opsTimer, + Map bundleSize) { + ImportReport importReport = + ImportReport.withValidationErrors( + validationReport, + opsTimer.stopTimer(), + bundleSize.values().stream().mapToInt(Integer::intValue).sum()); + + endImport(params, importReport); + + return importReport; + } + private boolean exitOnError(ValidationReport validationReport, TrackerImportParams params) { return validationReport.hasErrors() && params.getAtomicMode() == AtomicMode.ALL; } @@ -152,6 +228,14 @@ private Map calculatePayloadSize(TrackerBundle bundle) { TrackerType.RELATIONSHIP, bundle.getRelationships().size()); } + protected TrackerBundle preheatBundle(TrackerImportParams params) { + return trackerBundleService.create(params); + } + + protected void preProcessBundle(TrackerBundle bundle) { + trackerPreprocessService.preprocess(bundle); + } + protected PersistenceReport commitBundle(TrackerBundle trackerBundle) { PersistenceReport persistenceReport = trackerBundleService.commit(trackerBundle); @@ -160,7 +244,7 @@ protected PersistenceReport commitBundle(TrackerBundle trackerBundle) { Stream.of(TrackerType.ENROLLMENT, TrackerType.EVENT) .map(trackerType -> safelyGetSideEffectsDataBundles(persistenceReport, trackerType)) .flatMap(Collection::stream) - .toList(); + .collect(Collectors.toList()); trackerBundleService.handleTrackerSideEffects(sideEffectDataBundles); } @@ -181,6 +265,48 @@ protected PersistenceReport deleteBundle(TrackerBundle trackerBundle) { return trackerBundleService.delete(trackerBundle); } + private void startImport(TrackerImportParams params) { + if (null != params.getJobConfiguration()) { + notifier.notify(params.getJobConfiguration(), params.userStartInfo() + " Import:Start"); + } + } + + private void notifyOps(TrackerImportParams params, String validationOps, TimingsStats opsTimer) { + if (null != params.getJobConfiguration()) { + notifier.update( + params.getJobConfiguration(), + NotificationLevel.DEBUG, + params + + validationOps + + " completed in " + + opsTimer.get(validationOps) + + " Import:" + + validationOps); + } + } + + private void endImport(TrackerImportParams params, ImportReport importReport) { + if (null != params.getJobConfiguration()) { + notifier.update( + params.getJobConfiguration(), + params + " finished in " + importReport.getTimingsStats().get(TOTAL_OPS) + " Import:Done", + true); + + notifier.addJobSummary(params.getJobConfiguration(), importReport, ImportReport.class); + } + } + + private void endImportWithError( + TrackerImportParams params, ImportReport importReport, Exception e) { + notifier.update( + params.getJobConfiguration(), + NotificationLevel.ERROR, + params + " failed with exception: " + e.getMessage() + " Import:Error", + true); + + notifier.addJobSummary(params.getJobConfiguration(), importReport, ImportReport.class); + } + /** * Clone the TrackerImportReport and filters out validation data based on the provided {@link * PersistenceReport}. @@ -202,10 +328,11 @@ public ImportReport buildImportReport( if (originalValidationReport != null) { validationReport.addErrors(originalValidationReport.getErrors()); } - if (originalValidationReport != null - && (TrackerBundleReportMode.WARNINGS == reportMode - || TrackerBundleReportMode.FULL == reportMode)) { + if (originalValidationReport != null && TrackerBundleReportMode.WARNINGS == reportMode) { + validationReport.addWarnings(originalValidationReport.getWarnings()); + } else if (originalValidationReport != null && TrackerBundleReportMode.FULL == reportMode) { validationReport.addWarnings(originalValidationReport.getWarnings()); + importReportBuilder.timingsStats(originalImportReport.getTimingsStats()); } importReportBuilder.validationReport(validationReport); diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/ParamsConverter.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/ParamsConverter.java index 3aaafa746dd5..927c04736253 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/ParamsConverter.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/ParamsConverter.java @@ -28,15 +28,12 @@ package org.hisp.dhis.tracker.imports; import org.hisp.dhis.tracker.imports.bundle.TrackerBundle; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; -import org.hisp.dhis.user.User; /** * @author Luciano Fiandesio */ public class ParamsConverter { - public static TrackerBundle convert( - TrackerImportParams params, TrackerObjects trackerObjects, User user) { + public static TrackerBundle convert(TrackerImportParams params) { return TrackerBundle.builder() .importMode(params.getImportMode()) .importStrategy(params.getImportStrategy()) @@ -45,11 +42,11 @@ public static TrackerBundle convert( .skipRuleEngine(params.isSkipRuleEngine()) .flushMode(params.getFlushMode()) .validationMode(params.getValidationMode()) - .trackedEntities(trackerObjects.getTrackedEntities()) - .enrollments(trackerObjects.getEnrollments()) - .events(trackerObjects.getEvents()) - .relationships(trackerObjects.getRelationships()) - .user(user) + .trackedEntities(params.getTrackedEntities()) + .enrollments(params.getEnrollments()) + .events(params.getEvents()) + .relationships(params.getRelationships()) + .user(params.getUser()) .build(); } } diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/TrackerIdSchemeParam.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/TrackerIdSchemeParam.java index 161bc0bc8905..cde712ce7863 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/TrackerIdSchemeParam.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/TrackerIdSchemeParam.java @@ -29,7 +29,6 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; -import java.io.Serializable; import lombok.AccessLevel; import lombok.AllArgsConstructor; import lombok.Builder; @@ -51,7 +50,7 @@ // which is invalid. Drop @OpenApi.Property annotation after implementing support for the unpacking // of query param classes in OpenApiGenerator @OpenApi.Property(value = TrackerIdScheme.class) -public class TrackerIdSchemeParam implements Serializable { +public class TrackerIdSchemeParam { public static final TrackerIdSchemeParam UID = TrackerIdSchemeParam.of(TrackerIdScheme.UID, null); public static final TrackerIdSchemeParam CODE = diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/TrackerIdSchemeParams.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/TrackerIdSchemeParams.java index a2fb0826b1e5..83760db26294 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/TrackerIdSchemeParams.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/TrackerIdSchemeParams.java @@ -28,7 +28,6 @@ package org.hisp.dhis.tracker.imports; import com.fasterxml.jackson.annotation.JsonProperty; -import java.io.Serializable; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -51,7 +50,7 @@ @Builder @NoArgsConstructor @AllArgsConstructor -public class TrackerIdSchemeParams implements Serializable { +public class TrackerIdSchemeParams { /** Specific identifier to match data elements on. */ @JsonProperty @Builder.Default private TrackerIdSchemeParam dataElementIdScheme = TrackerIdSchemeParam.UID; diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/TrackerIdentifierCollector.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/TrackerIdentifierCollector.java index 7467d89bb261..d64c2653dc04 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/TrackerIdentifierCollector.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/TrackerIdentifierCollector.java @@ -53,7 +53,6 @@ import org.hisp.dhis.tracker.imports.domain.Note; import org.hisp.dhis.tracker.imports.domain.Relationship; import org.hisp.dhis.tracker.imports.domain.TrackedEntity; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.springframework.stereotype.Component; /** @@ -71,12 +70,12 @@ public class TrackerIdentifierCollector { private final ProgramRuleService programRuleService; - public Map, Set> collect(TrackerObjects trackerObjects) { + public Map, Set> collect(TrackerImportParams params) { final Map, Set> identifiers = new HashMap<>(); - collectTrackedEntities(identifiers, trackerObjects.getTrackedEntities()); - collectEnrollments(identifiers, trackerObjects.getEnrollments()); - collectEvents(identifiers, trackerObjects.getEvents()); - collectRelationships(identifiers, trackerObjects.getRelationships()); + collectTrackedEntities(identifiers, params.getTrackedEntities()); + collectEnrollments(identifiers, params.getEnrollments()); + collectEvents(identifiers, params.getEvents()); + collectRelationships(identifiers, params.getRelationships()); collectProgramRulesFields(identifiers); return identifiers; } diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/TrackerImportParams.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/TrackerImportParams.java index 6b5fc80f54fe..df96cda1f128 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/TrackerImportParams.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/TrackerImportParams.java @@ -28,12 +28,21 @@ package org.hisp.dhis.tracker.imports; import com.fasterxml.jackson.annotation.JsonProperty; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; import lombok.NoArgsConstructor; -import org.hisp.dhis.scheduling.JobParameters; +import org.hisp.dhis.scheduling.JobConfiguration; +import org.hisp.dhis.scheduling.JobType; import org.hisp.dhis.tracker.imports.bundle.TrackerBundleMode; +import org.hisp.dhis.tracker.imports.domain.Enrollment; +import org.hisp.dhis.tracker.imports.domain.Event; +import org.hisp.dhis.tracker.imports.domain.Relationship; +import org.hisp.dhis.tracker.imports.domain.TrackedEntity; +import org.hisp.dhis.user.User; /** * @author Morten Olav Hansen @@ -42,10 +51,13 @@ @Builder @NoArgsConstructor @AllArgsConstructor -public class TrackerImportParams implements JobParameters { +public class TrackerImportParams { /** User uid to use for import job. */ @JsonProperty private String userId; + /** User to use for import job. */ + private User user; + /** Should import be imported or just validated. */ @JsonProperty @Builder.Default private final TrackerBundleMode importMode = TrackerBundleMode.COMMIT; @@ -79,6 +91,50 @@ public class TrackerImportParams implements JobParameters { /** Name of file that was used for import (if available). */ @JsonProperty @Builder.Default private final String filename = null; + /** Job configuration */ + private JobConfiguration jobConfiguration; + @JsonProperty @Builder.Default private TrackerBundleReportMode reportMode = TrackerBundleReportMode.ERRORS; + + /** Tracked entities to import. */ + @JsonProperty @Builder.Default + private final List trackedEntities = new ArrayList<>(); + + /** Enrollments to import. */ + @JsonProperty @Builder.Default private final List enrollments = new ArrayList<>(); + + /** Events to import. */ + @JsonProperty @Builder.Default private final List events = new ArrayList<>(); + + /** Relationships to import. */ + @JsonProperty @Builder.Default private final List relationships = new ArrayList<>(); + + public TrackerImportParams setUser(User user) { + this.user = user; + + if (user != null) { + this.userId = user.getUid(); + } + + return this; + } + + @JsonProperty + public String getUsername() { + return User.username(user); + } + + @Override + public String toString() { + return Optional.ofNullable(this.getJobConfiguration()) + .map( + jobConfiguration -> + JobType.TRACKER_IMPORT_JOB + " ( " + jobConfiguration.getUid() + " )") + .orElse(JobType.TRACKER_IMPORT_JOB.toString()); + } + + public String userStartInfo() { + return this + " started by " + this.getUsername() + " ( " + this.userId + " )"; + } } diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/TrackerImportService.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/TrackerImportService.java index caa72a9b64f8..23d3222af8cc 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/TrackerImportService.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/TrackerImportService.java @@ -27,9 +27,6 @@ */ package org.hisp.dhis.tracker.imports; -import org.hisp.dhis.scheduling.JobProgress; -import org.hisp.dhis.scheduling.NoopJobProgress; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.report.ImportReport; /** @@ -40,25 +37,10 @@ public interface TrackerImportService { * Import object using provided params. Takes the objects through all phases of the importer from * preheating to validation, and then finished with a commit (unless its validate only) * - * @param params Parameters for import - * @param trackerObjects the objects to import + * @param params Parameters for import, including objects * @return Report giving status of import (and any errors) */ - default ImportReport importTracker(TrackerImportParams params, TrackerObjects trackerObjects) { - return importTracker(params, trackerObjects, NoopJobProgress.INSTANCE); - } - - /** - * Import object using provided params. Takes the objects through all phases of the importer from - * preheating to validation, and then finished with a commit (unless its validate only) - * - * @param params Parameters for import - * @param trackerObjects the objects to import - * @param jobProgress to track import progress - * @return Report giving status of import (and any errors) - */ - ImportReport importTracker( - TrackerImportParams params, TrackerObjects trackerObjects, JobProgress jobProgress); + ImportReport importTracker(TrackerImportParams params); /** * Build the report based on the mode selected by the client. diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/bundle/DefaultTrackerBundleService.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/bundle/DefaultTrackerBundleService.java index a8699eadad70..0374ea7aa1b7 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/bundle/DefaultTrackerBundleService.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/bundle/DefaultTrackerBundleService.java @@ -41,7 +41,6 @@ import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.bundle.persister.CommitService; import org.hisp.dhis.tracker.imports.bundle.persister.TrackerObjectDeletionService; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.job.TrackerSideEffectDataBundle; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheatService; @@ -49,7 +48,6 @@ import org.hisp.dhis.tracker.imports.report.PersistenceReport; import org.hisp.dhis.tracker.imports.report.TrackerTypeReport; import org.hisp.dhis.tracker.imports.sideeffect.SideEffectHandlerService; -import org.hisp.dhis.user.User; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Service; import org.springframework.transaction.annotation.Transactional; @@ -80,11 +78,9 @@ public void setSideEffectHandlers(List sideEffectHandl } @Override - public TrackerBundle create( - TrackerImportParams params, TrackerObjects trackerObjects, User user) { - TrackerBundle trackerBundle = ParamsConverter.convert(params, trackerObjects, user); - TrackerPreheat preheat = - trackerPreheatService.preheat(trackerObjects, params.getIdSchemes(), user); + public TrackerBundle create(TrackerImportParams params) { + TrackerBundle trackerBundle = ParamsConverter.convert(params); + TrackerPreheat preheat = trackerPreheatService.preheat(params); trackerBundle.setPreheat(preheat); return trackerBundle; diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/bundle/TrackerBundleService.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/bundle/TrackerBundleService.java index cdb50aee5cda..af8e46e72472 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/bundle/TrackerBundleService.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/bundle/TrackerBundleService.java @@ -29,10 +29,8 @@ import java.util.List; import org.hisp.dhis.tracker.imports.TrackerImportParams; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.job.TrackerSideEffectDataBundle; import org.hisp.dhis.tracker.imports.report.PersistenceReport; -import org.hisp.dhis.user.User; /** * @author Morten Olav Hansen @@ -44,7 +42,7 @@ public interface TrackerBundleService { * @param params Params object for this bundle. * @return Configured TrackerBundle instance(s) (if bundle splitting is enabled) */ - TrackerBundle create(TrackerImportParams params, TrackerObjects trackerObjects, User user); + TrackerBundle create(TrackerImportParams params); /** * Call rule engine for tracker bundle. diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Attribute.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Attribute.java index 37134c2f6bff..be0ccf18d421 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Attribute.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Attribute.java @@ -28,7 +28,6 @@ package org.hisp.dhis.tracker.imports.domain; import com.fasterxml.jackson.annotation.JsonProperty; -import java.io.Serializable; import java.time.Instant; import lombok.AllArgsConstructor; import lombok.Builder; @@ -43,7 +42,7 @@ @Builder @NoArgsConstructor @AllArgsConstructor -public class Attribute implements Serializable { +public class Attribute { @JsonProperty private MetadataIdentifier attribute; @JsonProperty private String code; diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Coordinate.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Coordinate.java index b00612269568..567290315e17 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Coordinate.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Coordinate.java @@ -28,7 +28,6 @@ package org.hisp.dhis.tracker.imports.domain; import com.fasterxml.jackson.annotation.JsonProperty; -import java.io.Serializable; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -42,7 +41,7 @@ @Builder @NoArgsConstructor @AllArgsConstructor -public class Coordinate implements Serializable { +public class Coordinate { @JsonProperty private Double latitude; @JsonProperty private Double longitude; diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/DataValue.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/DataValue.java index d0b0a7de9f93..fd1e6689c491 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/DataValue.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/DataValue.java @@ -28,7 +28,6 @@ package org.hisp.dhis.tracker.imports.domain; import com.fasterxml.jackson.annotation.JsonProperty; -import java.io.Serializable; import java.time.Instant; import lombok.AllArgsConstructor; import lombok.Builder; @@ -42,7 +41,7 @@ @Builder @NoArgsConstructor @AllArgsConstructor -public class DataValue implements Serializable { +public class DataValue { @JsonProperty private Instant createdAt; @JsonProperty private Instant updatedAt; diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Enrollment.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Enrollment.java index 0e702d9bbf81..d5ec22ed7d47 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Enrollment.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Enrollment.java @@ -28,7 +28,6 @@ package org.hisp.dhis.tracker.imports.domain; import com.fasterxml.jackson.annotation.JsonProperty; -import java.io.Serializable; import java.time.Instant; import java.util.ArrayList; import java.util.List; @@ -46,7 +45,7 @@ @Builder @NoArgsConstructor @AllArgsConstructor -public class Enrollment implements TrackerDto, Serializable { +public class Enrollment implements TrackerDto { @JsonProperty private String enrollment; @JsonProperty private Instant createdAt; diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Event.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Event.java index 5298abab4e03..4b30f68fb4c1 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Event.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Event.java @@ -29,7 +29,6 @@ import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonProperty; -import java.io.Serializable; import java.time.Instant; import java.util.ArrayList; import java.util.HashSet; @@ -50,7 +49,7 @@ @Builder @NoArgsConstructor @AllArgsConstructor -public class Event implements TrackerDto, Serializable { +public class Event implements TrackerDto { @JsonProperty private String event; @JsonProperty @Builder.Default private EventStatus status = EventStatus.ACTIVE; diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/MetadataIdentifier.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/MetadataIdentifier.java index 3ad1b265787b..3b28029e61b4 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/MetadataIdentifier.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/MetadataIdentifier.java @@ -29,7 +29,6 @@ import com.fasterxml.jackson.annotation.JsonCreator; import com.fasterxml.jackson.annotation.JsonProperty; -import java.io.Serializable; import java.util.Objects; import lombok.AccessLevel; import lombok.AllArgsConstructor; @@ -58,7 +57,7 @@ */ @Value @AllArgsConstructor(access = AccessLevel.PRIVATE) -public class MetadataIdentifier implements Serializable { +public class MetadataIdentifier { public static final MetadataIdentifier EMPTY_UID = MetadataIdentifier.ofUid((String) null); diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Note.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Note.java index 5401ed822ccc..cec45da897d7 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Note.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Note.java @@ -28,7 +28,6 @@ package org.hisp.dhis.tracker.imports.domain; import com.fasterxml.jackson.annotation.JsonProperty; -import java.io.Serializable; import java.time.Instant; import lombok.AllArgsConstructor; import lombok.Builder; @@ -45,7 +44,7 @@ @Builder @NoArgsConstructor @AllArgsConstructor -public class Note implements Serializable { +public class Note { @JsonProperty private String note; @JsonProperty private Instant storedAt; diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Relationship.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Relationship.java index 83beb87b27a5..5219571ab623 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Relationship.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/Relationship.java @@ -28,7 +28,6 @@ package org.hisp.dhis.tracker.imports.domain; import com.fasterxml.jackson.annotation.JsonProperty; -import java.io.Serializable; import java.time.Instant; import lombok.AllArgsConstructor; import lombok.Builder; @@ -43,7 +42,7 @@ @Builder @NoArgsConstructor @AllArgsConstructor -public class Relationship implements TrackerDto, Serializable { +public class Relationship implements TrackerDto { @JsonProperty private String relationship; @JsonProperty private String relationshipName; diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/RelationshipItem.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/RelationshipItem.java index cc80e0be0337..12e022a460ae 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/RelationshipItem.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/RelationshipItem.java @@ -28,7 +28,6 @@ package org.hisp.dhis.tracker.imports.domain; import com.fasterxml.jackson.annotation.JsonProperty; -import java.io.Serializable; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -41,7 +40,7 @@ @Builder @NoArgsConstructor @AllArgsConstructor -public class RelationshipItem implements Serializable { +public class RelationshipItem { @JsonProperty private String trackedEntity; diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/TrackedEntity.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/TrackedEntity.java index 29eaf07e3f9d..7aee6fba58e9 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/TrackedEntity.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/TrackedEntity.java @@ -28,7 +28,6 @@ package org.hisp.dhis.tracker.imports.domain; import com.fasterxml.jackson.annotation.JsonProperty; -import java.io.Serializable; import java.time.Instant; import java.util.ArrayList; import java.util.List; @@ -46,7 +45,7 @@ @Builder @NoArgsConstructor @AllArgsConstructor -public class TrackedEntity implements TrackerDto, Serializable { +public class TrackedEntity implements TrackerDto { @JsonProperty private String trackedEntity; @JsonProperty private MetadataIdentifier trackedEntityType; diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/User.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/User.java index 0509c17bad16..d30e8891540e 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/User.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/User.java @@ -30,7 +30,6 @@ import static org.apache.commons.lang3.StringUtils.isBlank; import com.fasterxml.jackson.annotation.JsonProperty; -import java.io.Serializable; import lombok.AllArgsConstructor; import lombok.Builder; import lombok.Data; @@ -43,7 +42,7 @@ @Builder @NoArgsConstructor @AllArgsConstructor -public class User implements Serializable { +public class User { @JsonProperty private String uid; @JsonProperty private String username; diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/job/TrackerImportThread.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/job/TrackerImportThread.java new file mode 100644 index 000000000000..cb2796a2cb23 --- /dev/null +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/job/TrackerImportThread.java @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2004-2022, University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * Neither the name of the HISP project nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.hisp.dhis.tracker.imports.job; + +import org.hisp.dhis.security.SecurityContextRunnable; +import org.hisp.dhis.tracker.imports.TrackerImportParams; +import org.hisp.dhis.tracker.imports.TrackerImportService; +import org.springframework.beans.factory.config.BeanDefinition; +import org.springframework.context.annotation.Scope; +import org.springframework.stereotype.Component; +import org.springframework.util.Assert; + +/** + * @author Morten Olav Hansen + */ +@Component +@Scope(BeanDefinition.SCOPE_PROTOTYPE) +public class TrackerImportThread extends SecurityContextRunnable { + private final TrackerImportService trackerImportService; + + private TrackerImportParams trackerImportParams; + + public TrackerImportThread(TrackerImportService trackerImportService) { + this.trackerImportService = trackerImportService; + } + + @Override + public void call() { + Assert.notNull(trackerImportParams, "Field trackerImportParams can not be null. "); + + trackerImportService.importTracker(trackerImportParams); // discard returned report + } + + public void setTrackerImportParams(TrackerImportParams trackerImportParams) { + this.trackerImportParams = trackerImportParams; + } +} diff --git a/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/imports/TrackerJobWebMessageResponse.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/job/TrackerJobWebMessageResponse.java similarity index 92% rename from dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/imports/TrackerJobWebMessageResponse.java rename to dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/job/TrackerJobWebMessageResponse.java index 3251757dc7ba..630f30f46e4a 100644 --- a/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/imports/TrackerJobWebMessageResponse.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/job/TrackerJobWebMessageResponse.java @@ -1,5 +1,5 @@ /* - * Copyright (c) 2004-2023, University of Oslo + * Copyright (c) 2004-2022, University of Oslo * All rights reserved. * * Redistribution and use in source and binary forms, with or without @@ -25,13 +25,16 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package org.hisp.dhis.webapi.controller.tracker.imports; +package org.hisp.dhis.tracker.imports.job; import com.fasterxml.jackson.annotation.JsonProperty; import lombok.Builder; import lombok.Data; import org.hisp.dhis.webmessage.WebMessageResponse; +/** + * @author Morten Olav Hansen + */ @Data @Builder public class TrackerJobWebMessageResponse implements WebMessageResponse { diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/job/TrackerMessage.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/job/TrackerMessage.java new file mode 100644 index 000000000000..ab8bcb5ceaab --- /dev/null +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/job/TrackerMessage.java @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2004-2022, University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * Neither the name of the HISP project nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.hisp.dhis.tracker.imports.job; + +import com.fasterxml.jackson.annotation.JsonProperty; +import com.fasterxml.jackson.databind.annotation.JsonDeserialize; +import com.fasterxml.jackson.databind.annotation.JsonPOJOBuilder; +import lombok.Builder; +import lombok.Value; +import org.hisp.dhis.artemis.MessageType; +import org.hisp.dhis.artemis.SerializableMessage; +import org.hisp.dhis.tracker.imports.TrackerImportParams; + +/** + * Used by Apache Artemis to pass tracker import jobs from the /api/tracker endpoint to the tracker + * import services. + * + * @author Morten Olav Hansen + */ +@Value +@Builder(builderClassName = "TrackerMessageBuilder") +@JsonDeserialize(builder = TrackerMessage.TrackerMessageBuilder.class) +public class TrackerMessage implements SerializableMessage { + @JsonProperty private final String uid; + + @JsonProperty private final String authentication; + + @JsonProperty private final TrackerImportParams trackerImportParams; + + @Override + public MessageType getMessageType() { + return MessageType.TRACKER_JOB; + } + + @JsonPOJOBuilder(withPrefix = "") + public static final class TrackerMessageBuilder {} +} diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/job/TrackerMessageManager.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/job/TrackerMessageManager.java new file mode 100644 index 000000000000..ef8d8abfbef7 --- /dev/null +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/job/TrackerMessageManager.java @@ -0,0 +1,89 @@ +/* + * Copyright (c) 2004-2022, University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * Neither the name of the HISP project nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.hisp.dhis.tracker.imports.job; + +import com.fasterxml.jackson.core.JsonProcessingException; +import com.fasterxml.jackson.databind.ObjectMapper; +import javax.jms.JMSException; +import javax.jms.TextMessage; +import org.hisp.dhis.artemis.Topics; +import org.hisp.dhis.common.AsyncTaskExecutor; +import org.hisp.dhis.scheduling.JobConfiguration; +import org.hisp.dhis.scheduling.JobType; +import org.hisp.dhis.security.AuthenticationSerializer; +import org.hisp.dhis.tracker.imports.TrackerImportParams; +import org.springframework.beans.factory.ObjectFactory; +import org.springframework.jms.annotation.JmsListener; +import org.springframework.security.core.context.SecurityContextHolder; +import org.springframework.stereotype.Component; + +/** + * @author Morten Olav Hansen + */ +@Component +public class TrackerMessageManager { + private final ObjectMapper objectMapper; + + private final AsyncTaskExecutor taskExecutor; + + private final ObjectFactory trackerImportThreadFactory; + + public TrackerMessageManager( + ObjectMapper objectMapper, + AsyncTaskExecutor taskExecutor, + ObjectFactory trackerImportThreadFactory) { + this.objectMapper = objectMapper; + this.taskExecutor = taskExecutor; + this.trackerImportThreadFactory = trackerImportThreadFactory; + } + + @JmsListener( + destination = Topics.TRACKER_IMPORT_JOB_TOPIC_NAME, + containerFactory = "jmsQueueListenerContainerFactory") + public void consume(TextMessage message) throws JMSException, JsonProcessingException { + String payload = message.getText(); + + TrackerMessage trackerMessage = objectMapper.readValue(payload, TrackerMessage.class); + TrackerImportParams trackerImportParams = trackerMessage.getTrackerImportParams(); + + JobConfiguration jobConfiguration = + new JobConfiguration("", JobType.TRACKER_IMPORT_JOB, trackerImportParams.getUserId()); + + jobConfiguration.setUid(trackerMessage.getUid()); + trackerImportParams.setJobConfiguration(jobConfiguration); + + TrackerImportThread trackerImportThread = trackerImportThreadFactory.getObject(); + trackerImportThread.setTrackerImportParams(trackerImportParams); + + SecurityContextHolder.getContext() + .setAuthentication( + AuthenticationSerializer.deserialize(trackerMessage.getAuthentication())); + + taskExecutor.executeTask(trackerImportThread); + } +} diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/DefaultTrackerPreheatService.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/DefaultTrackerPreheatService.java index c2de4078f69d..3051680314c2 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/DefaultTrackerPreheatService.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/DefaultTrackerPreheatService.java @@ -36,10 +36,8 @@ import lombok.extern.slf4j.Slf4j; import org.hisp.dhis.common.IdentifiableObjectManager; import org.hisp.dhis.preheat.PreheatException; -import org.hisp.dhis.tracker.imports.TrackerIdSchemeParams; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.preheat.supplier.PreheatSupplier; -import org.hisp.dhis.user.User; import org.springframework.beans.BeansException; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.ApplicationContext; @@ -72,18 +70,17 @@ public void setApplicationContext(ApplicationContext applicationContext) throws @Override @Transactional(readOnly = true) - public TrackerPreheat preheat( - TrackerObjects trackerObjects, TrackerIdSchemeParams idSchemeParams, User user) { + public TrackerPreheat preheat(TrackerImportParams params) { TrackerPreheat preheat = new TrackerPreheat(); - preheat.setIdSchemes(idSchemeParams); - preheat.setUser(user); + preheat.setIdSchemes(params.getIdSchemes()); + preheat.setUser(params.getUser()); checkNotNull(preheat.getUser(), "TrackerPreheat is missing the user object."); for (String supplier : preheatSuppliers) { final String beanName = Introspector.decapitalize(supplier); try { - ctx.getBean(beanName, PreheatSupplier.class).add(trackerObjects, preheat); + ctx.getBean(beanName, PreheatSupplier.class).add(params, preheat); } catch (BeansException beanException) { processException( "Unable to find a preheat supplier with name " diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/TrackerPreheatService.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/TrackerPreheatService.java index 05b97823a1ca..83425bab3a07 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/TrackerPreheatService.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/TrackerPreheatService.java @@ -27,9 +27,7 @@ */ package org.hisp.dhis.tracker.imports.preheat; -import org.hisp.dhis.tracker.imports.TrackerIdSchemeParams; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; -import org.hisp.dhis.user.User; +import org.hisp.dhis.tracker.imports.TrackerImportParams; /** * @author Morten Olav Hansen @@ -41,6 +39,5 @@ public interface TrackerPreheatService { * * @param params Params for preheating */ - TrackerPreheat preheat( - TrackerObjects trackerObjects, TrackerIdSchemeParams idSchemeParams, User user); + TrackerPreheat preheat(TrackerImportParams params); } diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/AbstractPreheatSupplier.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/AbstractPreheatSupplier.java index 8a7d1958a208..0b344e8e4d82 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/AbstractPreheatSupplier.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/AbstractPreheatSupplier.java @@ -33,7 +33,7 @@ import org.apache.commons.lang3.time.StopWatch; import org.hisp.dhis.common.IdentifiableObject; import org.hisp.dhis.hibernate.HibernateProxyUtils; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.hisp.dhis.tracker.imports.preheat.cache.PreheatCacheService; @@ -50,7 +50,7 @@ public abstract class AbstractPreheatSupplier implements PreheatSupplier { private final long CACHE_CAPACITY = 1000; @Override - public void add(TrackerObjects trackerObjects, TrackerPreheat preheat) { + public void add(TrackerImportParams params, TrackerPreheat preheat) { StopWatch watch = null; if (log.isDebugEnabled()) { log.debug("Executing preheat supplier: {}", this.getClass().getName()); @@ -58,7 +58,7 @@ public void add(TrackerObjects trackerObjects, TrackerPreheat preheat) { watch.start(); } - preheatAdd(trackerObjects, preheat); + preheatAdd(params, preheat); if (log.isDebugEnabled()) { if (watch != null && watch.isStarted()) { @@ -72,7 +72,7 @@ public void add(TrackerObjects trackerObjects, TrackerPreheat preheat) { } /** Template method: executes preheat logic from the subclass */ - public abstract void preheatAdd(TrackerObjects trackerObjects, TrackerPreheat preheat); + public abstract void preheatAdd(TrackerImportParams params, TrackerPreheat preheat); protected void addToCache(PreheatCacheService cache, List objects) { objects.forEach( diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/ClassBasedSupplier.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/ClassBasedSupplier.java index 93c256b92412..831441c6dc90 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/ClassBasedSupplier.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/ClassBasedSupplier.java @@ -35,7 +35,7 @@ import java.util.Set; import lombok.RequiredArgsConstructor; import org.hisp.dhis.tracker.imports.TrackerIdentifierCollector; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.hisp.dhis.tracker.imports.preheat.supplier.strategy.ClassBasedSupplierStrategy; import org.hisp.dhis.tracker.imports.preheat.supplier.strategy.GenericStrategy; @@ -67,13 +67,13 @@ public class ClassBasedSupplier extends AbstractPreheatSupplier implements Appli private final Map classStrategies; @Override - public void preheatAdd(TrackerObjects trackerObjects, TrackerPreheat preheat) { + public void preheatAdd(TrackerImportParams params, TrackerPreheat preheat) { /* * Collects all references from the payload and create a Map where key * is the reference type (e.g. Enrollment) and the value is a Set of * identifiers (e.g. a list of all Enrollment UIDs found in the payload) */ - Map, Set> identifierMap = identifierCollector.collect(trackerObjects); + Map, Set> identifierMap = identifierCollector.collect(params); identifierMap.forEach( (key, identifiers) -> { @@ -90,7 +90,7 @@ public void preheatAdd(TrackerObjects trackerObjects, TrackerPreheat preheat) { } else { context .getBean(Introspector.decapitalize(bean), ClassBasedSupplierStrategy.class) - .add(splitList, preheat); + .add(params, splitList, preheat); } }); } diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/DefaultsSupplier.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/DefaultsSupplier.java index 7db809ab2c3c..bf12d2c46db3 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/DefaultsSupplier.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/DefaultsSupplier.java @@ -36,7 +36,7 @@ import org.hisp.dhis.category.CategoryOptionCombo; import org.hisp.dhis.common.IdentifiableObject; import org.hisp.dhis.common.IdentifiableObjectManager; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.hisp.dhis.tracker.imports.preheat.cache.PreheatCacheService; import org.hisp.dhis.tracker.imports.preheat.mappers.CategoryComboMapper; @@ -59,7 +59,7 @@ public class DefaultsSupplier extends AbstractPreheatSupplier { @Nonnull private final PreheatCacheService cache; @Override - public void preheatAdd(TrackerObjects trackerObjects, TrackerPreheat preheat) { + public void preheatAdd(TrackerImportParams params, TrackerPreheat preheat) { // not using manager.getDefaults() as the collections of the entities // are still hibernate proxies // this leads to lazy init exceptions with - no session. reason is the diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/DuplicateRelationshipSupplier.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/DuplicateRelationshipSupplier.java index 89ec6171e11c..a91c405021ba 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/DuplicateRelationshipSupplier.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/DuplicateRelationshipSupplier.java @@ -34,8 +34,8 @@ import lombok.RequiredArgsConstructor; import org.hisp.dhis.relationship.RelationshipStore; import org.hisp.dhis.relationship.RelationshipType; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.domain.Relationship; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.hisp.dhis.tracker.imports.preheat.mappers.RelationshipMapper; import org.hisp.dhis.tracker.imports.util.RelationshipKeySupport; @@ -47,9 +47,9 @@ public class DuplicateRelationshipSupplier extends AbstractPreheatSupplier { @Nonnull private final RelationshipStore relationshipStore; @Override - public void preheatAdd(TrackerObjects trackerObjects, TrackerPreheat preheat) { + public void preheatAdd(TrackerImportParams params, TrackerPreheat preheat) { List relationships = - retrieveRelationshipKeys(trackerObjects.getRelationships(), preheat); + retrieveRelationshipKeys(params.getRelationships(), preheat); relationships.stream() .map(RelationshipMapper.INSTANCE::map) diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/EnrollmentSupplier.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/EnrollmentSupplier.java index e26b40d99e51..f8c9338257d0 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/EnrollmentSupplier.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/EnrollmentSupplier.java @@ -36,7 +36,7 @@ import org.hisp.dhis.program.Program; import org.hisp.dhis.program.ProgramStore; import org.hisp.dhis.program.ProgramType; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.hisp.dhis.tracker.imports.preheat.mappers.EnrollmentMapper; import org.springframework.stereotype.Component; @@ -52,7 +52,7 @@ public class EnrollmentSupplier extends AbstractPreheatSupplier { @Nonnull private final ProgramStore programStore; @Override - public void preheatAdd(TrackerObjects trackerObjects, TrackerPreheat preheat) { + public void preheatAdd(TrackerImportParams params, TrackerPreheat preheat) { List programsWithoutRegistration = preheat.getAll(Program.class).stream() .filter(program -> program.getProgramType().equals(ProgramType.WITHOUT_REGISTRATION)) diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/EnrollmentsWithAtLeastOneEventSupplier.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/EnrollmentsWithAtLeastOneEventSupplier.java index f3981a09d4d5..4a0190c49171 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/EnrollmentsWithAtLeastOneEventSupplier.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/EnrollmentsWithAtLeastOneEventSupplier.java @@ -33,7 +33,7 @@ import java.util.stream.Collectors; import org.hisp.dhis.common.IdentifiableObject; import org.hisp.dhis.program.Enrollment; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; @@ -64,7 +64,7 @@ protected EnrollmentsWithAtLeastOneEventSupplier(JdbcTemplate jdbcTemplate) { } @Override - public void preheatAdd(TrackerObjects trackerObjects, TrackerPreheat preheat) { + public void preheatAdd(TrackerImportParams params, TrackerPreheat preheat) { final Map enrollments = preheat.getEnrollments(); List programStageIds = enrollments.values().stream().map(IdentifiableObject::getId).collect(Collectors.toList()); diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/EventCategoryOptionComboSupplier.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/EventCategoryOptionComboSupplier.java index 8d3ce4dd9221..2409d9c2a61d 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/EventCategoryOptionComboSupplier.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/EventCategoryOptionComboSupplier.java @@ -40,9 +40,9 @@ import org.hisp.dhis.category.CategoryService; import org.hisp.dhis.program.Program; import org.hisp.dhis.program.ProgramStage; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.domain.Event; import org.hisp.dhis.tracker.imports.domain.MetadataIdentifier; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.hisp.dhis.tracker.imports.preheat.mappers.CategoryOptionComboMapper; import org.springframework.stereotype.Component; @@ -63,10 +63,10 @@ public class EventCategoryOptionComboSupplier extends AbstractPreheatSupplier { @Nonnull private final CategoryService categoryService; @Override - public void preheatAdd(TrackerObjects trackerObjects, TrackerPreheat preheat) { + public void preheatAdd(TrackerImportParams params, TrackerPreheat preheat) { List>> events = - trackerObjects.getEvents().stream() + params.getEvents().stream() .filter( e -> e.getAttributeOptionCombo().isBlank() diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/EventProgramStageMapSupplier.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/EventProgramStageMapSupplier.java index fa1a2128754d..bdf845453377 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/EventProgramStageMapSupplier.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/EventProgramStageMapSupplier.java @@ -31,8 +31,8 @@ import java.util.Objects; import java.util.stream.Collectors; import org.hisp.dhis.program.ProgramStage; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.domain.Event; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.RowCallbackHandler; @@ -72,13 +72,13 @@ protected EventProgramStageMapSupplier(JdbcTemplate jdbcTemplate) { } @Override - public void preheatAdd(TrackerObjects trackerObjects, TrackerPreheat preheat) { - if (trackerObjects.getEvents().isEmpty()) { + public void preheatAdd(TrackerImportParams params, TrackerPreheat preheat) { + if (params.getEvents().isEmpty()) { return; } List notRepeatableProgramStageUids = - trackerObjects.getEvents().stream() + params.getEvents().stream() .map(Event::getProgramStage) .filter(Objects::nonNull) .map(preheat::getProgramStage) @@ -90,7 +90,7 @@ public void preheatAdd(TrackerObjects trackerObjects, TrackerPreheat preheat) { .collect(Collectors.toList()); List enrollmentUids = - trackerObjects.getEvents().stream() + params.getEvents().stream() .map(Event::getEnrollment) .filter(Objects::nonNull) .distinct() diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/FileResourceSupplier.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/FileResourceSupplier.java index 186716c87d47..6a0cc39b88cd 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/FileResourceSupplier.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/FileResourceSupplier.java @@ -39,10 +39,10 @@ import org.hisp.dhis.trackedentity.TrackedEntityAttribute; import org.hisp.dhis.tracker.imports.TrackerIdSchemeParam; import org.hisp.dhis.tracker.imports.TrackerIdSchemeParams; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.domain.Attribute; import org.hisp.dhis.tracker.imports.domain.DataValue; import org.hisp.dhis.tracker.imports.domain.MetadataIdentifier; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.springframework.stereotype.Component; @@ -55,8 +55,9 @@ public class FileResourceSupplier extends AbstractPreheatSupplier { @Nonnull private final FileResourceService fileResourceService; @Override - public void preheatAdd(TrackerObjects trackerObjects, TrackerPreheat preheat) { - TrackerIdSchemeParams idSchemes = preheat.getIdSchemes(); + public void preheatAdd(TrackerImportParams params, TrackerPreheat preheat) { + TrackerIdSchemeParams idSchemes = params.getIdSchemes(); + List fileResourceAttributes = preheat.getAll(TrackedEntityAttribute.class).stream() .filter(at -> at.getValueType().isFile()) @@ -70,15 +71,15 @@ public void preheatAdd(TrackerObjects trackerObjects, TrackerPreheat preheat) { .collect(Collectors.toList()); List fileResourceIds = new ArrayList<>(); - trackerObjects + params .getTrackedEntities() .forEach( te -> collectResourceIds(fileResourceAttributes, fileResourceIds, te.getAttributes())); - trackerObjects + params .getEnrollments() .forEach( en -> collectResourceIds(fileResourceAttributes, fileResourceIds, en.getAttributes())); - trackerObjects + params .getEvents() .forEach( en -> diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/OrgUnitValueTypeSupplier.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/OrgUnitValueTypeSupplier.java index 9918e4f6bdf6..ef817a04a2da 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/OrgUnitValueTypeSupplier.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/OrgUnitValueTypeSupplier.java @@ -41,10 +41,10 @@ import org.hisp.dhis.trackedentity.TrackedEntityAttribute; import org.hisp.dhis.tracker.imports.TrackerIdSchemeParam; import org.hisp.dhis.tracker.imports.TrackerIdSchemeParams; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.domain.Attribute; import org.hisp.dhis.tracker.imports.domain.DataValue; import org.hisp.dhis.tracker.imports.domain.MetadataIdentifier; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.springframework.stereotype.Component; @@ -57,7 +57,7 @@ public class OrgUnitValueTypeSupplier extends AbstractPreheatSupplier { @Nonnull private final IdentifiableObjectManager manager; @Override - public void preheatAdd(TrackerObjects trackerObjects, TrackerPreheat preheat) { + public void preheatAdd(TrackerImportParams params, TrackerPreheat preheat) { TrackerIdSchemeParams idSchemes = preheat.getIdSchemes(); List orgUnitAttributes = @@ -73,13 +73,13 @@ public void preheatAdd(TrackerObjects trackerObjects, TrackerPreheat preheat) { .collect(Collectors.toList()); List orgUnitIds = new ArrayList<>(); - trackerObjects + params .getTrackedEntities() .forEach(te -> collectResourceIds(orgUnitAttributes, orgUnitIds, te.getAttributes())); - trackerObjects + params .getEnrollments() .forEach(en -> collectResourceIds(orgUnitAttributes, orgUnitIds, en.getAttributes())); - trackerObjects + params .getEvents() .forEach(ev -> collectResourceIds(orgUnitDataElements, orgUnitIds, ev.getDataValues())); diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/PeriodTypeSupplier.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/PeriodTypeSupplier.java index 94ae63808d93..a9d832164f76 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/PeriodTypeSupplier.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/PeriodTypeSupplier.java @@ -35,7 +35,7 @@ import org.hisp.dhis.period.Period; import org.hisp.dhis.period.PeriodStore; import org.hisp.dhis.tracker.imports.TrackerIdSchemeParam; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.hisp.dhis.tracker.imports.preheat.cache.PreheatCacheService; import org.springframework.stereotype.Component; @@ -51,7 +51,7 @@ public class PeriodTypeSupplier extends AbstractPreheatSupplier { @Nonnull private final PreheatCacheService cache; @Override - public void preheatAdd(TrackerObjects trackerObjects, TrackerPreheat preheat) { + public void preheatAdd(TrackerImportParams params, TrackerPreheat preheat) { if (cache.hasKey(Period.class.getName())) { preheat.put(TrackerIdSchemeParam.UID, cache.getAll(Period.class.getName())); } else { diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/PreheatSupplier.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/PreheatSupplier.java index 231addb5deb3..f57986b87bad 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/PreheatSupplier.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/PreheatSupplier.java @@ -28,7 +28,6 @@ package org.hisp.dhis.tracker.imports.preheat.supplier; import org.hisp.dhis.tracker.imports.TrackerImportParams; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; /** @@ -40,8 +39,8 @@ public interface PreheatSupplier { /** * Adds data to the {@link TrackerPreheat} using the supplied {@link TrackerImportParams} * - * @param trackerObjects {@link TrackerObjects} + * @param params {@link TrackerImportParams} * @param preheat {@link TrackerPreheat} */ - void add(TrackerObjects trackerObjects, TrackerPreheat preheat); + void add(TrackerImportParams params, TrackerPreheat preheat); } diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/ProgramOrgUnitsSupplier.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/ProgramOrgUnitsSupplier.java index ee4b26b85a5b..e44c6459bf5b 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/ProgramOrgUnitsSupplier.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/ProgramOrgUnitsSupplier.java @@ -31,7 +31,7 @@ import java.util.stream.Collectors; import org.hisp.dhis.common.IdentifiableObject; import org.hisp.dhis.organisationunit.OrganisationUnit; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.springframework.jdbc.core.JdbcTemplate; import org.springframework.jdbc.core.namedparam.MapSqlParameterSource; @@ -55,7 +55,7 @@ protected ProgramOrgUnitsSupplier(JdbcTemplate jdbcTemplate) { } @Override - public void preheatAdd(TrackerObjects trackerObjects, TrackerPreheat preheat) { + public void preheatAdd(TrackerImportParams params, TrackerPreheat preheat) { // fetch all existing Org Units from payload final List orgUnitIds = preheat.getAll(OrganisationUnit.class).stream() diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/ProgramOwnerSupplier.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/ProgramOwnerSupplier.java index 87c909951e01..7dcd04847c3f 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/ProgramOwnerSupplier.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/ProgramOwnerSupplier.java @@ -38,8 +38,8 @@ import org.hisp.dhis.trackedentity.TrackedEntity; import org.hisp.dhis.trackedentity.TrackedEntityProgramOwnerOrgUnit; import org.hisp.dhis.trackedentity.TrackedEntityProgramOwnerStore; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.domain.Event; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.hisp.dhis.tracker.imports.preheat.mappers.OrganisationUnitMapper; import org.springframework.stereotype.Component; @@ -53,18 +53,18 @@ public class ProgramOwnerSupplier extends AbstractPreheatSupplier { @Nonnull private final TrackedEntityProgramOwnerStore trackedEntityProgramOwnerStore; @Override - public void preheatAdd(TrackerObjects trackerObjects, TrackerPreheat preheat) { + public void preheatAdd(TrackerImportParams params, TrackerPreheat preheat) { final Map preheatedTrackedEntities = preheat.getTrackedEntities(); final Map preheatedEnrollments = preheat.getEnrollments(); Set teIds = new HashSet<>(); - for (org.hisp.dhis.tracker.imports.domain.Enrollment en : trackerObjects.getEnrollments()) { + for (org.hisp.dhis.tracker.imports.domain.Enrollment en : params.getEnrollments()) { TrackedEntity te = preheatedTrackedEntities.get(en.getTrackedEntity()); if (te != null) { teIds.add(te.getId()); } } - for (Event ev : trackerObjects.getEvents()) { + for (Event ev : params.getEvents()) { Enrollment enrollment = preheatedEnrollments.get(ev.getEnrollment()); if (enrollment != null && enrollment.getTrackedEntity() != null) { teIds.add(enrollment.getTrackedEntity().getId()); diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/TrackedEntityEnrollmentSupplier.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/TrackedEntityEnrollmentSupplier.java index 9072523a59e1..3b075fed7f1e 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/TrackedEntityEnrollmentSupplier.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/TrackedEntityEnrollmentSupplier.java @@ -37,7 +37,7 @@ import org.hisp.dhis.program.Enrollment; import org.hisp.dhis.program.Program; import org.hisp.dhis.program.ProgramStatus; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.hisp.dhis.tracker.imports.util.Constant; import org.springframework.jdbc.core.JdbcTemplate; @@ -97,9 +97,9 @@ protected TrackedEntityEnrollmentSupplier(JdbcTemplate jdbcTemplate) { } @Override - public void preheatAdd(TrackerObjects trackerObjects, TrackerPreheat preheat) { + public void preheatAdd(TrackerImportParams params, TrackerPreheat preheat) { List trackedEntityList = - trackerObjects.getEnrollments().stream() + params.getEnrollments().stream() .map(org.hisp.dhis.tracker.imports.domain.Enrollment::getTrackedEntity) .collect(Collectors.toList()); @@ -115,7 +115,7 @@ public void preheatAdd(TrackerObjects trackerObjects, TrackerPreheat preheat) { Map> trackedEntityToEnrollmentMap = new HashMap<>(); - if (trackerObjects.getEnrollments().isEmpty()) return; + if (params.getEnrollments().isEmpty()) return; for (List trackedEntityListSubList : trackedEntities) { queryTeiAndAddToMap(trackedEntityToEnrollmentMap, trackedEntityListSubList, programList); diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/UniqueAttributesSupplier.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/UniqueAttributesSupplier.java index 353bb1888b91..598a38bc0d74 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/UniqueAttributesSupplier.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/UniqueAttributesSupplier.java @@ -52,9 +52,9 @@ import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue; import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueService; import org.hisp.dhis.tracker.imports.TrackerIdSchemeParams; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.domain.Attribute; import org.hisp.dhis.tracker.imports.domain.MetadataIdentifier; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.hisp.dhis.tracker.imports.preheat.UniqueAttributeValue; import org.springframework.stereotype.Component; @@ -74,20 +74,20 @@ public class UniqueAttributesSupplier extends AbstractPreheatSupplier { @Nonnull private final TrackedEntityAttributeValueService trackedEntityAttributeValueService; @Override - public void preheatAdd(TrackerObjects trackerObjects, TrackerPreheat preheat) { + public void preheatAdd(TrackerImportParams params, TrackerPreheat preheat) { List uniqueTrackedEntityAttributes = trackedEntityAttributeService.getAllUniqueTrackedEntityAttributes(); Map> allUniqueAttributesByTrackedEntity = - getAllAttributesByTrackedEntity(trackerObjects, preheat, uniqueTrackedEntityAttributes); + getAllAttributesByTrackedEntity(params, preheat, uniqueTrackedEntityAttributes); List uniqueAttributeValuesFromPayload = getDuplicatedUniqueValuesInPayload(allUniqueAttributesByTrackedEntity); List uniqueAttributeValuesFromDB = getAlreadyPresentInDbUniqueValues( - preheat.getIdSchemes(), + params.getIdSchemes(), allUniqueAttributesByTrackedEntity, uniqueTrackedEntityAttributes); @@ -102,11 +102,11 @@ public void preheatAdd(TrackerObjects trackerObjects, TrackerPreheat preheat) { private Map> getAllAttributesByTrackedEntity( - TrackerObjects trackerObjects, + TrackerImportParams params, TrackerPreheat preheat, List uniqueTrackedEntityAttributes) { Map> teUniqueAttributes = - trackerObjects.getTrackedEntities().stream() + params.getTrackedEntities().stream() .collect( toMap( Function.identity(), @@ -115,10 +115,9 @@ public void preheatAdd(TrackerObjects trackerObjects, TrackerPreheat preheat) { Map> enrollmentUniqueAttributes = - trackerObjects.getEnrollments().stream() + params.getEnrollments().stream() .collect( - groupingBy( - e -> getEntityForEnrollment(trackerObjects, preheat, e.getTrackedEntity()))) + groupingBy(e -> getEntityForEnrollment(params, preheat, e.getTrackedEntity()))) .entrySet() .stream() .collect( @@ -137,12 +136,12 @@ public void preheatAdd(TrackerObjects trackerObjects, TrackerPreheat preheat) { } private org.hisp.dhis.tracker.imports.domain.TrackedEntity getEntityForEnrollment( - TrackerObjects trackerObjects, TrackerPreheat preheat, String teUid) { + TrackerImportParams params, TrackerPreheat preheat, String teUid) { TrackedEntity trackedEntity = preheat.getTrackedEntity(teUid); // Get te from Preheat Optional optionalTe = - trackerObjects.getTrackedEntities().stream() + params.getTrackedEntities().stream() .filter(te -> Objects.equals(te.getTrackedEntity(), teUid)) .findAny(); if (optionalTe.isPresent()) { @@ -153,7 +152,7 @@ private org.hisp.dhis.tracker.imports.domain.TrackedEntity getEntityForEnrollmen new org.hisp.dhis.tracker.imports.domain.TrackedEntity(); te.setTrackedEntity(teUid); te.setOrgUnit( - preheat.getIdSchemes().toMetadataIdentifier(trackedEntity.getOrganisationUnit())); + params.getIdSchemes().toMetadataIdentifier(trackedEntity.getOrganisationUnit())); return te; } else // TE is not present. but we do not fail here. // A validation error will be thrown in validation phase diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/UserSupplier.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/UserSupplier.java index ff98ac18a259..0f119ea5a100 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/UserSupplier.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/UserSupplier.java @@ -37,8 +37,8 @@ import org.apache.commons.lang3.StringUtils; import org.hisp.dhis.common.CodeGenerator; import org.hisp.dhis.common.IdentifiableObjectManager; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.domain.Event; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.domain.User; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.hisp.dhis.tracker.imports.preheat.mappers.UserMapper; @@ -56,9 +56,9 @@ public class UserSupplier extends AbstractPreheatSupplier { @Nonnull private final UserService userService; @Override - public void preheatAdd(TrackerObjects trackerObjects, TrackerPreheat preheat) { + public void preheatAdd(TrackerImportParams params, TrackerPreheat preheat) { Set userUids = - trackerObjects.getEvents().stream() + params.getEvents().stream() .filter(Objects::nonNull) .map(Event::getAssignedUser) .filter(Objects::nonNull) @@ -67,7 +67,7 @@ public void preheatAdd(TrackerObjects trackerObjects, TrackerPreheat preheat) { .collect(Collectors.toSet()); Set usernames = - trackerObjects.getEvents().stream() + params.getEvents().stream() .filter(Objects::nonNull) .map(Event::getAssignedUser) .filter(Objects::nonNull) diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/UsernameValueTypeSupplier.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/UsernameValueTypeSupplier.java index a2523c34631c..0702e56f7c44 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/UsernameValueTypeSupplier.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/UsernameValueTypeSupplier.java @@ -37,9 +37,9 @@ import org.hisp.dhis.common.ValueType; import org.hisp.dhis.trackedentity.TrackedEntityAttribute; import org.hisp.dhis.tracker.imports.TrackerIdSchemeParams; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.domain.Attribute; import org.hisp.dhis.tracker.imports.domain.MetadataIdentifier; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.hisp.dhis.tracker.imports.preheat.mappers.UserMapper; import org.hisp.dhis.user.User; @@ -56,7 +56,7 @@ public class UsernameValueTypeSupplier extends AbstractPreheatSupplier { @Nonnull private final UserService userService; @Override - public void preheatAdd(TrackerObjects trackerObjects, TrackerPreheat preheat) { + public void preheatAdd(TrackerImportParams params, TrackerPreheat preheat) { List attributes = preheat.getAll(TrackedEntityAttribute.class); TrackerIdSchemeParams idSchemes = preheat.getIdSchemes(); @@ -68,10 +68,10 @@ public void preheatAdd(TrackerObjects trackerObjects, TrackerPreheat preheat) { List usernames = new ArrayList<>(); - trackerObjects + params .getTrackedEntities() .forEach(te -> collectResourceIds(usernameAttributes, usernames, te.getAttributes())); - trackerObjects + params .getEnrollments() .forEach(en -> collectResourceIds(usernameAttributes, usernames, en.getAttributes())); diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/strategy/AbstractSchemaStrategy.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/strategy/AbstractSchemaStrategy.java index b33812b83b40..da4e957a096e 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/strategy/AbstractSchemaStrategy.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/strategy/AbstractSchemaStrategy.java @@ -44,6 +44,7 @@ import org.hisp.dhis.schema.SchemaService; import org.hisp.dhis.tracker.imports.TrackerIdScheme; import org.hisp.dhis.tracker.imports.TrackerIdSchemeParam; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.hisp.dhis.tracker.imports.preheat.cache.PreheatCacheService; import org.hisp.dhis.tracker.imports.preheat.mappers.CopyMapper; @@ -78,8 +79,9 @@ protected AbstractSchemaStrategy( } @Override - public void add(List> splitList, TrackerPreheat preheat) { - TrackerIdSchemeParam idSchemeParam = preheat.getIdSchemes().getByClass(getSchemaClass()); + public void add( + TrackerImportParams params, List> splitList, TrackerPreheat preheat) { + TrackerIdSchemeParam idSchemeParam = params.getIdSchemes().getByClass(getSchemaClass()); Schema schema = schemaService.getDynamicSchema(getSchemaClass()); queryForIdentifiableObjects(preheat, schema, idSchemeParam, splitList, mapper()); diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/strategy/ClassBasedSupplierStrategy.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/strategy/ClassBasedSupplierStrategy.java index 9b6811151a03..3bdff20cc475 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/strategy/ClassBasedSupplierStrategy.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/strategy/ClassBasedSupplierStrategy.java @@ -28,11 +28,12 @@ package org.hisp.dhis.tracker.imports.preheat.supplier.strategy; import java.util.List; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; /** * @author Luciano Fiandesio */ public interface ClassBasedSupplierStrategy { - void add(List> splitList, TrackerPreheat preheat); + void add(TrackerImportParams params, List> splitList, TrackerPreheat preheat); } diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/strategy/EnrollmentStrategy.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/strategy/EnrollmentStrategy.java index 03e8ea052440..2875cba5ee7d 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/strategy/EnrollmentStrategy.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/strategy/EnrollmentStrategy.java @@ -32,6 +32,7 @@ import lombok.RequiredArgsConstructor; import org.hisp.dhis.program.Enrollment; import org.hisp.dhis.program.EnrollmentStore; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.hisp.dhis.tracker.imports.preheat.mappers.EnrollmentMapper; import org.hisp.dhis.tracker.imports.preheat.supplier.DetachUtils; @@ -49,7 +50,8 @@ public class EnrollmentStrategy implements ClassBasedSupplierStrategy { @Nonnull private final EnrollmentStore enrollmentStore; @Override - public void add(List> splitList, TrackerPreheat preheat) { + public void add( + TrackerImportParams params, List> splitList, TrackerPreheat preheat) { for (List ids : splitList) { List enrollments = enrollmentStore.getIncludingDeleted(ids); diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/strategy/EventStrategy.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/strategy/EventStrategy.java index aceb75b5833d..5148447256d4 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/strategy/EventStrategy.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/strategy/EventStrategy.java @@ -32,6 +32,7 @@ import lombok.RequiredArgsConstructor; import org.hisp.dhis.program.Event; import org.hisp.dhis.program.EventStore; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.hisp.dhis.tracker.imports.preheat.mappers.EventMapper; import org.hisp.dhis.tracker.imports.preheat.supplier.DetachUtils; @@ -47,7 +48,8 @@ public class EventStrategy implements ClassBasedSupplierStrategy { @Nonnull private final EventStore eventStore; @Override - public void add(List> splitList, TrackerPreheat preheat) { + public void add( + TrackerImportParams params, List> splitList, TrackerPreheat preheat) { for (List ids : splitList) { List events = eventStore.getIncludingDeleted(ids); diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/strategy/NoteStrategy.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/strategy/NoteStrategy.java index 0b6fdcd25595..f0d7f3d971db 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/strategy/NoteStrategy.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/strategy/NoteStrategy.java @@ -32,6 +32,7 @@ import lombok.RequiredArgsConstructor; import org.hisp.dhis.note.Note; import org.hisp.dhis.note.NoteStore; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.hisp.dhis.tracker.imports.preheat.mappers.NoteMapper; import org.hisp.dhis.tracker.imports.preheat.supplier.DetachUtils; @@ -47,7 +48,8 @@ public class NoteStrategy implements ClassBasedSupplierStrategy { @Nonnull private final NoteStore noteStore; @Override - public void add(List> splitList, TrackerPreheat preheat) { + public void add( + TrackerImportParams params, List> splitList, TrackerPreheat preheat) { splitList.forEach( ids -> preheat.putNotes( diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/strategy/RelationshipStrategy.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/strategy/RelationshipStrategy.java index b44a38149515..20902c26b09f 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/strategy/RelationshipStrategy.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/strategy/RelationshipStrategy.java @@ -33,6 +33,7 @@ import lombok.RequiredArgsConstructor; import org.hisp.dhis.common.CodeGenerator; import org.hisp.dhis.relationship.RelationshipStore; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.domain.Relationship; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.hisp.dhis.tracker.imports.preheat.mappers.RelationshipMapper; @@ -49,7 +50,8 @@ public class RelationshipStrategy implements ClassBasedSupplierStrategy { @Nonnull private final RelationshipStore relationshipStore; @Override - public void add(List> splitList, TrackerPreheat preheat) { + public void add( + TrackerImportParams params, List> splitList, TrackerPreheat preheat) { List relationships = retrieveRelationships(splitList); preheat.putRelationships( diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/strategy/TrackerEntityStrategy.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/strategy/TrackerEntityStrategy.java index 182f8645c2cc..27f29f913215 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/strategy/TrackerEntityStrategy.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/preheat/supplier/strategy/TrackerEntityStrategy.java @@ -32,6 +32,7 @@ import lombok.RequiredArgsConstructor; import org.hisp.dhis.trackedentity.TrackedEntity; import org.hisp.dhis.trackedentity.TrackedEntityStore; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.hisp.dhis.tracker.imports.preheat.mappers.TrackedEntityMapper; import org.hisp.dhis.tracker.imports.preheat.supplier.DetachUtils; @@ -49,7 +50,8 @@ public class TrackerEntityStrategy implements ClassBasedSupplierStrategy { @Nonnull private TrackedEntityStore trackedEntityStore; @Override - public void add(List> splitList, TrackerPreheat preheat) { + public void add( + TrackerImportParams params, List> splitList, TrackerPreheat preheat) { for (List ids : splitList) { // Fetch all Tracked Entity present in the payload List trackedEntities = trackedEntityStore.getIncludingDeleted(ids); diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/report/ImportReport.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/report/ImportReport.java index 15670984a5de..d9545c36e13a 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/report/ImportReport.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/report/ImportReport.java @@ -75,6 +75,9 @@ public class ImportReport { */ @JsonProperty Stats stats; + /** A report object containing the elapsed time for each Import stage */ + @JsonProperty TimingsStats timingsStats; + /** A report containing the outcome of the commit stage (e.g. how many entities were persisted) */ @JsonProperty("bundleReport") PersistenceReport persistenceReport; @@ -93,13 +96,15 @@ public class ImportReport { * therefore all bundle objects were ignored. * * @param validationReport The validation report + * @param timingsStats The timing stats * @param bundleSize The sum of all bundle objects */ public static ImportReport withValidationErrors( - ValidationReport validationReport, int bundleSize) { + ValidationReport validationReport, TimingsStats timingsStats, int bundleSize) { return builder() .status(Status.ERROR) .validationReport(validationReport) + .timingsStats(timingsStats) .stats(Stats.builder().ignored(bundleSize).build()) .build(); } @@ -114,12 +119,15 @@ public static ImportReport withValidationErrors( * * @param message The error message * @param validationReport The validation report if available + * @param timingsStats The timing stats if available */ - public static ImportReport withError(String message, ValidationReport validationReport) { + public static ImportReport withError( + String message, ValidationReport validationReport, TimingsStats timingsStats) { // TODO shall we calculate stats in this case? return builder() .status(Status.ERROR) .validationReport(validationReport) + .timingsStats(timingsStats) .message(message) .build(); } @@ -134,6 +142,7 @@ public static ImportReport withError(String message, ValidationReport validation * @param persistenceReport The report containing how many bundle objects were successfully * persisted * @param validationReport The validation report if available + * @param timingsStats The timing stats if available * @param bundleSize a map containing the size of each entity type in the Bundle - before the * validation */ @@ -141,6 +150,7 @@ public static ImportReport withImportCompleted( Status status, PersistenceReport persistenceReport, ValidationReport validationReport, + TimingsStats timingsStats, Map bundleSize) { Stats stats = Stats.builder().build(); Stats brs = persistenceReport.getStats(); @@ -149,6 +159,7 @@ public static ImportReport withImportCompleted( return builder() .status(status) .validationReport(validationReport) + .timingsStats(timingsStats) .persistenceReport(processBundleReport(persistenceReport, bundleSize)) .stats(stats) .build(); diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/report/TimingsStats.java b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/report/TimingsStats.java new file mode 100644 index 000000000000..594ac149f333 --- /dev/null +++ b/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/report/TimingsStats.java @@ -0,0 +1,153 @@ +/* + * Copyright (c) 2004-2022, University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * Neither the name of the HISP project nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.hisp.dhis.tracker.imports.report; + +import com.fasterxml.jackson.annotation.JsonIgnore; +import com.fasterxml.jackson.annotation.JsonProperty; +import java.util.LinkedHashMap; +import java.util.Map; +import java.util.function.Supplier; +import lombok.Data; +import org.hisp.dhis.commons.timer.SystemTimer; +import org.hisp.dhis.commons.timer.Timer; + +/** + * This report keeps track of the elapsed time for each stage of the validation process. + * + * @author Luciano Fiandesio + */ +@Data +public class TimingsStats { + public static final String PREHEAT_OPS = "preheat"; + + public static final String PREPROCESS_OPS = "preprocess"; + + public static final String COMMIT_OPS = "commit"; + + public static final String VALIDATION_OPS = "validation"; + + public static final String PROGRAMRULE_OPS = "programrule"; + + public static final String VALIDATE_PROGRAMRULE_OPS = "programruleValidation"; + + public static final String TOTAL_OPS = "totalImport"; + + public static final String PREPARE_REQUEST_OPS = "prepareRequest"; + + public static final String TOTAL_REQUEST_OPS = "totalRequest"; + + @JsonProperty private Map timers = new LinkedHashMap<>(); + + private static final String DEFAULT_VALUE = "0.0 sec."; + + public String getPrepareRequest() { + return timers.getOrDefault(PREPARE_REQUEST_OPS, DEFAULT_VALUE); + } + + public String getValidation() { + return timers.getOrDefault(VALIDATION_OPS, DEFAULT_VALUE); + } + + public String getCommit() { + return timers.getOrDefault(COMMIT_OPS, DEFAULT_VALUE); + } + + public String getPreheat() { + return timers.getOrDefault(PREHEAT_OPS, DEFAULT_VALUE); + } + + public String getProgramRule() { + return timers.getOrDefault(PROGRAMRULE_OPS, DEFAULT_VALUE); + } + + public String getTotalImport() { + return timers.getOrDefault(TOTAL_OPS, DEFAULT_VALUE); + } + + public String getTotalRequest() { + + return timers.getOrDefault(TOTAL_REQUEST_OPS, DEFAULT_VALUE); + } + + @JsonIgnore private Timer totalTimer; + + public TimingsStats() { + totalTimer = new SystemTimer().start(); + } + + public void set(String timedOperation, String elapsed) { + this.timers.put(timedOperation, elapsed); + } + + /** + * Executes the given Supplier and measure the elapsed time. + * + * @param timedOperation the operation name to place in the timers map + * @param supplier ths Supplier to execute + * @return the result of the Supplier invocation + */ + public T exec(String timedOperation, Supplier supplier) { + Timer timer = new SystemTimer().start(); + + T result = supplier.get(); + + timer.stop(); + + this.set(timedOperation, timer.toString()); + + return result; + } + + /** + * Executes the given operation. + * + * @param timedOperation the operation name to place in the timers map + * @param runnable the operation to execute + */ + public void execVoid(String timedOperation, Runnable runnable) { + Timer timer = new SystemTimer().start(); + + runnable.run(); + + timer.stop(); + + this.set(timedOperation, timer.toString()); + } + + public TimingsStats stopTimer() { + if (totalTimer != null) { + totalTimer.stop(); + this.timers.put(TOTAL_OPS, totalTimer.toString()); + } + return this; + } + + public String get(String validationOps) { + return this.timers.getOrDefault(validationOps, DEFAULT_VALUE); + } +} diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/TrackerIdentifierCollectorTest.java b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/TrackerIdentifierCollectorTest.java index 8fe3e1309cb0..89a605f721e1 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/TrackerIdentifierCollectorTest.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/TrackerIdentifierCollectorTest.java @@ -65,7 +65,6 @@ import org.hisp.dhis.tracker.imports.domain.Relationship; import org.hisp.dhis.tracker.imports.domain.RelationshipItem; import org.hisp.dhis.tracker.imports.domain.TrackedEntity; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -82,6 +81,13 @@ void setUp() { @Test void collectTrackedEntities() { + + TrackerIdSchemeParams idSchemes = + TrackerIdSchemeParams.builder() + .idScheme(TrackerIdSchemeParam.ofAttribute("NTVsGflP5Ix")) + .orgUnitIdScheme(TrackerIdSchemeParam.NAME) + .build(); + TrackedEntity trackedEntity = TrackedEntity.builder() .trackedEntity(uid()) @@ -90,10 +96,10 @@ void collectTrackedEntities() { .attributes(teAttributes("VohJnvWfvyo", "qv9xOw8fBzy")) .build(); - TrackerObjects trackerObjects = - TrackerObjects.builder().trackedEntities(singletonList(trackedEntity)).build(); + TrackerImportParams params = + params(idSchemes).trackedEntities(singletonList(trackedEntity)).build(); - Map, Set> ids = collector.collect(trackerObjects); + Map, Set> ids = collector.collect(params); assertNotNull(ids); assertContainsOnly(Set.of(trackedEntity.getTrackedEntity()), ids.get(TrackedEntity.class)); @@ -104,6 +110,13 @@ void collectTrackedEntities() { @Test void collectEnrollments() { + + TrackerIdSchemeParams idSchemes = + TrackerIdSchemeParams.builder() + .orgUnitIdScheme(TrackerIdSchemeParam.NAME) + .programIdScheme(TrackerIdSchemeParam.ofAttribute("NTVsGflP5Ix")) + .build(); + Enrollment enrollment = Enrollment.builder() .enrollment(uid()) @@ -113,10 +126,9 @@ void collectEnrollments() { .attributes(teAttributes("VohJnvWfvyo", "qv9xOw8fBzy")) .build(); - TrackerObjects trackerObjects = - TrackerObjects.builder().enrollments(singletonList(enrollment)).build(); + TrackerImportParams params = params(idSchemes).enrollments(singletonList(enrollment)).build(); - Map, Set> ids = collector.collect(trackerObjects); + Map, Set> ids = collector.collect(params); assertNotNull(ids); assertContainsOnly(Set.of(enrollment.getUid()), ids.get(Enrollment.class)); @@ -150,9 +162,9 @@ void collectEvents() { .notes(List.of(Note.builder().note("i1vviSlidJE").value("nice day!").build())) .build(); - TrackerObjects trackerObjects = TrackerObjects.builder().events(singletonList(event)).build(); + TrackerImportParams params = params(idSchemes).events(singletonList(event)).build(); - Map, Set> ids = collector.collect(trackerObjects); + Map, Set> ids = collector.collect(params); assertNotNull(ids); assertContainsOnly(Set.of(event.getUid()), ids.get(Event.class)); @@ -170,9 +182,10 @@ void collectEvents() { void collectEventsSkipsNotesWithoutAnId() { Event event = Event.builder().notes(List.of(Note.builder().value("nice day!").build())).build(); - TrackerObjects trackerObjects = TrackerObjects.builder().events(singletonList(event)).build(); + TrackerImportParams params = + params(TrackerIdSchemeParams.builder().build()).events(singletonList(event)).build(); - Map, Set> ids = collector.collect(trackerObjects); + Map, Set> ids = collector.collect(params); assertNotNull(ids); assertNull(ids.get(org.hisp.dhis.note.Note.class)); @@ -183,9 +196,10 @@ void collectEventsSkipsNotesWithoutAValue() { Event event = Event.builder().notes(List.of(Note.builder().note("i1vviSlidJE").build())).build(); - TrackerObjects trackerObjects = TrackerObjects.builder().events(singletonList(event)).build(); + TrackerImportParams params = + params(TrackerIdSchemeParams.builder().build()).events(singletonList(event)).build(); - Map, Set> ids = collector.collect(trackerObjects); + Map, Set> ids = collector.collect(params); assertNotNull(ids); assertNull(ids.get(org.hisp.dhis.note.Note.class)); @@ -208,10 +222,10 @@ void collectRelationships() { .to(RelationshipItem.builder().event(uid()).build()) .build(); - TrackerObjects trackerObjects = - TrackerObjects.builder().relationships(singletonList(relationship)).build(); + TrackerImportParams params = + params(idSchemes).relationships(singletonList(relationship)).build(); - Map, Set> ids = collector.collect(trackerObjects); + Map, Set> ids = collector.collect(params); assertNotNull(ids); assertContainsOnly(Set.of(relationship.getRelationship()), ids.get(Relationship.class)); diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackerImporterServiceTest.java b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackerImporterServiceTest.java index dc32d1c6c9f0..d1b6c5948c67 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackerImporterServiceTest.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackerImporterServiceTest.java @@ -39,14 +39,12 @@ import java.util.List; import java.util.stream.Collectors; import org.hisp.dhis.random.BeanRandomizer; -import org.hisp.dhis.scheduling.NoopJobProgress; import org.hisp.dhis.system.notification.Notifier; import org.hisp.dhis.tracker.imports.DefaultTrackerImportService; import org.hisp.dhis.tracker.imports.ParamsConverter; import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.TrackerUserService; import org.hisp.dhis.tracker.imports.domain.Event; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.preprocess.TrackerPreprocessService; import org.hisp.dhis.tracker.imports.report.PersistenceReport; import org.hisp.dhis.tracker.imports.validation.ValidationResult; @@ -80,26 +78,27 @@ class TrackerImporterServiceTest { private TrackerImportParams params = null; - private TrackerObjects trackerObjects; - private final BeanRandomizer rnd = BeanRandomizer.create(); @BeforeEach public void setUp() { subject = new DefaultTrackerImportService( - trackerBundleService, validationService, trackerPreprocessService, trackerUserService); + trackerBundleService, + validationService, + trackerPreprocessService, + trackerUserService, + notifier); final List events = rnd.objects(Event.class, 3).collect(Collectors.toList()); - params = TrackerImportParams.builder().userId("123").build(); - - trackerObjects = - TrackerObjects.builder() + params = + TrackerImportParams.builder() .events(events) .enrollments(new ArrayList<>()) .relationships(new ArrayList<>()) .trackedEntities(new ArrayList<>()) + .userId("123") .build(); PersistenceReport persistenceReport = PersistenceReport.emptyReport(); @@ -111,26 +110,25 @@ public void setUp() { when(validationService.validateRuleEngine(any(TrackerBundle.class))) .thenReturn(validationResult); when(trackerPreprocessService.preprocess(any(TrackerBundle.class))) - .thenReturn(ParamsConverter.convert(params, trackerObjects, new User())); + .thenReturn(ParamsConverter.convert(params)); } @Test void testSkipSideEffect() { TrackerImportParams parameters = - TrackerImportParams.builder().skipSideEffects(true).userId("123").build(); - - TrackerObjects objects = - TrackerObjects.builder() - .events(trackerObjects.getEvents()) + TrackerImportParams.builder() + .events(params.getEvents()) .enrollments(new ArrayList<>()) .relationships(new ArrayList<>()) .trackedEntities(new ArrayList<>()) + .skipSideEffects(true) + .userId("123") .build(); - when(trackerBundleService.create(any(TrackerImportParams.class), any(), any())) - .thenReturn(ParamsConverter.convert(parameters, objects, new User())); + when(trackerBundleService.create(any(TrackerImportParams.class))) + .thenReturn(ParamsConverter.convert(parameters)); - subject.importTracker(parameters, trackerObjects, NoopJobProgress.INSTANCE); + subject.importTracker(parameters); verify(trackerBundleService, times(0)).handleTrackerSideEffects(anyList()); } @@ -140,10 +138,10 @@ void testWithSideEffects() { doAnswer(invocationOnMock -> null) .when(trackerBundleService) .handleTrackerSideEffects(anyList()); - when(trackerBundleService.create(any(TrackerImportParams.class), any(), any())) - .thenReturn(ParamsConverter.convert(params, trackerObjects, new User())); + when(trackerBundleService.create(any(TrackerImportParams.class))) + .thenReturn(ParamsConverter.convert(params)); - subject.importTracker(params, trackerObjects, NoopJobProgress.INSTANCE); + subject.importTracker(params); verify(trackerBundleService, times(1)).handleTrackerSideEffects(anyList()); } diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/DefaultTrackerPreheatServiceTest.java b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/DefaultTrackerPreheatServiceTest.java index 84455dbeba55..7b317272e009 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/DefaultTrackerPreheatServiceTest.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/DefaultTrackerPreheatServiceTest.java @@ -37,9 +37,8 @@ import java.util.Collections; import java.util.List; import org.hisp.dhis.common.IdentifiableObjectManager; -import org.hisp.dhis.tracker.imports.TrackerIdSchemeParams; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.domain.TrackedEntity; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.preheat.supplier.ClassBasedSupplier; import org.hisp.dhis.tracker.imports.preheat.supplier.PreheatSupplier; import org.hisp.dhis.user.User; @@ -73,13 +72,12 @@ class DefaultTrackerPreheatServiceTest { private DefaultTrackerPreheatService preheatService; - private final TrackerObjects preheatParams = - TrackerObjects.builder() + private final TrackerImportParams preheatParams = + TrackerImportParams.builder() + .user(getUser()) .trackedEntities(Collections.singletonList(new TrackedEntity())) .build(); - private final TrackerIdSchemeParams idSchemeParams = TrackerIdSchemeParams.builder().build(); - @BeforeEach public void setUp() { preheatService = @@ -97,7 +95,7 @@ void shouldGetFromContextAndAdd() { doCallRealMethod().when(classBasedSupplier).add(any(), any()); - preheatService.preheat(preheatParams, idSchemeParams, getUser()); + preheatService.preheat(preheatParams); verify(applicationContext).getBean(bean.getValue(), preheatSupplierClassCaptor.getValue()); verify(classBasedSupplier).add(any(), any()); @@ -109,7 +107,7 @@ void shouldDoNothingWhenSupplierBeanNotFound() { when(applicationContext.getBean(bean.capture(), preheatSupplierClassCaptor.capture())) .thenThrow(new BeanCreationException("e")); - preheatService.preheat(preheatParams, idSchemeParams, getUser()); + preheatService.preheat(preheatParams); verify(applicationContext).getBean(bean.getValue(), preheatSupplierClassCaptor.getValue()); verify(classBasedSupplier, times(0)).add(any(), any()); @@ -122,7 +120,7 @@ void shouldDoNothingWhenAddException() { .thenReturn(classBasedSupplier); doThrow(new RuntimeException("e")).when(classBasedSupplier).add(any(), any()); - preheatService.preheat(preheatParams, idSchemeParams, getUser()); + preheatService.preheat(preheatParams); verify(applicationContext).getBean(bean.getValue(), preheatSupplierClassCaptor.getValue()); verify(classBasedSupplier).add(any(), any()); diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/ClassBasedSupplierTest.java b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/ClassBasedSupplierTest.java index e1b10b5a4f5f..3f0afe9218d1 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/ClassBasedSupplierTest.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/ClassBasedSupplierTest.java @@ -36,8 +36,8 @@ import java.util.HashMap; import java.util.HashSet; import org.hisp.dhis.tracker.imports.TrackerIdentifierCollector; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.domain.TrackedEntity; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.hisp.dhis.tracker.imports.preheat.supplier.strategy.ClassBasedSupplierStrategy; import org.hisp.dhis.tracker.imports.preheat.supplier.strategy.GenericStrategy; @@ -63,7 +63,7 @@ class ClassBasedSupplierTest { @Mock private GenericStrategy genericStrategy; - @Mock private TrackerObjects trackerObjects; + @Mock private TrackerImportParams trackerImportParams; @Mock private TrackerEntityStrategy trackerEntityStrategy; @@ -74,7 +74,7 @@ public void setUp() { classBasedSupplier = new ClassBasedSupplier(identifierCollector, strategiesMap); classBasedSupplier.setApplicationContext(applicationContext); - when(identifierCollector.collect(trackerObjects)) + when(identifierCollector.collect(trackerImportParams)) .thenReturn( new HashMap<>() { { @@ -91,7 +91,7 @@ void verifyGenericStrategy() { when(applicationContext.getBean(Constant.GENERIC_STRATEGY_BEAN, GenericStrategy.class)) .thenReturn(genericStrategy); - classBasedSupplier.preheatAdd(trackerObjects, new TrackerPreheat()); + classBasedSupplier.preheatAdd(trackerImportParams, new TrackerPreheat()); verify(applicationContext).getBean(Constant.GENERIC_STRATEGY_BEAN, GenericStrategy.class); } @@ -102,7 +102,7 @@ void verifyClassBasedSupplierStrategy() { when(applicationContext.getBean(anyString(), eq(ClassBasedSupplierStrategy.class))) .thenReturn(trackerEntityStrategy); - classBasedSupplier.preheatAdd(trackerObjects, new TrackerPreheat()); + classBasedSupplier.preheatAdd(trackerImportParams, new TrackerPreheat()); verify(applicationContext).getBean("classbasedstrategy", ClassBasedSupplierStrategy.class); } diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/DuplicateRelationshipSupplierTest.java b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/DuplicateRelationshipSupplierTest.java index 6f2e07b02199..f9fada079f06 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/DuplicateRelationshipSupplierTest.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/DuplicateRelationshipSupplierTest.java @@ -39,9 +39,9 @@ import org.hisp.dhis.relationship.RelationshipType; import org.hisp.dhis.trackedentity.TrackedEntity; import org.hisp.dhis.tracker.imports.TrackerIdSchemeParam; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.domain.MetadataIdentifier; import org.hisp.dhis.tracker.imports.domain.RelationshipItem; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -148,12 +148,12 @@ void verifySupplier() { when(relationshipStore.getByUid(List.of(REL_A_UID, REL_B_UID))) .thenReturn(List.of(relationshipA(), relationshipB())); - TrackerObjects trackerObjects = - TrackerObjects.builder() + TrackerImportParams trackerImportParams = + TrackerImportParams.builder() .relationships(List.of(relationshipA, relationshipB, relationshipC)) .build(); - supplier.preheatAdd(trackerObjects, preheat); + supplier.preheatAdd(trackerImportParams, preheat); assertTrue(preheat.isDuplicate(relationshipA)); assertFalse(preheat.isDuplicate(invertTeToTeRelationship(relationshipA))); diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/EnrollmentSupplierTest.java b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/EnrollmentSupplierTest.java index e10fbf54cbd2..b04efa42457a 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/EnrollmentSupplierTest.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/EnrollmentSupplierTest.java @@ -43,7 +43,7 @@ import org.hisp.dhis.program.ProgramStore; import org.hisp.dhis.random.BeanRandomizer; import org.hisp.dhis.tracker.imports.TrackerIdSchemeParam; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -73,7 +73,7 @@ class EnrollmentSupplierTest extends DhisConvenienceTest { private Program programWithoutRegistration; - private TrackerObjects params; + private TrackerImportParams params; private final BeanRandomizer rnd = BeanRandomizer.create(); @@ -100,7 +100,7 @@ public void setUp() { when(enrollmentStore.getByPrograms(Lists.newArrayList(programWithoutRegistration))) .thenReturn(enrollments); - params = TrackerObjects.builder().build(); + params = TrackerImportParams.builder().build(); preheat = new TrackerPreheat(); } diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/EventCategoryOptionComboSupplierTest.java b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/EventCategoryOptionComboSupplierTest.java index a251928a66b0..2274ae89f00e 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/EventCategoryOptionComboSupplierTest.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/EventCategoryOptionComboSupplierTest.java @@ -52,9 +52,9 @@ import org.hisp.dhis.program.ProgramStage; import org.hisp.dhis.tracker.imports.TrackerIdSchemeParam; import org.hisp.dhis.tracker.imports.TrackerIdSchemeParams; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.domain.Event; import org.hisp.dhis.tracker.imports.domain.MetadataIdentifier; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -97,10 +97,10 @@ void shouldPreheatEventAOCIfNotProvided() { .attributeCategoryOptions(categoryOptionIds(identifierParams, options)) .build(); List events = List.of(event); - TrackerObjects params = TrackerObjects.builder().events(events).build(); + TrackerImportParams params = + TrackerImportParams.builder().idSchemes(identifierParams).events(events).build(); when(preheat.getProgram(event.getProgram())).thenReturn(program); - when(preheat.getIdSchemes()).thenReturn(identifierParams); options.forEach( o -> when(preheat.getCategoryOption(identifierParams.toMetadataIdentifier(o))) @@ -142,9 +142,9 @@ void shouldPreheatEventAOCIfNotProvidedAndEventHasProgramStageButNoProgram() { .attributeCategoryOptions(categoryOptionIds(identifierParams, options)) .build(); List events = List.of(event); - TrackerObjects params = TrackerObjects.builder().events(events).build(); + TrackerImportParams params = + TrackerImportParams.builder().idSchemes(identifierParams).events(events).build(); - when(preheat.getIdSchemes()).thenReturn(identifierParams); when(preheat.getProgramStage(event.getProgramStage())).thenReturn(stage); options.forEach( o -> @@ -183,9 +183,9 @@ void shouldPreheatEventAOCIfNotProvidedOnlyIfNotAlreadyFetched() { .attributeCategoryOptions(categoryOptionIds(identifierParams, options)) .build(); List events = List.of(event, event); - TrackerObjects params = TrackerObjects.builder().events(events).build(); + TrackerImportParams params = + TrackerImportParams.builder().idSchemes(identifierParams).events(events).build(); - when(preheat.getIdSchemes()).thenReturn(identifierParams); when(preheat.getProgram(event.getProgram())).thenReturn(program); options.forEach( o -> @@ -228,9 +228,9 @@ void shouldPreheatEventAOCEvenIfNotFound() { .attributeCategoryOptions(categoryOptionIds(identifierParams, options)) .build(); List events = List.of(event, event); - TrackerObjects params = TrackerObjects.builder().events(events).build(); + TrackerImportParams params = + TrackerImportParams.builder().idSchemes(identifierParams).events(events).build(); - when(preheat.getIdSchemes()).thenReturn(identifierParams); when(preheat.getProgram(event.getProgram())).thenReturn(program); options.forEach( o -> @@ -271,9 +271,9 @@ void shouldNotPreheatEventAOCIfNotProvidedAndCONotFound() { .attributeCategoryOptions(categoryOptionIds(identifierParams, options)) .build(); List events = List.of(event); - TrackerObjects params = TrackerObjects.builder().events(events).build(); + TrackerImportParams params = + TrackerImportParams.builder().idSchemes(identifierParams).events(events).build(); - when(preheat.getIdSchemes()).thenReturn(identifierParams); when(preheat.getProgram(event.getProgram())).thenReturn(program); when(categoryService.getCategoryOptionCombo(categoryCombo, aoc.getCategoryOptions())) .thenReturn(aoc); @@ -304,9 +304,8 @@ void shouldNotPreheatEventAOCIfEventHasNoProgram() { .programStage(MetadataIdentifier.EMPTY_UID) .build(); List events = List.of(event); - TrackerObjects params = TrackerObjects.builder().events(events).build(); - - when(preheat.getIdSchemes()).thenReturn(identifierParams); + TrackerImportParams params = + TrackerImportParams.builder().idSchemes(identifierParams).events(events).build(); supplier.preheatAdd(params, preheat); @@ -336,9 +335,8 @@ void shouldNotPreheatEventAOCIfEventHasNoProgramAndNoProgramStage() { .programStage(MetadataIdentifier.EMPTY_UID) .build(); List events = List.of(event); - TrackerObjects params = TrackerObjects.builder().events(events).build(); - - when(preheat.getIdSchemes()).thenReturn(identifierParams); + TrackerImportParams params = + TrackerImportParams.builder().idSchemes(identifierParams).events(events).build(); options.forEach( o -> @@ -376,9 +374,9 @@ void shouldNotPreheatEventAOCIfEventHasNoProgramAndItsProgramStageHasNoProgram() .attributeCategoryOptions(categoryOptionIds(identifierParams, options)) .build(); List events = List.of(event); - TrackerObjects params = TrackerObjects.builder().events(events).build(); + TrackerImportParams params = + TrackerImportParams.builder().idSchemes(identifierParams).events(events).build(); - when(preheat.getIdSchemes()).thenReturn(identifierParams); when(preheat.getProgramStage(event.getProgramStage())).thenReturn(stage); options.forEach( o -> @@ -414,9 +412,9 @@ void shouldNotPreheatEventAOCIfAOCAndCOsAreProvided() { .attributeOptionCombo(identifierParams.toMetadataIdentifier(aoc)) .build(); List events = List.of(event); - TrackerObjects params = TrackerObjects.builder().events(events).build(); + TrackerImportParams params = + TrackerImportParams.builder().idSchemes(identifierParams).events(events).build(); - when(preheat.getIdSchemes()).thenReturn(identifierParams); when(preheat.getProgram(event.getProgram())).thenReturn(program); supplier.preheatAdd(params, preheat); @@ -445,9 +443,9 @@ void shouldNotPreheatEventAOCIfAOCIsProvided() { .attributeOptionCombo(identifierParams.toMetadataIdentifier(aoc)) .build(); List events = List.of(event); - TrackerObjects params = TrackerObjects.builder().events(events).build(); + TrackerImportParams params = + TrackerImportParams.builder().idSchemes(identifierParams).events(events).build(); - when(preheat.getIdSchemes()).thenReturn(identifierParams); when(preheat.getProgram(event.getProgram())).thenReturn(program); supplier.preheatAdd(params, preheat); @@ -476,9 +474,9 @@ void shouldNotPreheatEventAOCIfNoCategoryOptionsProvided() { .attributeOptionCombo(identifierParams.toMetadataIdentifier((CategoryOptionCombo) null)) .build(); List events = List.of(event); - TrackerObjects params = TrackerObjects.builder().events(events).build(); + TrackerImportParams params = + TrackerImportParams.builder().idSchemes(identifierParams).events(events).build(); - when(preheat.getIdSchemes()).thenReturn(identifierParams); when(preheat.getProgram(event.getProgram())).thenReturn(program); supplier.preheatAdd(params, preheat); diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/FileResourceSupplierTest.java b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/FileResourceSupplierTest.java index e34306bd2eeb..d5a67a2cb66e 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/FileResourceSupplierTest.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/FileResourceSupplierTest.java @@ -43,13 +43,14 @@ import org.hisp.dhis.fileresource.FileResourceService; import org.hisp.dhis.trackedentity.TrackedEntityAttribute; import org.hisp.dhis.tracker.imports.TrackerIdSchemeParam; +import org.hisp.dhis.tracker.imports.TrackerIdSchemeParams; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.domain.Attribute; import org.hisp.dhis.tracker.imports.domain.DataValue; import org.hisp.dhis.tracker.imports.domain.Enrollment; import org.hisp.dhis.tracker.imports.domain.Event; import org.hisp.dhis.tracker.imports.domain.MetadataIdentifier; import org.hisp.dhis.tracker.imports.domain.TrackedEntity; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -85,8 +86,8 @@ void testSupplierAddsFileResourcesReferencedByTeAttributes() { when(fileResourceService.getFileResources(List.of("kKacJUdANDC"))) .thenReturn(List.of(fileResource)); - TrackerObjects params = - TrackerObjects.builder() + TrackerImportParams params = + params(TrackerIdSchemeParams.builder().build()) .trackedEntities( List.of( trackedEntity(numericAttribute(), fileAttribute("hQKI6KcEu5t", "kKacJUdANDC")))) @@ -101,8 +102,8 @@ void testSupplierAddsFileResourcesReferencedByTeAttributes() { void testSupplierDoesNotAddFileResourceIfTeAttributeValueIsEmpty() { preheat.put(TrackerIdSchemeParam.UID, List.of(teaFileResource("hQKI6KcEu5t"))); - TrackerObjects params = - TrackerObjects.builder() + TrackerImportParams params = + params(TrackerIdSchemeParams.builder().build()) .trackedEntities(List.of(trackedEntity(fileAttribute("hQKI6KcEu5t", "")))) .build(); @@ -121,8 +122,8 @@ void testSupplierAddsFileResourcesReferencedByEnrollmentAttributes() { when(fileResourceService.getFileResources(List.of("kKacJUdANDC"))) .thenReturn(List.of(fileResource)); - TrackerObjects params = - TrackerObjects.builder() + TrackerImportParams params = + params(TrackerIdSchemeParams.builder().build()) .enrollments( List.of( enrollment(numericAttribute(), fileAttribute("hQKI6KcEu5t", "kKacJUdANDC")))) @@ -143,8 +144,8 @@ void testSupplierAddsFileResourcesReferencedByEventDataElement() { when(fileResourceService.getFileResources(List.of("kKacJUdANDC"))) .thenReturn(List.of(fileResource)); - TrackerObjects params = - TrackerObjects.builder() + TrackerImportParams params = + params(TrackerIdSchemeParams.builder().build()) .events( List.of(event(dataValue("numeric", "2"), dataValue("hQKI6KcEu5t", "kKacJUdANDC")))) .build(); @@ -158,8 +159,10 @@ void testSupplierAddsFileResourcesReferencedByEventDataElement() { void testSupplierDoesNotAddFileResourceIfEventDataValueValueIsEmpty() { preheat.put(TrackerIdSchemeParam.UID, List.of(fileDataElement("hQKI6KcEu5t"))); - TrackerObjects params = - TrackerObjects.builder().events(List.of(event(dataValue("hQKI6KcEu5t", "")))).build(); + TrackerImportParams params = + params(TrackerIdSchemeParams.builder().build()) + .events(List.of(event(dataValue("hQKI6KcEu5t", "")))) + .build(); supplier.preheatAdd(params, preheat); @@ -186,6 +189,10 @@ private FileResource fileResource(String uid) { return fileResource; } + private TrackerImportParams.TrackerImportParamsBuilder params(TrackerIdSchemeParams idSchemes) { + return TrackerImportParams.builder().idSchemes(idSchemes); + } + private Attribute numericAttribute() { return Attribute.builder() .attribute(MetadataIdentifier.ofUid("numeric")) diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/OrgUnitValueTypeSupplierTest.java b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/OrgUnitValueTypeSupplierTest.java index 9326f0ea7e24..d887a5538c29 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/OrgUnitValueTypeSupplierTest.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/OrgUnitValueTypeSupplierTest.java @@ -43,13 +43,14 @@ import org.hisp.dhis.organisationunit.OrganisationUnit; import org.hisp.dhis.trackedentity.TrackedEntityAttribute; import org.hisp.dhis.tracker.imports.TrackerIdSchemeParam; +import org.hisp.dhis.tracker.imports.TrackerIdSchemeParams; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.domain.Attribute; import org.hisp.dhis.tracker.imports.domain.DataValue; import org.hisp.dhis.tracker.imports.domain.Enrollment; import org.hisp.dhis.tracker.imports.domain.Event; import org.hisp.dhis.tracker.imports.domain.MetadataIdentifier; import org.hisp.dhis.tracker.imports.domain.TrackedEntity; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -81,8 +82,8 @@ void testSupplierAddsOrgUnitReferencedByTeAttributes() { when(manager.getByUid(OrganisationUnit.class, List.of("kKacJUdANDC"))) .thenReturn(List.of(orgUnit)); - TrackerObjects params = - TrackerObjects.builder() + TrackerImportParams params = + params(TrackerIdSchemeParams.builder().build()) .trackedEntities( List.of( trackedEntity( @@ -98,8 +99,8 @@ void testSupplierAddsOrgUnitReferencedByTeAttributes() { void testSupplierDoesNotAddOrgUnitIfTeAttributeValueIsEmpty() { preheat.put(TrackerIdSchemeParam.UID, List.of(teaOrgUnit("hQKI6KcEu5t"))); - TrackerObjects params = - TrackerObjects.builder() + TrackerImportParams params = + params(TrackerIdSchemeParams.builder().build()) .trackedEntities(List.of(trackedEntity(orgUnitAttribute("hQKI6KcEu5t", "")))) .build(); @@ -117,8 +118,8 @@ void testSupplierAddsOrgUnitReferencedByEnrollmentAttributes() { when(manager.getByUid(OrganisationUnit.class, List.of("kKacJUdANDC"))) .thenReturn(List.of(orgUnit)); - TrackerObjects params = - TrackerObjects.builder() + TrackerImportParams params = + params(TrackerIdSchemeParams.builder().build()) .enrollments( List.of( enrollment(numericAttribute(), orgUnitAttribute("hQKI6KcEu5t", "kKacJUdANDC")))) @@ -139,8 +140,8 @@ void testSupplierAddsOrgUnitReferencedByEventDataElement() { when(manager.getByUid(OrganisationUnit.class, List.of("kKacJUdANDC"))) .thenReturn(List.of(orgUnit)); - TrackerObjects params = - TrackerObjects.builder() + TrackerImportParams params = + params(TrackerIdSchemeParams.builder().build()) .events( List.of(event(dataValue("numeric", "2"), dataValue("hQKI6KcEu5t", "kKacJUdANDC")))) .build(); @@ -154,8 +155,10 @@ void testSupplierAddsOrgUnitReferencedByEventDataElement() { void testSupplierDoesNotAddOrgUnitIfEventDataValueValueIsEmpty() { preheat.put(TrackerIdSchemeParam.UID, List.of(orgUnitDataElement("hQKI6KcEu5t"))); - TrackerObjects params = - TrackerObjects.builder().events(List.of(event(dataValue("hQKI6KcEu5t", "")))).build(); + TrackerImportParams params = + params(TrackerIdSchemeParams.builder().build()) + .events(List.of(event(dataValue("hQKI6KcEu5t", "")))) + .build(); supplier.preheatAdd(params, preheat); @@ -182,6 +185,10 @@ private OrganisationUnit orgUnit(String uid) { return orgUnit; } + private TrackerImportParams.TrackerImportParamsBuilder params(TrackerIdSchemeParams idSchemes) { + return TrackerImportParams.builder().idSchemes(idSchemes); + } + private Attribute numericAttribute() { return Attribute.builder() .attribute(MetadataIdentifier.ofUid("numeric")) diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/PeriodTypeSupplierTest.java b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/PeriodTypeSupplierTest.java index c9c06a56d47e..c2a6cde37893 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/PeriodTypeSupplierTest.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/PeriodTypeSupplierTest.java @@ -36,7 +36,7 @@ import org.hisp.dhis.period.Period; import org.hisp.dhis.period.PeriodStore; import org.hisp.dhis.random.BeanRandomizer; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.hisp.dhis.tracker.imports.preheat.cache.DefaultPreheatCacheService; import org.hisp.dhis.tracker.imports.preheat.cache.PreheatCacheService; @@ -70,7 +70,7 @@ void verifySupplier() { final List periods = rnd.objects(Period.class, 20).collect(Collectors.toList()); when(periodStore.getAll()).thenReturn(periods); - final TrackerObjects params = TrackerObjects.builder().build(); + final TrackerImportParams params = TrackerImportParams.builder().build(); TrackerPreheat preheat = new TrackerPreheat(); this.supplier.preheatAdd(params, preheat); diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/UniqueAttributeSupplierTest.java b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/UniqueAttributeSupplierTest.java index 078b526715ac..99c86ac1f564 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/UniqueAttributeSupplierTest.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/UniqueAttributeSupplierTest.java @@ -49,8 +49,8 @@ import org.hisp.dhis.trackedentity.TrackedEntityAttributeService; import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue; import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueService; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.domain.MetadataIdentifier; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; @@ -77,7 +77,7 @@ class UniqueAttributeSupplierTest extends DhisConvenienceTest { @Mock private TrackedEntityAttributeValueService trackedEntityAttributeValueService; - private TrackerObjects params; + private TrackerImportParams params; private TrackerPreheat preheat; @@ -91,7 +91,7 @@ class UniqueAttributeSupplierTest extends DhisConvenienceTest { @BeforeEach public void setUp() { - params = TrackerObjects.builder().build(); + params = TrackerImportParams.builder().build(); preheat = new TrackerPreheat(); uniqueAttribute = createTrackedEntityAttribute('A', ValueType.TEXT); OrganisationUnit orgUnit = createOrganisationUnit('A'); @@ -121,8 +121,8 @@ void verifySupplierWhenNoUniqueAttributeIsPresentInTheSystem() { void verifySupplierWhenTeAndEnrollmentHaveTheSameUniqueAttribute() { when(trackedEntityAttributeService.getAllUniqueTrackedEntityAttributes()) .thenReturn(Collections.singletonList(uniqueAttribute)); - TrackerObjects importParams = - TrackerObjects.builder() + TrackerImportParams importParams = + TrackerImportParams.builder() .trackedEntities(Collections.singletonList(trackedEntity())) .enrollments(Collections.singletonList(enrollment(TE_UID))) .build(); @@ -136,8 +136,8 @@ void verifySupplierWhenTeAndEnrollmentHaveTheSameUniqueAttribute() { void verifySupplierWhenTwoTesHaveAttributeWithSameUniqueValue() { when(trackedEntityAttributeService.getAllUniqueTrackedEntityAttributes()) .thenReturn(Collections.singletonList(uniqueAttribute)); - TrackerObjects importParams = - TrackerObjects.builder().trackedEntities(sameUniqueAttributeTrackedEntities()).build(); + TrackerImportParams importParams = + TrackerImportParams.builder().trackedEntities(sameUniqueAttributeTrackedEntities()).build(); this.supplier.preheatAdd(importParams, preheat); @@ -148,8 +148,8 @@ void verifySupplierWhenTwoTesHaveAttributeWithSameUniqueValue() { void verifySupplierWhenTeAndEnrollmentFromAnotherTeHaveAttributeWithSameUniqueValue() { when(trackedEntityAttributeService.getAllUniqueTrackedEntityAttributes()) .thenReturn(Collections.singletonList(uniqueAttribute)); - TrackerObjects importParams = - TrackerObjects.builder() + TrackerImportParams importParams = + TrackerImportParams.builder() .trackedEntities(Collections.singletonList(trackedEntity())) .enrollments(Collections.singletonList(enrollment(ANOTHER_TE_UID))) .build(); @@ -169,8 +169,8 @@ void verifySupplierWhenTeinPayloadAndDBHaveTheSameUniqueAttribute() { when(trackedEntityAttributeValueService.getUniqueAttributeByValues( trackedEntityAttributeListMap)) .thenReturn(attributeValues); - TrackerObjects importParams = - TrackerObjects.builder() + TrackerImportParams importParams = + TrackerImportParams.builder() .trackedEntities(Collections.singletonList(trackedEntity())) .build(); @@ -189,8 +189,8 @@ void verifySupplierWhenTeinPayloadAndAnotherTeInDBHaveTheSameUniqueAttribute() { when(trackedEntityAttributeValueService.getUniqueAttributeByValues( trackedEntityAttributeListMap)) .thenReturn(attributeValues); - TrackerObjects importParams = - TrackerObjects.builder() + TrackerImportParams importParams = + TrackerImportParams.builder() .trackedEntities(Collections.singletonList(anotherTrackedEntity())) .build(); diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/UserSupplierTest.java b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/UserSupplierTest.java index 96c9f3b8ab90..40539cd8d544 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/UserSupplierTest.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/UserSupplierTest.java @@ -41,8 +41,8 @@ import org.hisp.dhis.common.CodeGenerator; import org.hisp.dhis.common.IdentifiableObjectManager; import org.hisp.dhis.random.BeanRandomizer; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.domain.Event; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.domain.User; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.hisp.dhis.user.UserService; @@ -97,7 +97,7 @@ void verifyUserSupplierByUid() { when(manager.getByUid(eq(org.hisp.dhis.user.User.class), argThat(t -> t.containsAll(userIds)))) .thenReturn(users); - final TrackerObjects params = TrackerObjects.builder().events(events).build(); + final TrackerImportParams params = TrackerImportParams.builder().events(events).build(); TrackerPreheat preheat = new TrackerPreheat(); this.supplier.preheatAdd(params, preheat); @@ -117,7 +117,7 @@ void verifyUserSupplierByUsername() { when(userService.getUsersByUsernames(argThat(t -> t.containsAll(usernames)))).thenReturn(users); - final TrackerObjects params = TrackerObjects.builder().events(events).build(); + final TrackerImportParams params = TrackerImportParams.builder().events(events).build(); TrackerPreheat preheat = new TrackerPreheat(); this.supplier.preheatAdd(params, preheat); diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/strategy/TrackerEntityStrategyTest.java b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/strategy/TrackerEntityStrategyTest.java index 679a55664aa9..c312d29bdfd7 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/strategy/TrackerEntityStrategyTest.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/preheat/supplier/strategy/TrackerEntityStrategyTest.java @@ -33,6 +33,7 @@ import java.util.List; import org.hisp.dhis.trackedentity.TrackedEntity; import org.hisp.dhis.trackedentity.TrackedEntityStore; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -54,6 +55,11 @@ class TrackerEntityStrategyTest { @Test void verifyStrategyAddRightTeToPreheat() { + final List trackedEntities = + trackedEntities(); + final TrackerImportParams params = + TrackerImportParams.builder().trackedEntities(trackedEntities).build(); + final List uids = List.of("TEIA", "TEIB"); List> splitUids = new ArrayList<>(); @@ -65,7 +71,7 @@ void verifyStrategyAddRightTeToPreheat() { teB.setUid("TEIB"); List dbTrackedEntities = List.of(teA, teB); when(trackedEntityStore.getIncludingDeleted(uids)).thenReturn(dbTrackedEntities); - strategy.add(splitUids, preheat); + strategy.add(params, splitUids, preheat); Mockito.verify(trackedEntityStore).getIncludingDeleted(uids); Mockito.verify(preheat).putTrackedEntities(dbTrackedEntities); diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/report/ImportReportTest.java b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/report/ImportReportTest.java index 7d031bb72fe9..ef6e59e58bef 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/report/ImportReportTest.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/report/ImportReportTest.java @@ -57,6 +57,8 @@ void testImportCalculatesIgnoredValues() { // Create validation report with 3 objects ValidationReport validationReport = ValidationReport.emptyReport(); validationReport.addErrors(rnd.objects(Error.class, 3).collect(Collectors.toList())); + // Create empty Timing Stats report + TimingsStats timingsStats = new TimingsStats(); // Create payload map Map originalPayload = new HashMap<>(); originalPayload.put(TrackerType.TRACKED_ENTITY, 10); @@ -64,7 +66,7 @@ void testImportCalculatesIgnoredValues() { // Method under test ImportReport rep = ImportReport.withImportCompleted( - Status.OK, persistenceReport, validationReport, originalPayload); + Status.OK, persistenceReport, validationReport, timingsStats, originalPayload); assertThat(rep.getStats().getCreated(), is(8)); assertThat(rep.getStats().getUpdated(), is(6)); assertThat(rep.getStats().getIgnored(), is(3)); diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/report/TrackerBundleImportReportTest.java b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/report/TrackerBundleImportReportTest.java index 0ecabc5ed491..9fac55491548 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/report/TrackerBundleImportReportTest.java +++ b/dhis-2/dhis-services/dhis-service-tracker/src/test/java/org/hisp/dhis/tracker/imports/report/TrackerBundleImportReportTest.java @@ -28,9 +28,17 @@ package org.hisp.dhis.tracker.imports.report; import static org.hisp.dhis.tracker.TrackerType.TRACKED_ENTITY; +import static org.hisp.dhis.tracker.imports.report.TimingsStats.COMMIT_OPS; +import static org.hisp.dhis.tracker.imports.report.TimingsStats.PREHEAT_OPS; +import static org.hisp.dhis.tracker.imports.report.TimingsStats.PREPARE_REQUEST_OPS; +import static org.hisp.dhis.tracker.imports.report.TimingsStats.PROGRAMRULE_OPS; +import static org.hisp.dhis.tracker.imports.report.TimingsStats.TOTAL_OPS; +import static org.hisp.dhis.tracker.imports.report.TimingsStats.TOTAL_REQUEST_OPS; +import static org.hisp.dhis.tracker.imports.report.TimingsStats.VALIDATION_OPS; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertNotNull; +import static org.junit.jupiter.api.Assertions.assertNull; import static org.junit.jupiter.api.Assertions.assertTrue; import com.fasterxml.jackson.core.JsonProcessingException; @@ -84,6 +92,7 @@ void testImportReportErrors() { assertNotNull(report.getValidationReport()); assertTrue(report.getValidationReport().hasErrors()); assertFalse(report.getValidationReport().hasWarnings()); + assertNull(report.getTimingsStats()); } @Test @@ -97,6 +106,7 @@ void testImportReportWarnings() { assertNotNull(report.getValidationReport()); assertTrue(report.getValidationReport().hasErrors()); assertTrue(report.getValidationReport().hasWarnings()); + assertNull(report.getTimingsStats()); } @Test @@ -109,6 +119,12 @@ void testImportReportFull() { assertNotNull(report.getValidationReport()); assertTrue(report.getValidationReport().hasErrors()); assertTrue(report.getValidationReport().hasWarnings()); + assertNotNull(report.getTimingsStats()); + assertEquals("1 sec.", report.getTimingsStats().getProgramRule()); + assertEquals("2 sec.", report.getTimingsStats().getCommit()); + assertEquals("3 sec.", report.getTimingsStats().getPreheat()); + assertEquals("4 sec.", report.getTimingsStats().getValidation()); + assertEquals("10 sec.", report.getTimingsStats().getTotalImport()); } @Test @@ -125,7 +141,15 @@ void testSerializingAndDeserializingImportReport() throws JsonProcessingExceptio typeReport.getStats().setDeleted(3); typeReportMap.put(TRACKED_ENTITY, typeReport); PersistenceReport persistenceReport = new PersistenceReport(typeReportMap); - + // Build TimingsStats + TimingsStats timingsStats = new TimingsStats(); + timingsStats.set(COMMIT_OPS, "0.1 sec."); + timingsStats.set(PREHEAT_OPS, "0.2 sec."); + timingsStats.set(PROGRAMRULE_OPS, "0.3 sec."); + timingsStats.set(TOTAL_OPS, "0.4 sec."); + timingsStats.set(VALIDATION_OPS, "4 sec."); + timingsStats.set(PREPARE_REQUEST_OPS, "0.5 sec."); + timingsStats.set(TOTAL_REQUEST_OPS, "0.6 sec."); // Build ValidationReport ValidationReport tvr = ValidationReport.emptyReport(); @@ -147,7 +171,8 @@ void testSerializingAndDeserializingImportReport() throws JsonProcessingExceptio final Map bundleSize = new HashMap<>(); bundleSize.put(TRACKED_ENTITY, 1); ImportReport toSerializeReport = - ImportReport.withImportCompleted(Status.ERROR, persistenceReport, tvr, bundleSize); + ImportReport.withImportCompleted( + Status.ERROR, persistenceReport, tvr, timingsStats, bundleSize); // Serialize TrackerImportReport into String String jsonString = jsonMapper.writeValueAsString(toSerializeReport); // Deserialize the String back into TrackerImportReport @@ -219,7 +244,28 @@ void testSerializingAndDeserializingImportReport() throws JsonProcessingExceptio assertEquals( toSerializeReport.getValidationReport().getWarnings().get(0).getTrackerType(), deserializedReport.getValidationReport().getWarnings().get(0).getTrackerType()); - + // Verify TimingsStats + assertEquals( + toSerializeReport.getTimingsStats().getCommit(), + deserializedReport.getTimingsStats().getCommit()); + assertEquals( + toSerializeReport.getTimingsStats().getPreheat(), + deserializedReport.getTimingsStats().getPreheat()); + assertEquals( + toSerializeReport.getTimingsStats().getPrepareRequest(), + deserializedReport.getTimingsStats().getPrepareRequest()); + assertEquals( + toSerializeReport.getTimingsStats().getProgramRule(), + deserializedReport.getTimingsStats().getProgramRule()); + assertEquals( + toSerializeReport.getTimingsStats().getTotalImport(), + deserializedReport.getTimingsStats().getTotalImport()); + assertEquals( + toSerializeReport.getTimingsStats().getTotalRequest(), + deserializedReport.getTimingsStats().getTotalRequest()); + assertEquals( + toSerializeReport.getTimingsStats().getValidation(), + deserializedReport.getTimingsStats().getValidation()); assertEquals(toSerializeReport.getStats(), deserializedReport.getStats()); } @@ -235,7 +281,17 @@ private ImportReport createImportReport() { final Map bundleSize = new HashMap<>(); bundleSize.put(TRACKED_ENTITY, 1); return ImportReport.withImportCompleted( - Status.OK, createBundleReport(), createValidationReport(), bundleSize); + Status.OK, createBundleReport(), createValidationReport(), createTimingStats(), bundleSize); + } + + private TimingsStats createTimingStats() { + TimingsStats timingsStats = new TimingsStats(); + timingsStats.set(PROGRAMRULE_OPS, "1 sec."); + timingsStats.set(COMMIT_OPS, "2 sec."); + timingsStats.set(PREHEAT_OPS, "3 sec."); + timingsStats.set(VALIDATION_OPS, "4 sec."); + timingsStats.set(TOTAL_OPS, "10 sec."); + return timingsStats; } private ValidationReport createValidationReport() { diff --git a/dhis-2/dhis-test-e2e/src/main/java/org/hisp/dhis/actions/tracker/TrackerImportExportActions.java b/dhis-2/dhis-test-e2e/src/main/java/org/hisp/dhis/actions/tracker/TrackerImportExportActions.java index 47cfb12c81ed..94e394d1fa67 100644 --- a/dhis-2/dhis-test-e2e/src/main/java/org/hisp/dhis/actions/tracker/TrackerImportExportActions.java +++ b/dhis-2/dhis-test-e2e/src/main/java/org/hisp/dhis/actions/tracker/TrackerImportExportActions.java @@ -92,10 +92,6 @@ public TrackerApiResponse postAndGetJobReport( return getJobReportByImportResponse(response); } - public ApiResponse checkJobReport(String jobId, String reportMode) { - return this.get(String.format("/jobs/%s/report?reportMode=%s", jobId, reportMode)); - } - public TrackerApiResponse getJobReport(String jobId, String reportMode) { ApiResponse response = this.get(String.format("/jobs/%s/report?reportMode=%s", jobId, reportMode)); @@ -193,10 +189,6 @@ private TrackerApiResponse getJobReportByImportResponse(ApiResponse response) { this.waitUntilJobIsCompleted(jobId); - Callable jobIsCompleted = () -> this.checkJobReport(jobId, "FULL").statusCode(); - - with().atMost(21, TimeUnit.SECONDS).await().until(() -> jobIsCompleted.call() == 200); - return this.getJobReport(jobId, "FULL"); } } diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/dxf2/deprecated/tracker/AclEventExporterTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/dxf2/deprecated/tracker/AclEventExporterTest.java index f4cc60be6c72..7d9e651125c2 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/dxf2/deprecated/tracker/AclEventExporterTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/dxf2/deprecated/tracker/AclEventExporterTest.java @@ -53,7 +53,6 @@ import org.hisp.dhis.program.Program; import org.hisp.dhis.program.ProgramStage; import org.hisp.dhis.tracker.TrackerTest; -import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.TrackerImportService; import org.hisp.dhis.user.User; import org.junit.jupiter.api.BeforeEach; @@ -76,9 +75,9 @@ class AclEventExporterTest extends TrackerTest { protected void initTest() throws IOException { setUpMetadata("tracker/simple_metadata.json"); User userA = userService.getUser("M5zQapPyTZI"); - TrackerImportParams params = TrackerImportParams.builder().userId(userA.getUid()).build(); assertNoErrors( - trackerImportService.importTracker(params, fromJson("tracker/event_and_enrollment.json"))); + trackerImportService.importTracker( + fromJson("tracker/event_and_enrollment.json", userA.getUid()))); orgUnit = get(OrganisationUnit.class, "h4w96yEMlzO"); ProgramStage programStage = get(ProgramStage.class, "NpsdDv6kKSO"); program = programStage.getProgram(); diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/TrackerTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/TrackerTest.java index fe5f8e89b73b..2e12ed6d11e1 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/TrackerTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/TrackerTest.java @@ -44,7 +44,7 @@ import org.hisp.dhis.render.RenderFormat; import org.hisp.dhis.render.RenderService; import org.hisp.dhis.test.integration.SingleSetupIntegrationTestBase; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.user.CurrentUserService; import org.hisp.dhis.user.User; import org.hisp.dhis.user.UserService; @@ -107,8 +107,26 @@ protected ObjectBundle setUpMetadata(String path, User user) throws IOException return bundle; } - protected TrackerObjects fromJson(String path) throws IOException { + protected TrackerImportParams fromJson(String path) throws IOException { + TrackerImportParams trackerImportParams = _fromJson(path); + trackerImportParams.setUser(currentUserService.getCurrentUser()); + return trackerImportParams; + } + + protected TrackerImportParams fromJson(String path, String userUid) throws IOException { + TrackerImportParams trackerImportParams = _fromJson(path); + trackerImportParams.setUserId(userUid); + return trackerImportParams; + } + + protected TrackerImportParams fromJson(String path, User user) throws IOException { + TrackerImportParams trackerImportParams = _fromJson(path); + trackerImportParams.setUser(user); + return trackerImportParams; + } + + private TrackerImportParams _fromJson(String path) throws IOException { return renderService.fromJson( - new ClassPathResource(path).getInputStream(), TrackerObjects.class); + new ClassPathResource(path).getInputStream(), TrackerImportParams.class); } } diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/export/OrderAndPaginationExporterTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/export/OrderAndPaginationExporterTest.java index 6a6c1fcbf173..28c706005044 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/export/OrderAndPaginationExporterTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/export/OrderAndPaginationExporterTest.java @@ -74,7 +74,6 @@ import org.hisp.dhis.tracker.export.relationship.RelationshipService; import org.hisp.dhis.tracker.export.trackedentity.TrackedEntityOperationParams; import org.hisp.dhis.tracker.export.trackedentity.TrackedEntityService; -import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.TrackerImportService; import org.hisp.dhis.user.User; import org.hisp.dhis.webapi.controller.event.mapper.SortDirection; @@ -111,9 +110,9 @@ class OrderAndPaginationExporterTest extends TrackerTest { protected void initTest() throws IOException { setUpMetadata("tracker/simple_metadata.json"); importUser = userService.getUser("M5zQapPyTZI"); - TrackerImportParams params = TrackerImportParams.builder().userId(importUser.getUid()).build(); assertNoErrors( - trackerImportService.importTracker(params, fromJson("tracker/event_and_enrollment.json"))); + trackerImportService.importTracker( + fromJson("tracker/event_and_enrollment.json", importUser.getUid()))); orgUnit = get(OrganisationUnit.class, "h4w96yEMlzO"); programStage = get(ProgramStage.class, "NpsdDv6kKSO"); trackedEntityType = get(TrackedEntityType.class, "ja8NY4PW7Xm"); diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/export/event/AclEventExporterTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/export/event/AclEventExporterTest.java index edfe77fef37e..c2bd259a0c20 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/export/event/AclEventExporterTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/export/event/AclEventExporterTest.java @@ -59,7 +59,6 @@ import org.hisp.dhis.program.Program; import org.hisp.dhis.program.ProgramStage; import org.hisp.dhis.tracker.TrackerTest; -import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.TrackerImportService; import org.hisp.dhis.user.User; import org.junit.jupiter.api.BeforeEach; @@ -85,9 +84,9 @@ class AclEventExporterTest extends TrackerTest { protected void initTest() throws IOException { setUpMetadata("tracker/simple_metadata.json"); User userA = userService.getUser("M5zQapPyTZI"); - TrackerImportParams params = TrackerImportParams.builder().userId(userA.getUid()).build(); assertNoErrors( - trackerImportService.importTracker(params, fromJson("tracker/event_and_enrollment.json"))); + trackerImportService.importTracker( + fromJson("tracker/event_and_enrollment.json", userA.getUid()))); orgUnit = get(OrganisationUnit.class, "h4w96yEMlzO"); ProgramStage programStage = get(ProgramStage.class, "NpsdDv6kKSO"); program = programStage.getProgram(); diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/export/event/EventExporterTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/export/event/EventExporterTest.java index e99448529462..33ebe1e366ef 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/export/event/EventExporterTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/export/event/EventExporterTest.java @@ -72,7 +72,6 @@ import org.hisp.dhis.relationship.RelationshipItem; import org.hisp.dhis.trackedentity.TrackedEntity; import org.hisp.dhis.tracker.TrackerTest; -import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.TrackerImportService; import org.hisp.dhis.user.User; import org.hisp.dhis.util.DateUtils; @@ -109,9 +108,9 @@ class EventExporterTest extends TrackerTest { protected void initTest() throws IOException { setUpMetadata("tracker/simple_metadata.json"); importUser = userService.getUser("M5zQapPyTZI"); - TrackerImportParams params = TrackerImportParams.builder().userId(importUser.getUid()).build(); assertNoErrors( - trackerImportService.importTracker(params, fromJson("tracker/event_and_enrollment.json"))); + trackerImportService.importTracker( + fromJson("tracker/event_and_enrollment.json", importUser.getUid()))); orgUnit = get(OrganisationUnit.class, "h4w96yEMlzO"); programStage = get(ProgramStage.class, "NpsdDv6kKSO"); program = programStage.getProgram(); diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/AtomicModeIntegrationTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/AtomicModeIntegrationTest.java index 9f6ea3eff68c..da06b0dad95d 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/AtomicModeIntegrationTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/AtomicModeIntegrationTest.java @@ -34,7 +34,6 @@ import java.io.IOException; import org.hisp.dhis.trackedentity.TrackedEntityService; import org.hisp.dhis.tracker.TrackerTest; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.report.ImportReport; import org.hisp.dhis.tracker.imports.report.Status; import org.junit.jupiter.api.Test; @@ -53,12 +52,10 @@ public void initTest() throws IOException { @Test void testImportSuccessWithAtomicModeObjectIfThereIsAnErrorInOneTEI() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/one_valid_tei_and_one_invalid.json"); - TrackerImportParams params = - TrackerImportParams.builder().atomicMode(AtomicMode.OBJECT).build(); + TrackerImportParams params = fromJson("tracker/one_valid_tei_and_one_invalid.json"); + params.setAtomicMode(AtomicMode.OBJECT); - ImportReport trackerImportTeiReport = - trackerImportService.importTracker(params, trackerObjects); + ImportReport trackerImportTeiReport = trackerImportService.importTracker(params); assertNotNull(trackerImportTeiReport); assertEquals(Status.OK, trackerImportTeiReport.getStatus()); @@ -69,11 +66,10 @@ void testImportSuccessWithAtomicModeObjectIfThereIsAnErrorInOneTEI() throws IOEx @Test void testImportFailWithAtomicModeAllIfThereIsAnErrorInOneTEI() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/one_valid_tei_and_one_invalid.json"); - TrackerImportParams params = TrackerImportParams.builder().atomicMode(AtomicMode.ALL).build(); + TrackerImportParams params = fromJson("tracker/one_valid_tei_and_one_invalid.json"); + params.setAtomicMode(AtomicMode.ALL); - ImportReport trackerImportTeiReport = - trackerImportService.importTracker(params, trackerObjects); + ImportReport trackerImportTeiReport = trackerImportService.importTracker(params); assertNotNull(trackerImportTeiReport); assertEquals(Status.ERROR, trackerImportTeiReport.getStatus()); assertEquals(1, trackerImportTeiReport.getValidationReport().getErrors().size()); diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/TrackerImportParamsSerdeTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/TrackerImportParamsSerdeTest.java index 0ae89946dddc..21edc0b6da59 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/TrackerImportParamsSerdeTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/TrackerImportParamsSerdeTest.java @@ -28,6 +28,7 @@ package org.hisp.dhis.tracker.imports; import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.CoreMatchers.nullValue; import static org.hamcrest.MatcherAssert.assertThat; import java.io.IOException; @@ -82,7 +83,12 @@ void testJsonSerialization() throws Exception { + "\"validationMode\":\"SKIP\"," + "\"skipPatternValidation\":false," + "\"skipSideEffects\":false," - + "\"skipRuleEngine\":true}", + + "\"skipRuleEngine\":true," + + "\"trackedEntities\":[]," + + "\"enrollments\":[]," + + "\"events\":[]," + + "\"relationships\":[]," + + "\"username\":\"system-process\"}", json, JSONCompareMode.LENIENT); } @@ -105,7 +111,12 @@ void testJsonDeserialization() throws IOException { + "\"validationMode\":\"SKIP\"," + "\"skipPatternValidation\":true," + "\"skipSideEffects\":true," - + "\"skipRuleEngine\":true}"; + + "\"skipRuleEngine\":true," + + "\"trackedEntities\":[]," + + "\"enrollments\":[]," + + "\"events\":[]," + + "\"relationships\":[]," + + "\"username\":\"system-process\"}"; final TrackerImportParams trackerImportParams = renderService.fromJson(json, TrackerImportParams.class); @@ -118,6 +129,7 @@ void testJsonDeserialization() throws IOException { assertThat(trackerImportParams.isSkipPatternValidation(), is(true)); assertThat(trackerImportParams.isSkipSideEffects(), is(true)); assertThat(trackerImportParams.isSkipRuleEngine(), is(true)); + assertThat(trackerImportParams.getUser(), is(nullValue())); TrackerIdSchemeParams idSchemes = trackerImportParams.getIdSchemes(); assertThat(idSchemes.getIdScheme(), is(TrackerIdSchemeParam.CODE)); assertThat(idSchemes.getProgramIdScheme().getIdScheme(), is(TrackerIdScheme.ATTRIBUTE)); diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/EventDataValueTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/EventDataValueTest.java index 8b5ef6f4e2f1..bd652e88b4df 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/EventDataValueTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/EventDataValueTest.java @@ -45,7 +45,6 @@ import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.TrackerImportService; import org.hisp.dhis.tracker.imports.TrackerImportStrategy; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.report.ImportReport; import org.hisp.dhis.user.User; import org.junit.jupiter.api.Test; @@ -67,11 +66,12 @@ protected void initTest() throws IOException { setUpMetadata("tracker/simple_metadata.json"); final User userA = userService.getUser("M5zQapPyTZI"); injectSecurityContext(userA); - TrackerImportParams params = TrackerImportParams.builder().userId(userA.getUid()).build(); - TrackerObjects trackerObjects = fromJson("tracker/single_tei.json"); - assertNoErrors(trackerImportService.importTracker(params, trackerObjects)); - TrackerObjects enTrackerObjects = fromJson("tracker/single_enrollment.json"); - assertNoErrors(trackerImportService.importTracker(params, enTrackerObjects)); + + TrackerImportParams teiParams = fromJson("tracker/single_tei.json", userA.getUid()); + assertNoErrors(trackerImportService.importTracker(teiParams)); + TrackerImportParams enrollmentParams = + fromJson("tracker/single_enrollment.json", userA.getUid()); + assertNoErrors(trackerImportService.importTracker(enrollmentParams)); manager.flush(); } @@ -79,7 +79,7 @@ protected void initTest() throws IOException { void successWhenEventHasNoProgramAndHasProgramStage() throws IOException { ImportReport importReport = trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/validations/events-with_no_program.json")); + fromJson("tracker/validations/events-with_no_program.json")); assertNoErrors(importReport); } @@ -87,8 +87,7 @@ void successWhenEventHasNoProgramAndHasProgramStage() throws IOException { @Test void testEventDataValue() throws IOException { ImportReport importReport = - trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/event_with_data_values.json")); + trackerImportService.importTracker(fromJson("tracker/event_with_data_values.json")); assertNoErrors(importReport); List events = manager.getAll(Event.class); @@ -100,9 +99,8 @@ void testEventDataValue() throws IOException { @Test void testEventDataValueUpdate() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/event_with_data_values.json"); - TrackerImportParams params = new TrackerImportParams(); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + ImportReport importReport = + trackerImportService.importTracker(fromJson("tracker/event_with_data_values.json")); assertNoErrors(importReport); List events = manager.getAll(Event.class); @@ -111,10 +109,17 @@ void testEventDataValueUpdate() throws IOException { Set eventDataValues = event.getEventDataValues(); assertEquals(4, eventDataValues.size()); // update - - TrackerObjects updatedTrackerObjects = fromJson("tracker/event_with_updated_data_values.json"); - params.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE); - importReport = trackerImportService.importTracker(params, updatedTrackerObjects); + TrackerImportParams trackerImportParams = + fromJson("tracker/event_with_updated_data_values.json"); + // make sure that the uid property is populated as well - otherwise + // update will + // not work + trackerImportParams + .getEvents() + .get(0) + .setEvent(trackerImportParams.getEvents().get(0).getEvent()); + trackerImportParams.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE); + importReport = trackerImportService.importTracker(trackerImportParams); assertNoErrors(importReport); List updatedEvents = manager.getAll(Event.class); assertEquals(1, updatedEvents.size()); diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/LastUpdateImportTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/LastUpdateImportTest.java index e04fe595b412..af021c5f645b 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/LastUpdateImportTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/LastUpdateImportTest.java @@ -44,7 +44,6 @@ import org.hisp.dhis.tracker.imports.domain.Enrollment; import org.hisp.dhis.tracker.imports.domain.EnrollmentStatus; import org.hisp.dhis.tracker.imports.domain.MetadataIdentifier; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.report.ImportReport; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -62,19 +61,16 @@ class LastUpdateImportTest extends TrackerTest { protected void initTest() throws IOException { setUpMetadata("tracker/simple_metadata.json"); injectAdminUser(); - TrackerObjects trackerObjects = fromJson("tracker/single_tei.json"); - assertNoErrors(trackerImportService.importTracker(new TrackerImportParams(), trackerObjects)); - trackedEntity = trackerObjects.getTrackedEntities().get(0); - assertNoErrors( - trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/single_enrollment.json"))); + TrackerImportParams trackerImportParams = fromJson("tracker/single_tei.json"); + assertNoErrors(trackerImportService.importTracker(trackerImportParams)); + trackedEntity = trackerImportParams.getTrackedEntities().get(0); + assertNoErrors(trackerImportService.importTracker(fromJson("tracker/single_enrollment.json"))); manager.flush(); } @Test void shouldUpdateTeiIfTeiIsUpdated() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/single_tei.json"); - TrackerImportParams trackerImportParams = new TrackerImportParams(); + TrackerImportParams trackerImportParams = fromJson("tracker/single_tei.json"); trackerImportParams.setImportStrategy(TrackerImportStrategy.UPDATE); Attribute attribute = Attribute.builder() @@ -84,7 +80,7 @@ void shouldUpdateTeiIfTeiIsUpdated() throws IOException { trackedEntity.setAttributes(Collections.singletonList(attribute)); Date lastUpdateBefore = trackedEntityService.getTrackedEntity(trackedEntity.getTrackedEntity()).getLastUpdated(); - assertNoErrors(trackerImportService.importTracker(trackerImportParams, trackerObjects)); + assertNoErrors(trackerImportService.importTracker(trackerImportParams)); assertTrue( manager .get(TrackedEntity.class, trackedEntity.getTrackedEntity()) @@ -95,15 +91,14 @@ void shouldUpdateTeiIfTeiIsUpdated() throws IOException { @Test void shouldUpdateTeiIfEventIsUpdated() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/event_with_data_values.json"); - TrackerImportParams trackerImportParams = new TrackerImportParams(); + TrackerImportParams trackerImportParams = fromJson("tracker/event_with_data_values.json"); Date lastUpdateBefore = trackedEntityService.getTrackedEntity(trackedEntity.getTrackedEntity()).getLastUpdated(); - assertNoErrors(trackerImportService.importTracker(trackerImportParams, trackerObjects)); + assertNoErrors(trackerImportService.importTracker(trackerImportParams)); - trackerObjects = fromJson("tracker/event_with_updated_data_values.json"); + trackerImportParams = fromJson("tracker/event_with_updated_data_values.json"); trackerImportParams.setImportStrategy(TrackerImportStrategy.UPDATE); - assertNoErrors(trackerImportService.importTracker(trackerImportParams, trackerObjects)); + assertNoErrors(trackerImportService.importTracker(trackerImportParams)); manager.clear(); assertTrue( manager @@ -115,15 +110,13 @@ void shouldUpdateTeiIfEventIsUpdated() throws IOException { @Test void shouldUpdateTeiIfEnrollmentIsUpdated() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/single_enrollment.json"); - TrackerImportParams trackerImportParams = new TrackerImportParams(); + TrackerImportParams trackerImportParams = fromJson("tracker/single_enrollment.json"); Date lastUpdateBefore = trackedEntityService.getTrackedEntity(trackedEntity.getTrackedEntity()).getLastUpdated(); - Enrollment enrollment = trackerObjects.getEnrollments().get(0); + Enrollment enrollment = trackerImportParams.getEnrollments().get(0); enrollment.setStatus(EnrollmentStatus.COMPLETED); trackerImportParams.setImportStrategy(TrackerImportStrategy.UPDATE); - ImportReport importReport = - trackerImportService.importTracker(trackerImportParams, trackerObjects); + ImportReport importReport = trackerImportService.importTracker(trackerImportParams); assertNoErrors(importReport); manager.clear(); assertTrue( diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/OwnershipTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/OwnershipTest.java index 017ca502bac7..9c2517c0ad18 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/OwnershipTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/OwnershipTest.java @@ -52,7 +52,6 @@ import org.hisp.dhis.tracker.imports.TrackerImportService; import org.hisp.dhis.tracker.imports.TrackerImportStrategy; import org.hisp.dhis.tracker.imports.domain.EnrollmentStatus; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.report.ImportReport; import org.hisp.dhis.tracker.imports.validation.ValidationCode; import org.hisp.dhis.user.User; @@ -81,16 +80,18 @@ protected void initTest() throws IOException { injectSecurityContext(superUser); nonSuperUser = userService.getUser("Tu9fv8ezgHl"); - TrackerImportParams params = TrackerImportParams.builder().userId(superUser.getUid()).build(); assertNoErrors( - trackerImportService.importTracker(params, fromJson("tracker/ownership_tei.json"))); + trackerImportService.importTracker( + fromJson("tracker/ownership_tei.json", superUser.getUid()))); assertNoErrors( - trackerImportService.importTracker(params, fromJson("tracker/ownership_enrollment.json"))); + trackerImportService.importTracker( + fromJson("tracker/ownership_enrollment.json", superUser.getUid()))); } @Test void testProgramOwnerWhenEnrolled() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/ownership_enrollment.json"); + TrackerImportParams enrollmentParams = + fromJson("tracker/ownership_enrollment.json", nonSuperUser.getUid()); List trackedEntities = manager.getAll(TrackedEntity.class); @@ -103,11 +104,15 @@ void testProgramOwnerWhenEnrolled() throws IOException { assertNotNull(tepo.getTrackedEntity()); assertNotNull(tepo.getProgram()); assertNotNull(tepo.getOrganisationUnit()); - assertTrue(trackerObjects.getEnrollments().get(0).getProgram().isEqualTo(tepo.getProgram())); + assertTrue(enrollmentParams.getEnrollments().get(0).getProgram().isEqualTo(tepo.getProgram())); assertTrue( - trackerObjects.getEnrollments().get(0).getOrgUnit().isEqualTo(tepo.getOrganisationUnit())); + enrollmentParams + .getEnrollments() + .get(0) + .getOrgUnit() + .isEqualTo(tepo.getOrganisationUnit())); assertEquals( - trackerObjects.getEnrollments().get(0).getTrackedEntity(), + enrollmentParams.getEnrollments().get(0).getTrackedEntity(), tepo.getTrackedEntity().getUid()); } @@ -115,13 +120,14 @@ void testProgramOwnerWhenEnrolled() throws IOException { void testClientDatesForTeiEnrollmentEvent() throws IOException { User nonSuperUser = userService.getUser(this.nonSuperUser.getUid()); injectSecurityContext(nonSuperUser); - TrackerImportParams params = - TrackerImportParams.builder().userId(nonSuperUser.getUid()).build(); - TrackerObjects trackerObjects = fromJson("tracker/ownership_event.json"); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + + TrackerImportParams trackerImportParams = + fromJson("tracker/ownership_event.json", nonSuperUser); + ImportReport importReport = trackerImportService.importTracker(trackerImportParams); manager.flush(); - TrackerObjects teTrackerObjects = fromJson("tracker/ownership_tei.json"); - TrackerObjects enTrackerObjects = fromJson("tracker/ownership_enrollment.json"); + TrackerImportParams teParams = fromJson("tracker/ownership_tei.json", nonSuperUser); + TrackerImportParams enrollmentParams = + fromJson("tracker/ownership_enrollment.json", nonSuperUser); assertNoErrors(importReport); List trackedEntities = manager.getAll(TrackedEntity.class); @@ -130,10 +136,10 @@ void testClientDatesForTeiEnrollmentEvent() throws IOException { assertNotNull(te.getCreatedAtClient()); assertNotNull(te.getLastUpdatedAtClient()); assertEquals( - DateUtils.fromInstant(teTrackerObjects.getTrackedEntities().get(0).getCreatedAtClient()), + DateUtils.fromInstant(teParams.getTrackedEntities().get(0).getCreatedAtClient()), te.getCreatedAtClient()); assertEquals( - DateUtils.fromInstant(teTrackerObjects.getTrackedEntities().get(0).getUpdatedAtClient()), + DateUtils.fromInstant(teParams.getTrackedEntities().get(0).getUpdatedAtClient()), te.getLastUpdatedAtClient()); Set enrollments = te.getEnrollments(); assertEquals(1, enrollments.size()); @@ -141,10 +147,10 @@ void testClientDatesForTeiEnrollmentEvent() throws IOException { assertNotNull(enrollment.getCreatedAtClient()); assertNotNull(enrollment.getLastUpdatedAtClient()); assertEquals( - DateUtils.fromInstant(enTrackerObjects.getEnrollments().get(0).getCreatedAtClient()), + DateUtils.fromInstant(enrollmentParams.getEnrollments().get(0).getCreatedAtClient()), enrollment.getCreatedAtClient()); assertEquals( - DateUtils.fromInstant(enTrackerObjects.getEnrollments().get(0).getUpdatedAtClient()), + DateUtils.fromInstant(enrollmentParams.getEnrollments().get(0).getUpdatedAtClient()), enrollment.getLastUpdatedAtClient()); Set events = enrollment.getEvents(); assertEquals(1, events.size()); @@ -152,35 +158,34 @@ void testClientDatesForTeiEnrollmentEvent() throws IOException { assertNotNull(event.getCreatedAtClient()); assertNotNull(event.getLastUpdatedAtClient()); assertEquals( - DateUtils.fromInstant(trackerObjects.getEvents().get(0).getCreatedAtClient()), + DateUtils.fromInstant(trackerImportParams.getEvents().get(0).getCreatedAtClient()), event.getCreatedAtClient()); assertEquals( - DateUtils.fromInstant(trackerObjects.getEvents().get(0).getUpdatedAtClient()), + DateUtils.fromInstant(trackerImportParams.getEvents().get(0).getUpdatedAtClient()), event.getLastUpdatedAtClient()); } @Test void testUpdateEnrollment() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/ownership_enrollment.json"); - TrackerImportParams params = - TrackerImportParams.builder().userId(nonSuperUser.getUid()).build(); + TrackerImportParams enrollmentParams = + fromJson("tracker/ownership_enrollment.json", nonSuperUser.getUid()); List enrollments = manager.getAll(Enrollment.class); assertEquals(2, enrollments.size()); Enrollment enrollment = enrollments.stream().filter(e -> e.getUid().equals("TvctPPhpD8u")).findAny().get(); - compareEnrollmentBasicProperties(enrollment, trackerObjects.getEnrollments().get(0)); + compareEnrollmentBasicProperties(enrollment, enrollmentParams.getEnrollments().get(0)); assertNull(enrollment.getCompletedBy()); assertNull(enrollment.getCompletedDate()); org.hisp.dhis.tracker.imports.domain.Enrollment updatedEnrollment = - trackerObjects.getEnrollments().get(0); + enrollmentParams.getEnrollments().get(0); updatedEnrollment.setStatus(EnrollmentStatus.COMPLETED); updatedEnrollment.setCreatedAtClient(Instant.now()); updatedEnrollment.setUpdatedAtClient(Instant.now()); updatedEnrollment.setEnrolledAt(Instant.now()); updatedEnrollment.setOccurredAt(Instant.now()); - params.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE); - ImportReport updatedReport = trackerImportService.importTracker(params, trackerObjects); + enrollmentParams.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE); + ImportReport updatedReport = trackerImportService.importTracker(enrollmentParams); manager.flush(); assertNoErrors(updatedReport); assertEquals(1, updatedReport.getStats().getUpdated()); @@ -194,14 +199,13 @@ void testUpdateEnrollment() throws IOException { @Test void testDeleteEnrollment() throws IOException { - TrackerImportParams params = - TrackerImportParams.builder().userId(nonSuperUser.getUid()).build(); - TrackerObjects trackerObjects = fromJson("tracker/ownership_enrollment.json"); + TrackerImportParams enrollmentParams = + fromJson("tracker/ownership_enrollment.json", nonSuperUser.getUid()); List enrollments = manager.getAll(Enrollment.class); assertEquals(2, enrollments.size()); enrollments.stream().filter(e -> e.getUid().equals("TvctPPhpD8u")).findAny().get(); - params.setImportStrategy(TrackerImportStrategy.DELETE); - ImportReport updatedReport = trackerImportService.importTracker(params, trackerObjects); + enrollmentParams.setImportStrategy(TrackerImportStrategy.DELETE); + ImportReport updatedReport = trackerImportService.importTracker(enrollmentParams); assertNoErrors(updatedReport); assertEquals(1, updatedReport.getStats().getDeleted()); enrollments = manager.getAll(Enrollment.class); @@ -211,20 +215,19 @@ void testDeleteEnrollment() throws IOException { @Test void testCreateEnrollmentAfterDeleteEnrollment() throws IOException { injectSecurityContext(userService.getUser(nonSuperUser.getUid())); - TrackerImportParams params = - TrackerImportParams.builder().userId(nonSuperUser.getUid()).build(); - TrackerObjects trackerObjects = fromJson("tracker/ownership_enrollment.json"); + TrackerImportParams enrollmentParams = + fromJson("tracker/ownership_enrollment.json", nonSuperUser.getUid()); List enrollments = manager.getAll(Enrollment.class); assertEquals(2, enrollments.size()); - params.setImportStrategy(TrackerImportStrategy.DELETE); - ImportReport updatedReport = trackerImportService.importTracker(params, trackerObjects); + enrollmentParams.setImportStrategy(TrackerImportStrategy.DELETE); + ImportReport updatedReport = trackerImportService.importTracker(enrollmentParams); assertNoErrors(updatedReport); assertEquals(1, updatedReport.getStats().getDeleted()); enrollments = manager.getAll(Enrollment.class); assertEquals(1, enrollments.size()); - params.setImportStrategy(TrackerImportStrategy.CREATE); - trackerObjects.getEnrollments().get(0).setEnrollment(CodeGenerator.generateUid()); - updatedReport = trackerImportService.importTracker(params, trackerObjects); + enrollmentParams.setImportStrategy(TrackerImportStrategy.CREATE); + enrollmentParams.getEnrollments().get(0).setEnrollment(CodeGenerator.generateUid()); + updatedReport = trackerImportService.importTracker(enrollmentParams); assertNoErrors(updatedReport); assertEquals(1, updatedReport.getStats().getCreated()); enrollments = manager.getAll(Enrollment.class); @@ -234,22 +237,21 @@ void testCreateEnrollmentAfterDeleteEnrollment() throws IOException { @Test void testCreateEnrollmentWithoutOwnership() throws IOException { injectSecurityContext(userService.getUser(nonSuperUser.getUid())); - TrackerImportParams params = - TrackerImportParams.builder().userId(nonSuperUser.getUid()).build(); - TrackerObjects trackerObjects = fromJson("tracker/ownership_enrollment.json"); + TrackerImportParams enrollmentParams = + fromJson("tracker/ownership_enrollment.json", nonSuperUser); List enrollments = manager.getAll(Enrollment.class); assertEquals(2, enrollments.size()); - params.setImportStrategy(TrackerImportStrategy.DELETE); - ImportReport updatedReport = trackerImportService.importTracker(params, trackerObjects); + enrollmentParams.setImportStrategy(TrackerImportStrategy.DELETE); + ImportReport updatedReport = trackerImportService.importTracker(enrollmentParams); assertNoErrors(updatedReport); assertEquals(1, updatedReport.getStats().getDeleted()); TrackedEntity te = manager.get(TrackedEntity.class, "IOR1AXXl24H"); OrganisationUnit ou = manager.get(OrganisationUnit.class, "B1nCbRV3qtP"); Program pgm = manager.get(Program.class, "BFcipDERJnf"); trackerOwnershipManager.transferOwnership(te, pgm, ou, true, false); - params.setImportStrategy(TrackerImportStrategy.CREATE); - trackerObjects.getEnrollments().get(0).setEnrollment(CodeGenerator.generateUid()); - updatedReport = trackerImportService.importTracker(params, trackerObjects); + enrollmentParams.setImportStrategy(TrackerImportStrategy.CREATE); + enrollmentParams.getEnrollments().get(0).setEnrollment(CodeGenerator.generateUid()); + updatedReport = trackerImportService.importTracker(enrollmentParams); assertEquals(1, updatedReport.getStats().getIgnored()); assertHasError(updatedReport, ValidationCode.E1102); } @@ -260,12 +262,11 @@ void testDeleteEnrollmentWithoutOwnership() throws IOException { TrackedEntity te = manager.get(TrackedEntity.class, "IOR1AXXl24H"); OrganisationUnit ou = manager.get(OrganisationUnit.class, "B1nCbRV3qtP"); Program pgm = manager.get(Program.class, "BFcipDERJnf"); - TrackerImportParams params = - TrackerImportParams.builder().userId(nonSuperUser.getUid()).build(); - TrackerObjects trackerObjects = fromJson("tracker/ownership_enrollment.json"); + TrackerImportParams enrollmentParams = + fromJson("tracker/ownership_enrollment.json", nonSuperUser.getUid()); trackerOwnershipManager.transferOwnership(te, pgm, ou, true, false); - params.setImportStrategy(TrackerImportStrategy.DELETE); - ImportReport updatedReport = trackerImportService.importTracker(params, trackerObjects); + enrollmentParams.setImportStrategy(TrackerImportStrategy.DELETE); + ImportReport updatedReport = trackerImportService.importTracker(enrollmentParams); assertEquals(1, updatedReport.getStats().getIgnored()); assertHasError(updatedReport, ValidationCode.E1102); } @@ -277,11 +278,10 @@ void testUpdateEnrollmentWithoutOwnership() throws IOException { OrganisationUnit ou = manager.get(OrganisationUnit.class, "B1nCbRV3qtP"); Program pgm = manager.get(Program.class, "BFcipDERJnf"); trackerOwnershipManager.transferOwnership(te, pgm, ou, true, false); - TrackerImportParams params = - TrackerImportParams.builder().userId(nonSuperUser.getUid()).build(); - TrackerObjects trackerObjects = fromJson("tracker/ownership_enrollment.json"); - params.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE); - ImportReport updatedReport = trackerImportService.importTracker(params, trackerObjects); + TrackerImportParams enrollmentParams = + fromJson("tracker/ownership_enrollment.json", nonSuperUser.getUid()); + enrollmentParams.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE); + ImportReport updatedReport = trackerImportService.importTracker(enrollmentParams); assertEquals(1, updatedReport.getStats().getIgnored()); assertHasError(updatedReport, ValidationCode.E1102); } diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/RelationshipImportTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/RelationshipImportTest.java index c617089cb10c..fb8daad5335f 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/RelationshipImportTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/RelationshipImportTest.java @@ -37,7 +37,6 @@ import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.TrackerImportService; import org.hisp.dhis.tracker.imports.TrackerImportStrategy; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.report.ImportReport; import org.hisp.dhis.tracker.imports.report.Status; import org.hisp.dhis.user.User; @@ -59,33 +58,31 @@ class RelationshipImportTest extends TrackerTest { protected void initTest() throws IOException { setUpMetadata("tracker/simple_metadata.json"); userA = userService.getUser("M5zQapPyTZI"); - TrackerImportParams params = TrackerImportParams.builder().userId(userA.getUid()).build(); - assertNoErrors(trackerImportService.importTracker(params, fromJson("tracker/single_tei.json"))); assertNoErrors( - trackerImportService.importTracker(params, fromJson("tracker/single_enrollment.json"))); + trackerImportService.importTracker(fromJson("tracker/single_tei.json", userA.getUid()))); assertNoErrors( - trackerImportService.importTracker(params, fromJson("tracker/single_event.json"))); + trackerImportService.importTracker( + fromJson("tracker/single_enrollment.json", userA.getUid()))); + assertNoErrors( + trackerImportService.importTracker(fromJson("tracker/single_event.json", userA.getUid()))); manager.flush(); } @Test void successImportingRelationships() throws IOException { - ImportReport importReport = - trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/relationships.json")); + TrackerImportParams trackerImportParams = fromJson("tracker/relationships.json"); + ImportReport importReport = trackerImportService.importTracker(trackerImportParams); assertThat(importReport.getStatus(), is(Status.OK)); assertThat(importReport.getStats().getCreated(), is(2)); } @Test void successUpdateRelationships() throws IOException { - TrackerImportParams trackerImportParams = new TrackerImportParams(); - TrackerObjects trackerObjects = fromJson("tracker/relationships.json"); - trackerImportService.importTracker(trackerImportParams, trackerObjects); - trackerObjects = fromJson("tracker/relationshipToUpdate.json"); + TrackerImportParams trackerImportParams = fromJson("tracker/relationships.json"); + trackerImportService.importTracker(trackerImportParams); + trackerImportParams = fromJson("tracker/relationshipToUpdate.json"); trackerImportParams.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE); - ImportReport importReport = - trackerImportService.importTracker(trackerImportParams, trackerObjects); + ImportReport importReport = trackerImportService.importTracker(trackerImportParams); assertThat(importReport.getStatus(), is(Status.OK)); assertThat(importReport.getStats().getCreated(), is(0)); assertThat(importReport.getStats().getIgnored(), is(1)); diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/ReportSummaryDeleteIntegrationTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/ReportSummaryDeleteIntegrationTest.java index b3e5a208b125..ed4ca445cb27 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/ReportSummaryDeleteIntegrationTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/ReportSummaryDeleteIntegrationTest.java @@ -41,7 +41,6 @@ import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.TrackerImportService; import org.hisp.dhis.tracker.imports.TrackerImportStrategy; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.report.ImportReport; import org.hisp.dhis.tracker.imports.report.PersistenceReport; import org.hisp.dhis.tracker.imports.validation.ValidationCode; @@ -59,13 +58,12 @@ class ReportSummaryDeleteIntegrationTest extends TrackerTest { protected void initTest() throws IOException { setUpMetadata("tracker/tracker_basic_metadata.json"); injectAdminUser(); - TrackerObjects trackerObjects = fromJson("tracker/tracker_basic_data_before_deletion.json"); - assertEquals(13, trackerObjects.getTrackedEntities().size()); - assertEquals(2, trackerObjects.getEnrollments().size()); - assertEquals(2, trackerObjects.getEvents().size()); + TrackerImportParams params = fromJson("tracker/tracker_basic_data_before_deletion.json"); + assertEquals(13, params.getTrackedEntities().size()); + assertEquals(2, params.getEnrollments().size()); + assertEquals(2, params.getEvents().size()); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); PersistenceReport persistenceReport = importReport.getPersistenceReport(); assertImportedObjects(13, persistenceReport, TrackerType.TRACKED_ENTITY); @@ -79,12 +77,11 @@ protected void initTest() throws IOException { @Test void testTrackedEntityDeletion() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/tracked_entity_basic_data_for_deletion.json"); - TrackerImportParams params = new TrackerImportParams(); + TrackerImportParams params = fromJson("tracker/tracked_entity_basic_data_for_deletion.json"); params.setImportStrategy(TrackerImportStrategy.DELETE); - assertEquals(9, trackerObjects.getTrackedEntities().size()); + assertEquals(9, params.getTrackedEntities().size()); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertNoErrors(importReport); assertDeletedObjects(9, importReport.getPersistenceReport(), TrackerType.TRACKED_ENTITY); // remaining @@ -96,11 +93,10 @@ void testTrackedEntityDeletion() throws IOException { void testEnrollmentDeletion() throws IOException { dbmsManager.clearSession(); assertEquals(2, manager.getAll(Event.class).size()); - TrackerImportParams params = new TrackerImportParams(); - TrackerObjects trackerObjects = fromJson("tracker/enrollment_basic_data_for_deletion.json"); + TrackerImportParams params = fromJson("tracker/enrollment_basic_data_for_deletion.json"); params.setImportStrategy(TrackerImportStrategy.DELETE); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertNoErrors(importReport); assertDeletedObjects(1, importReport.getPersistenceReport(), TrackerType.ENROLLMENT); @@ -112,11 +108,10 @@ void testEnrollmentDeletion() throws IOException { @Test void testEventDeletion() throws IOException { - TrackerImportParams params = new TrackerImportParams(); - TrackerObjects trackerObjects = fromJson("tracker/event_basic_data_for_deletion.json"); + TrackerImportParams params = fromJson("tracker/event_basic_data_for_deletion.json"); params.setImportStrategy(TrackerImportStrategy.DELETE); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertNoErrors(importReport); assertDeletedObjects(1, importReport.getPersistenceReport(), TrackerType.EVENT); @@ -126,23 +121,21 @@ void testEventDeletion() throws IOException { @Test void testNonExistentEnrollment() throws IOException { - TrackerImportParams params = new TrackerImportParams(); - TrackerObjects trackerObjects = + TrackerImportParams params = fromJson("tracker/non_existent_enrollment_basic_data_for_deletion.json"); params.setImportStrategy(TrackerImportStrategy.DELETE); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertHasError(importReport, ValidationCode.E1081); } @Test void testDeleteMultipleEntities() throws IOException { - TrackerImportParams params = new TrackerImportParams(); - TrackerObjects trackerObjects = fromJson("tracker/tracker_data_for_deletion.json"); + TrackerImportParams params = fromJson("tracker/tracker_data_for_deletion.json"); params.setImportStrategy(TrackerImportStrategy.DELETE); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertNoErrors(importReport); assertDeletedObjects(1, importReport.getPersistenceReport(), TrackerType.ENROLLMENT); diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/ReportSummaryIntegrationTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/ReportSummaryIntegrationTest.java index c0c7e4575eb4..3f2221e5af24 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/ReportSummaryIntegrationTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/ReportSummaryIntegrationTest.java @@ -37,7 +37,6 @@ import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.TrackerImportService; import org.hisp.dhis.tracker.imports.TrackerImportStrategy; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.report.ImportReport; import org.hisp.dhis.tracker.imports.report.Status; import org.hisp.dhis.user.User; @@ -60,12 +59,11 @@ protected void initTest() throws IOException { @Test void testStatsCountForOneCreatedTEI() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/single_tei.json"); - TrackerImportParams params = - TrackerImportParams.builder().atomicMode(AtomicMode.OBJECT).userId(userA.getUid()).build(); + TrackerImportParams params = fromJson("tracker/single_tei.json"); + params.setUserId(userA.getUid()); + params.setAtomicMode(AtomicMode.OBJECT); - ImportReport trackerImportTeiReport = - trackerImportService.importTracker(params, trackerObjects); + ImportReport trackerImportTeiReport = trackerImportService.importTracker(params); assertNoErrors(trackerImportTeiReport); assertEquals(1, trackerImportTeiReport.getStats().getCreated()); @@ -76,19 +74,16 @@ void testStatsCountForOneCreatedTEI() throws IOException { @Test void testStatsCountForOneCreatedAndOneUpdatedTEI() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/single_tei.json"); - TrackerImportParams params = - TrackerImportParams.builder().atomicMode(AtomicMode.OBJECT).userId(userA.getUid()).build(); + TrackerImportParams params = fromJson("tracker/single_tei.json"); params.setUserId(userA.getUid()); - trackerImportService.importTracker(params, trackerObjects); + trackerImportService.importTracker(params); - trackerObjects = fromJson("tracker/one_update_tei_and_one_new_tei.json"); + params = fromJson("tracker/one_update_tei_and_one_new_tei.json"); params.setUserId(userA.getUid()); params.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE); - ImportReport trackerImportTeiReport = - trackerImportService.importTracker(params, trackerObjects); + ImportReport trackerImportTeiReport = trackerImportService.importTracker(params); assertNoErrors(trackerImportTeiReport); assertEquals(1, trackerImportTeiReport.getStats().getCreated()); @@ -99,20 +94,17 @@ void testStatsCountForOneCreatedAndOneUpdatedTEI() throws IOException { @Test void testStatsCountForOneCreatedAndOneUpdatedTEIAndOneInvalidTEI() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/single_tei.json"); - TrackerImportParams params = - TrackerImportParams.builder().atomicMode(AtomicMode.OBJECT).userId(userA.getUid()).build(); + TrackerImportParams params = fromJson("tracker/single_tei.json"); params.setUserId(userA.getUid()); - trackerImportService.importTracker(params, trackerObjects); + trackerImportService.importTracker(params); - trackerObjects = fromJson("tracker/one_update_tei_and_one_new_tei_and_one_invalid_tei.json"); + params = fromJson("tracker/one_update_tei_and_one_new_tei_and_one_invalid_tei.json"); params.setUserId(userA.getUid()); params.setAtomicMode(AtomicMode.OBJECT); params.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE); - ImportReport trackerImportTeiReport = - trackerImportService.importTracker(params, trackerObjects); + ImportReport trackerImportTeiReport = trackerImportService.importTracker(params); assertNotNull(trackerImportTeiReport); assertEquals(Status.OK, trackerImportTeiReport.getStatus()); @@ -125,19 +117,16 @@ void testStatsCountForOneCreatedAndOneUpdatedTEIAndOneInvalidTEI() throws IOExce @Test void testStatsCountForOneCreatedEnrollment() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/single_tei.json"); - TrackerImportParams params = - TrackerImportParams.builder().atomicMode(AtomicMode.OBJECT).userId(userA.getUid()).build(); + TrackerImportParams params = fromJson("tracker/single_tei.json"); params.setUserId(userA.getUid()); - trackerImportService.importTracker(params, trackerObjects); + trackerImportService.importTracker(params); - trackerObjects = fromJson("tracker/single_enrollment.json"); + params = fromJson("tracker/single_enrollment.json"); params.setUserId(userA.getUid()); params.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE); - ImportReport trackerImportEnrollmentReport = - trackerImportService.importTracker(params, trackerObjects); + ImportReport trackerImportEnrollmentReport = trackerImportService.importTracker(params); assertNoErrors(trackerImportEnrollmentReport); assertEquals(1, trackerImportEnrollmentReport.getStats().getCreated()); @@ -148,19 +137,16 @@ void testStatsCountForOneCreatedEnrollment() throws IOException { @Test void testStatsCountForOneCreatedEnrollmentAndUpdateSameEnrollment() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/single_tei.json"); - TrackerImportParams params = - TrackerImportParams.builder().atomicMode(AtomicMode.OBJECT).userId(userA.getUid()).build(); + TrackerImportParams params = fromJson("tracker/single_tei.json"); params.setUserId(userA.getUid()); - trackerImportService.importTracker(params, trackerObjects); + trackerImportService.importTracker(params); - trackerObjects = fromJson("tracker/single_enrollment.json"); + params = fromJson("tracker/single_enrollment.json"); params.setUserId(userA.getUid()); params.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE); - ImportReport trackerImportEnrollmentReport = - trackerImportService.importTracker(params, trackerObjects); + ImportReport trackerImportEnrollmentReport = trackerImportService.importTracker(params); assertNoErrors(trackerImportEnrollmentReport); assertEquals(1, trackerImportEnrollmentReport.getStats().getCreated()); @@ -168,11 +154,11 @@ void testStatsCountForOneCreatedEnrollmentAndUpdateSameEnrollment() throws IOExc assertEquals(0, trackerImportEnrollmentReport.getStats().getIgnored()); assertEquals(0, trackerImportEnrollmentReport.getStats().getDeleted()); - trackerObjects = fromJson("tracker/single_enrollment.json"); + params = fromJson("tracker/single_enrollment.json"); params.setUserId(userA.getUid()); params.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE); - trackerImportEnrollmentReport = trackerImportService.importTracker(params, trackerObjects); + trackerImportEnrollmentReport = trackerImportService.importTracker(params); assertNoErrors(trackerImportEnrollmentReport); assertEquals(0, trackerImportEnrollmentReport.getStats().getCreated()); @@ -183,24 +169,21 @@ void testStatsCountForOneCreatedEnrollmentAndUpdateSameEnrollment() throws IOExc @Test void testStatsCountForOneUpdateEnrollmentAndOneCreatedEnrollment() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/one_update_tei_and_one_new_tei.json"); - TrackerImportParams params = - TrackerImportParams.builder().atomicMode(AtomicMode.OBJECT).userId(userA.getUid()).build(); + TrackerImportParams params = fromJson("tracker/one_update_tei_and_one_new_tei.json"); params.setUserId(userA.getUid()); - trackerImportService.importTracker(params, trackerObjects); + trackerImportService.importTracker(params); - trackerObjects = fromJson("tracker/single_enrollment.json"); + params = fromJson("tracker/single_enrollment.json"); params.setUserId(userA.getUid()); - trackerImportService.importTracker(params, trackerObjects); + trackerImportService.importTracker(params); - trackerObjects = fromJson("tracker/one_update_enrollment_and_one_new_enrollment.json"); + params = fromJson("tracker/one_update_enrollment_and_one_new_enrollment.json"); params.setUserId(userA.getUid()); params.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE); - ImportReport trackerImportEnrollmentReport = - trackerImportService.importTracker(params, trackerObjects); + ImportReport trackerImportEnrollmentReport = trackerImportService.importTracker(params); assertNoErrors(trackerImportEnrollmentReport); assertEquals(1, trackerImportEnrollmentReport.getStats().getCreated()); @@ -212,26 +195,23 @@ void testStatsCountForOneUpdateEnrollmentAndOneCreatedEnrollment() throws IOExce @Test void testStatsCountForOneUpdateEnrollmentAndOneCreatedEnrollmentAndOneInvalidEnrollment() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/three_teis.json"); - TrackerImportParams params = - TrackerImportParams.builder().atomicMode(AtomicMode.OBJECT).userId(userA.getUid()).build(); + TrackerImportParams params = fromJson("tracker/three_teis.json"); params.setUserId(userA.getUid()); - trackerImportService.importTracker(params, trackerObjects); + trackerImportService.importTracker(params); - trackerObjects = fromJson("tracker/single_enrollment.json"); + params = fromJson("tracker/single_enrollment.json"); params.setUserId(userA.getUid()); - trackerImportService.importTracker(params, trackerObjects); + trackerImportService.importTracker(params); - trackerObjects = fromJson("tracker/one_update_and_one_new_and_one_invalid_enrollment.json"); + params = fromJson("tracker/one_update_and_one_new_and_one_invalid_enrollment.json"); params.setUserId(userA.getUid()); params.setAtomicMode(AtomicMode.OBJECT); params.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE); - ImportReport trackerImportEnrollmentReport = - trackerImportService.importTracker(params, trackerObjects); + ImportReport trackerImportEnrollmentReport = trackerImportService.importTracker(params); assertNotNull(trackerImportEnrollmentReport); assertEquals(Status.OK, trackerImportEnrollmentReport.getStatus()); @@ -244,23 +224,20 @@ void testStatsCountForOneUpdateEnrollmentAndOneCreatedEnrollmentAndOneInvalidEnr @Test void testStatsCountForOneCreatedEvent() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/single_tei.json"); - TrackerImportParams params = - TrackerImportParams.builder().atomicMode(AtomicMode.OBJECT).userId(userA.getUid()).build(); + TrackerImportParams params = fromJson("tracker/single_tei.json"); params.setUserId(userA.getUid()); - trackerImportService.importTracker(params, trackerObjects); + trackerImportService.importTracker(params); - trackerObjects = fromJson("tracker/single_enrollment.json"); + params = fromJson("tracker/single_enrollment.json"); params.setUserId(userA.getUid()); - trackerImportService.importTracker(params, trackerObjects); + trackerImportService.importTracker(params); - trackerObjects = fromJson("tracker/single_event.json"); + params = fromJson("tracker/single_event.json"); params.setUserId(userA.getUid()); - ImportReport trackerImportEventReport = - trackerImportService.importTracker(params, trackerObjects); + ImportReport trackerImportEventReport = trackerImportService.importTracker(params); assertNoErrors(trackerImportEventReport); assertEquals(1, trackerImportEventReport.getStats().getCreated()); @@ -271,29 +248,26 @@ void testStatsCountForOneCreatedEvent() throws IOException { @Test void testStatsCountForOneUpdateEventAndOneNewEvent() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/single_tei.json"); - TrackerImportParams params = - TrackerImportParams.builder().atomicMode(AtomicMode.OBJECT).userId(userA.getUid()).build(); + TrackerImportParams params = fromJson("tracker/single_tei.json"); params.setUserId(userA.getUid()); - trackerImportService.importTracker(params, trackerObjects); + trackerImportService.importTracker(params); - trackerObjects = fromJson("tracker/single_enrollment.json"); + params = fromJson("tracker/single_enrollment.json"); params.setUserId(userA.getUid()); - trackerImportService.importTracker(params, trackerObjects); + trackerImportService.importTracker(params); - trackerObjects = fromJson("tracker/single_event.json"); + params = fromJson("tracker/single_event.json"); params.setUserId(userA.getUid()); - trackerImportService.importTracker(params, trackerObjects); + trackerImportService.importTracker(params); - trackerObjects = fromJson("tracker/one_update_event_and_one_new_event.json"); + params = fromJson("tracker/one_update_event_and_one_new_event.json"); params.setUserId(userA.getUid()); params.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE); - ImportReport trackerImportEventReport = - trackerImportService.importTracker(params, trackerObjects); + ImportReport trackerImportEventReport = trackerImportService.importTracker(params); assertNoErrors(trackerImportEventReport); assertEquals(1, trackerImportEventReport.getStats().getCreated()); @@ -304,30 +278,27 @@ void testStatsCountForOneUpdateEventAndOneNewEvent() throws IOException { @Test void testStatsCountForOneUpdateEventAndOneNewEventAndOneInvalidEvent() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/single_tei.json"); - TrackerImportParams params = - TrackerImportParams.builder().atomicMode(AtomicMode.OBJECT).userId(userA.getUid()).build(); + TrackerImportParams params = fromJson("tracker/single_tei.json"); params.setUserId(userA.getUid()); - trackerImportService.importTracker(params, trackerObjects); + trackerImportService.importTracker(params); - trackerObjects = fromJson("tracker/single_enrollment.json"); + params = fromJson("tracker/single_enrollment.json"); params.setUserId(userA.getUid()); - trackerImportService.importTracker(params, trackerObjects); + trackerImportService.importTracker(params); - trackerObjects = fromJson("tracker/single_event.json"); + params = fromJson("tracker/single_event.json"); params.setUserId(userA.getUid()); - trackerImportService.importTracker(params, trackerObjects); + trackerImportService.importTracker(params); - trackerObjects = fromJson("tracker/one_update_and_one_new_and_one_invalid_event.json"); + params = fromJson("tracker/one_update_and_one_new_and_one_invalid_event.json"); params.setUserId(userA.getUid()); params.setAtomicMode(AtomicMode.OBJECT); params.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE); - ImportReport trackerImportEventReport = - trackerImportService.importTracker(params, trackerObjects); + ImportReport trackerImportEventReport = trackerImportService.importTracker(params); assertNotNull(trackerImportEventReport); assertEquals(Status.OK, trackerImportEventReport.getStatus()); diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackedEntityAttributeTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackedEntityAttributeTest.java index b5c6e904bbf9..b5d2f85398d7 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackedEntityAttributeTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackedEntityAttributeTest.java @@ -41,10 +41,8 @@ import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue; import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueService; import org.hisp.dhis.tracker.TrackerTest; -import org.hisp.dhis.tracker.imports.TrackerIdSchemeParams; import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.TrackerImportService; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheatService; import org.hisp.dhis.tracker.imports.report.ImportReport; @@ -72,10 +70,8 @@ protected void initTest() throws IOException { @Test void testTrackedAttributePreheater() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/te_with_tea_data.json"); - TrackerPreheat preheat = - trackerPreheatService.preheat( - trackerObjects, new TrackerIdSchemeParams(), userService.getUser(ADMIN_USER_UID)); + TrackerImportParams trackerImportParams = fromJson("tracker/te_with_tea_data.json"); + TrackerPreheat preheat = trackerPreheatService.preheat(trackerImportParams); assertNotNull(preheat.get(OrganisationUnit.class, "cNEZTkdAvmg")); assertNotNull(preheat.get(TrackedEntityType.class, "KrYIdvLxkMb")); assertNotNull(preheat.get(TrackedEntityAttribute.class, "sYn3tkL3XKa")); @@ -85,9 +81,8 @@ void testTrackedAttributePreheater() throws IOException { @Test void testTrackedAttributeValueBundleImporter() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/te_with_tea_data.json"); - TrackerImportParams params = TrackerImportParams.builder().build(); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + ImportReport importReport = + trackerImportService.importTracker(fromJson("tracker/te_with_tea_data.json")); assertNoErrors(importReport); List trackedEntities = manager.getAll(TrackedEntity.class); diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackedEntityAttributeValueAuditTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackedEntityAttributeValueAuditTest.java index dd59653ea493..bbc7722dc71a 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackedEntityAttributeValueAuditTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackedEntityAttributeValueAuditTest.java @@ -46,7 +46,6 @@ import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.TrackerImportService; import org.hisp.dhis.tracker.imports.TrackerImportStrategy; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.report.ImportReport; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -72,8 +71,7 @@ protected void initTest() throws IOException { @Test void testTrackedEntityAttributeValueAuditCreate() throws IOException { assertNoErrors( - trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/te_program_with_tea_data.json"))); + trackerImportService.importTracker(fromJson("tracker/te_program_with_tea_data.json"))); List trackedEntities = manager.getAll(TrackedEntity.class); assertEquals(1, trackedEntities.size()); @@ -97,10 +95,9 @@ void testTrackedEntityAttributeValueAuditCreate() throws IOException { @Test void testTrackedEntityAttributeValueAuditDelete() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/te_program_with_tea_data.json"); + TrackerImportParams trackerImportParams = fromJson("tracker/te_program_with_tea_data.json"); - TrackerImportParams params = new TrackerImportParams(); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + ImportReport importReport = trackerImportService.importTracker(trackerImportParams); assertNoErrors(importReport); List attributeValues1 = trackedEntityAttributeValueService.getTrackedEntityAttributeValues( @@ -109,9 +106,9 @@ void testTrackedEntityAttributeValueAuditDelete() throws IOException { attributeValues1.stream() .map(TrackedEntityAttributeValue::getAttribute) .collect(Collectors.toList()); - trackerObjects = fromJson("tracker/te_program_with_tea_null_data.json"); - params.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE); - importReport = trackerImportService.importTracker(params, trackerObjects); + trackerImportParams = fromJson("tracker/te_program_with_tea_null_data.json"); + trackerImportParams.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE); + importReport = trackerImportService.importTracker(trackerImportParams); assertNoErrors(importReport); List trackedEntities = manager.getAll(TrackedEntity.class); List attributeValueAudits = diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackedEntityDataValueAuditTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackedEntityDataValueAuditTest.java index a90f2ee01e9a..fd8935c6ea60 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackedEntityDataValueAuditTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackedEntityDataValueAuditTest.java @@ -43,7 +43,6 @@ import org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValueAudit; import org.hisp.dhis.trackedentitydatavalue.TrackedEntityDataValueAuditService; import org.hisp.dhis.tracker.TrackerTest; -import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.TrackerImportService; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -78,16 +77,15 @@ protected void initTest() throws IOException { @Test void testTrackedEntityDataValueAuditCreate() throws IOException { - TrackerImportParams params = new TrackerImportParams(); assertNoErrors( trackerImportService.importTracker( - params, fromJson("tracker/event_and_enrollment_with_data_values.json"))); + fromJson("tracker/event_and_enrollment_with_data_values.json"))); assertNoErrors( trackerImportService.importTracker( - params, fromJson("tracker/event_with_data_values_for_update_audit.json"))); + fromJson("tracker/event_with_data_values_for_update_audit.json"))); assertNoErrors( trackerImportService.importTracker( - params, fromJson("tracker/event_with_data_values_for_delete_audit.json"))); + fromJson("tracker/event_with_data_values_for_delete_audit.json"))); dataElement = manager.search(DataElement.class, DE); event = manager.search(Event.class, PSI); diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackedEntityProgramAttributeEncryptionTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackedEntityProgramAttributeEncryptionTest.java index 3c3c8dee2e8e..875ff64b62b5 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackedEntityProgramAttributeEncryptionTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackedEntityProgramAttributeEncryptionTest.java @@ -38,7 +38,6 @@ import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue; import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueService; import org.hisp.dhis.tracker.TrackerTest; -import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.TrackerImportService; import org.hisp.dhis.tracker.imports.report.ImportReport; import org.junit.jupiter.api.Test; @@ -69,7 +68,6 @@ protected void initTest() throws IOException { void testTrackedEntityProgramAttributeEncryptedValue() throws IOException { ImportReport importReport = trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/te_program_with_tea_encryption_data.json")); assertNoErrors(importReport); diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackedEntityProgramAttributeFileResourceTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackedEntityProgramAttributeFileResourceTest.java index 5338a1ddaee6..825b17d073e2 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackedEntityProgramAttributeFileResourceTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackedEntityProgramAttributeFileResourceTest.java @@ -43,7 +43,6 @@ import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue; import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValueService; import org.hisp.dhis.tracker.TrackerTest; -import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.TrackerImportService; import org.hisp.dhis.tracker.imports.report.ImportReport; import org.junit.jupiter.api.Test; @@ -83,7 +82,6 @@ void testTrackedEntityProgramAttributeFileResourceValue() throws IOException { assertFalse(fileResource.isAssigned()); ImportReport importReport = trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/te_program_with_tea_fileresource_data.json")); assertNoErrors(importReport); diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackedEntityProgramAttributeTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackedEntityProgramAttributeTest.java index 2a0d63d8c3d4..1bd7a6209887 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackedEntityProgramAttributeTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackedEntityProgramAttributeTest.java @@ -40,7 +40,6 @@ import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.TrackerImportService; import org.hisp.dhis.tracker.imports.TrackerImportStrategy; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.report.ImportReport; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -64,9 +63,8 @@ protected void initTest() throws IOException { @Test void testTrackedEntityProgramAttributeValue() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/te_program_with_tea_data.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + TrackerImportParams trackerImportParams = fromJson("tracker/te_program_with_tea_data.json"); + ImportReport importReport = trackerImportService.importTracker(trackerImportParams); assertNoErrors(importReport); List trackedEntities = manager.getAll(TrackedEntity.class); @@ -79,9 +77,8 @@ void testTrackedEntityProgramAttributeValue() throws IOException { @Test void testTrackedEntityProgramAttributeValueUpdate() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/te_program_with_tea_data.json"); - TrackerImportParams params = new TrackerImportParams(); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + TrackerImportParams trackerImportParams = fromJson("tracker/te_program_with_tea_data.json"); + ImportReport importReport = trackerImportService.importTracker(trackerImportParams); assertNoErrors(importReport); List trackedEntities = manager.getAll(TrackedEntity.class); @@ -92,9 +89,9 @@ void testTrackedEntityProgramAttributeValueUpdate() throws IOException { assertEquals(5, attributeValues.size()); manager.clear(); // update - trackerObjects = fromJson("tracker/te_program_with_tea_update_data.json"); - params.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE); - importReport = trackerImportService.importTracker(params, trackerObjects); + trackerImportParams = fromJson("tracker/te_program_with_tea_update_data.json"); + trackerImportParams.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE); + importReport = trackerImportService.importTracker(trackerImportParams); assertNoErrors(importReport); trackedEntities = manager.getAll(TrackedEntity.class); @@ -107,9 +104,8 @@ void testTrackedEntityProgramAttributeValueUpdate() throws IOException { @Test void testTrackedEntityProgramAttributeValueUpdateAndDelete() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/te_program_with_tea_data.json"); - TrackerImportParams params = new TrackerImportParams(); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + TrackerImportParams trackerImportParams = fromJson("tracker/te_program_with_tea_data.json"); + ImportReport importReport = trackerImportService.importTracker(trackerImportParams); assertNoErrors(importReport); List trackedEntities = manager.getAll(TrackedEntity.class); @@ -120,9 +116,9 @@ void testTrackedEntityProgramAttributeValueUpdateAndDelete() throws IOException assertEquals(5, attributeValues.size()); manager.clear(); // update - trackerObjects = fromJson("tracker/te_program_with_tea_update_data.json"); - params.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE); - importReport = trackerImportService.importTracker(params, trackerObjects); + trackerImportParams = fromJson("tracker/te_program_with_tea_update_data.json"); + trackerImportParams.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE); + importReport = trackerImportService.importTracker(trackerImportParams); assertNoErrors(importReport); trackedEntities = manager.getAll(TrackedEntity.class); @@ -133,9 +129,9 @@ void testTrackedEntityProgramAttributeValueUpdateAndDelete() throws IOException assertEquals(5, attributeValues.size()); manager.clear(); // delete - trackerObjects = fromJson("tracker/te_program_with_tea_delete_data.json"); - params.setImportStrategy(TrackerImportStrategy.DELETE); - importReport = trackerImportService.importTracker(params, trackerObjects); + trackerImportParams = fromJson("tracker/te_program_with_tea_delete_data.json"); + trackerImportParams.setImportStrategy(TrackerImportStrategy.DELETE); + importReport = trackerImportService.importTracker(trackerImportParams); assertNoErrors(importReport); trackedEntities = manager.getAll(TrackedEntity.class); diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackerBundleServiceTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackerBundleServiceTest.java index 85f96e5a0bce..3519956be68b 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackerBundleServiceTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackerBundleServiceTest.java @@ -39,7 +39,6 @@ import org.hisp.dhis.trackedentity.TrackedEntity; import org.hisp.dhis.tracker.TrackerTest; import org.hisp.dhis.tracker.imports.TrackerImportParams; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -68,11 +67,9 @@ void testVerifyMetadata() { @Test void testTrackedEntityImport() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/trackedentity_basic_data.json"); - assertEquals(13, trackerObjects.getTrackedEntities().size()); - TrackerBundle trackerBundle = - trackerBundleService.create( - new TrackerImportParams(), trackerObjects, userService.getUser(ADMIN_USER_UID)); + TrackerImportParams trackerImportParams = fromJson("tracker/trackedentity_basic_data.json"); + assertEquals(13, trackerImportParams.getTrackedEntities().size()); + TrackerBundle trackerBundle = trackerBundleService.create(trackerImportParams); trackerBundleService.commit(trackerBundle); List trackedEntities = manager.getAll(TrackedEntity.class); assertEquals(13, trackedEntities.size()); diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackerEventBundleServiceTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackerEventBundleServiceTest.java index 36acc0dc557c..b67f6fcc79fd 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackerEventBundleServiceTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackerEventBundleServiceTest.java @@ -38,7 +38,6 @@ import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.TrackerImportService; import org.hisp.dhis.tracker.imports.TrackerImportStrategy; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.report.ImportReport; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -59,10 +58,9 @@ protected void initTest() throws IOException { @Test void testCreateSingleEventData() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/event_events_and_enrollment.json"); - assertEquals(8, trackerObjects.getEvents().size()); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + TrackerImportParams trackerImportParams = fromJson("tracker/event_events_and_enrollment.json"); + assertEquals(8, trackerImportParams.getEvents().size()); + ImportReport importReport = trackerImportService.importTracker(trackerImportParams); assertNoErrors(importReport); List events = eventStore.getAll(); @@ -71,15 +69,13 @@ void testCreateSingleEventData() throws IOException { @Test void testUpdateSingleEventData() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/event_events_and_enrollment.json"); - TrackerImportParams trackerImportParams = new TrackerImportParams(); + TrackerImportParams trackerImportParams = fromJson("tracker/event_events_and_enrollment.json"); trackerImportParams.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE); - ImportReport importReport = - trackerImportService.importTracker(trackerImportParams, trackerObjects); + ImportReport importReport = trackerImportService.importTracker(trackerImportParams); assertNoErrors(importReport); assertEquals(8, eventStore.getAll().size()); - importReport = trackerImportService.importTracker(trackerImportParams, trackerObjects); + importReport = trackerImportService.importTracker(trackerImportParams); assertNoErrors(importReport); assertEquals(8, eventStore.getAll().size()); diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackerProgramRuleBundleServiceTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackerProgramRuleBundleServiceTest.java index f548deddc7e6..17ecd943bbc5 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackerProgramRuleBundleServiceTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackerProgramRuleBundleServiceTest.java @@ -40,7 +40,6 @@ import org.hisp.dhis.programrule.ProgramRuleService; import org.hisp.dhis.tracker.TrackerTest; import org.hisp.dhis.tracker.imports.TrackerImportParams; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -71,11 +70,9 @@ protected void initTest() throws IOException { @Test void testRunRuleEngineForEventOnBundleCreate() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/event_events_and_enrollment.json"); - assertEquals(8, trackerObjects.getEvents().size()); - TrackerBundle trackerBundle = - trackerBundleService.create( - new TrackerImportParams(), trackerObjects, userService.getUser(ADMIN_USER_UID)); + TrackerImportParams trackerImportParams = fromJson("tracker/event_events_and_enrollment.json"); + assertEquals(8, trackerImportParams.getEvents().size()); + TrackerBundle trackerBundle = trackerBundleService.create(trackerImportParams); trackerBundle = trackerBundleService.runRuleEngine(trackerBundle); assertEquals(trackerBundle.getEvents().size(), trackerBundle.getEventRuleEffects().size()); } diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackerSideEffectHandlerServiceTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackerSideEffectHandlerServiceTest.java index 373b53f17c34..91bf43adb2f4 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackerSideEffectHandlerServiceTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/bundle/TrackerSideEffectHandlerServiceTest.java @@ -36,7 +36,6 @@ import org.hisp.dhis.common.IdentifiableObjectManager; import org.hisp.dhis.program.notification.ProgramNotificationInstance; import org.hisp.dhis.tracker.TrackerTest; -import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.TrackerImportService; import org.hisp.dhis.tracker.imports.report.ImportReport; import org.junit.jupiter.api.Disabled; @@ -62,7 +61,6 @@ protected void initTest() throws IOException { void testRuleEngineSideEffectHandlerService() throws IOException { ImportReport importReport = trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/enrollment_data_with_program_rule_side_effects.json")); assertNoErrors(importReport); diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/preheat/TrackerPreheatIdentifiersTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/preheat/TrackerPreheatIdentifiersTest.java index 2d6d3a54558a..c77a08a2de31 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/preheat/TrackerPreheatIdentifiersTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/preheat/TrackerPreheatIdentifiersTest.java @@ -30,6 +30,7 @@ import static org.hamcrest.CoreMatchers.notNullValue; import static org.hamcrest.MatcherAssert.assertThat; import static org.hamcrest.Matchers.is; +import static org.hisp.dhis.tracker.imports.TrackerIdSchemeParams.builder; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotNull; @@ -53,11 +54,10 @@ import org.hisp.dhis.tracker.TrackerTest; import org.hisp.dhis.tracker.imports.TrackerIdSchemeParam; import org.hisp.dhis.tracker.imports.TrackerIdSchemeParams; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.domain.DataValue; import org.hisp.dhis.tracker.imports.domain.Event; import org.hisp.dhis.tracker.imports.domain.MetadataIdentifier; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; -import org.hisp.dhis.user.User; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -69,12 +69,9 @@ class TrackerPreheatIdentifiersTest extends TrackerTest { @Autowired private IdentifiableObjectManager manager; - private User user; - @Override protected void initTest() throws IOException { setUpMetadata("tracker/identifier_metadata.json"); - user = currentUserService.getCurrentUser(); } @Test @@ -84,10 +81,9 @@ void testOrgUnitIdentifiers() { String id = pair.getLeft(); TrackerIdSchemeParam param = pair.getRight(); Event event = Event.builder().orgUnit(param.toMetadataIdentifier(id)).build(); - TrackerObjects trackerObjects = TrackerObjects.builder().events(List.of(event)).build(); - TrackerIdSchemeParams params = TrackerIdSchemeParams.builder().orgUnitIdScheme(param).build(); + TrackerImportParams params = buildParams(event, builder().orgUnitIdScheme(param).build()); - TrackerPreheat preheat = trackerPreheatService.preheat(trackerObjects, params, user); + TrackerPreheat preheat = trackerPreheatService.preheat(params); assertPreheatedObjectExists(preheat, OrganisationUnit.class, param, id); } @@ -100,11 +96,10 @@ void testProgramStageIdentifiers() { String id = pair.getLeft(); TrackerIdSchemeParam param = pair.getRight(); Event event = Event.builder().programStage(param.toMetadataIdentifier(id)).build(); - TrackerObjects trackerObjects = TrackerObjects.builder().events(List.of(event)).build(); - TrackerIdSchemeParams params = - TrackerIdSchemeParams.builder().programStageIdScheme(param).build(); + TrackerImportParams params = + buildParams(event, builder().programStageIdScheme(param).build()); - TrackerPreheat preheat = trackerPreheatService.preheat(trackerObjects, params, user); + TrackerPreheat preheat = trackerPreheatService.preheat(params); assertPreheatedObjectExists(preheat, ProgramStage.class, param, id); } @@ -123,11 +118,9 @@ void testDataElementIdentifiers() { .programStage(MetadataIdentifier.ofUid("NpsdDv6kKSO")) .dataValues(Collections.singleton(dv1)) .build(); - TrackerObjects trackerObjects = TrackerObjects.builder().events(List.of(event)).build(); - TrackerIdSchemeParams params = - TrackerIdSchemeParams.builder().dataElementIdScheme(param).build(); + TrackerImportParams params = buildParams(event, builder().dataElementIdScheme(param).build()); - TrackerPreheat preheat = trackerPreheatService.preheat(trackerObjects, params, user); + TrackerPreheat preheat = trackerPreheatService.preheat(params); assertPreheatedObjectExists(preheat, DataElement.class, param, id); } @@ -141,11 +134,10 @@ void testCategoryOptionIdentifiers() { TrackerIdSchemeParam param = pair.getRight(); Event event = Event.builder().attributeCategoryOptions(Set.of(param.toMetadataIdentifier(id))).build(); - TrackerObjects trackerObjects = TrackerObjects.builder().events(List.of(event)).build(); - TrackerIdSchemeParams params = - TrackerIdSchemeParams.builder().categoryOptionIdScheme(param).build(); + TrackerImportParams params = + buildParams(event, builder().categoryOptionIdScheme(param).build()); - TrackerPreheat preheat = trackerPreheatService.preheat(trackerObjects, params, user); + TrackerPreheat preheat = trackerPreheatService.preheat(params); assertPreheatedObjectExists(preheat, CategoryOption.class, param, id); } @@ -158,11 +150,10 @@ void testCategoryOptionComboIdentifiers() { String id = pair.getLeft(); TrackerIdSchemeParam param = pair.getRight(); Event event = Event.builder().attributeOptionCombo(param.toMetadataIdentifier(id)).build(); - TrackerObjects trackerObjects = TrackerObjects.builder().events(List.of(event)).build(); - TrackerIdSchemeParams params = - TrackerIdSchemeParams.builder().categoryOptionComboIdScheme(param).build(); + TrackerImportParams params = + buildParams(event, builder().categoryOptionComboIdScheme(param).build()); - TrackerPreheat preheat = trackerPreheatService.preheat(trackerObjects, params, user); + TrackerPreheat preheat = trackerPreheatService.preheat(params); assertPreheatedObjectExists(preheat, CategoryOptionCombo.class, param, id); } @@ -171,10 +162,10 @@ void testCategoryOptionComboIdentifiers() { @Test void testDefaultsWithIdSchemeUID() { - TrackerObjects trackerObjects = TrackerObjects.builder().build(); - TrackerIdSchemeParams params = TrackerIdSchemeParams.builder().build(); + TrackerImportParams params = + TrackerImportParams.builder().user(currentUserService.getCurrentUser()).build(); - TrackerPreheat preheat = trackerPreheatService.preheat(trackerObjects, params, user); + TrackerPreheat preheat = trackerPreheatService.preheat(params); assertPreheatHasDefault(preheat, Category.class); assertPreheatHasDefault(preheat, CategoryCombo.class); @@ -184,15 +175,20 @@ void testDefaultsWithIdSchemeUID() { @Test void testDefaultsWithIdSchemesOtherThanUID() { - TrackerObjects trackerObjects = TrackerObjects.builder().events(List.of(new Event())).build(); - TrackerIdSchemeParams params = - TrackerIdSchemeParams.builder() - .idScheme(TrackerIdSchemeParam.NAME) - .categoryOptionIdScheme(TrackerIdSchemeParam.ofAttribute(CodeGenerator.generateUid())) - .categoryOptionComboIdScheme(TrackerIdSchemeParam.CODE) - .build(); - TrackerPreheat preheat = trackerPreheatService.preheat(trackerObjects, params, user); + Event event = new Event(); + + TrackerImportParams params = + buildParams( + event, + builder() + .idScheme(TrackerIdSchemeParam.NAME) + .categoryOptionIdScheme( + TrackerIdSchemeParam.ofAttribute(CodeGenerator.generateUid())) + .categoryOptionComboIdScheme(TrackerIdSchemeParam.CODE) + .build()); + + TrackerPreheat preheat = trackerPreheatService.preheat(params); assertPreheatHasDefault(preheat, Category.class); assertPreheatHasDefault(preheat, CategoryCombo.class); @@ -200,6 +196,16 @@ void testDefaultsWithIdSchemesOtherThanUID() { assertPreheatHasDefault(preheat, CategoryOptionCombo.class); } + private TrackerImportParams buildParams(Event event, TrackerIdSchemeParams idParams) { + TrackerImportParams params = + TrackerImportParams.builder() + .events(Collections.singletonList(event)) + .user(currentUserService.getCurrentUser()) + .idSchemes(idParams) + .build(); + return params; + } + private List> buildDataSet( String uid, String code, String name) { List> data = new ArrayList<>(); diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/preheat/TrackerPreheatServiceIntegrationTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/preheat/TrackerPreheatServiceIntegrationTest.java index 78ba1fa1b5c6..5f3f45d3f588 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/preheat/TrackerPreheatServiceIntegrationTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/preheat/TrackerPreheatServiceIntegrationTest.java @@ -45,10 +45,10 @@ import org.hisp.dhis.trackedentity.TrackedEntityTypeService; import org.hisp.dhis.tracker.imports.TrackerIdSchemeParam; import org.hisp.dhis.tracker.imports.TrackerIdSchemeParams; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.domain.Enrollment; import org.hisp.dhis.tracker.imports.domain.MetadataIdentifier; import org.hisp.dhis.tracker.imports.domain.TrackedEntity; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.user.User; import org.hisp.dhis.user.UserService; import org.junit.jupiter.api.Test; @@ -120,21 +120,20 @@ void testPreheatWithDifferentIdSchemes() { .trackedEntity("TE123456789") .build(); - TrackerObjects trackerObjects = - TrackerObjects.builder() + TrackerImportParams params = + TrackerImportParams.builder() + .user(currentUser) .trackedEntities(Lists.newArrayList(teA)) .enrollments(Lists.newArrayList(enrollmentA)) + .idSchemes( + TrackerIdSchemeParams.builder() + .idScheme(TrackerIdSchemeParam.UID) + .orgUnitIdScheme(TrackerIdSchemeParam.CODE) + .programIdScheme(TrackerIdSchemeParam.ofAttribute(ATTRIBUTE_UID)) + .build()) .build(); - TrackerIdSchemeParams idSchemeParams = - TrackerIdSchemeParams.builder() - .idScheme(TrackerIdSchemeParam.UID) - .orgUnitIdScheme(TrackerIdSchemeParam.CODE) - .programIdScheme(TrackerIdSchemeParam.ofAttribute(ATTRIBUTE_UID)) - .build(); - - TrackerPreheat preheat = - trackerPreheatService.preheat(trackerObjects, idSchemeParams, currentUser); + TrackerPreheat preheat = trackerPreheatService.preheat(params); assertNotNull(preheat); // asserting on specific fields instead of plain assertEquals since diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/preheat/TrackerPreheatServiceTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/preheat/TrackerPreheatServiceTest.java index b17a4517f57b..4cfaa53f987a 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/preheat/TrackerPreheatServiceTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/preheat/TrackerPreheatServiceTest.java @@ -42,11 +42,12 @@ import org.hisp.dhis.organisationunit.OrganisationUnit; import org.hisp.dhis.program.ProgramStage; import org.hisp.dhis.tracker.TrackerTest; +import org.hisp.dhis.tracker.imports.TrackerIdSchemeParam; import org.hisp.dhis.tracker.imports.TrackerIdSchemeParams; import org.hisp.dhis.tracker.imports.TrackerIdentifierCollector; +import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.domain.MetadataIdentifier; import org.hisp.dhis.tracker.imports.domain.TrackedEntity; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -64,11 +65,11 @@ protected void initTest() {} @Test void testCollectIdentifiersEvents() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/event_events.json"); - assertTrue(trackerObjects.getTrackedEntities().isEmpty()); - assertTrue(trackerObjects.getEnrollments().isEmpty()); - assertFalse(trackerObjects.getEvents().isEmpty()); - Map, Set> collectedMap = identifierCollector.collect(trackerObjects); + TrackerImportParams params = fromJson("tracker/event_events.json"); + assertTrue(params.getTrackedEntities().isEmpty()); + assertTrue(params.getEnrollments().isEmpty()); + assertFalse(params.getEvents().isEmpty()); + Map, Set> collectedMap = identifierCollector.collect(params); assertTrue(collectedMap.containsKey(DataElement.class)); assertTrue(collectedMap.containsKey(ProgramStage.class)); assertTrue(collectedMap.containsKey(OrganisationUnit.class)); @@ -102,8 +103,12 @@ void testCollectIdentifiersEvents() throws IOException { @Test void testCollectIdentifiersAttributeValues() { - TrackerObjects trackerObjects = - TrackerObjects.builder() + TrackerImportParams params = + TrackerImportParams.builder() + .idSchemes( + TrackerIdSchemeParams.builder() + .idScheme(TrackerIdSchemeParam.ofAttribute("ATTR1234567")) + .build()) .trackedEntities( Lists.newArrayList( TrackedEntity.builder() @@ -111,10 +116,10 @@ void testCollectIdentifiersAttributeValues() { .orgUnit(MetadataIdentifier.ofCode("OU123456789")) .build())) .build(); - assertFalse(trackerObjects.getTrackedEntities().isEmpty()); - assertTrue(trackerObjects.getEnrollments().isEmpty()); - assertTrue(trackerObjects.getEvents().isEmpty()); - Map, Set> collectedMap = identifierCollector.collect(trackerObjects); + assertFalse(params.getTrackedEntities().isEmpty()); + assertTrue(params.getEnrollments().isEmpty()); + assertTrue(params.getEvents().isEmpty()); + Map, Set> collectedMap = identifierCollector.collect(params); assertTrue(collectedMap.containsKey(TrackedEntity.class)); Set trackedEntities = collectedMap.get(TrackedEntity.class); assertTrue(collectedMap.containsKey(OrganisationUnit.class)); @@ -127,20 +132,21 @@ void testCollectIdentifiersAttributeValues() { @Test void testPreheatValidation() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/event_events.json"); - assertTrue(trackerObjects.getTrackedEntities().isEmpty()); - assertTrue(trackerObjects.getEnrollments().isEmpty()); - assertFalse(trackerObjects.getEvents().isEmpty()); + TrackerImportParams params = fromJson("tracker/event_events.json"); + assertTrue(params.getTrackedEntities().isEmpty()); + assertTrue(params.getEnrollments().isEmpty()); + assertFalse(params.getEvents().isEmpty()); } @Test void testPreheatEvents() throws IOException { setUpMetadata("tracker/event_metadata.json"); - TrackerObjects trackerObjects = fromJson("tracker/event_events.json"); + TrackerImportParams params = fromJson("tracker/event_events.json"); + assertTrue(params.getTrackedEntities().isEmpty()); + assertTrue(params.getEnrollments().isEmpty()); + assertFalse(params.getEvents().isEmpty()); - TrackerPreheat preheat = - trackerPreheatService.preheat( - trackerObjects, new TrackerIdSchemeParams(), userService.getUser(ADMIN_USER_UID)); + TrackerPreheat preheat = trackerPreheatService.preheat(params); assertNotNull(preheat); assertFalse(preheat.getAll(DataElement.class).isEmpty()); diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/programrule/ProgramRuleAssignActionTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/programrule/ProgramRuleAssignActionTest.java index 3d27f7cd4b68..e33d41b7760a 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/programrule/ProgramRuleAssignActionTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/programrule/ProgramRuleAssignActionTest.java @@ -52,7 +52,6 @@ import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.TrackerImportService; import org.hisp.dhis.tracker.imports.TrackerImportStrategy; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.report.ImportReport; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -87,31 +86,28 @@ public void initTest() throws IOException { assignProgramRule(); trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/programrule/tei_enrollment_completed_event.json")); } @Test void shouldImportWithWarningWhenDataElementWithSameValueIsAssignedByAssignRule() throws IOException { - TrackerImportParams params = new TrackerImportParams(); - TrackerObjects trackerObjects = + TrackerImportParams params = fromJson("tracker/programrule/event_update_datavalue_same_value.json"); params.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertHasOnlyWarnings(importReport, E1308); } @Test void shouldNotImportWhenDataElementWithDifferentValueIsAssignedByAssignRule() throws IOException { - TrackerImportParams params = new TrackerImportParams(); - TrackerObjects trackerObjects = + TrackerImportParams params = fromJson("tracker/programrule/event_update_datavalue_different_value.json"); params.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertHasOnlyErrors(importReport, E1307); } @@ -121,12 +117,11 @@ void shouldNotImportWhenDataElementWithDifferentValueIsAssignedByAssignRule() th shouldImportWithWarningWhenDataElementWithDifferentValueIsAssignedByAssignRuleAndOverwriteKeyIsTrue() throws IOException { systemSettingManager.saveSystemSetting(SettingKey.RULE_ENGINE_ASSIGN_OVERWRITE, true); - TrackerImportParams params = new TrackerImportParams(); - TrackerObjects trackerObjects = + TrackerImportParams params = fromJson("tracker/programrule/event_update_datavalue_different_value.json"); params.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertHasOnlyWarnings(importReport, E1308); } @@ -136,12 +131,11 @@ void shouldNotImportWhenDataElementWithDifferentValueIsAssignedByAssignRule() th shouldImportWithWarningWhenDataElementWithDifferentAndEmptyValueIsAssignedByAssignRuleAndOverwriteKeyIsTrue() throws IOException { systemSettingManager.saveSystemSetting(SettingKey.RULE_ENGINE_ASSIGN_OVERWRITE, true); - TrackerImportParams params = new TrackerImportParams(); - TrackerObjects trackerObjects = + TrackerImportParams params = fromJson("tracker/programrule/event_update_datavalue_empty_value.json"); params.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertHasOnlyWarnings(importReport, E1308); } diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/programrule/ProgramRuleTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/programrule/ProgramRuleTest.java index 44e00dbd3e3a..484ca72cfc65 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/programrule/ProgramRuleTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/programrule/ProgramRuleTest.java @@ -55,7 +55,6 @@ import org.hisp.dhis.tracker.TrackerTest; import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.TrackerImportService; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.report.ImportReport; import org.hisp.dhis.tracker.imports.validation.ValidationCode; import org.junit.jupiter.api.Test; @@ -112,8 +111,7 @@ public void initTest() throws IOException { @Test void shouldImportEnrollmentWithNoWarningsWhenThereAreNoProgramRules() throws IOException { ImportReport report = - trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/programrule/tei_enrollment.json")); + trackerImportService.importTracker(fromJson("tracker/programrule/tei_enrollment.json")); assertNoErrorsAndNoWarnings(report); } @@ -122,8 +120,7 @@ void shouldImportEnrollmentWithNoWarningsWhenThereAreNoProgramRules() throws IOE void shouldImportEnrollmentWithWarningsWhenAWarningIsTriggered() throws IOException { alwaysTrueWarningProgramRule(); ImportReport report = - trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/programrule/tei_enrollment.json")); + trackerImportService.importTracker(fromJson("tracker/programrule/tei_enrollment.json")); assertHasOnlyWarnings(report, E1300); } @@ -132,8 +129,7 @@ void shouldImportEnrollmentWithWarningsWhenAWarningIsTriggered() throws IOExcept void shouldNotImportEnrollmentWhenAnErrorIsTriggered() throws IOException { alwaysTrueErrorProgramRule(); ImportReport report = - trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/programrule/tei_enrollment.json")); + trackerImportService.importTracker(fromJson("tracker/programrule/tei_enrollment.json")); assertHasOnlyErrors(report, E1300); } @@ -142,8 +138,7 @@ void shouldNotImportEnrollmentWhenAnErrorIsTriggered() throws IOException { void shouldImportProgramEventWithWarningsWhenAWarningIsTriggered() throws IOException { alwaysTrueWarningProgramEventProgramRule(); ImportReport report = - trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/programrule/program_event.json")); + trackerImportService.importTracker(fromJson("tracker/programrule/program_event.json")); assertHasOnlyWarnings(report, E1300); } @@ -153,8 +148,7 @@ void shouldNotImportProgramEventWhenAnErrorIsTriggeredBasedOnConditionEvaluating throws IOException { conditionWithConstantEvaluatesToTrue(); ImportReport report = - trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/programrule/program_event.json")); + trackerImportService.importTracker(fromJson("tracker/programrule/program_event.json")); assertHasOnlyErrors(report, E1300); } @@ -163,8 +157,7 @@ void shouldNotImportProgramEventWhenAnErrorIsTriggeredBasedOnConditionEvaluating void shouldNotImportProgramEventWhenAnErrorIsTriggered() throws IOException { alwaysTrueErrorProgramEventProgramRule(); ImportReport report = - trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/programrule/program_event.json")); + trackerImportService.importTracker(fromJson("tracker/programrule/program_event.json")); assertHasOnlyErrors(report, E1300); } @@ -173,7 +166,6 @@ void shouldNotImportProgramEventWhenAnErrorIsTriggered() throws IOException { void shouldImportEventWithNoWarningsWhenThereAreNoProgramRules() throws IOException { ImportReport report = trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/programrule/tei_enrollment_completed_event.json")); assertNoErrorsAndNoWarnings(report); @@ -182,14 +174,11 @@ void shouldImportEventWithNoWarningsWhenThereAreNoProgramRules() throws IOExcept @Test void shouldImportEventWithWarningsWhenAWarningIsTriggered() throws IOException { ImportReport report = - trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/programrule/tei_enrollment.json")); + trackerImportService.importTracker(fromJson("tracker/programrule/tei_enrollment.json")); assertNoErrors(report); alwaysTrueWarningProgramRule(); - report = - trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/programrule/event.json")); + report = trackerImportService.importTracker(fromJson("tracker/programrule/event.json")); assertHasOnlyWarnings(report, E1300); } @@ -197,14 +186,11 @@ void shouldImportEventWithWarningsWhenAWarningIsTriggered() throws IOException { @Test void shouldNotImportEventWhenAnErrorIsTriggered() throws IOException { ImportReport report = - trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/programrule/tei_enrollment.json")); + trackerImportService.importTracker(fromJson("tracker/programrule/tei_enrollment.json")); assertNoErrors(report); alwaysTrueErrorProgramRule(); - report = - trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/programrule/event.json")); + report = trackerImportService.importTracker(fromJson("tracker/programrule/event.json")); assertHasOnlyErrors(report, E1300); } @@ -215,7 +201,6 @@ void shouldNotValidateEventAndValidateEnrollmentWhenAnErrorIsTriggeredOnEvent() onCompleteErrorProgramRule(); ImportReport report = trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/programrule/tei_enrollment_completed_event.json")); assertHasOnlyErrors(report, E1300); @@ -226,7 +211,6 @@ void shouldNotImportEventAndEnrollmentWhenAnErrorIsTriggeredOnEnrollment() throw onCompleteErrorProgramRule(); ImportReport report = trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/programrule/tei_completed_enrollment_event.json")); assertAll( @@ -238,14 +222,11 @@ void shouldNotImportEventAndEnrollmentWhenAnErrorIsTriggeredOnEnrollment() throw void shouldImportEventWhenAnErrorIsTriggeredOnEnrollmentAlreadyPresentInDB() throws IOException { ImportReport report = trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/programrule/tei_completed_enrollment.json")); assertNoErrorsAndNoWarnings(report); onCompleteErrorProgramRule(); - report = - trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/programrule/event.json")); + report = trackerImportService.importTracker(fromJson("tracker/programrule/event.json")); assertNoErrorsAndNoWarnings(report); } @@ -256,7 +237,6 @@ void shouldNotImportWhenErrorIsTriggeredOnAllEntities() throws IOException { alwaysTrueErrorProgramEventProgramRule(); ImportReport report = trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/programrule/tei_enrollment_event_programevent.json")); assertAll( @@ -268,10 +248,9 @@ void shouldNotImportWhenErrorIsTriggeredOnAllEntities() throws IOException { @Test void shouldImportWithWarningWhenARuleWithASyntaxErrorIsTriggered() throws IOException { syntaxErrorRule(); - TrackerObjects trackerObjects = fromJson("tracker/programrule/tei_enrollment.json"); + TrackerImportParams params = fromJson("tracker/programrule/tei_enrollment.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertHasOnlyWarnings(importReport, E1300); } @@ -280,11 +259,10 @@ void shouldImportWithWarningWhenARuleWithASyntaxErrorIsTriggered() throws IOExce void shouldImportWithWarningWhenAWarningIsTriggeredOnEventInSameProgramStage() throws IOException { programStageWarningRule(); - TrackerObjects trackerObjects = + TrackerImportParams params = fromJson("tracker/programrule/tei_enrollment_completed_event.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertHasOnlyWarnings(importReport, E1300); } @@ -293,12 +271,11 @@ void shouldImportWithWarningWhenAWarningIsTriggeredOnEventInSameProgramStage() void shouldImportWithNoWarningsWhenAWarningIsTriggeredOnEventInDifferentProgramStage() throws IOException { programStageWarningRule(); - TrackerObjects trackerObjects = + TrackerImportParams params = fromJson( "tracker/programrule/tei_enrollment_completed_event_from_another_program_stage.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertNoErrorsAndNoWarnings(importReport); } @@ -307,11 +284,10 @@ void shouldImportWithNoWarningsWhenAWarningIsTriggeredOnEventInDifferentProgramS void shouldImportWithNoWarningsWhenAWarningIsTriggeredOnActiveEventInOnCompleteProgramStage() throws IOException { programStage2WarningRule(); - TrackerObjects trackerObjects = + TrackerImportParams params = fromJson("tracker/programrule/tei_enrollment_event_from_another_program_stage.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertNoErrorsAndNoWarnings(importReport); } @@ -320,12 +296,11 @@ void shouldImportWithNoWarningsWhenAWarningIsTriggeredOnActiveEventInOnCompleteP void shouldImportWithWarningsWhenAWarningIsTriggeredOnCompletedEventInOnCompleteProgramStage() throws IOException { programStage2WarningRule(); - TrackerObjects trackerObjects = + TrackerImportParams params = fromJson( "tracker/programrule/tei_enrollment_completed_event_from_another_program_stage.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertHasOnlyWarnings(importReport, E1300); } @@ -334,12 +309,11 @@ void shouldImportWithWarningsWhenAWarningIsTriggeredOnCompletedEventInOnComplete void shouldImportWithNoWarningsWhenAWarningIsTriggeredWithADataElementFromADifferentProgramStage() throws IOException { programStage2WrongDataElementWarningRule(); - TrackerObjects trackerObjects = + TrackerImportParams params = fromJson( "tracker/programrule/tei_enrollment_completed_event_from_another_program_stage.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertNoErrorsAndNoWarnings(importReport); } @@ -347,11 +321,10 @@ void shouldImportWithNoWarningsWhenAWarningIsTriggeredWithADataElementFromADiffe @Test void shouldNotImportWithWhenDataElementHasValue() throws IOException { showErrorWhenVariableHasValueRule(); - TrackerObjects trackerObjects = + TrackerImportParams params = fromJson("tracker/programrule/tei_completed_enrollment_event.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertHasOnlyErrors(importReport, E1300); } @@ -359,11 +332,10 @@ void shouldNotImportWithWhenDataElementHasValue() throws IOException { @Test void shouldImportWithNoWarningsWhenDataElementHasNoValue() throws IOException { showErrorWhenVariableHasValueRule(); - TrackerObjects trackerObjects = + TrackerImportParams params = fromJson("tracker/programrule/tei_enrollment_event_with_no_data_value.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertNoErrorsAndNoWarnings(importReport); } @@ -371,11 +343,10 @@ void shouldImportWithNoWarningsWhenDataElementHasNoValue() throws IOException { @Test void shouldImportWithNoWarningsWhenDataElementHasNullValue() throws IOException { showErrorWhenVariableHasValueRule(); - TrackerObjects trackerObjects = + TrackerImportParams params = fromJson("tracker/programrule/tei_enrollment_event_with_null_data_value.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertNoErrorsAndNoWarnings(importReport); } diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/EnrollmentAttrValidationTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/EnrollmentAttrValidationTest.java index 56fa2c88b94b..c724d547332e 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/EnrollmentAttrValidationTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/EnrollmentAttrValidationTest.java @@ -36,7 +36,6 @@ import org.hisp.dhis.tracker.TrackerTest; import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.TrackerImportService; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.report.ImportReport; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -55,130 +54,119 @@ protected void initTest() throws IOException { injectAdminUser(); assertNoErrors( trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/validations/enrollments_te_te-data_2.json"))); manager.flush(); } @Test void failValidationWhenTrackedEntityAttributeHasWrongOptionValue() throws IOException { - TrackerObjects trackerObjects = + TrackerImportParams params = fromJson("tracker/validations/enrollments_te_with_invalid_option_value.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertHasOnlyErrors(importReport, ValidationCode.E1125); } @Test void successValidationWhenTrackedEntityAttributeHasValidOptionValue() throws IOException { - TrackerObjects trackerObjects = + TrackerImportParams params = fromJson("tracker/validations/enrollments_te_with_valid_option_value.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertNoErrors(importReport); } @Test void testAttributesMissingUid() throws IOException { - TrackerObjects trackerObjects = + TrackerImportParams params = fromJson("tracker/validations/enrollments_te_attr-missing-uuid.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertHasOnlyErrors(importReport, ValidationCode.E1075); } @Test void testAttributesMissingValues() throws IOException { - TrackerObjects trackerObjects = + TrackerImportParams params = fromJson("tracker/validations/enrollments_te_attr-missing-value.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertHasOnlyErrors(importReport, ValidationCode.E1076); } @Test void testAttributesMissingTeA() throws IOException { - TrackerObjects trackerObjects = + TrackerImportParams params = fromJson("tracker/validations/enrollments_te_attr-non-existing.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertHasOnlyErrors(importReport, ValidationCode.E1006); } @Test void testAttributesMissingMandatory() throws IOException { - TrackerObjects trackerObjects = + TrackerImportParams params = fromJson("tracker/validations/enrollments_te_attr-missing-mandatory.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertHasOnlyErrors(importReport, ValidationCode.E1018); } @Test void testAttributesUniquenessInSameTei() throws IOException { - TrackerObjects trackerObjects = + TrackerImportParams params = fromJson("tracker/validations/enrollments_te_unique_attr_same_tei.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertNoErrors(importReport); } @Test void testAttributesUniquenessAlreadyInDB() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/validations/enrollments_te_te-data_3.json"); + TrackerImportParams params = fromJson("tracker/validations/enrollments_te_te-data_3.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertNoErrors(importReport); manager.flush(); manager.clear(); - trackerObjects = fromJson("tracker/validations/enrollments_te_unique_attr_same_tei.json"); + params = fromJson("tracker/validations/enrollments_te_unique_attr_same_tei.json"); - importReport = trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + importReport = trackerImportService.importTracker(params); assertNoErrors(importReport); manager.flush(); manager.clear(); - trackerObjects = fromJson("tracker/validations/enrollments_te_unique_attr_in_db.json"); + params = fromJson("tracker/validations/enrollments_te_unique_attr_in_db.json"); - importReport = trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + importReport = trackerImportService.importTracker(params); assertHasOnlyErrors(importReport, ValidationCode.E1064); } @Test void testAttributesUniquenessInDifferentTeis() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/validations/enrollments_te_te-data_3.json"); - assertNoErrors(trackerImportService.importTracker(new TrackerImportParams(), trackerObjects)); + TrackerImportParams params = fromJson("tracker/validations/enrollments_te_te-data_3.json"); + assertNoErrors(trackerImportService.importTracker(params)); manager.flush(); manager.clear(); - trackerObjects = fromJson("tracker/validations/enrollments_te_unique_attr.json"); + params = fromJson("tracker/validations/enrollments_te_unique_attr.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertHasErrors(importReport, 2, ValidationCode.E1064); } @Test void testAttributesOnlyProgramAttrAllowed() throws IOException { - TrackerObjects trackerObjects = + TrackerImportParams params = fromJson("tracker/validations/enrollments_te_attr-only-program-attr.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertHasOnlyErrors(importReport, ValidationCode.E1019); } diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/EnrollmentImportValidationTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/EnrollmentImportValidationTest.java index 9feb1f5ff0ad..e66a95192ca9 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/EnrollmentImportValidationTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/EnrollmentImportValidationTest.java @@ -40,11 +40,9 @@ import org.hisp.dhis.program.EnrollmentService; import org.hisp.dhis.tracker.TrackerTest; import org.hisp.dhis.tracker.TrackerType; -import org.hisp.dhis.tracker.imports.TrackerIdSchemeParams; import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.TrackerImportService; import org.hisp.dhis.tracker.imports.TrackerImportStrategy; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheat; import org.hisp.dhis.tracker.imports.preheat.TrackerPreheatService; import org.hisp.dhis.tracker.imports.report.ImportReport; @@ -70,33 +68,28 @@ protected void initTest() throws IOException { injectAdminUser(); assertNoErrors( trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/validations/enrollments_te_te-data.json"))); manager.flush(); } @Test void testEnrollmentValidationOkAll() throws IOException { - TrackerObjects trackerObjects = + TrackerImportParams params = fromJson("tracker/validations/enrollments_te_enrollments-data.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertNoErrors(importReport); } @Test void testPreheatOwnershipForSubsequentEnrollment() throws IOException { - TrackerObjects trackerObjects = + TrackerImportParams params = fromJson("tracker/validations/enrollments_te_enrollments-data.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertNoErrors(importReport); - TrackerObjects secondTrackerObjects = + TrackerImportParams secondParams = fromJson("tracker/validations/enrollments_te_enrollments-data.json"); - TrackerPreheat preheat = - trackerPreheatService.preheat( - secondTrackerObjects, new TrackerIdSchemeParams(), userService.getUser(ADMIN_USER_UID)); - secondTrackerObjects + TrackerPreheat preheat = trackerPreheatService.preheat(secondParams); + secondParams .getEnrollments() .forEach( e -> { @@ -120,34 +113,31 @@ void testDisplayIncidentDateTrueButDateValueIsInvalid() { @Test void testNoWriteAccessToOrg() throws IOException { - TrackerObjects trackerObjects = + TrackerImportParams params = fromJson("tracker/validations/enrollments_te_enrollments-data.json"); User user = userService.getUser(USER_2); - TrackerImportParams params = new TrackerImportParams(); - params.setUserId(user.getUid()); + params.setUser(user); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertHasErrors(importReport, 4, ValidationCode.E1000); } @Test void testOnlyProgramAttributesAllowedOnEnrollments() throws IOException { - TrackerObjects trackerObjects = + TrackerImportParams params = fromJson("tracker/validations/enrollments_error_non_program_attr.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertHasErrors(importReport, 3, ValidationCode.E1019); } @Test void testAttributesOk() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/validations/enrollments_te_attr-data.json"); + TrackerImportParams params = fromJson("tracker/validations/enrollments_te_attr-data.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertNoErrors(importReport); assertEquals( @@ -163,49 +153,45 @@ void testAttributesOk() throws IOException { @Test void testDeleteCascadeEnrollments() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/validations/enrollments_te_attr-data.json"); - TrackerImportParams params = new TrackerImportParams(); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + TrackerImportParams params = fromJson("tracker/validations/enrollments_te_attr-data.json"); + ImportReport importReport = trackerImportService.importTracker(params); assertNoErrors(importReport); manager.flush(); importEvents(); manager.flush(); - trackerObjects = fromJson("tracker/validations/enrollments_te_attr-data.json"); + params = fromJson("tracker/validations/enrollments_te_attr-data.json"); User user2 = userService.getUser(USER_4); - params.setUserId(user2.getUid()); + params.setUser(user2); params.setImportStrategy(TrackerImportStrategy.DELETE); - ImportReport trackerImportDeleteReport = - trackerImportService.importTracker(params, trackerObjects); + ImportReport trackerImportDeleteReport = trackerImportService.importTracker(params); assertHasOnlyErrors(trackerImportDeleteReport, ValidationCode.E1103, ValidationCode.E1091); } protected void importEvents() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/validations/events-with-registration.json"); - TrackerImportParams params = new TrackerImportParams(); + TrackerImportParams params = fromJson("tracker/validations/events-with-registration.json"); params.setImportStrategy(TrackerImportStrategy.CREATE); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertNoErrors(importReport); } @Test void testActiveEnrollmentAlreadyExists() throws IOException { - TrackerObjects trackerObjects = + TrackerImportParams trackerImportParams = fromJson("tracker/validations/enrollments_double-tei-enrollment_part1.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(trackerImportParams); assertNoErrors(importReport); - TrackerObjects trackerObjects2 = + TrackerImportParams trackerImportParams1 = fromJson("tracker/validations/enrollments_double-tei-enrollment_part2.json"); - importReport = trackerImportService.importTracker(new TrackerImportParams(), trackerObjects2); + importReport = trackerImportService.importTracker(trackerImportParams1); ValidationReport validationResult = importReport.getValidationReport(); @@ -214,21 +200,19 @@ void testActiveEnrollmentAlreadyExists() throws IOException { @Test void testEnrollmentDeleteOk() throws IOException { - TrackerObjects trackerObjects = + TrackerImportParams paramsCreate = fromJson("tracker/validations/enrollments_te_enrollments-data.json"); - TrackerImportParams params = new TrackerImportParams(); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + ImportReport importReport = trackerImportService.importTracker(paramsCreate); assertNoErrors(importReport); manager.flush(); manager.clear(); - TrackerObjects deleteTrackerObjects = + TrackerImportParams paramsDelete = fromJson("tracker/validations/enrollments_te_enrollments-data-delete.json"); - params.setImportStrategy(TrackerImportStrategy.DELETE); + paramsDelete.setImportStrategy(TrackerImportStrategy.DELETE); - ImportReport importReportDelete = - trackerImportService.importTracker(params, deleteTrackerObjects); + ImportReport importReportDelete = trackerImportService.importTracker(paramsDelete); assertNoErrors(importReportDelete); assertEquals(1, importReportDelete.getStats().getDeleted()); @@ -237,11 +221,9 @@ void testEnrollmentDeleteOk() throws IOException { /** Notes with no value are ignored */ @Test void testBadEnrollmentNoteNoValue() throws IOException { - TrackerObjects trackerObjects = - fromJson("tracker/validations/enrollments_bad-note-no-value.json"); + TrackerImportParams params = fromJson("tracker/validations/enrollments_bad-note-no-value.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertNoErrors(importReport); } diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/EnrollmentSecurityImportValidationTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/EnrollmentSecurityImportValidationTest.java index 3e5218410c38..650514afb414 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/EnrollmentSecurityImportValidationTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/EnrollmentSecurityImportValidationTest.java @@ -53,7 +53,6 @@ import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.TrackerImportService; import org.hisp.dhis.tracker.imports.TrackerImportStrategy; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.report.ImportReport; import org.hisp.dhis.user.User; import org.hisp.dhis.user.UserService; @@ -164,21 +163,19 @@ protected void initTest() throws IOException { injectAdminUser(); assertNoErrors( trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/validations/enrollments_te_te-data.json"))); } @Test void testNoWriteAccessToOrg() throws IOException { - TrackerObjects trackerObjects = + TrackerImportParams params = fromJson("tracker/validations/enrollments_te_enrollments-data.json"); User user = userService.getUser(USER_2); injectSecurityContext(user); - TrackerImportParams params = new TrackerImportParams(); - params.setUserId(user.getUid()); + params.setUser(user); params.setImportStrategy(TrackerImportStrategy.CREATE); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertHasErrors(importReport, 4, ValidationCode.E1000); } @@ -196,12 +193,11 @@ void testUserNoAccessToTrackedEntity() throws IOException { createUserWithAuth("user1").setOrganisationUnits(Sets.newHashSet(organisationUnitA)); userService.addUser(user); injectSecurityContext(user); - TrackerObjects trackerObjects = fromJson("tracker/validations/enrollments_no-access-tei.json"); - TrackerImportParams params = new TrackerImportParams(); - params.setUserId(user.getUid()); + TrackerImportParams params = fromJson("tracker/validations/enrollments_no-access-tei.json"); + params.setUser(user); params.setImportStrategy(TrackerImportStrategy.CREATE); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertHasOnlyErrors(importReport, ValidationCode.E1104); } @@ -223,13 +219,11 @@ void testUserNoWriteAccessToProgram() throws IOException { createUserWithAuth("user1").setOrganisationUnits(Sets.newHashSet(organisationUnitA)); userService.addUser(user); injectSecurityContext(user); - TrackerObjects trackerObjects = - fromJson("tracker/validations/enrollments_no-access-program.json"); - TrackerImportParams params = new TrackerImportParams(); - params.setUserId(user.getUid()); + TrackerImportParams params = fromJson("tracker/validations/enrollments_no-access-program.json"); + params.setUser(user); params.setImportStrategy(TrackerImportStrategy.CREATE); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertHasOnlyErrors(importReport, ValidationCode.E1091); } @@ -247,13 +241,11 @@ void testUserHasWriteAccessToProgram() throws IOException { createUserWithAuth("user1").setOrganisationUnits(Sets.newHashSet(organisationUnitA)); userService.addUser(user); injectSecurityContext(user); - TrackerObjects trackerObjects = - fromJson("tracker/validations/enrollments_no-access-program.json"); - TrackerImportParams params = new TrackerImportParams(); - params.setUserId(user.getUid()); + TrackerImportParams params = fromJson("tracker/validations/enrollments_no-access-program.json"); + params.setUser(user); params.setImportStrategy(TrackerImportStrategy.CREATE); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertNoErrors(importReport); } @@ -270,13 +262,12 @@ void testUserHasNoAccessToProgramTeiType() throws IOException { User user = createUserWithAuth("user1").setOrganisationUnits(Sets.newHashSet(organisationUnitA)); injectSecurityContext(user); - TrackerObjects trackerObjects = + TrackerImportParams params = fromJson("tracker/validations/enrollments_program-teitype-missmatch.json"); - TrackerImportParams params = new TrackerImportParams(); - params.setUserId(user.getUid()); + params.setUser(user); params.setImportStrategy(TrackerImportStrategy.CREATE); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertHasOnlyErrors(importReport, ValidationCode.E1104); } diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/EventImportValidationTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/EventImportValidationTest.java index 97701106ae92..6faf730c1010 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/EventImportValidationTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/EventImportValidationTest.java @@ -59,7 +59,6 @@ import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.TrackerImportService; import org.hisp.dhis.tracker.imports.TrackerImportStrategy; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.report.ImportReport; import org.hisp.dhis.tracker.imports.report.TrackerTypeReport; import org.hisp.dhis.user.User; @@ -86,11 +85,9 @@ protected void initTest() throws IOException { injectAdminUser(); assertNoErrors( trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/validations/enrollments_te_te-data.json"))); assertNoErrors( trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/validations/enrollments_te_enrollments-data.json"))); } @@ -98,7 +95,6 @@ protected void initTest() throws IOException { void testInvalidEnrollmentPreventsValidEventFromBeingCreated() throws IOException { ImportReport importReport = trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/validations/invalid_enrollment_with_valid_event.json")); assertHasOnlyErrors(importReport, ValidationCode.E1070, ValidationCode.E5000); @@ -108,7 +104,6 @@ void testInvalidEnrollmentPreventsValidEventFromBeingCreated() throws IOExceptio void failValidationWhenTrackedEntityAttributeHasWrongOptionValue() throws IOException { ImportReport importReport = trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/validations/events-with_invalid_option_value.json")); assertHasOnlyErrors(importReport, ValidationCode.E1125); @@ -118,7 +113,6 @@ void failValidationWhenTrackedEntityAttributeHasWrongOptionValue() throws IOExce void successWhenTrackedEntityAttributeHasValidOptionValue() throws IOException { ImportReport importReport = trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/validations/events-with_valid_option_value.json")); assertNoErrors(importReport); @@ -128,7 +122,6 @@ void successWhenTrackedEntityAttributeHasValidOptionValue() throws IOException { void testEventValidationOkAll() throws IOException { ImportReport importReport = trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/validations/events-with-registration.json")); assertNoErrors(importReport); @@ -138,7 +131,6 @@ void testEventValidationOkAll() throws IOException { void testEventValidationOkWithoutAttributeOptionCombo() throws IOException { ImportReport importReport = trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/validations/events-without-attribute-option-combo.json")); assertNoErrors(importReport); @@ -146,24 +138,24 @@ void testEventValidationOkWithoutAttributeOptionCombo() throws IOException { @Test void testTrackerAndProgramEventUpdateSuccess() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/validations/program_and_tracker_events.json"); - TrackerImportParams params = new TrackerImportParams(); - assertNoErrors(trackerImportService.importTracker(params, trackerObjects)); + TrackerImportParams trackerBundleParams = + fromJson("tracker/validations/program_and_tracker_events.json"); + assertNoErrors(trackerImportService.importTracker(trackerBundleParams)); - params.setImportStrategy(UPDATE); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + trackerBundleParams.setImportStrategy(UPDATE); + ImportReport importReport = trackerImportService.importTracker(trackerBundleParams); assertNoErrors(importReport); } @Test void testCantWriteAccessCatCombo() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/validations/events-cat-write-access.json"); - TrackerImportParams params = new TrackerImportParams(); + TrackerImportParams trackerImportParams = + fromJson("tracker/validations/events-cat-write-access.json"); User user = userService.getUser(USER_6); - params.setUserId(user.getUid()); + trackerImportParams.setUser(user); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + ImportReport importReport = trackerImportService.importTracker(trackerImportParams); assertHasOnlyErrors( importReport, @@ -175,45 +167,44 @@ void testCantWriteAccessCatCombo() throws IOException { @Test void testNoWriteAccessToOrg() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/validations/events-with-registration.json"); - TrackerImportParams params = new TrackerImportParams(); + TrackerImportParams trackerBundleParams = + fromJson("tracker/validations/events-with-registration.json"); User user = userService.getUser(USER_2); - params.setUserId(user.getUid()); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + trackerBundleParams.setUser(user); + ImportReport importReport = trackerImportService.importTracker(trackerBundleParams); assertHasOnlyErrors(importReport, ValidationCode.E1000); } @Test void testNonRepeatableProgramStage() throws IOException { - TrackerObjects trackerObjects = + TrackerImportParams trackerImportParams = fromJson("tracker/validations/events_non-repeatable-programstage_part1.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(trackerImportParams); assertNoErrors(importReport); - trackerObjects = fromJson("tracker/validations/events_non-repeatable-programstage_part2.json"); + trackerImportParams = + fromJson("tracker/validations/events_non-repeatable-programstage_part2.json"); - importReport = trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + importReport = trackerImportService.importTracker(trackerImportParams); assertHasOnlyErrors(importReport, ValidationCode.E1039); } @Test void shouldSuccessfullyImportRepeatedEventsInEventProgram() throws IOException { - TrackerObjects trackerObjects = + TrackerImportParams trackerImportParams = fromJson("tracker/validations/program_events_non-repeatable-programstage_part1.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(trackerImportParams); assertNoErrors(importReport); - trackerObjects = + trackerImportParams = fromJson("tracker/validations/program_events_non-repeatable-programstage_part2.json"); - importReport = trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + importReport = trackerImportService.importTracker(trackerImportParams); assertNoErrors(importReport); } @@ -228,7 +219,6 @@ void testWrongScheduledDateString() { void testEventProgramHasNonDefaultCategoryCombo() throws IOException { ImportReport importReport = trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/validations/events_non-default-combo.json")); assertHasOnlyErrors(importReport, ValidationCode.E1055); @@ -238,7 +228,6 @@ void testEventProgramHasNonDefaultCategoryCombo() throws IOException { void testCategoryOptionComboNotFound() throws IOException { ImportReport importReport = trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/validations/events_cant-find-cat-opt-combo.json")); assertHasOnlyErrors(importReport, ValidationCode.E1115); @@ -248,7 +237,6 @@ void testCategoryOptionComboNotFound() throws IOException { void testCategoryOptionComboNotFoundGivenSubsetOfCategoryOptions() throws IOException { ImportReport importReport = trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/validations/events_cant-find-aoc-with-subset-of-cos.json")); assertHasOnlyErrors(importReport, ValidationCode.E1117); @@ -258,7 +246,6 @@ void testCategoryOptionComboNotFoundGivenSubsetOfCategoryOptions() throws IOExce void testCOFoundButAOCNotFound() throws IOException { ImportReport importReport = trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/validations/events_cant-find-aoc-but-co-exists.json")); assertHasOnlyErrors(importReport, ValidationCode.E1115); @@ -268,7 +255,6 @@ void testCOFoundButAOCNotFound() throws IOException { void testCategoryOptionsNotFound() throws IOException { ImportReport importReport = trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/validations/events_cant-find-cat-option.json")); assertHasOnlyErrors(importReport, ValidationCode.E1116); @@ -278,7 +264,6 @@ void testCategoryOptionsNotFound() throws IOException { void testAttributeCategoryOptionNotInProgramCC() throws IOException { ImportReport importReport = trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/validations/events-aoc-not-in-program-cc.json")); assertHasOnlyErrors(importReport, ValidationCode.E1054); @@ -288,7 +273,6 @@ void testAttributeCategoryOptionNotInProgramCC() throws IOException { void testAttributeCategoryOptionAndCODoNotMatch() throws IOException { ImportReport importReport = trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/validations/events-aoc-and-co-dont-match.json")); assertHasOnlyErrors(importReport, ValidationCode.E1117); @@ -299,7 +283,6 @@ void testAttributeCategoryOptionCannotBeFoundForEventProgramCCAndGivenCategoryOp throws IOException { ImportReport importReport = trackerImportService.importTracker( - new TrackerImportParams(), fromJson( "tracker/validations/events_cant-find-cat-option-combo-for-given-cc-and-co.json")); @@ -310,7 +293,6 @@ void testAttributeCategoryOptionCannotBeFoundForEventProgramCCAndGivenCategoryOp void testWrongDatesInCatCombo() throws IOException { ImportReport importReport = trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/validations/events_combo-date-wrong.json")); assertHasOnlyErrors(importReport, ValidationCode.E1056, ValidationCode.E1057); @@ -380,43 +362,41 @@ private void testDeletedEventFails(TrackerImportStrategy importStrategy) { assertNotNull(event); // When -> Soft-delete the event programStageServiceInstance.deleteEvent(event); - TrackerObjects trackerObjects = fromJson("tracker/validations/events-with-notes-data.json"); - TrackerImportParams params = new TrackerImportParams(); - params.setImportStrategy(importStrategy); + TrackerImportParams trackerBundleParams = + fromJson("tracker/validations/events-with-notes-data.json"); + trackerBundleParams.setImportStrategy(importStrategy); // When - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + ImportReport importReport = trackerImportService.importTracker(trackerBundleParams); assertHasOnlyErrors(importReport, ValidationCode.E1082); } @Test void testEventDeleteOk() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/validations/events-with-registration.json"); - TrackerImportParams params = new TrackerImportParams(); + TrackerImportParams trackerBundleParams = + fromJson("tracker/validations/events-with-registration.json"); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + ImportReport importReport = trackerImportService.importTracker(trackerBundleParams); assertNoErrors(importReport); manager.flush(); manager.clear(); - TrackerObjects deleteTrackerObjects = fromJson("tracker/validations/event-data-delete.json"); - params.setImportStrategy(DELETE); + TrackerImportParams paramsDelete = fromJson("tracker/validations/event-data-delete.json"); + paramsDelete.setImportStrategy(DELETE); - ImportReport importReportDelete = - trackerImportService.importTracker(params, deleteTrackerObjects); + ImportReport importReportDelete = trackerImportService.importTracker(paramsDelete); assertNoErrors(importReportDelete); assertEquals(1, importReportDelete.getStats().getDeleted()); } private ImportReport createEvent(String jsonPayload) throws IOException { // Given - TrackerObjects trackerObjects = fromJson(jsonPayload); - TrackerImportParams params = new TrackerImportParams(); - params.setImportStrategy(CREATE_AND_UPDATE); + TrackerImportParams trackerBundleParams = fromJson(jsonPayload); + trackerBundleParams.setImportStrategy(CREATE_AND_UPDATE); // When - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + ImportReport importReport = trackerImportService.importTracker(trackerBundleParams); // Then assertNoErrors(importReport); return importReport; diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/EventSecurityImportValidationTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/EventSecurityImportValidationTest.java index 250bf1a50eb4..f870d492eb17 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/EventSecurityImportValidationTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/EventSecurityImportValidationTest.java @@ -63,7 +63,6 @@ import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.TrackerImportService; import org.hisp.dhis.tracker.imports.TrackerImportStrategy; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.report.ImportReport; import org.hisp.dhis.user.User; import org.hisp.dhis.user.UserService; @@ -128,11 +127,9 @@ protected void initTest() throws IOException { injectAdminUser(); assertNoErrors( trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/validations/enrollments_te_te-data.json"))); assertNoErrors( trackerImportService.importTracker( - new TrackerImportParams(), fromJson("tracker/validations/enrollments_te_enrollments-data.json"))); manager.flush(); } @@ -220,14 +217,14 @@ private void setupMetadata() { @Test void testNoWriteAccessToProgramStage() throws IOException { setupMetadata(); - TrackerObjects trackerObjects = + TrackerImportParams trackerBundleParams = fromJson("tracker/validations/events_error-no-programStage-access.json"); - TrackerImportParams params = new TrackerImportParams(); User user = userService.getUser(USER_3); - params.setUserId(user.getUid()); + trackerBundleParams.setUser(user); user.addOrganisationUnit(organisationUnitA); manager.update(user); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + trackerBundleParams.setUser(user); + ImportReport importReport = trackerImportService.importTracker(trackerBundleParams); assertHasOnlyErrors(importReport, ValidationCode.E1095, ValidationCode.E1096); } @@ -235,15 +232,16 @@ void testNoWriteAccessToProgramStage() throws IOException { @Test void testNoUncompleteEventAuth() throws IOException { setupMetadata(); - TrackerObjects trackerObjects = fromJson("tracker/validations/events_error-no-uncomplete.json"); - TrackerImportParams params = new TrackerImportParams(); + TrackerImportParams params = fromJson("tracker/validations/events_error-no-uncomplete.json"); params.setImportStrategy(TrackerImportStrategy.CREATE); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertNoErrors(importReport); // Change just inserted Event to status COMPLETED... Event zwwuwNp6gVd = programStageServiceInstance.getEvent("ZwwuwNp6gVd"); zwwuwNp6gVd.setStatus(EventStatus.COMPLETED); manager.update(zwwuwNp6gVd); + TrackerImportParams trackerBundleParams = + fromJson("tracker/validations/events_error-no-uncomplete.json"); programA.setPublicAccess(AccessStringHelper.FULL); manager.update(programA); programStageA.setPublicAccess(AccessStringHelper.FULL); @@ -255,9 +253,9 @@ void testNoUncompleteEventAuth() throws IOException { manager.update(user); manager.flush(); manager.clear(); - params.setUserId(user.getUid()); - params.setImportStrategy(TrackerImportStrategy.UPDATE); - importReport = trackerImportService.importTracker(params, trackerObjects); + trackerBundleParams.setUserId(user.getUid()); + trackerBundleParams.setImportStrategy(TrackerImportStrategy.UPDATE); + importReport = trackerImportService.importTracker(trackerBundleParams); assertHasOnlyErrors(importReport, ValidationCode.E1083); } } diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/TeTaEncryptionValidationTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/TeTaEncryptionValidationTest.java index dcf6a55beaba..f8bc741c6541 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/TeTaEncryptionValidationTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/TeTaEncryptionValidationTest.java @@ -35,7 +35,6 @@ import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.TrackerImportService; import org.hisp.dhis.tracker.imports.TrackerImportStrategy; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.report.ImportReport; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -51,33 +50,32 @@ protected void initTest() throws IOException { @Test void testUniqueFailInOrgUnit() throws IOException { - TrackerImportParams params = new TrackerImportParams(); - TrackerObjects trackerObjects = + TrackerImportParams trackerImportParams = fromJson("tracker/validations/te-program_with_tea_unique_data_in_country.json"); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + ImportReport importReport = trackerImportService.importTracker(trackerImportParams); assertNoErrors(importReport); - trackerObjects = + trackerImportParams = fromJson("tracker/validations/te-program_with_tea_unique_data_in_country.json"); - params.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE); - importReport = trackerImportService.importTracker(params, trackerObjects); + trackerImportParams.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE); + importReport = trackerImportService.importTracker(trackerImportParams); assertNoErrors(importReport); - trackerObjects = fromJson("tracker/validations/te-program_with_tea_unique_data_in_region.json"); - importReport = trackerImportService.importTracker(params, trackerObjects); + trackerImportParams = + fromJson("tracker/validations/te-program_with_tea_unique_data_in_region.json"); + importReport = trackerImportService.importTracker(trackerImportParams); assertNoErrors(importReport); } @Test void testUniqueFail() throws IOException { - TrackerObjects trackerObjects = + TrackerImportParams trackerImportParams = fromJson("tracker/validations/te-program_with_tea_unique_data.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(trackerImportParams); assertNoErrors(importReport); - trackerObjects = fromJson("tracker/validations/te-program_with_tea_unique_data2.json"); + trackerImportParams = fromJson("tracker/validations/te-program_with_tea_unique_data2.json"); - importReport = trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + importReport = trackerImportService.importTracker(trackerImportParams); assertHasOnlyErrors(importReport, ValidationCode.E1064); } diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/TeTaValidationTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/TeTaValidationTest.java index f13c725ba1a7..ca60d780448d 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/TeTaValidationTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/TeTaValidationTest.java @@ -45,7 +45,6 @@ import org.hisp.dhis.tracker.TrackerTest; import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.TrackerImportService; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.report.ImportReport; import org.junit.jupiter.api.Test; import org.springframework.beans.factory.annotation.Autowired; @@ -81,9 +80,9 @@ void testTrackedEntityProgramAttributeFileResourceValue() throws IOException { File file = File.createTempFile("file-resource", "test"); fileResourceService.saveFileResource(fileResource, file); assertFalse(fileResource.isAssigned()); - TrackerObjects trackerObjects = + TrackerImportParams trackerImportParams = fromJson("tracker/validations/te-program_with_tea_fileresource_data.json"); - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + trackerImportService.importTracker(trackerImportParams); List trackedEntities = manager.getAll(TrackedEntity.class); assertEquals(1, trackedEntities.size()); TrackedEntity trackedEntity = trackedEntities.get(0); @@ -107,9 +106,9 @@ void testFileAlreadyAssign() throws IOException { File file = File.createTempFile("file-resource", "test"); fileResourceService.saveFileResource(fileResource, file); assertFalse(fileResource.isAssigned()); - TrackerObjects trackerObjects = + TrackerImportParams trackerImportParams = fromJson("tracker/validations/te-program_with_tea_fileresource_data.json"); - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + trackerImportService.importTracker(trackerImportParams); List trackedEntities = manager.getAll(TrackedEntity.class); assertEquals(1, trackedEntities.size()); TrackedEntity trackedEntity = trackedEntities.get(0); @@ -118,18 +117,17 @@ void testFileAlreadyAssign() throws IOException { assertEquals(1, attributeValues.size()); fileResource = fileResourceService.getFileResource(fileResource.getUid()); assertTrue(fileResource.isAssigned()); - trackerObjects = fromJson("tracker/validations/te-program_with_tea_fileresource_data2.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + trackerImportParams = + fromJson("tracker/validations/te-program_with_tea_fileresource_data2.json"); + ImportReport importReport = trackerImportService.importTracker(trackerImportParams); assertHasOnlyErrors(importReport, ValidationCode.E1009); } @Test void testNoFileRef() throws IOException { - TrackerObjects trackerObjects = + TrackerImportParams trackerImportParams = fromJson("tracker/validations/te-program_with_tea_fileresource_data.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(trackerImportParams); assertHasOnlyErrors(importReport, ValidationCode.E1084); List trackedEntities = manager.getAll(TrackedEntity.class); @@ -138,44 +136,40 @@ void testNoFileRef() throws IOException { @Test void testTeaMaxTextValueLength() throws IOException { - TrackerObjects trackerObjects = + TrackerImportParams trackerImportParams = fromJson("tracker/validations/te-program_with_tea_too_long_text_value.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(trackerImportParams); assertHasOnlyErrors(importReport, ValidationCode.E1077); } @Test void testTeaInvalidFormat() throws IOException { - TrackerObjects trackerObjects = + TrackerImportParams trackerImportParams = fromJson("tracker/validations/te-program_with_tea_invalid_format_value.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(trackerImportParams); assertHasOnlyErrors(importReport, ValidationCode.E1085); } @Test void testTeaInvalidImage() throws IOException { - TrackerObjects trackerObjects = + TrackerImportParams trackerImportParams = fromJson("tracker/validations/te-program_with_tea_invalid_image_value.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(trackerImportParams); assertHasOnlyErrors(importReport, ValidationCode.E1085, ValidationCode.E1007); } @Test void testTeaIsNull() throws IOException { - TrackerObjects trackerObjects = + TrackerImportParams trackerImportParams = fromJson("tracker/validations/te-program_with_tea_invalid_value_isnull.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(trackerImportParams); assertHasOnlyErrors(importReport, ValidationCode.E1076); } diff --git a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/TrackedEntityImportValidationTest.java b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/TrackedEntityImportValidationTest.java index d5f62d263553..a32b48287d1c 100644 --- a/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/TrackedEntityImportValidationTest.java +++ b/dhis-2/dhis-test-integration/src/test/java/org/hisp/dhis/tracker/imports/validation/TrackedEntityImportValidationTest.java @@ -45,7 +45,6 @@ import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.TrackerImportService; import org.hisp.dhis.tracker.imports.TrackerImportStrategy; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.hisp.dhis.tracker.imports.report.ImportReport; import org.hisp.dhis.user.User; import org.junit.jupiter.api.Test; @@ -67,53 +66,47 @@ protected void initTest() throws IOException { @Test void failValidationWhenTrackedEntityAttributeHasWrongOptionValue() throws IOException { - TrackerObjects trackerObjects = - fromJson("tracker/validations/te-with_invalid_option_value.json"); + TrackerImportParams params = fromJson("tracker/validations/te-with_invalid_option_value.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertHasOnlyErrors(importReport, ValidationCode.E1125); } @Test void successValidationWhenTrackedEntityAttributeHasValidOptionValue() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/validations/te-with_valid_option_value.json"); + TrackerImportParams params = fromJson("tracker/validations/te-with_valid_option_value.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertNoErrors(importReport); } @Test void failValidationWhenTrackedEntityAttributesHaveSameUniqueValues() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/validations/te-with_unique_attributes.json"); + TrackerImportParams params = fromJson("tracker/validations/te-with_unique_attributes.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertHasErrors(importReport, 2, ValidationCode.E1064); } @Test void testTeValidationOkAll() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/validations/te-data_with_different_ou.json"); + TrackerImportParams params = fromJson("tracker/validations/te-data_with_different_ou.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertNoErrors(importReport); } @Test void testNoCreateTeiAccessOutsideCaptureScopeOu() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/validations/te-data_with_different_ou.json"); - TrackerImportParams params = new TrackerImportParams(); + TrackerImportParams params = fromJson("tracker/validations/te-data_with_different_ou.json"); User user = userService.getUser(USER_7); - params.setUserId(user.getUid()); + params.setUser(user); params.setAtomicMode(AtomicMode.OBJECT); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertHasOnlyErrors(importReport, ValidationCode.E1000); assertEquals(2, importReport.getStats().getCreated()); assertEquals(1, importReport.getStats().getIgnored()); @@ -121,9 +114,8 @@ void testNoCreateTeiAccessOutsideCaptureScopeOu() throws IOException { @Test void testUpdateAccessInSearchScopeOu() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/validations/te-data_with_different_ou.json"); - TrackerImportParams params = new TrackerImportParams(); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + TrackerImportParams params = fromJson("tracker/validations/te-data_with_different_ou.json"); + ImportReport importReport = trackerImportService.importTracker(params); assertNoErrors(importReport); assertEquals(3, importReport.getStats().getCreated()); // For some reason teiSearchOrgunits is not created properly from @@ -133,30 +125,29 @@ void testUpdateAccessInSearchScopeOu() throws IOException { user.setTeiSearchOrganisationUnits(new HashSet<>(user.getDataViewOrganisationUnits())); userService.updateUser(user); dbmsManager.clearSession(); - trackerObjects = fromJson("tracker/validations/te-data_with_different_ou.json"); + params = fromJson("tracker/validations/te-data_with_different_ou.json"); user = userService.getUser(USER_8); - params.setUserId(user.getUid()); + params.setUser(user); params.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE); params.setAtomicMode(AtomicMode.OBJECT); - importReport = trackerImportService.importTracker(params, trackerObjects); + importReport = trackerImportService.importTracker(params); assertNoErrors(importReport); assertEquals(3, importReport.getStats().getUpdated()); } @Test void testNoUpdateAccessOutsideSearchScopeOu() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/validations/te-data_with_different_ou.json"); - TrackerImportParams params = new TrackerImportParams(); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + TrackerImportParams params = fromJson("tracker/validations/te-data_with_different_ou.json"); + ImportReport importReport = trackerImportService.importTracker(params); assertNoErrors(importReport); assertEquals(3, importReport.getStats().getCreated()); dbmsManager.clearSession(); - trackerObjects = fromJson("tracker/validations/te-data_with_different_ou.json"); + params = fromJson("tracker/validations/te-data_with_different_ou.json"); User user = userService.getUser(USER_7); - params.setUserId(user.getUid()); + params.setUser(user); params.setImportStrategy(TrackerImportStrategy.CREATE_AND_UPDATE); params.setAtomicMode(AtomicMode.OBJECT); - importReport = trackerImportService.importTracker(params, trackerObjects); + importReport = trackerImportService.importTracker(params); assertHasOnlyErrors(importReport, ValidationCode.E1003); assertEquals(2, importReport.getStats().getUpdated()); assertEquals(1, importReport.getStats().getIgnored()); @@ -164,89 +155,81 @@ void testNoUpdateAccessOutsideSearchScopeOu() throws IOException { @Test void testNoWriteAccessInAcl() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/validations/te-data_ok.json"); - TrackerImportParams params = new TrackerImportParams(); + TrackerImportParams params = fromJson("tracker/validations/te-data_ok.json"); User user = userService.getUser(USER_1); - params.setUserId(user.getUid()); + params.setUser(user); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertHasErrors(importReport, 13, ValidationCode.E1001); } @Test void testWriteAccessInAclViaUserGroup() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/validations/te-data_ok.json"); - TrackerImportParams params = new TrackerImportParams(); + TrackerImportParams params = fromJson("tracker/validations/te-data_ok.json"); User user = userService.getUser(USER_3); params.setUserId(user.getUid()); - params.setUserId(user.getUid()); + params.setUser(user); user.setPassword("user4password"); injectSecurityContext(user); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertNoErrors(importReport); } @Test void testGeoOk() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/validations/te-data_error_geo-ok.json"); - TrackerImportParams params = new TrackerImportParams(); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + TrackerImportParams params = fromJson("tracker/validations/te-data_error_geo-ok.json"); + ImportReport importReport = trackerImportService.importTracker(params); assertNoErrors(importReport); } @Test void testTeAttrNonExistentAttr() throws IOException { - TrackerObjects trackerObjects = + TrackerImportParams params = fromJson("tracker/validations/te-data_error_attr-non-existing.json"); - TrackerImportParams params = new TrackerImportParams(); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertHasErrors(importReport, 2, ValidationCode.E1006); } @Test void testDeleteCascadeEnrollments() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/validations/enrollments_te_te-data.json"); - TrackerImportParams params = new TrackerImportParams(); - assertNoErrors(trackerImportService.importTracker(params, trackerObjects)); + TrackerImportParams params = fromJson("tracker/validations/enrollments_te_te-data.json"); + assertNoErrors(trackerImportService.importTracker(params)); importEnrollments(); manager.flush(); manager.clear(); - trackerObjects = fromJson("tracker/validations/enrollments_te_te-data.json"); + params = fromJson("tracker/validations/enrollments_te_te-data.json"); User user2 = userService.getUser(USER_4); - params.setUserId(user2.getUid()); + params.setUser(user2); params.setImportStrategy(TrackerImportStrategy.DELETE); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertHasErrors(importReport, 2, ValidationCode.E1100); } @Test void testTeDeleteOk() throws IOException { - TrackerObjects trackerObjects = fromJson("tracker/validations/te-data.json"); - TrackerImportParams params = new TrackerImportParams(); - ImportReport importReport = trackerImportService.importTracker(params, trackerObjects); + TrackerImportParams params = fromJson("tracker/validations/te-data.json"); + ImportReport importReport = trackerImportService.importTracker(params); assertNoErrors(importReport); manager.flush(); manager.clear(); - TrackerObjects deleteTrackerObjects = fromJson("tracker/validations/te-data-delete.json"); - params.setImportStrategy(TrackerImportStrategy.DELETE); + TrackerImportParams paramsDelete = fromJson("tracker/validations/te-data-delete.json"); + paramsDelete.setImportStrategy(TrackerImportStrategy.DELETE); - ImportReport importReportDelete = - trackerImportService.importTracker(params, deleteTrackerObjects); + ImportReport importReportDelete = trackerImportService.importTracker(paramsDelete); assertNoErrors(importReportDelete); assertEquals(1, importReportDelete.getStats().getDeleted()); } protected void importEnrollments() throws IOException { - TrackerObjects trackerObjects = + TrackerImportParams params = fromJson("tracker/validations/enrollments_te_enrollments-data.json"); - ImportReport importReport = - trackerImportService.importTracker(new TrackerImportParams(), trackerObjects); + ImportReport importReport = trackerImportService.importTracker(params); assertNoErrors(importReport); } } diff --git a/dhis-2/dhis-test-web-api/src/test/java/org/hisp/dhis/webapi/controller/tracker/imports/TrackerImportControllerTest.java b/dhis-2/dhis-test-web-api/src/test/java/org/hisp/dhis/webapi/controller/tracker/imports/TrackerImportControllerTest.java index 62d15b1863a8..d0a0837fe6bf 100644 --- a/dhis-2/dhis-test-web-api/src/test/java/org/hisp/dhis/webapi/controller/tracker/imports/TrackerImportControllerTest.java +++ b/dhis-2/dhis-test-web-api/src/test/java/org/hisp/dhis/webapi/controller/tracker/imports/TrackerImportControllerTest.java @@ -28,7 +28,7 @@ package org.hisp.dhis.webapi.controller.tracker.imports; import org.hisp.dhis.web.HttpStatus; -import org.hisp.dhis.webapi.DhisControllerIntegrationTest; +import org.hisp.dhis.webapi.DhisControllerConvenienceTest; import org.junit.jupiter.api.Test; /** @@ -37,7 +37,7 @@ * * @author Jan Bernitt */ -class TrackerImportControllerTest extends DhisControllerIntegrationTest { +class TrackerImportControllerTest extends DhisControllerConvenienceTest { @Test void shouldSucceedWhenAllValidParametersArePassed() { assertWebMessage( diff --git a/dhis-2/dhis-web-api/pom.xml b/dhis-2/dhis-web-api/pom.xml index 10823ccd10f0..6a0c8894417b 100644 --- a/dhis-2/dhis-web-api/pom.xml +++ b/dhis-2/dhis-web-api/pom.xml @@ -214,6 +214,10 @@ org.hisp.dhis.rules rule-engine + + org.hisp.dhis + dhis-support-artemis + org.hisp.dhis dhis-service-acl diff --git a/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/imports/Body.java b/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/imports/Body.java index b60d474a7065..6ab8f2d79861 100644 --- a/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/imports/Body.java +++ b/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/imports/Body.java @@ -29,7 +29,6 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; -import java.io.Serializable; import java.util.ArrayList; import java.util.List; import lombok.AllArgsConstructor; @@ -55,7 +54,7 @@ @NoArgsConstructor @AllArgsConstructor @JsonDeserialize(converter = BodyConverter.class) -class Body implements Serializable { +class Body { /** Tracked entities to import. */ @JsonProperty @Builder.Default private final List trackedEntities = new ArrayList<>(); diff --git a/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/imports/TrackerAsyncImporter.java b/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/imports/TrackerAsyncImporter.java new file mode 100644 index 000000000000..50ecec4368ad --- /dev/null +++ b/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/imports/TrackerAsyncImporter.java @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2004-2022, University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * Neither the name of the HISP project nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.hisp.dhis.webapi.controller.tracker.imports; + +import javax.annotation.Nonnull; +import lombok.RequiredArgsConstructor; +import org.hisp.dhis.artemis.MessageManager; +import org.hisp.dhis.artemis.Topics; +import org.hisp.dhis.security.AuthenticationSerializer; +import org.hisp.dhis.tracker.imports.TrackerImportParams; +import org.hisp.dhis.tracker.imports.job.TrackerMessage; +import org.hisp.dhis.tracker.imports.report.ImportReport; +import org.springframework.security.core.Authentication; +import org.springframework.stereotype.Component; + +/** + * @author Luca Cambi + */ +@Component +@RequiredArgsConstructor +public class TrackerAsyncImporter { + @Nonnull private final MessageManager messageManager; + + public ImportReport importTracker( + TrackerImportParams params, Authentication authentication, String uid) { + TrackerMessage trackerMessage = + TrackerMessage.builder() + .trackerImportParams(params) + .authentication(AuthenticationSerializer.serialize(authentication)) + .uid(uid) + .build(); + + messageManager.sendQueue(Topics.TRACKER_IMPORT_JOB_TOPIC_NAME, trackerMessage); + + return null; // empty report is not + // returned + // in async creation + } +} diff --git a/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/imports/TrackerImportController.java b/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/imports/TrackerImportController.java index e882355a4df9..2610b91c51ac 100644 --- a/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/imports/TrackerImportController.java +++ b/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/imports/TrackerImportController.java @@ -32,33 +32,28 @@ import static org.hisp.dhis.webapi.utils.ContextUtils.setNoStore; import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE; -import java.io.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; -import java.io.ObjectOutputStream; import java.util.Deque; import java.util.List; import java.util.Optional; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import lombok.RequiredArgsConstructor; +import org.hisp.dhis.common.CodeGenerator; import org.hisp.dhis.common.DhisApiVersion; import org.hisp.dhis.common.OpenApi; import org.hisp.dhis.commons.util.StreamUtils; import org.hisp.dhis.dxf2.webmessage.WebMessage; -import org.hisp.dhis.feedback.ConflictException; import org.hisp.dhis.feedback.NotFoundException; import org.hisp.dhis.scheduling.JobConfiguration; -import org.hisp.dhis.scheduling.JobConfigurationService; -import org.hisp.dhis.scheduling.JobSchedulerService; import org.hisp.dhis.scheduling.JobType; import org.hisp.dhis.system.notification.Notification; import org.hisp.dhis.system.notification.Notifier; import org.hisp.dhis.tracker.imports.TrackerBundleReportMode; import org.hisp.dhis.tracker.imports.TrackerImportParams; import org.hisp.dhis.tracker.imports.TrackerImportService; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; +import org.hisp.dhis.tracker.imports.job.TrackerJobWebMessageResponse; import org.hisp.dhis.tracker.imports.report.ImportReport; import org.hisp.dhis.tracker.imports.report.Status; import org.hisp.dhis.user.CurrentUser; @@ -70,7 +65,7 @@ import org.locationtech.jts.io.ParseException; import org.springframework.http.HttpStatus; import org.springframework.http.ResponseEntity; -import org.springframework.util.MimeType; +import org.springframework.security.core.context.SecurityContextHolder; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; @@ -92,60 +87,33 @@ public class TrackerImportController { static final String TRACKER_JOB_ADDED = "Tracker job added"; + private final TrackerSyncImporter syncImporter; + + private final TrackerAsyncImporter asyncImporter; + private final TrackerImportService trackerImportService; private final CsvService csvEventService; private final Notifier notifier; - private final JobSchedulerService jobSchedulerService; - - private final JobConfigurationService jobConfigurationService; - @PostMapping(value = "", consumes = APPLICATION_JSON_VALUE, produces = APPLICATION_JSON_VALUE) @ResponseBody public WebMessage asyncPostJsonTracker( HttpServletRequest request, RequestParams requestParams, @CurrentUser User currentUser, - @RequestBody Body body) - throws ConflictException, NotFoundException, IOException { + @RequestBody Body body) { + String jobId = CodeGenerator.generateUid(); TrackerImportParams trackerImportParams = - TrackerImportParamsMapper.trackerImportParams(currentUser.getUid(), requestParams); - TrackerObjects trackerObjects = - TrackerImportParamsMapper.trackerObjects(body, trackerImportParams.getIdSchemes()); - - return startAsyncTracker( - trackerImportParams, - MimeType.valueOf("application/json"), - trackerObjects, - currentUser, - request); - } + TrackerImportParamsMapper.trackerImportParams( + true, jobId, currentUser.getUid(), requestParams, body); - private WebMessage startAsyncTracker( - TrackerImportParams params, - MimeType contentType, - TrackerObjects trackerObjects, - User user, - HttpServletRequest request) - throws IOException, ConflictException, NotFoundException { - JobConfiguration config = new JobConfiguration(JobType.TRACKER_IMPORT_JOB); - config.setExecutedBy(user.getUid()); - config.setJobParameters(params); - ByteArrayOutputStream baos = new ByteArrayOutputStream(); - ObjectOutputStream oos = new ObjectOutputStream(baos); + asyncImporter.importTracker( + trackerImportParams, SecurityContextHolder.getContext().getAuthentication(), jobId); - oos.writeObject(trackerObjects); - - oos.flush(); - oos.close(); - - InputStream is = new ByteArrayInputStream(baos.toByteArray()); - - jobSchedulerService.executeNow(jobConfigurationService.create(config, contentType, is)); - String jobId = config.getUid(); String location = ContextUtils.getRootPath(request) + "/tracker/jobs/" + jobId; + return ok(TRACKER_JOB_ADDED) .setLocation("/tracker/jobs/" + jobId) .setResponse(TrackerJobWebMessageResponse.builder().id(jobId).location(location).build()); @@ -157,13 +125,12 @@ private WebMessage startAsyncTracker( params = {"async=false"}) public ResponseEntity syncPostJsonTracker( RequestParams requestParams, @CurrentUser User currentUser, @RequestBody Body body) { - TrackerImportParams params = - TrackerImportParamsMapper.trackerImportParams(currentUser.getUid(), requestParams); - TrackerObjects trackerObjects = - TrackerImportParamsMapper.trackerObjects(body, params.getIdSchemes()); - ImportReport importReport = - trackerImportService.buildImportReport( - trackerImportService.importTracker(params, trackerObjects), params.getReportMode()); + String jobId = CodeGenerator.generateUid(); + TrackerImportParams trackerImportParams = + TrackerImportParamsMapper.trackerImportParams( + false, jobId, currentUser.getUid(), requestParams, body); + + ImportReport importReport = syncImporter.importTracker(trackerImportParams); ResponseEntity.BodyBuilder builder = importReport.getStatus() == Status.ERROR @@ -183,25 +150,26 @@ public WebMessage asyncPostCsvTracker( RequestParams importRequest, @CurrentUser User currentUser, @RequestParam(required = false, defaultValue = "true") boolean skipFirst) - throws IOException, ParseException, ConflictException, NotFoundException { + throws IOException, ParseException { InputStream inputStream = StreamUtils.wrapAndCheckCompressionFormat(request.getInputStream()); List events = csvEventService.read(inputStream, skipFirst); Body body = Body.builder().events(events).build(); + String jobId = CodeGenerator.generateUid(); TrackerImportParams trackerImportParams = - TrackerImportParamsMapper.trackerImportParams(currentUser.getUid(), importRequest); + TrackerImportParamsMapper.trackerImportParams( + true, jobId, currentUser.getUid(), importRequest, body); - TrackerObjects trackerObjects = - TrackerImportParamsMapper.trackerObjects(body, trackerImportParams.getIdSchemes()); + asyncImporter.importTracker( + trackerImportParams, SecurityContextHolder.getContext().getAuthentication(), jobId); + + String location = ContextUtils.getRootPath(request) + "/tracker/jobs/" + jobId; - return startAsyncTracker( - trackerImportParams, - MimeType.valueOf("application/csv"), - trackerObjects, - currentUser, - request); + return ok(TRACKER_JOB_ADDED) + .setLocation("/tracker/jobs/" + jobId) + .setResponse(TrackerJobWebMessageResponse.builder().id(jobId).location(location).build()); } @PostMapping( @@ -221,14 +189,12 @@ public ResponseEntity syncPostCsvTracker( List events = csvEventService.read(inputStream, skipFirst); Body body = Body.builder().events(events).build(); + String jobId = CodeGenerator.generateUid(); TrackerImportParams trackerImportParams = - TrackerImportParamsMapper.trackerImportParams(currentUser.getUid(), importRequest); - TrackerObjects trackerObjects = - TrackerImportParamsMapper.trackerObjects(body, trackerImportParams.getIdSchemes()); - ImportReport importReport = - trackerImportService.buildImportReport( - trackerImportService.importTracker(trackerImportParams, trackerObjects), - trackerImportParams.getReportMode()); + TrackerImportParamsMapper.trackerImportParams( + false, jobId, currentUser.getUid(), importRequest, body); + + ImportReport importReport = syncImporter.importTracker(trackerImportParams); ResponseEntity.BodyBuilder builder = importReport.getStatus() == Status.ERROR @@ -255,6 +221,6 @@ public ImportReport getJobReport( return Optional.ofNullable(notifier.getJobSummaryByJobId(JobType.TRACKER_IMPORT_JOB, uid)) .map(report -> trackerImportService.buildImportReport((ImportReport) report, reportMode)) - .orElseThrow(() -> new NotFoundException("Summary for job " + uid + " does not exist")); + .orElseThrow(() -> new NotFoundException(JobConfiguration.class, uid)); } } diff --git a/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/imports/TrackerImportJob.java b/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/imports/TrackerImportJob.java deleted file mode 100644 index 518fcf6b72fe..000000000000 --- a/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/imports/TrackerImportJob.java +++ /dev/null @@ -1,102 +0,0 @@ -/* - * Copyright (c) 2004-2022, University of Oslo - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions are met: - * Redistributions of source code must retain the above copyright notice, this - * list of conditions and the following disclaimer. - * - * Redistributions in binary form must reproduce the above copyright notice, - * this list of conditions and the following disclaimer in the documentation - * and/or other materials provided with the distribution. - * Neither the name of the HISP project nor the names of its contributors may - * be used to endorse or promote products derived from this software without - * specific prior written permission. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND - * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED - * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR - * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; - * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON - * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT - * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS - * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ -package org.hisp.dhis.webapi.controller.tracker.imports; - -import java.io.IOException; -import java.io.InputStream; -import java.io.ObjectInputStream; -import java.util.function.Consumer; -import lombok.RequiredArgsConstructor; -import lombok.extern.slf4j.Slf4j; -import org.hisp.dhis.fileresource.FileResource; -import org.hisp.dhis.fileresource.FileResourceService; -import org.hisp.dhis.scheduling.Job; -import org.hisp.dhis.scheduling.JobConfiguration; -import org.hisp.dhis.scheduling.JobProgress; -import org.hisp.dhis.scheduling.JobType; -import org.hisp.dhis.system.notification.Notifier; -import org.hisp.dhis.tracker.imports.TrackerImportParams; -import org.hisp.dhis.tracker.imports.TrackerImportService; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; -import org.hisp.dhis.tracker.imports.report.ImportReport; -import org.hisp.dhis.tracker.imports.report.Stats; -import org.hisp.dhis.tracker.imports.report.Status; -import org.springframework.stereotype.Component; - -@Slf4j -@Component -@RequiredArgsConstructor -public class TrackerImportJob implements Job { - private final TrackerImportService trackerImportService; - private final FileResourceService fileResourceService; - private final Notifier notifier; - - @Override - public JobType getJobType() { - return JobType.TRACKER_IMPORT_JOB; - } - - @Override - public void execute(JobConfiguration config, JobProgress progress) { - progress.startingProcess("Tracker import started"); - TrackerImportParams params = (TrackerImportParams) config.getJobParameters(); - progress.startingStage("Loading file resource"); - FileResource data = - progress.runStage(() -> fileResourceService.getFileResource(config.getUid())); - progress.startingStage("Loading file content"); - try (InputStream input = - progress.runStage(() -> fileResourceService.getFileResourceContent(data))) { - ImportReport report = - trackerImportService.importTracker(params, toTrackerObjects(input), progress); - if (report == null) { - progress.failedProcess("Import failed, no summary available"); - return; - } - notifier.addJobSummary(config, report, ImportReport.class); - Stats stats = report.getStats(); - Consumer endProcess = - report.getStatus() == Status.ERROR ? progress::failedProcess : progress::completedProcess; - endProcess.accept( - "Import complete with status %s, %d created, %d updated, %d deleted, %d ignored" - .formatted( - report.getStatus(), - stats.getCreated(), - stats.getUpdated(), - stats.getDeleted(), - stats.getIgnored())); - } catch (Exception ex) { - progress.failedProcess(ex); - } - } - - private TrackerObjects toTrackerObjects(InputStream input) - throws IOException, ClassNotFoundException { - ObjectInputStream ois = new ObjectInputStream(input); - return (TrackerObjects) ois.readObject(); - } -} diff --git a/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/imports/TrackerImportParamsMapper.java b/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/imports/TrackerImportParamsMapper.java index e1de076a1d4b..5eb64ff0609d 100644 --- a/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/imports/TrackerImportParamsMapper.java +++ b/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/imports/TrackerImportParamsMapper.java @@ -27,10 +27,11 @@ */ package org.hisp.dhis.webapi.controller.tracker.imports; +import org.hisp.dhis.scheduling.JobConfiguration; +import org.hisp.dhis.scheduling.JobType; import org.hisp.dhis.tracker.imports.TrackerIdSchemeParam; import org.hisp.dhis.tracker.imports.TrackerIdSchemeParams; import org.hisp.dhis.tracker.imports.TrackerImportParams; -import org.hisp.dhis.tracker.imports.domain.TrackerObjects; import org.mapstruct.factory.Mappers; public class TrackerImportParamsMapper { @@ -45,19 +46,10 @@ public class TrackerImportParamsMapper { private static final RelationshipMapper RELATIONSHIP_MAPPER = Mappers.getMapper(RelationshipMapper.class); - public static TrackerObjects trackerObjects(Body body, TrackerIdSchemeParams idSchemeParams) { - return TrackerObjects.builder() - .trackedEntities( - TRACKED_ENTITY_MAPPER.fromCollection(body.getTrackedEntities(), idSchemeParams)) - .enrollments(ENROLLMENT_MAPPER.fromCollection(body.getEnrollments(), idSchemeParams)) - .events(EVENT_MAPPER.fromCollection(body.getEvents(), idSchemeParams)) - .relationships(RELATIONSHIP_MAPPER.fromCollection(body.getRelationships(), idSchemeParams)) - .build(); - } - private TrackerImportParamsMapper() {} - public static TrackerImportParams trackerImportParams(String userId, RequestParams request) { + public static TrackerImportParams trackerImportParams( + boolean isAsync, String jobId, String userId, RequestParams request, Body params) { TrackerIdSchemeParam defaultIdSchemeParam = request.getIdScheme(); TrackerIdSchemeParams idSchemeParams = TrackerIdSchemeParams.builder() @@ -85,7 +77,21 @@ public static TrackerImportParams trackerImportParams(String userId, RequestPara .skipSideEffects(request.isSkipSideEffects()) .skipRuleEngine(request.isSkipRuleEngine()) .reportMode(request.getReportMode()) - .userId(userId); + .userId(userId) + .trackedEntities( + TRACKED_ENTITY_MAPPER.fromCollection(params.getTrackedEntities(), idSchemeParams)) + .enrollments(ENROLLMENT_MAPPER.fromCollection(params.getEnrollments(), idSchemeParams)) + .events(EVENT_MAPPER.fromCollection(params.getEvents(), idSchemeParams)) + .relationships( + RELATIONSHIP_MAPPER.fromCollection(params.getRelationships(), idSchemeParams)); + + if (!isAsync) { + JobConfiguration jobConfiguration = + new JobConfiguration("", JobType.TRACKER_IMPORT_JOB, userId); + jobConfiguration.setUid(jobId); + paramsBuilder.jobConfiguration(jobConfiguration); + } + return paramsBuilder.build(); } diff --git a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/TrackerObjects.java b/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/imports/TrackerSyncImporter.java similarity index 63% rename from dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/TrackerObjects.java rename to dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/imports/TrackerSyncImporter.java index 02a8a89dba76..c48ae7a81316 100644 --- a/dhis-2/dhis-services/dhis-service-tracker/src/main/java/org/hisp/dhis/tracker/imports/domain/TrackerObjects.java +++ b/dhis-2/dhis-web-api/src/main/java/org/hisp/dhis/webapi/controller/tracker/imports/TrackerSyncImporter.java @@ -25,30 +25,27 @@ * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -package org.hisp.dhis.tracker.imports.domain; +package org.hisp.dhis.webapi.controller.tracker.imports; -import com.fasterxml.jackson.annotation.JsonProperty; -import java.io.Serializable; -import java.util.ArrayList; -import java.util.List; -import lombok.AllArgsConstructor; -import lombok.Builder; -import lombok.EqualsAndHashCode; -import lombok.Getter; -import lombok.NoArgsConstructor; -import lombok.ToString; +import javax.annotation.Nonnull; +import lombok.RequiredArgsConstructor; +import org.hisp.dhis.tracker.imports.TrackerImportParams; +import org.hisp.dhis.tracker.imports.TrackerImportService; +import org.hisp.dhis.tracker.imports.report.ImportReport; +import org.springframework.stereotype.Component; -@Getter -@ToString -@EqualsAndHashCode -@Builder -@NoArgsConstructor -@AllArgsConstructor -public class TrackerObjects implements Serializable { - @JsonProperty @Builder.Default - private final List trackedEntities = new ArrayList<>(); +/** + * @author Luca Cambi + */ +@Component +@RequiredArgsConstructor +public class TrackerSyncImporter { + + @Nonnull private final TrackerImportService trackerImportService; + + public ImportReport importTracker(TrackerImportParams params) { + ImportReport importReport = trackerImportService.importTracker(params); - @JsonProperty @Builder.Default private final List enrollments = new ArrayList<>(); - @JsonProperty @Builder.Default private final List events = new ArrayList<>(); - @JsonProperty @Builder.Default private final List relationships = new ArrayList<>(); + return trackerImportService.buildImportReport(importReport, params.getReportMode()); + } } diff --git a/dhis-2/dhis-web-api/src/test/java/org/hisp/dhis/webapi/controller/tracker/imports/TrackerImportControllerTest.java b/dhis-2/dhis-web-api/src/test/java/org/hisp/dhis/webapi/controller/tracker/imports/TrackerImportControllerTest.java index 67610dbe6e05..acb52aa69679 100644 --- a/dhis-2/dhis-web-api/src/test/java/org/hisp/dhis/webapi/controller/tracker/imports/TrackerImportControllerTest.java +++ b/dhis-2/dhis-web-api/src/test/java/org/hisp/dhis/webapi/controller/tracker/imports/TrackerImportControllerTest.java @@ -29,7 +29,6 @@ import static java.util.Collections.singletonList; import static org.hamcrest.Matchers.hasSize; -import static org.hisp.dhis.scheduling.JobType.TRACKER_IMPORT_JOB; import static org.hisp.dhis.webapi.controller.tracker.imports.TrackerImportController.TRACKER_JOB_ADDED; import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.fail; @@ -51,8 +50,7 @@ import org.hisp.dhis.feedback.NotFoundException; import org.hisp.dhis.render.DefaultRenderService; import org.hisp.dhis.render.RenderService; -import org.hisp.dhis.scheduling.JobConfigurationService; -import org.hisp.dhis.scheduling.JobSchedulerService; +import org.hisp.dhis.scheduling.JobType; import org.hisp.dhis.schema.SchemaService; import org.hisp.dhis.system.notification.Notification; import org.hisp.dhis.system.notification.Notifier; @@ -60,6 +58,7 @@ import org.hisp.dhis.tracker.imports.report.ImportReport; import org.hisp.dhis.tracker.imports.report.PersistenceReport; import org.hisp.dhis.tracker.imports.report.Status; +import org.hisp.dhis.tracker.imports.report.TimingsStats; import org.hisp.dhis.tracker.imports.report.ValidationReport; import org.hisp.dhis.webapi.controller.CrudControllerAdvice; import org.hisp.dhis.webapi.controller.tracker.ControllerSupport; @@ -86,13 +85,13 @@ class TrackerImportControllerTest { @Mock private DefaultTrackerImportService trackerImportService; - @Mock private CsvService csvEventService; + @Mock private TrackerSyncImporter syncImporter; - @Mock private Notifier notifier; + @Mock private TrackerAsyncImporter asyncImporter; - @Mock private JobSchedulerService jobSchedulerService; + @Mock private CsvService csvEventService; - @Mock private JobConfigurationService jobConfigurationService; + @Mock private Notifier notifier; private RenderService renderService; @@ -107,11 +106,7 @@ public void setUp() { // Controller under test final TrackerImportController controller = new TrackerImportController( - trackerImportService, - csvEventService, - notifier, - jobSchedulerService, - jobConfigurationService); + syncImporter, asyncImporter, trackerImportService, csvEventService, notifier); mockMvc = MockMvcBuilders.standaloneSetup(controller) @@ -145,17 +140,19 @@ void verifyAsyncForCsv() throws Exception { .andExpect(content().contentType("application/json")); verify(csvEventService).read(any(), eq(true)); + verify(asyncImporter).importTracker(any(), any(), any()); } @Test void verifySyncResponseShouldBeOkWhenImportReportStatusIsOk() throws Exception { // When - when(trackerImportService.buildImportReport(any(), any())) + when(syncImporter.importTracker(any())) .thenReturn( ImportReport.withImportCompleted( Status.OK, PersistenceReport.emptyReport(), ValidationReport.emptyReport(), + new TimingsStats(), new HashMap<>())); // Then @@ -173,7 +170,7 @@ void verifySyncResponseShouldBeOkWhenImportReportStatusIsOk() throws Exception { .getResponse() .getContentAsString(); - verify(trackerImportService).importTracker(any(), any()); + verify(syncImporter).importTracker(any()); try { renderService.fromJson(contentAsString, ImportReport.class); @@ -185,12 +182,13 @@ void verifySyncResponseShouldBeOkWhenImportReportStatusIsOk() throws Exception { @Test void verifySyncResponseForCsvShouldBeOkWhenImportReportStatusIsOk() throws Exception { // When - when(trackerImportService.buildImportReport(any(), any())) + when(syncImporter.importTracker(any())) .thenReturn( ImportReport.withImportCompleted( Status.OK, PersistenceReport.emptyReport(), ValidationReport.emptyReport(), + new TimingsStats(), new HashMap<>())); // Then @@ -208,7 +206,7 @@ void verifySyncResponseForCsvShouldBeOkWhenImportReportStatusIsOk() throws Excep .getContentAsString(); verify(csvEventService).read(any(), eq(true)); - verify(trackerImportService).importTracker(any(), any()); + verify(syncImporter).importTracker(any()); try { renderService.fromJson(contentAsString, ImportReport.class); @@ -221,8 +219,10 @@ void verifySyncResponseForCsvShouldBeOkWhenImportReportStatusIsOk() throws Excep void verifySyncResponseShouldBeConflictWhenImportReportStatusIsError() throws Exception { String errorMessage = "errorMessage"; // When - when(trackerImportService.buildImportReport(any(), any())) - .thenReturn(ImportReport.withError(errorMessage, ValidationReport.emptyReport())); + when(syncImporter.importTracker(any())) + .thenReturn( + ImportReport.withError( + "errorMessage", ValidationReport.emptyReport(), new TimingsStats())); // Then String contentAsString = @@ -239,7 +239,7 @@ void verifySyncResponseShouldBeConflictWhenImportReportStatusIsError() throws Ex .getResponse() .getContentAsString(); - verify(trackerImportService).importTracker(any(), any()); + verify(syncImporter).importTracker(any()); try { renderService.fromJson(contentAsString, ImportReport.class); @@ -252,8 +252,10 @@ void verifySyncResponseShouldBeConflictWhenImportReportStatusIsError() throws Ex void verifySyncResponseForCsvShouldBeConflictWhenImportReportStatusIsError() throws Exception { String errorMessage = "errorMessage"; // When - when(trackerImportService.buildImportReport(any(), any())) - .thenReturn(ImportReport.withError(errorMessage, ValidationReport.emptyReport())); + when(syncImporter.importTracker(any())) + .thenReturn( + ImportReport.withError( + "errorMessage", ValidationReport.emptyReport(), new TimingsStats())); // Then String contentAsString = @@ -270,7 +272,7 @@ void verifySyncResponseForCsvShouldBeConflictWhenImportReportStatusIsError() thr .getContentAsString(); verify(csvEventService).read(any(), eq(true)); - verify(trackerImportService).importTracker(any(), any()); + verify(syncImporter).importTracker(any()); try { renderService.fromJson(contentAsString, ImportReport.class); @@ -283,7 +285,7 @@ void verifySyncResponseForCsvShouldBeConflictWhenImportReportStatusIsError() thr void verifyShouldFindJob() throws Exception { String uid = CodeGenerator.generateUid(); // When - when(notifier.getNotificationsByJobId(TRACKER_IMPORT_JOB, uid)) + when(notifier.getNotificationsByJobId(JobType.TRACKER_IMPORT_JOB, uid)) .thenReturn(new LinkedList<>(singletonList(new Notification()))); // Then @@ -303,7 +305,7 @@ void verifyShouldFindJob() throws Exception { .getResponse() .getContentAsString(); - verify(notifier).getNotificationsByJobId(TRACKER_IMPORT_JOB, uid); + verify(notifier).getNotificationsByJobId(JobType.TRACKER_IMPORT_JOB, uid); } @Test @@ -315,10 +317,11 @@ void verifyShouldFindJobReport() throws Exception { Status.OK, PersistenceReport.emptyReport(), ValidationReport.emptyReport(), + new TimingsStats(), new HashMap<>()); // When - when(notifier.getJobSummaryByJobId(TRACKER_IMPORT_JOB, uid)).thenReturn(importReport); + when(notifier.getJobSummaryByJobId(JobType.TRACKER_IMPORT_JOB, uid)).thenReturn(importReport); when(trackerImportService.buildImportReport(any(), any())).thenReturn(importReport); @@ -337,7 +340,7 @@ void verifyShouldFindJobReport() throws Exception { .getResponse() .getContentAsString(); - verify(notifier).getJobSummaryByJobId(TRACKER_IMPORT_JOB, uid); + verify(notifier).getJobSummaryByJobId(JobType.TRACKER_IMPORT_JOB, uid); verify(trackerImportService).buildImportReport(any(), any()); try { @@ -352,7 +355,7 @@ void verifyShouldThrowWhenJobReportNotFound() throws Exception { String uid = CodeGenerator.generateUid(); // When - when(notifier.getJobSummaryByJobId(TRACKER_IMPORT_JOB, uid)).thenReturn(null); + when(notifier.getJobSummaryByJobId(JobType.TRACKER_IMPORT_JOB, uid)).thenReturn(null); // Then mockMvc diff --git a/dhis-2/dhis-web-api/src/test/java/org/hisp/dhis/webapi/controller/tracker/imports/TrackerImportParamsMapperTest.java b/dhis-2/dhis-web-api/src/test/java/org/hisp/dhis/webapi/controller/tracker/imports/TrackerImportParamsMapperTest.java index 8cb802fe993d..63e8c827f19e 100644 --- a/dhis-2/dhis-web-api/src/test/java/org/hisp/dhis/webapi/controller/tracker/imports/TrackerImportParamsMapperTest.java +++ b/dhis-2/dhis-web-api/src/test/java/org/hisp/dhis/webapi/controller/tracker/imports/TrackerImportParamsMapperTest.java @@ -63,7 +63,8 @@ void testValidationMode() { e -> { RequestParams requestParams = RequestParams.builder().validationMode(e).build(); TrackerImportParams params = - TrackerImportParamsMapper.trackerImportParams("userId", requestParams); + TrackerImportParamsMapper.trackerImportParams( + true, JOB_ID, "userId", requestParams, Body.builder().build()); assertThat(params.getValidationMode(), is(e)); }); } @@ -75,7 +76,8 @@ void testImportMode() { e -> { RequestParams requestParams = RequestParams.builder().importMode(e).build(); TrackerImportParams params = - TrackerImportParamsMapper.trackerImportParams("userId", requestParams); + TrackerImportParamsMapper.trackerImportParams( + true, JOB_ID, "userId", requestParams, Body.builder().build()); assertThat(params.getImportMode(), is(e)); }); } @@ -87,7 +89,8 @@ void testAtomicMode() { e -> { RequestParams requestParams = RequestParams.builder().atomicMode(e).build(); TrackerImportParams params = - TrackerImportParamsMapper.trackerImportParams("userId", requestParams); + TrackerImportParamsMapper.trackerImportParams( + true, JOB_ID, "userId", requestParams, Body.builder().build()); assertThat(params.getAtomicMode(), is(e)); }); } @@ -99,7 +102,8 @@ void testFlushMode() { e -> { RequestParams requestParams = RequestParams.builder().flushMode(e).build(); TrackerImportParams params = - TrackerImportParamsMapper.trackerImportParams("userId", requestParams); + TrackerImportParamsMapper.trackerImportParams( + true, JOB_ID, "userId", requestParams, Body.builder().build()); assertThat(params.getFlushMode(), is(e)); }); } @@ -111,7 +115,8 @@ void testImportStrategy() { e -> { RequestParams requestParams = RequestParams.builder().importStrategy(e).build(); TrackerImportParams params = - TrackerImportParamsMapper.trackerImportParams("userId", requestParams); + TrackerImportParamsMapper.trackerImportParams( + true, JOB_ID, "userId", requestParams, Body.builder().build()); assertThat(params.getImportStrategy(), is(e)); }); } @@ -121,7 +126,8 @@ void testIdSchemeUsingIdSchemeName() { RequestParams requestParams = RequestParams.builder().idScheme(TrackerIdSchemeParam.NAME).build(); TrackerImportParams params = - TrackerImportParamsMapper.trackerImportParams("userId", requestParams); + TrackerImportParamsMapper.trackerImportParams( + true, JOB_ID, "userId", requestParams, Body.builder().build()); TrackerIdSchemeParam expected = TrackerIdSchemeParam.NAME; assertEquals(expected, params.getIdSchemes().getIdScheme()); @@ -138,7 +144,8 @@ void testIdSchemeUsingIdSchemeAttribute() { RequestParams requestParams = RequestParams.builder().idScheme(TrackerIdSchemeParam.ofAttribute("WSiOAALYocA")).build(); TrackerImportParams params = - TrackerImportParamsMapper.trackerImportParams("userId", requestParams); + TrackerImportParamsMapper.trackerImportParams( + true, JOB_ID, "userId", requestParams, Body.builder().build()); TrackerIdSchemeParam expected = TrackerIdSchemeParam.ofAttribute("WSiOAALYocA"); assertEquals(expected, params.getIdSchemes().getIdScheme()); @@ -157,7 +164,8 @@ void testOrgUnitIdentifier() { e -> { RequestParams requestParams = RequestParams.builder().orgUnitIdScheme(e).build(); TrackerImportParams params = - TrackerImportParamsMapper.trackerImportParams("userId", requestParams); + TrackerImportParamsMapper.trackerImportParams( + true, JOB_ID, "userId", requestParams, Body.builder().build()); assertThat( params.getIdSchemes().getOrgUnitIdScheme().getIdScheme(), is(e.getIdScheme())); }); @@ -170,7 +178,8 @@ void testProgramIdentifier() { e -> { RequestParams requestParams = RequestParams.builder().programIdScheme(e).build(); TrackerImportParams params = - TrackerImportParamsMapper.trackerImportParams("userId", requestParams); + TrackerImportParamsMapper.trackerImportParams( + true, JOB_ID, "userId", requestParams, Body.builder().build()); assertThat( params.getIdSchemes().getProgramIdScheme().getIdScheme(), is(e.getIdScheme())); }); @@ -183,7 +192,8 @@ void testProgramIdentifierUsingIdSchemeAttribute() { .programIdScheme(TrackerIdSchemeParam.ofAttribute("WSiOAALYocA")) .build(); TrackerImportParams params = - TrackerImportParamsMapper.trackerImportParams("userId", requestParams); + TrackerImportParamsMapper.trackerImportParams( + true, JOB_ID, "userId", requestParams, Body.builder().build()); assertEquals( TrackerIdSchemeParam.ofAttribute("WSiOAALYocA"), @@ -197,7 +207,8 @@ void testProgramStageIdentifier() { e -> { RequestParams requestParams = RequestParams.builder().programStageIdScheme(e).build(); TrackerImportParams params = - TrackerImportParamsMapper.trackerImportParams("userId", requestParams); + TrackerImportParamsMapper.trackerImportParams( + true, JOB_ID, "userId", requestParams, Body.builder().build()); assertThat( params.getIdSchemes().getProgramStageIdScheme().getIdScheme(), is(e.getIdScheme())); @@ -211,7 +222,8 @@ void testDataElementIdentifier() { e -> { RequestParams requestParams = RequestParams.builder().dataElementIdScheme(e).build(); TrackerImportParams params = - TrackerImportParamsMapper.trackerImportParams("userId", requestParams); + TrackerImportParamsMapper.trackerImportParams( + true, JOB_ID, "userId", requestParams, Body.builder().build()); assertThat( params.getIdSchemes().getDataElementIdScheme().getIdScheme(), is(e.getIdScheme())); @@ -226,7 +238,8 @@ void testCategoryOptionComboIdentifier() { RequestParams requestParams = RequestParams.builder().categoryOptionComboIdScheme(e).build(); TrackerImportParams params = - TrackerImportParamsMapper.trackerImportParams("userId", requestParams); + TrackerImportParamsMapper.trackerImportParams( + true, JOB_ID, "userId", requestParams, Body.builder().build()); assertThat( params.getIdSchemes().getCategoryOptionComboIdScheme().getIdScheme(), is(e.getIdScheme())); @@ -241,7 +254,8 @@ void testCategoryOptionIdentifier() { RequestParams requestParams = RequestParams.builder().categoryOptionIdScheme(e).build(); TrackerImportParams params = - TrackerImportParamsMapper.trackerImportParams("userId", requestParams); + TrackerImportParamsMapper.trackerImportParams( + true, JOB_ID, "userId", requestParams, Body.builder().build()); assertThat( params.getIdSchemes().getCategoryOptionIdScheme().getIdScheme(), is(e.getIdScheme())); diff --git a/dhis-2/dhis-web-api/src/test/java/org/hisp/dhis/webapi/controller/tracker/imports/TrackerImporterImplTest.java b/dhis-2/dhis-web-api/src/test/java/org/hisp/dhis/webapi/controller/tracker/imports/TrackerImporterImplTest.java new file mode 100644 index 000000000000..0d1fd39f68b8 --- /dev/null +++ b/dhis-2/dhis-web-api/src/test/java/org/hisp/dhis/webapi/controller/tracker/imports/TrackerImporterImplTest.java @@ -0,0 +1,98 @@ +/* + * Copyright (c) 2004-2022, University of Oslo + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * Redistributions of source code must retain the above copyright notice, this + * list of conditions and the following disclaimer. + * + * Redistributions in binary form must reproduce the above copyright notice, + * this list of conditions and the following disclaimer in the documentation + * and/or other materials provided with the distribution. + * Neither the name of the HISP project nor the names of its contributors may + * be used to endorse or promote products derived from this software without + * specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ +package org.hisp.dhis.webapi.controller.tracker.imports; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.eq; +import static org.mockito.Mockito.doNothing; +import static org.mockito.Mockito.times; +import static org.mockito.Mockito.verify; + +import org.hisp.dhis.artemis.MessageManager; +import org.hisp.dhis.artemis.Topics; +import org.hisp.dhis.scheduling.JobConfiguration; +import org.hisp.dhis.scheduling.JobType; +import org.hisp.dhis.tracker.imports.TrackerBundleReportMode; +import org.hisp.dhis.tracker.imports.TrackerImportParams; +import org.hisp.dhis.tracker.imports.TrackerImportService; +import org.hisp.dhis.tracker.imports.job.TrackerMessage; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; +import org.mockito.ArgumentCaptor; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.junit.jupiter.MockitoExtension; + +@ExtendWith(MockitoExtension.class) +class TrackerImporterImplTest { + @InjectMocks TrackerAsyncImporter asyncImporter; + + @InjectMocks TrackerSyncImporter syncImporter; + + @Mock TrackerImportService trackerImportService; + + @Mock MessageManager messageManager; + + @Test + void shouldCreateReportSync() { + TrackerImportParams params = + TrackerImportParams.builder() + .jobConfiguration(new JobConfiguration("", JobType.TRACKER_IMPORT_JOB, "userId")) + .reportMode(TrackerBundleReportMode.FULL) + .build(); + + syncImporter.importTracker(params); + + verify(trackerImportService).importTracker(params); + verify(trackerImportService).buildImportReport(any(), eq(TrackerBundleReportMode.FULL)); + } + + @Test + void shouldSendMessageToQueueAsync() { + ArgumentCaptor queueNameCaptor = ArgumentCaptor.forClass(String.class); + ArgumentCaptor trackerMessageCaptor = + ArgumentCaptor.forClass(TrackerMessage.class); + + doNothing() + .when(messageManager) + .sendQueue(queueNameCaptor.capture(), trackerMessageCaptor.capture()); + + TrackerImportParams params = + TrackerImportParams.builder() + .jobConfiguration(new JobConfiguration("", JobType.TRACKER_IMPORT_JOB, "userId")) + .build(); + + asyncImporter.importTracker(params, null, ""); + + verify(trackerImportService, times(0)).importTracker(any()); + verify(messageManager).sendQueue(any(), any()); + assertEquals(Topics.TRACKER_IMPORT_JOB_TOPIC_NAME, queueNameCaptor.getValue()); + assertEquals(params, trackerMessageCaptor.getValue().getTrackerImportParams()); + } +}