diff --git a/dhis-2/dhis-test-web-api/src/test/java/org/hisp/dhis/webapi/controller/tracker/JsonAssertions.java b/dhis-2/dhis-test-web-api/src/test/java/org/hisp/dhis/webapi/controller/tracker/JsonAssertions.java index 44dce4e051b1..b006553f812e 100644 --- a/dhis-2/dhis-test-web-api/src/test/java/org/hisp/dhis/webapi/controller/tracker/JsonAssertions.java +++ b/dhis-2/dhis-test-web-api/src/test/java/org/hisp/dhis/webapi/controller/tracker/JsonAssertions.java @@ -35,8 +35,10 @@ import java.util.Collection; import java.util.HashSet; import java.util.List; +import java.util.Optional; import java.util.Set; import java.util.function.Function; +import java.util.function.Predicate; import org.hisp.dhis.jsontree.JsonArray; import org.hisp.dhis.jsontree.JsonList; import org.hisp.dhis.jsontree.JsonObject; @@ -171,6 +173,24 @@ public static void assertHasMember(JsonObject json, String name) { assertTrue(json.has(name), String.format("member \"%s\" should be in %s", name, json)); } + /** + * Asserts that the actual list contains an element matching the predicate. If the element is + * found, it is returned. + * + * @param actual the list to search + * @param predicate the predicate to match the element + * @param messageSubject the subject of the message in case the element is not found + * @return the element that matches the predicate + * @param the type of the elements in the list + */ + public static T assertContains( + JsonList actual, Predicate predicate, String messageSubject) { + Optional element = actual.stream().filter(predicate).findFirst(); + assertTrue( + element.isPresent(), () -> String.format("%s not found in %s", messageSubject, actual)); + return element.get(); + } + public static void assertContainsAll( Collection expected, JsonList actual, Function toValue) { assertFalse( diff --git a/dhis-2/dhis-test-web-api/src/test/java/org/hisp/dhis/webapi/controller/tracker/export/enrollment/EnrollmentsExportControllerTest.java b/dhis-2/dhis-test-web-api/src/test/java/org/hisp/dhis/webapi/controller/tracker/export/enrollment/EnrollmentsExportControllerTest.java index 75299997d412..c1db357afa70 100644 --- a/dhis-2/dhis-test-web-api/src/test/java/org/hisp/dhis/webapi/controller/tracker/export/enrollment/EnrollmentsExportControllerTest.java +++ b/dhis-2/dhis-test-web-api/src/test/java/org/hisp/dhis/webapi/controller/tracker/export/enrollment/EnrollmentsExportControllerTest.java @@ -27,254 +27,269 @@ */ package org.hisp.dhis.webapi.controller.tracker.export.enrollment; +import static org.hisp.dhis.test.utils.Assertions.assertNotEmpty; import static org.hisp.dhis.test.utils.Assertions.assertStartsWith; +import static org.hisp.dhis.webapi.controller.tracker.JsonAssertions.assertContains; import static org.hisp.dhis.webapi.controller.tracker.JsonAssertions.assertHasMember; import static org.hisp.dhis.webapi.controller.tracker.JsonAssertions.assertHasNoMember; import static org.hisp.dhis.webapi.controller.tracker.JsonAssertions.assertHasOnlyMembers; +import static org.junit.jupiter.api.Assertions.assertAll; 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.assertTrue; -import java.util.Date; +import java.io.IOException; +import java.util.ArrayList; import java.util.List; -import java.util.Set; -import org.hisp.dhis.category.CategoryOptionCombo; -import org.hisp.dhis.category.CategoryService; -import org.hisp.dhis.common.CodeGenerator; +import java.util.Map; +import java.util.function.Supplier; +import org.hisp.dhis.common.IdentifiableObject; import org.hisp.dhis.common.IdentifiableObjectManager; import org.hisp.dhis.common.ValueType; -import org.hisp.dhis.dataelement.DataElement; +import org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundle; +import org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundleMode; +import org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundleParams; +import org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundleService; +import org.hisp.dhis.dxf2.metadata.objectbundle.ObjectBundleValidationService; +import org.hisp.dhis.dxf2.metadata.objectbundle.feedback.ObjectBundleValidationReport; import org.hisp.dhis.eventdatavalue.EventDataValue; import org.hisp.dhis.http.HttpStatus; +import org.hisp.dhis.importexport.ImportStrategy; import org.hisp.dhis.jsontree.JsonList; -import org.hisp.dhis.note.Note; -import org.hisp.dhis.organisationunit.OrganisationUnit; import org.hisp.dhis.program.Enrollment; -import org.hisp.dhis.program.EnrollmentStatus; import org.hisp.dhis.program.Event; -import org.hisp.dhis.program.Program; -import org.hisp.dhis.program.ProgramStage; import org.hisp.dhis.relationship.Relationship; -import org.hisp.dhis.relationship.RelationshipEntity; -import org.hisp.dhis.relationship.RelationshipItem; -import org.hisp.dhis.relationship.RelationshipType; -import org.hisp.dhis.security.acl.AccessStringHelper; -import org.hisp.dhis.test.webapi.H2ControllerIntegrationTestBase; -import org.hisp.dhis.trackedentity.TrackedEntity; +import org.hisp.dhis.render.RenderFormat; +import org.hisp.dhis.render.RenderService; +import org.hisp.dhis.test.webapi.PostgresControllerIntegrationTestBase; import org.hisp.dhis.trackedentity.TrackedEntityAttribute; -import org.hisp.dhis.trackedentity.TrackedEntityType; -import org.hisp.dhis.trackedentityattributevalue.TrackedEntityAttributeValue; +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.Status; +import org.hisp.dhis.tracker.imports.report.ValidationReport; import org.hisp.dhis.user.User; import org.hisp.dhis.webapi.controller.tracker.JsonAttribute; +import org.hisp.dhis.webapi.controller.tracker.JsonDataValue; import org.hisp.dhis.webapi.controller.tracker.JsonEnrollment; import org.hisp.dhis.webapi.controller.tracker.JsonEvent; import org.hisp.dhis.webapi.controller.tracker.JsonNote; import org.hisp.dhis.webapi.controller.tracker.JsonRelationship; -import org.hisp.dhis.webapi.controller.tracker.JsonRelationshipItem; +import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.TestInstance; import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.io.ClassPathResource; import org.springframework.transaction.annotation.Transactional; @Transactional -class EnrollmentsExportControllerTest extends H2ControllerIntegrationTestBase { +@TestInstance(TestInstance.Lifecycle.PER_CLASS) +class EnrollmentsExportControllerTest extends PostgresControllerIntegrationTestBase { - private static final String ATTRIBUTE_VALUE = "value"; + @Autowired private RenderService renderService; - @Autowired private IdentifiableObjectManager manager; - - @Autowired private CategoryService categoryService; - - private CategoryOptionCombo coc; - - private OrganisationUnit orgUnit; - - private User owner; + @Autowired private ObjectBundleService objectBundleService; - private Program program; + @Autowired private ObjectBundleValidationService objectBundleValidationService; - private TrackedEntity te; + @Autowired private TrackerImportService trackerImportService; - private Enrollment enrollment; - - private TrackedEntityAttribute tea; + @Autowired private IdentifiableObjectManager manager; - private Relationship relationship; + private User importUser; + + protected ObjectBundle setUpMetadata(String path) throws IOException { + Map, List> metadata = + renderService.fromMetadata(new ClassPathResource(path).getInputStream(), RenderFormat.JSON); + ObjectBundleParams params = new ObjectBundleParams(); + params.setObjectBundleMode(ObjectBundleMode.COMMIT); + params.setImportStrategy(ImportStrategy.CREATE); + params.setObjects(metadata); + ObjectBundle bundle = objectBundleService.create(params); + assertNoErrors(objectBundleValidationService.validate(bundle)); + objectBundleService.commit(bundle); + return bundle; + } - private ProgramStage programStage; + protected TrackerObjects fromJson(String path) throws IOException { + return renderService.fromJson( + new ClassPathResource(path).getInputStream(), TrackerObjects.class); + } - private Event event; + @BeforeAll + void setUp() throws IOException { + setUpMetadata("tracker/simple_metadata.json"); - private DataElement dataElement; + importUser = userService.getUser("tTgjgobT1oS"); + injectSecurityContextUser(importUser); - private TrackedEntityAttributeValue trackedEntityAttributeValue; + TrackerImportParams params = TrackerImportParams.builder().build(); + assertNoErrors( + trackerImportService.importTracker(params, fromJson("tracker/event_and_enrollment.json"))); - private EventDataValue eventDataValue; + manager.flush(); + manager.clear(); + } @BeforeEach - void setUp() { - owner = makeUser("o"); - manager.save(owner, false); - - coc = categoryService.getDefaultCategoryOptionCombo(); - - orgUnit = createOrganisationUnit('A'); - manager.save(orgUnit); - - User user = createUserWithId("tester", CodeGenerator.generateUid()); - user.addOrganisationUnit(orgUnit); - user.setTeiSearchOrganisationUnits(Set.of(orgUnit)); - this.userService.updateUser(user); - - program = createProgram('A'); - manager.save(program); - - TrackedEntityType trackedEntityType = createTrackedEntityType('A'); - manager.save(trackedEntityType); - - tea = createTrackedEntityAttribute('A'); - tea.getSharing().setOwner(owner); - manager.save(tea, false); - - te = createTrackedEntity(orgUnit); - te.setTrackedEntityType(trackedEntityType); - manager.save(te); - - trackedEntityAttributeValue = new TrackedEntityAttributeValue(); - trackedEntityAttributeValue.setAttribute(tea); - trackedEntityAttributeValue.setTrackedEntity(te); - trackedEntityAttributeValue.setStoredBy("user"); - trackedEntityAttributeValue.setValue(ATTRIBUTE_VALUE); - te.setTrackedEntityAttributeValues(Set.of(trackedEntityAttributeValue)); - manager.update(te); - - program.setProgramAttributes(List.of(createProgramTrackedEntityAttribute(program, tea))); - - programStage = createProgramStage('A', program); - manager.save(programStage); - - enrollment = enrollment(te); - event = event(); - enrollment.setEvents(Set.of(event)); - manager.update(enrollment); - - manager.save(relationship(enrollment, te)); + void setUpUser() { + switchContextToUser(importUser); } @Test void getEnrollmentById() { - JsonEnrollment enrollment = - GET("/tracker/enrollments/{id}", this.enrollment.getUid()) + Enrollment enrollment = get(Enrollment.class, "TvctPPhpD8z"); + + JsonEnrollment jsonEnrollment = + GET("/tracker/enrollments/{id}", enrollment.getUid()) .content(HttpStatus.OK) .as(JsonEnrollment.class); - assertDefaultResponse(enrollment); + assertDefaultResponse(enrollment, jsonEnrollment); } @Test void getEnrollmentByIdWithFields() { - JsonEnrollment enrollment = - GET("/tracker/enrollments/{id}?fields=orgUnit,status", this.enrollment.getUid()) + Enrollment enrollment = get(Enrollment.class, "TvctPPhpD8z"); + + JsonEnrollment jsonEnrollment = + GET("/tracker/enrollments/{id}?fields=orgUnit,status", enrollment.getUid()) .content(HttpStatus.OK) .as(JsonEnrollment.class); - assertHasOnlyMembers(enrollment, "orgUnit", "status"); - assertEquals(this.enrollment.getOrganisationUnit().getUid(), enrollment.getOrgUnit()); - assertEquals(this.enrollment.getStatus().toString(), enrollment.getStatus()); + assertHasOnlyMembers(jsonEnrollment, "orgUnit", "status"); + assertEquals(enrollment.getOrganisationUnit().getUid(), jsonEnrollment.getOrgUnit()); + assertEquals(enrollment.getStatus().toString(), jsonEnrollment.getStatus()); } @Test void getEnrollmentByIdWithNotes() { - enrollment.setNotes(List.of(note("oqXG28h988k", "my notes", owner.getUid()))); + Enrollment enrollment = get(Enrollment.class, "TvctPPhpD8z"); + assertNotEmpty(enrollment.getNotes(), "test expects an enrollment with notes"); - JsonEnrollment enrollment = - GET("/tracker/enrollments/{uid}?fields=notes", this.enrollment.getUid()) + JsonEnrollment jsonEnrollment = + GET("/tracker/enrollments/{uid}?fields=notes", enrollment.getUid()) .content(HttpStatus.OK) .as(JsonEnrollment.class); - JsonNote note = enrollment.getNotes().get(0); - assertEquals("oqXG28h988k", note.getNote()); - assertEquals("my notes", note.getValue()); - assertEquals(owner.getUid(), note.getStoredBy()); + JsonNote note = jsonEnrollment.getNotes().get(0); + assertEquals("f9423652692", note.getNote()); + assertEquals("enrollment comment value", note.getValue()); } @Test void getEnrollmentByIdWithAttributes() { - JsonEnrollment enrollment = - GET("/tracker/enrollments/{id}?fields=attributes", this.enrollment.getUid()) + Enrollment enrollment = get(Enrollment.class, "TvctPPhpD8z"); + assertNotEmpty( + enrollment.getTrackedEntity().getTrackedEntityAttributeValues(), + "test expects an enrollment with attribute values"); + TrackedEntityAttribute ptea = get(TrackedEntityAttribute.class, "dIVt4l5vIOa"); + + JsonEnrollment jsonEnrollment = + GET("/tracker/enrollments/{id}?fields=attributes", enrollment.getUid()) .content(HttpStatus.OK) .as(JsonEnrollment.class); - assertHasOnlyMembers(enrollment, "attributes"); - JsonAttribute attribute = enrollment.getAttributes().get(0); - assertEquals(tea.getUid(), attribute.getAttribute()); - TrackedEntityAttribute expected = trackedEntityAttributeValue.getAttribute(); - assertEquals(trackedEntityAttributeValue.getValue(), attribute.getValue()); - assertEquals(expected.getValueType().toString(), attribute.getValueType()); + assertHasOnlyMembers(jsonEnrollment, "attributes"); + JsonAttribute attribute = jsonEnrollment.getAttributes().get(0); + assertEquals(ptea.getUid(), attribute.getAttribute()); + assertEquals("Frank PTEA", attribute.getValue()); + assertEquals(ValueType.TEXT.name(), attribute.getValueType()); assertHasMember(attribute, "createdAt"); assertHasMember(attribute, "updatedAt"); assertHasMember(attribute, "displayName"); assertHasMember(attribute, "code"); - assertHasMember(attribute, "storedBy"); } @Test void getEnrollmentByIdWithRelationshipsFields() { - JsonList relationships = + Relationship relationship = get(Relationship.class, "p53a6314631"); + assertNotNull( + relationship.getTo().getEnrollment(), + "test expects relationship to have a 'to' enrollment"); + Enrollment enrollment = relationship.getTo().getEnrollment(); + + JsonList jsonRelationships = GET("/tracker/enrollments/{id}?fields=relationships", enrollment.getUid()) .content(HttpStatus.OK) .getList("relationships", JsonRelationship.class); - JsonRelationship jsonRelationship = relationships.get(0); - assertEquals(relationship.getUid(), jsonRelationship.getRelationship()); - - JsonRelationshipItem.JsonEnrollment enrollment = jsonRelationship.getFrom().getEnrollment(); - assertEquals(relationship.getFrom().getEnrollment().getUid(), enrollment.getEnrollment()); - assertEquals( - relationship.getFrom().getEnrollment().getTrackedEntity().getUid(), - enrollment.getTrackedEntity()); - - JsonRelationshipItem.JsonTrackedEntity trackedEntity = - jsonRelationship.getTo().getTrackedEntity(); - assertEquals( - relationship.getTo().getTrackedEntity().getUid(), trackedEntity.getTrackedEntity()); - - assertHasMember(jsonRelationship, "relationshipName"); - assertHasMember(jsonRelationship, "relationshipType"); - assertHasMember(jsonRelationship, "createdAt"); - assertHasMember(jsonRelationship, "updatedAt"); - assertHasMember(jsonRelationship, "bidirectional"); + JsonRelationship jsonRelationship = + assertContains( + jsonRelationships, + re -> relationship.getUid().equals(re.getRelationship()), + relationship.getUid()); + + assertAll( + "relationship JSON", + () -> + assertEquals( + relationship.getFrom().getTrackedEntity().getUid(), + jsonRelationship.getFrom().getTrackedEntity().getTrackedEntity()), + () -> + assertEquals( + relationship.getTo().getEnrollment().getUid(), + jsonRelationship.getTo().getEnrollment().getEnrollment()), + () -> assertHasMember(jsonRelationship, "relationshipName"), + () -> assertHasMember(jsonRelationship, "relationshipType"), + () -> assertHasMember(jsonRelationship, "createdAt"), + () -> assertHasMember(jsonRelationship, "updatedAt"), + () -> assertHasMember(jsonRelationship, "bidirectional")); } @Test void getEnrollmentByIdWithEventsFields() { - JsonList events = - GET("/tracker/enrollments/{id}?fields=events", enrollment.getUid()) + Event event = get(Event.class, "pTzf9KYMk72"); + assertNotNull(event.getEnrollment(), "test expects an event with an enrollment"); + assertNotEmpty(event.getEventDataValues(), "test expects an event with data values"); + EventDataValue eventDataValue = event.getEventDataValues().iterator().next(); + + JsonList jsonEvents = + GET("/tracker/enrollments/{id}?fields=events", event.getEnrollment().getUid()) .content(HttpStatus.OK) .getList("events", JsonEvent.class); - JsonEvent event = events.get(0); - assertEquals(this.event.getUid(), event.getEvent()); - assertEquals(enrollment.getUid(), event.getEnrollment()); - assertEquals(te.getUid(), event.getTrackedEntity()); - assertEquals(dataElement.getUid(), event.getDataValues().get(0).getDataElement()); - assertEquals(eventDataValue.getValue(), event.getDataValues().get(0).getValue()); - assertEquals(program.getUid(), event.getProgram()); - - assertHasMember(event, "status"); - assertHasMember(event, "followUp"); - assertHasMember(event, "followup"); - assertEquals(program.getUid(), event.getProgram()); - assertEquals(orgUnit.getUid(), event.getOrgUnit()); - assertFalse(event.getDeleted()); + JsonEvent jsonEvent = jsonEvents.get(0); + assertAll( + "event JSON", + () -> assertEquals(event.getUid(), jsonEvent.getEvent()), + () -> assertEquals(event.getEnrollment().getUid(), jsonEvent.getEnrollment()), + () -> + assertEquals( + event.getEnrollment().getTrackedEntity().getUid(), jsonEvent.getTrackedEntity()), + () -> assertEquals(event.getProgramStage().getProgram().getUid(), jsonEvent.getProgram()), + () -> assertEquals(event.getOrganisationUnit().getUid(), jsonEvent.getOrgUnit()), + () -> { + JsonDataValue jsonDataValue = + assertContains( + jsonEvent.getDataValues(), + dv -> eventDataValue.getDataElement().equals(dv.getDataElement()), + eventDataValue.getDataElement()); + assertEquals( + eventDataValue.getValue(), + jsonDataValue.getValue(), + "data value for data element " + eventDataValue.getDataElement()); + }, + () -> assertHasMember(jsonEvent, "status"), + () -> assertHasMember(jsonEvent, "followUp"), + () -> assertHasMember(jsonEvent, "followup"), + () -> assertEquals(event.isDeleted(), jsonEvent.getDeleted())); } @Test void getEnrollmentByIdWithExcludedFields() { + Event event = get(Event.class, "pTzf9KYMk72"); + assertNotNull(event.getEnrollment(), "test expects an event with an enrollment"); + assertNotNull( + event.getRelationshipItems(), "test expects an event with at least one relationship"); + assertTrue( (GET( "/tracker/enrollments/{id}?fields=!attributes,!relationships,!events", - enrollment.getUid()) + event.getEnrollment().getUid()) .content(HttpStatus.OK)) .isEmpty()); } @@ -295,89 +310,66 @@ void getEnrollmentsFailsIfGivenEnrollmentAndEnrollmentsParameters() { .getMessage()); } - private Event event() { - Event eventA = new Event(enrollment, programStage, enrollment.getOrganisationUnit(), coc); - eventA.setAutoFields(); - - eventDataValue = new EventDataValue(); - eventDataValue.setValue("value"); - dataElement = createDataElement('A'); - dataElement.setValueType(ValueType.TEXT); - manager.save(dataElement); - eventDataValue.setDataElement(dataElement.getUid()); - Set eventDataValues = Set.of(eventDataValue); - eventA.setEventDataValues(eventDataValues); - manager.save(eventA); - return eventA; + private void assertDefaultResponse(Enrollment expected, JsonEnrollment actual) { + assertFalse(actual.isEmpty()); + assertEquals(expected.getUid(), actual.getEnrollment()); + assertEquals(expected.getTrackedEntity().getUid(), actual.getTrackedEntity()); + assertEquals(expected.getProgram().getUid(), actual.getProgram()); + assertEquals(expected.getStatus().name(), actual.getStatus()); + assertEquals(expected.getOrganisationUnit().getUid(), actual.getOrgUnit()); + assertEquals(expected.getFollowup(), actual.getBoolean("followUp").bool()); + assertEquals(expected.isDeleted(), actual.getBoolean("deleted").bool()); + assertHasMember(actual, "enrolledAt"); + assertHasMember(actual, "occurredAt"); + assertHasMember(actual, "createdAt"); + assertHasMember(actual, "createdAtClient"); + assertHasMember(actual, "updatedAt"); + assertHasMember(actual, "notes"); + assertHasNoMember(actual, "relationships"); + assertHasNoMember(actual, "events"); + assertHasNoMember(actual, "attributes"); } - private Relationship relationship(Enrollment from, TrackedEntity to) { - relationship = new Relationship(); - - RelationshipItem fromItem = new RelationshipItem(); - fromItem.setEnrollment(from); - from.getRelationshipItems().add(fromItem); - relationship.setFrom(fromItem); - fromItem.setRelationship(relationship); - - RelationshipItem toItem = new RelationshipItem(); - toItem.setTrackedEntity(to); - to.getRelationshipItems().add(toItem); - relationship.setTo(toItem); - toItem.setRelationship(relationship); - - RelationshipType type = createRelationshipType('A'); - type.getFromConstraint().setRelationshipEntity(RelationshipEntity.PROGRAM_INSTANCE); - type.getToConstraint().setRelationshipEntity(RelationshipEntity.TRACKED_ENTITY_INSTANCE); - type.getSharing().setPublicAccess(AccessStringHelper.DEFAULT); - manager.save(type, false); - - relationship.setRelationshipType(type); - relationship.setKey(type.getUid()); - relationship.setInvertedKey(type.getUid()); - relationship.setAutoFields(); - - manager.save(relationship, false); - return relationship; + private T get(Class type, String uid) { + T t = manager.get(type, uid); + assertNotNull( + t, + () -> + String.format( + "'%s' with uid '%s' should have been created", type.getSimpleName(), uid)); + return t; } - private void assertDefaultResponse(JsonEnrollment enrollment) { - assertFalse(enrollment.isEmpty()); - assertEquals(this.enrollment.getUid(), enrollment.getEnrollment()); - assertEquals(te.getUid(), enrollment.getTrackedEntity()); - assertEquals(program.getUid(), enrollment.getProgram()); - assertEquals("COMPLETED", enrollment.getStatus()); - assertEquals(orgUnit.getUid(), enrollment.getOrgUnit()); - assertTrue(enrollment.getBoolean("followUp").bool()); - assertFalse(enrollment.getBoolean("deleted").bool()); - assertHasMember(enrollment, "enrolledAt"); - assertHasMember(enrollment, "occurredAt"); - assertHasMember(enrollment, "createdAt"); - assertHasMember(enrollment, "createdAtClient"); - assertHasMember(enrollment, "updatedAt"); - assertHasMember(enrollment, "notes"); - assertHasNoMember(enrollment, "relationships"); - assertHasNoMember(enrollment, "events"); - assertHasNoMember(enrollment, "attributes"); + public static void assertNoErrors(ImportReport report) { + assertNotNull(report); + assertEquals( + Status.OK, + report.getStatus(), + errorMessage( + "Expected import with status OK, instead got:%n", report.getValidationReport())); } - private Enrollment enrollment(TrackedEntity te) { - Enrollment enrollment = new Enrollment(program, te, orgUnit); - enrollment.setAutoFields(); - enrollment.setEnrollmentDate(new Date()); - enrollment.setOccurredDate(new Date()); - enrollment.setStatus(EnrollmentStatus.COMPLETED); - enrollment.setFollowup(true); - manager.save(enrollment, false); - te.setEnrollments(Set.of(enrollment)); - manager.save(te, false); - return enrollment; + private static Supplier errorMessage(String errorTitle, ValidationReport report) { + return () -> { + StringBuilder msg = new StringBuilder(errorTitle); + report + .getErrors() + .forEach( + e -> { + msg.append(e.getErrorCode()); + msg.append(": "); + msg.append(e.getMessage()); + msg.append('\n'); + }); + return msg.toString(); + }; } - private Note note(String uid, String value, String storedBy) { - Note note = new Note(value, storedBy); - note.setUid(uid); - manager.save(note, false); - return note; + public static void assertNoErrors(ObjectBundleValidationReport report) { + assertNotNull(report); + List errors = new ArrayList<>(); + report.forEachErrorReport(err -> errors.add(err.toString())); + assertFalse( + report.hasErrorReports(), String.format("Expected no errors, instead got: %s%n", errors)); } } diff --git a/dhis-2/dhis-test-web-api/src/test/resources/tracker/event_and_enrollment.json b/dhis-2/dhis-test-web-api/src/test/resources/tracker/event_and_enrollment.json index 24248d000bc0..c43114728de9 100644 --- a/dhis-2/dhis-test-web-api/src/test/resources/tracker/event_and_enrollment.json +++ b/dhis-2/dhis-test-web-api/src/test/resources/tracker/event_and_enrollment.json @@ -347,6 +347,12 @@ }, "value": "Frank PTEA" } + ], + "notes": [ + { + "note": "f9423652692", + "value": "enrollment comment value" + } ] }, { @@ -1150,6 +1156,19 @@ "to": { "trackedEntity": "QesgJkTyTCk" } + }, + { + "relationship": "p53a6314631", + "relationshipType": { + "idScheme": "UID", + "identifier": "xLmPUYJX8Ks" + }, + "from": { + "trackedEntity": "dUE514NMOlo" + }, + "to": { + "enrollment": "nxP7UnKhomJ" + } } ], "username": "system-process"