From bf1116069670e023ebb2653722236e5e8770975e Mon Sep 17 00:00:00 2001 From: Volker Schmidt Date: Sun, 18 Aug 2019 09:26:51 +0200 Subject: [PATCH] Adds unit tests for FHIR to DHIS2 care related resources. --- ...ignmentDhisToFhirTransformerUtilsTest.java | 147 ++++++++ ...R4FhirCarePlanToEnrollmentTransformer.java | 14 +- ...irCarePlanToEnrollmentTransformerTest.java | 258 ++++++++++++++ ...ResponseToProgramStageTransformerTest.java | 320 ++++++++++++++++++ ...ignmentDhisToFhirTransformerUtilsTest.java | 145 ++++++++ .../fhir/metadata/model/ProgramStageRule.java | 13 +- .../CustomProgramStageRuleRepositoryImpl.java | 3 +- ...ignmentFhirToDhisTransformerUtilsTest.java | 140 ++++++++ 8 files changed, 1025 insertions(+), 15 deletions(-) create mode 100644 fhir-dstu3/src/test/java/org/dhis2/fhir/adapter/fhir/transform/dhis/impl/util/dstu3/Dstu3AssignmentDhisToFhirTransformerUtilsTest.java create mode 100644 fhir-r4/src/test/java/org/dhis2/fhir/adapter/fhir/transform/fhir/impl/enrollment/r4/R4FhirCarePlanToEnrollmentTransformerTest.java create mode 100644 fhir-r4/src/test/java/org/dhis2/fhir/adapter/fhir/transform/fhir/impl/program/r4/R4FhirQuestionnaireResponseToProgramStageTransformerTest.java create mode 100644 fhir-r4/src/test/java/org/dhis2/fhir/adapter/fhir/transform/fhir/impl/util/r4/R4AssignmentDhisToFhirTransformerUtilsTest.java create mode 100644 fhir/src/test/java/org/dhis2/fhir/adapter/fhir/transform/fhir/impl/util/AssignmentFhirToDhisTransformerUtilsTest.java diff --git a/fhir-dstu3/src/test/java/org/dhis2/fhir/adapter/fhir/transform/dhis/impl/util/dstu3/Dstu3AssignmentDhisToFhirTransformerUtilsTest.java b/fhir-dstu3/src/test/java/org/dhis2/fhir/adapter/fhir/transform/dhis/impl/util/dstu3/Dstu3AssignmentDhisToFhirTransformerUtilsTest.java new file mode 100644 index 00000000..48124694 --- /dev/null +++ b/fhir-dstu3/src/test/java/org/dhis2/fhir/adapter/fhir/transform/dhis/impl/util/dstu3/Dstu3AssignmentDhisToFhirTransformerUtilsTest.java @@ -0,0 +1,147 @@ +package org.dhis2.fhir.adapter.fhir.transform.dhis.impl.util.dstu3; + +/* + * Copyright (c) 2004-2019, 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. + */ + +import org.dhis2.fhir.adapter.dhis.model.DhisResourceId; +import org.dhis2.fhir.adapter.dhis.model.DhisResourceType; +import org.dhis2.fhir.adapter.fhir.data.repository.FhirDhisAssignmentRepository; +import org.dhis2.fhir.adapter.fhir.metadata.model.EnrollmentRule; +import org.dhis2.fhir.adapter.fhir.metadata.model.FhirClient; +import org.dhis2.fhir.adapter.fhir.metadata.model.FhirResourceType; +import org.dhis2.fhir.adapter.fhir.metadata.repository.FhirClientRepository; +import org.dhis2.fhir.adapter.fhir.model.FhirVersion; +import org.dhis2.fhir.adapter.fhir.script.ScriptExecutionContext; +import org.dhis2.fhir.adapter.fhir.transform.dhis.DhisToFhirTransformerContext; +import org.dhis2.fhir.adapter.fhir.transform.dhis.model.DhisRequest; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnit; +import org.mockito.junit.MockitoRule; + +import java.util.Objects; +import java.util.Optional; +import java.util.UUID; + +import static org.junit.Assert.assertNull; + +/** + * Unit tests for {@link Dstu3AssignmentDhisToFhirTransformerUtils}. + * + * @author volsch + */ +public class Dstu3AssignmentDhisToFhirTransformerUtilsTest +{ + @Mock + private ScriptExecutionContext scriptExecutionContext; + + @Mock + private FhirDhisAssignmentRepository assignmentRepository; + + @Mock + private FhirClientRepository fhirClientRepository; + + @Mock + private DhisToFhirTransformerContext transformerContext; + + @Mock + private DhisRequest dhisRequest; + + @Mock + private FhirClient fhirClient; + + @InjectMocks + private Dstu3AssignmentDhisToFhirTransformerUtils utils; + + @Rule + public MockitoRule rule = MockitoJUnit.rule().silent(); + + @Before + public void setUp() + { + Mockito.doReturn( dhisRequest ).when( transformerContext ).getDhisRequest(); + } + + @Test + public void getFhirVersions() + { + Assert.assertEquals( FhirVersion.DSTU3_ONLY, utils.getFhirVersions() ); + } + + @Test + public void getMappedFhirIdNull() + { + assertNull( utils.getMappedFhirId( transformerContext, new EnrollmentRule(), DhisResourceType.ENROLLMENT, null, FhirResourceType.CARE_PLAN ) ); + } + + @Test + public void getMappedFhirIdSync() + { + Assert.assertEquals( "PlanDefinition/hajdhcjsu89", + Objects.requireNonNull( utils.getMappedFhirId( transformerContext, new EnrollmentRule(), DhisResourceType.ENROLLMENT, "hajdhcjsu89", FhirResourceType.PLAN_DEFINITION ) ).getReferenceElement().getValue() ); + } + + @Test + public void getMappedFhirIdDhis() + { + Mockito.doReturn( true ).when( dhisRequest ).isDhisFhirId(); + + Assert.assertEquals( "CarePlan/hajdhcjsu89", + Objects.requireNonNull( utils.getMappedFhirId( transformerContext, new EnrollmentRule(), DhisResourceType.ENROLLMENT, "hajdhcjsu89", FhirResourceType.CARE_PLAN ) ).getReferenceElement().getValue() ); + } + + @Test + public void getMappedFhirId() + { + final UUID fhirClientId = UUID.randomUUID(); + + Mockito.doReturn( fhirClientId ).when( transformerContext ).getFhirClientId(); + Mockito.doReturn( Optional.of( fhirClient ) ).when( fhirClientRepository ).findOneByIdCached( Mockito.eq( fhirClientId ) ); + Mockito.doReturn( "4711" ).when( assignmentRepository ).findFirstFhirResourceId( Mockito.same( fhirClient ), Mockito.eq( new DhisResourceId( DhisResourceType.ENROLLMENT, "hajdhcjsu89" ) ) ); + + Assert.assertEquals( "CarePlan/4711", + Objects.requireNonNull( utils.getMappedFhirId( transformerContext, new EnrollmentRule(), DhisResourceType.ENROLLMENT, "hajdhcjsu89", FhirResourceType.CARE_PLAN ) ).getReferenceElement().getValue() ); + } + + @Test + public void getMappedFhirIdNullMapping() + { + final UUID fhirClientId = UUID.randomUUID(); + + Mockito.doReturn( fhirClientId ).when( transformerContext ).getFhirClientId(); + Mockito.doReturn( Optional.of( fhirClient ) ).when( fhirClientRepository ).findOneByIdCached( Mockito.eq( fhirClientId ) ); + Mockito.doReturn( null ).when( assignmentRepository ).findFirstFhirResourceId( Mockito.same( fhirClient ), Mockito.eq( new DhisResourceId( DhisResourceType.ENROLLMENT, "hajdhcjsu89" ) ) ); + + assertNull( utils.getMappedFhirId( transformerContext, new EnrollmentRule(), DhisResourceType.ENROLLMENT, "hajdhcjsu89", FhirResourceType.CARE_PLAN ) ); + } +} \ No newline at end of file diff --git a/fhir-r4/src/main/java/org/dhis2/fhir/adapter/fhir/transform/fhir/impl/enrollment/r4/R4FhirCarePlanToEnrollmentTransformer.java b/fhir-r4/src/main/java/org/dhis2/fhir/adapter/fhir/transform/fhir/impl/enrollment/r4/R4FhirCarePlanToEnrollmentTransformer.java index 96e656fe..7966398c 100644 --- a/fhir-r4/src/main/java/org/dhis2/fhir/adapter/fhir/transform/fhir/impl/enrollment/r4/R4FhirCarePlanToEnrollmentTransformer.java +++ b/fhir-r4/src/main/java/org/dhis2/fhir/adapter/fhir/transform/fhir/impl/enrollment/r4/R4FhirCarePlanToEnrollmentTransformer.java @@ -108,15 +108,9 @@ protected void transformInternal( @Nonnull FhirToDhisTransformerContext context, .orElseThrow( () -> new TransformerMappingException( "Care plan contains location that cannot be mapped." ) ) ); enrollment.setTrackedEntityInstanceId( scriptedTrackedEntityInstance.getId() ); - if ( fhirCarePlan.getPeriod().hasStart() ) - { - enrollment.setEnrollmentDate( ZonedDateTime.ofInstant( - fhirCarePlan.getPeriod().getStart().toInstant(), zoneId ) ); - } - else - { - enrollment.setEnrollmentDate( null ); - } + final Date enrollmentDate = fhirCarePlan.getPeriod().getStart(); + enrollment.setEnrollmentDate( enrollmentDate == null ? null : + ZonedDateTime.ofInstant( enrollmentDate.toInstant(), zoneId ) ); final Date incidentDate = IncidentDateExtensionUtils.getValue( fhirCarePlan ); enrollment.setIncidentDate( incidentDate == null ? null : ZonedDateTime.ofInstant( incidentDate.toInstant(), zoneId ) ); @@ -169,7 +163,7 @@ else if ( !fhirCarePlan.getInstantiatesCanonical().isEmpty() ) final IIdType id; try { - id = FhirUriUtils.createIdFromUri( uri, FhirResourceType.CARE_PLAN ); + id = FhirUriUtils.createIdFromUri( uri, FhirResourceType.PLAN_DEFINITION ); } catch ( IllegalArgumentException e ) { diff --git a/fhir-r4/src/test/java/org/dhis2/fhir/adapter/fhir/transform/fhir/impl/enrollment/r4/R4FhirCarePlanToEnrollmentTransformerTest.java b/fhir-r4/src/test/java/org/dhis2/fhir/adapter/fhir/transform/fhir/impl/enrollment/r4/R4FhirCarePlanToEnrollmentTransformerTest.java new file mode 100644 index 00000000..37b9dc10 --- /dev/null +++ b/fhir-r4/src/test/java/org/dhis2/fhir/adapter/fhir/transform/fhir/impl/enrollment/r4/R4FhirCarePlanToEnrollmentTransformerTest.java @@ -0,0 +1,258 @@ +package org.dhis2.fhir.adapter.fhir.transform.fhir.impl.enrollment.r4; + +/* + * Copyright (c) 2004-2019, 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. + */ + +import ca.uhn.fhir.model.primitive.IdDt; +import org.dhis2.fhir.adapter.dhis.converter.ValueConverter; +import org.dhis2.fhir.adapter.dhis.model.ReferenceType; +import org.dhis2.fhir.adapter.dhis.orgunit.OrganizationUnitService; +import org.dhis2.fhir.adapter.dhis.tracker.program.Enrollment; +import org.dhis2.fhir.adapter.dhis.tracker.program.EnrollmentService; +import org.dhis2.fhir.adapter.dhis.tracker.program.EnrollmentStatus; +import org.dhis2.fhir.adapter.dhis.tracker.program.ProgramMetadataService; +import org.dhis2.fhir.adapter.dhis.tracker.program.WritableProgram; +import org.dhis2.fhir.adapter.dhis.tracker.trackedentity.TrackedEntityAttributes; +import org.dhis2.fhir.adapter.dhis.tracker.trackedentity.TrackedEntityMetadataService; +import org.dhis2.fhir.adapter.dhis.tracker.trackedentity.TrackedEntityService; +import org.dhis2.fhir.adapter.dhis.tracker.trackedentity.TrackedEntityType; +import org.dhis2.fhir.adapter.fhir.data.repository.FhirDhisAssignmentRepository; +import org.dhis2.fhir.adapter.fhir.extension.IncidentDateExtensionUtils; +import org.dhis2.fhir.adapter.fhir.extension.LocationExtensionUtils; +import org.dhis2.fhir.adapter.fhir.metadata.model.EnrollmentRule; +import org.dhis2.fhir.adapter.fhir.metadata.model.ExecutableScript; +import org.dhis2.fhir.adapter.fhir.metadata.model.FhirResourceMapping; +import org.dhis2.fhir.adapter.fhir.metadata.model.RuleInfo; +import org.dhis2.fhir.adapter.fhir.metadata.repository.FhirResourceMappingRepository; +import org.dhis2.fhir.adapter.fhir.metadata.repository.TrackedEntityRuleRepository; +import org.dhis2.fhir.adapter.fhir.model.FhirVersion; +import org.dhis2.fhir.adapter.fhir.script.ScriptExecutionContext; +import org.dhis2.fhir.adapter.fhir.script.ScriptExecutor; +import org.dhis2.fhir.adapter.fhir.transform.TransformerDataException; +import org.dhis2.fhir.adapter.fhir.transform.fhir.FhirToDhisTransformerContext; +import org.dhis2.fhir.adapter.fhir.transform.scripted.ScriptedTrackedEntityInstance; +import org.dhis2.fhir.adapter.fhir.transform.scripted.WritableScriptedEnrollment; +import org.dhis2.fhir.adapter.fhir.transform.scripted.WritableScriptedTrackedEntityInstance; +import org.hl7.fhir.r4.model.CarePlan; +import org.hl7.fhir.r4.model.Patient; +import org.hl7.fhir.r4.model.Reference; +import org.hl7.fhir.r4.model.ResourceFactory; +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnit; +import org.mockito.junit.MockitoRule; + +import java.time.ZonedDateTime; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +/** + * Unit tests for {@link R4FhirCarePlanToEnrollmentTransformer}. + * + * @author volsch + */ +public class R4FhirCarePlanToEnrollmentTransformerTest +{ + @Mock + private ScriptExecutor scriptExecutor; + + @Mock + private TrackedEntityMetadataService trackedEntityMetadataService; + + @Mock + private TrackedEntityService trackedEntityService; + + @Mock + private TrackedEntityRuleRepository trackedEntityRuleRepository; + + @Mock + private OrganizationUnitService organizationUnitService; + + @Mock + private ProgramMetadataService programMetadataService; + + @Mock + private EnrollmentService enrollmentService; + + @Mock + private FhirResourceMappingRepository resourceMappingRepository; + + @Mock + private FhirDhisAssignmentRepository fhirDhisAssignmentRepository; + + @Mock + private ScriptExecutionContext scriptExecutionContext; + + @Mock + private ValueConverter valueConverter; + + @Mock + private FhirToDhisTransformerContext transformerContext; + + @Mock + private FhirResourceMapping fhirResourceMapping; + + @Mock + private TrackedEntityType trackedEntityType; + + @Mock + private TrackedEntityAttributes trackedEntityAttributes; + + @Mock + private ExecutableScript orgUnitExecutableScript; + + @InjectMocks + private R4FhirCarePlanToEnrollmentTransformer transformer; + + @Rule + public MockitoRule rule = MockitoJUnit.rule(); + + @Test + public void transformInternal() + { + final WritableProgram program = new WritableProgram(); + program.setId( "x72638dsydu" ); + program.setName( "Test Program" ); + program.setDescription( "Test Description" ); + program.setWithoutRegistration( false ); + program.setTrackedEntityTypeId( "a1234567890" ); + program.setStages( new ArrayList<>() ); + + final Patient patient = new Patient(); + patient.setId( new IdDt( "Patient/lyau3syaixys" ) ); + + final ScriptedTrackedEntityInstance scriptedTrackedEntityInstance = new WritableScriptedTrackedEntityInstance( trackedEntityMetadataService, trackedEntityService, trackedEntityAttributes, trackedEntityType, "lyau3syaixys", + scriptExecutionContext, valueConverter ); + + final Enrollment enrollment = new Enrollment(); + + final CarePlan fhirCarePlan = new CarePlan(); + fhirCarePlan.addInstantiatesUri( "PlanDefinition/x72638dsydu" ); + fhirCarePlan.setStatus( CarePlan.CarePlanStatus.COMPLETED ); + fhirCarePlan.getPeriod().setStart( Date.from( ZonedDateTime.parse( "2011-12-03T10:15:30+01:00" ).toInstant() ) ); + LocationExtensionUtils.setValue( fhirCarePlan, new Reference( "Location/j82jdy28dusx" ) ); + IncidentDateExtensionUtils.setValue( fhirCarePlan, Date.from( ZonedDateTime.parse( "2011-12-01T10:11:30+01:00" ).toInstant() ), ResourceFactory::createType ); + + final Map scriptVariables = new HashMap<>(); + + Mockito.doReturn( orgUnitExecutableScript ).when( fhirResourceMapping ) + .getImpEnrollmentOrgLookupScript(); + Mockito.doReturn( org.dhis2.fhir.adapter.dhis.model.Reference.createIdReference( "j82jdy28dusx" ) ) + .when( scriptExecutor ).execute( Mockito.same( orgUnitExecutableScript ), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any() ); + + transformer.transformInternal( transformerContext, new RuleInfo<>( new EnrollmentRule(), Collections.emptyList() ), scriptVariables, fhirResourceMapping, + program, fhirCarePlan, scriptedTrackedEntityInstance, new WritableScriptedEnrollment( program, enrollment, scriptedTrackedEntityInstance, scriptExecutionContext, valueConverter ) ); + + Assert.assertEquals( "x72638dsydu", enrollment.getProgramId() ); + Assert.assertEquals( "j82jdy28dusx", enrollment.getOrgUnitId() ); + Assert.assertEquals( "lyau3syaixys", enrollment.getTrackedEntityInstanceId() ); + Assert.assertEquals( EnrollmentStatus.COMPLETED, enrollment.getStatus() ); + Assert.assertEquals( ZonedDateTime.parse( "2011-12-03T10:15:30+01:00" ).toLocalDateTime(), enrollment.getEnrollmentDate().toLocalDateTime() ); + Assert.assertEquals( ZonedDateTime.parse( "2011-12-01T10:11:30+01:00" ).toLocalDateTime(), enrollment.getIncidentDate().toLocalDateTime() ); + } + + @Test + public void getFhirVersions() + { + Assert.assertEquals( FhirVersion.R4_ONLY, transformer.getFhirVersions() ); + } + + @Test + public void convertStatusActive() + { + Assert.assertEquals( EnrollmentStatus.ACTIVE, transformer.convertStatus( CarePlan.CarePlanStatus.ACTIVE ) ); + } + + @Test + public void convertStatusCompleted() + { + Assert.assertEquals( EnrollmentStatus.COMPLETED, transformer.convertStatus( CarePlan.CarePlanStatus.COMPLETED ) ); + } + + @Test + public void convertStatusRevoked() + { + Assert.assertEquals( EnrollmentStatus.CANCELLED, transformer.convertStatus( CarePlan.CarePlanStatus.REVOKED ) ); + } + + @Test( expected = TransformerDataException.class ) + public void convertStatusNull() + { + transformer.convertStatus( null ); + } + + @Test( expected = TransformerDataException.class ) + public void convertStatusOther() + { + transformer.convertStatus( CarePlan.CarePlanStatus.DRAFT ); + } + + @Test + public void getProgramRefUri() + { + final CarePlan carePlan = new CarePlan(); + carePlan.addInstantiatesUri( "PlanDefinition/sj87dhyui82" ); + + Assert.assertEquals( new org.dhis2.fhir.adapter.dhis.model.Reference( "sj87dhyui82", ReferenceType.ID ), + transformer.getProgramRef( transformerContext, new RuleInfo<>( new EnrollmentRule(), Collections.emptyList() ), new HashMap<>(), carePlan ) ); + } + + @Test + public void getProgramRefCanonical() + { + final CarePlan carePlan = new CarePlan(); + carePlan.addInstantiatesCanonical( "PlanDefinition/sj87dhyui82" ); + + Assert.assertEquals( new org.dhis2.fhir.adapter.dhis.model.Reference( "sj87dhyui82", ReferenceType.ID ), + transformer.getProgramRef( transformerContext, new RuleInfo<>( new EnrollmentRule(), Collections.emptyList() ), new HashMap<>(), carePlan ) ); + } + + @Test + public void getProgramRefCanonicalWithoutType() + { + final CarePlan carePlan = new CarePlan(); + carePlan.addInstantiatesCanonical( "sj87dhyui82" ); + + Assert.assertEquals( new org.dhis2.fhir.adapter.dhis.model.Reference( "sj87dhyui82", ReferenceType.ID ), + transformer.getProgramRef( transformerContext, new RuleInfo<>( new EnrollmentRule(), Collections.emptyList() ), new HashMap<>(), carePlan ) ); + } + + @Test( expected = TransformerDataException.class ) + public void getProgramRefNull() + { + final CarePlan carePlan = new CarePlan(); + transformer.getProgramRef( transformerContext, new RuleInfo<>( new EnrollmentRule(), Collections.emptyList() ), new HashMap<>(), carePlan ); + } +} \ No newline at end of file diff --git a/fhir-r4/src/test/java/org/dhis2/fhir/adapter/fhir/transform/fhir/impl/program/r4/R4FhirQuestionnaireResponseToProgramStageTransformerTest.java b/fhir-r4/src/test/java/org/dhis2/fhir/adapter/fhir/transform/fhir/impl/program/r4/R4FhirQuestionnaireResponseToProgramStageTransformerTest.java new file mode 100644 index 00000000..68049c0d --- /dev/null +++ b/fhir-r4/src/test/java/org/dhis2/fhir/adapter/fhir/transform/fhir/impl/program/r4/R4FhirQuestionnaireResponseToProgramStageTransformerTest.java @@ -0,0 +1,320 @@ +package org.dhis2.fhir.adapter.fhir.transform.fhir.impl.program.r4; + +/* + * Copyright (c) 2004-2019, 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. + */ + +import ca.uhn.fhir.model.primitive.IdDt; +import org.dhis2.fhir.adapter.dhis.converter.ValueConverter; +import org.dhis2.fhir.adapter.dhis.model.ReferenceType; +import org.dhis2.fhir.adapter.dhis.model.WritableDataElement; +import org.dhis2.fhir.adapter.dhis.model.WritableDataValue; +import org.dhis2.fhir.adapter.dhis.orgunit.OrganizationUnitService; +import org.dhis2.fhir.adapter.dhis.tracker.program.EnrollmentService; +import org.dhis2.fhir.adapter.dhis.tracker.program.Event; +import org.dhis2.fhir.adapter.dhis.tracker.program.EventStatus; +import org.dhis2.fhir.adapter.dhis.tracker.program.ProgramMetadataService; +import org.dhis2.fhir.adapter.dhis.tracker.program.ProgramStageMetadataService; +import org.dhis2.fhir.adapter.dhis.tracker.program.WritableProgram; +import org.dhis2.fhir.adapter.dhis.tracker.program.WritableProgramStage; +import org.dhis2.fhir.adapter.dhis.tracker.program.WritableProgramStageDataElement; +import org.dhis2.fhir.adapter.dhis.tracker.trackedentity.TrackedEntityAttributes; +import org.dhis2.fhir.adapter.dhis.tracker.trackedentity.TrackedEntityMetadataService; +import org.dhis2.fhir.adapter.dhis.tracker.trackedentity.TrackedEntityService; +import org.dhis2.fhir.adapter.dhis.tracker.trackedentity.TrackedEntityType; +import org.dhis2.fhir.adapter.fhir.data.repository.FhirDhisAssignmentRepository; +import org.dhis2.fhir.adapter.fhir.extension.DueDateExtensionUtils; +import org.dhis2.fhir.adapter.fhir.extension.EventStatusExtensionUtils; +import org.dhis2.fhir.adapter.fhir.extension.LocationExtensionUtils; +import org.dhis2.fhir.adapter.fhir.metadata.model.ExecutableScript; +import org.dhis2.fhir.adapter.fhir.metadata.model.FhirResourceMapping; +import org.dhis2.fhir.adapter.fhir.metadata.model.ProgramStageRule; +import org.dhis2.fhir.adapter.fhir.metadata.model.RuleInfo; +import org.dhis2.fhir.adapter.fhir.metadata.repository.FhirResourceMappingRepository; +import org.dhis2.fhir.adapter.fhir.metadata.repository.TrackedEntityRuleRepository; +import org.dhis2.fhir.adapter.fhir.model.FhirVersion; +import org.dhis2.fhir.adapter.fhir.script.ScriptExecutionContext; +import org.dhis2.fhir.adapter.fhir.script.ScriptExecutor; +import org.dhis2.fhir.adapter.fhir.transform.TransformerDataException; +import org.dhis2.fhir.adapter.fhir.transform.dhis.impl.util.r4.R4ValueTypeFhirToDhisTransformerUtils; +import org.dhis2.fhir.adapter.fhir.transform.fhir.FhirToDhisTransformerContext; +import org.dhis2.fhir.adapter.fhir.transform.fhir.impl.util.AssignmentFhirToDhisTransformerUtils; +import org.dhis2.fhir.adapter.fhir.transform.scripted.ScriptedTrackedEntityInstance; +import org.dhis2.fhir.adapter.fhir.transform.scripted.WritableScriptedEvent; +import org.dhis2.fhir.adapter.fhir.transform.scripted.WritableScriptedTrackedEntityInstance; +import org.dhis2.fhir.adapter.model.ValueType; +import org.hl7.fhir.r4.model.Patient; +import org.hl7.fhir.r4.model.QuestionnaireResponse; +import org.hl7.fhir.r4.model.Reference; +import org.hl7.fhir.r4.model.ResourceFactory; +import org.hl7.fhir.r4.model.StringType; +import org.junit.Assert; +import org.junit.Rule; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnit; +import org.mockito.junit.MockitoRule; + +import java.time.ZonedDateTime; +import java.util.ArrayList; +import java.util.Collections; +import java.util.Date; +import java.util.HashMap; +import java.util.Map; + +/** + * Unit tests for {@link R4FhirQuestionnaireResponseToProgramStageTransformer}. + * + * @author volsch + */ +public class R4FhirQuestionnaireResponseToProgramStageTransformerTest +{ + @Mock + private ScriptExecutor scriptExecutor; + + @Mock + private TrackedEntityMetadataService trackedEntityMetadataService; + + @Mock + private TrackedEntityService trackedEntityService; + + @Mock + private TrackedEntityRuleRepository trackedEntityRuleRepository; + + @Mock + private OrganizationUnitService organizationUnitService; + + @Mock + private ProgramMetadataService programMetadataService; + + @Mock + private ProgramStageMetadataService programStageMetadataService; + + @Mock + private EnrollmentService enrollmentService; + + @Mock + private FhirResourceMappingRepository resourceMappingRepository; + + @Mock + private FhirDhisAssignmentRepository fhirDhisAssignmentRepository; + + @Mock + private ScriptExecutionContext scriptExecutionContext; + + @Mock + private ValueConverter valueConverter; + + @Mock + private FhirToDhisTransformerContext transformerContext; + + @Mock + private FhirResourceMapping fhirResourceMapping; + + @Mock + private TrackedEntityType trackedEntityType; + + @Mock + private TrackedEntityAttributes trackedEntityAttributes; + + @Mock + private ExecutableScript orgUnitExecutableScript; + + @Mock + private AssignmentFhirToDhisTransformerUtils assignmentUtils; + + @InjectMocks + private R4FhirQuestionnaireResponseToProgramStageTransformer transformer; + + @Rule + public MockitoRule rule = MockitoJUnit.rule(); + + @Test + public void getFhirVersions() + { + Assert.assertEquals( FhirVersion.R4_ONLY, transformer.getFhirVersions() ); + } + + @Test + public void transformInternal() + { + final WritableProgram program = new WritableProgram(); + program.setId( "x72638dsydu" ); + program.setName( "Test Program" ); + program.setDescription( "Test Description" ); + program.setWithoutRegistration( false ); + program.setTrackedEntityTypeId( "a1234567890" ); + program.setStages( new ArrayList<>() ); + + final WritableProgramStage programStage = new WritableProgramStage(); + programStage.setId( "x12638bsyda" ); + programStage.setName( "Test Stage 1" ); + programStage.setDescription( "Test Description 1" ); + programStage.setRepeatable( false ); + programStage.setDataElements( new ArrayList<>() ); + + WritableProgramStageDataElement programStageDataElement = new WritableProgramStageDataElement(); + WritableDataElement dataElement = new WritableDataElement(); + dataElement.setId( "d0123456789" ); + dataElement.setName( "Value 1" ); + dataElement.setValueType( ValueType.TEXT ); + programStageDataElement.setElement( dataElement ); + programStage.getDataElements().add( programStageDataElement ); + + programStageDataElement = new WritableProgramStageDataElement(); + dataElement = new WritableDataElement(); + dataElement.setId( "d7123456789" ); + dataElement.setName( "Value 2" ); + dataElement.setValueType( ValueType.TEXT ); + programStageDataElement.setElement( dataElement ); + programStage.getDataElements().add( programStageDataElement ); + + programStageDataElement = new WritableProgramStageDataElement(); + dataElement = new WritableDataElement(); + dataElement.setId( "f7123456789" ); + dataElement.setName( "Value 3" ); + dataElement.setValueType( ValueType.TEXT ); + programStageDataElement.setElement( dataElement ); + programStage.getDataElements().add( programStageDataElement ); + + final Patient patient = new Patient(); + patient.setId( new IdDt( "Patient/lyau3syaixys" ) ); + + final ScriptedTrackedEntityInstance scriptedTrackedEntityInstance = new WritableScriptedTrackedEntityInstance( trackedEntityMetadataService, trackedEntityService, trackedEntityAttributes, trackedEntityType, "lyau3syaixys", + scriptExecutionContext, valueConverter ); + + final ArrayList dataValues = new ArrayList<>(); + dataValues.add( new WritableDataValue( "d0123456789", "Test Value 1x" ) ); + dataValues.add( new WritableDataValue( "d7123456789", "Test Value 2x" ) ); + + final Event event = new Event(); + event.setDataValues( dataValues ); + + final QuestionnaireResponse fhirQuestionnaireResponse = new QuestionnaireResponse(); + fhirQuestionnaireResponse.setAuthored( Date.from( ZonedDateTime.parse( "2011-12-03T10:15:30+01:00" ).toInstant() ) ); + fhirQuestionnaireResponse.setQuestionnaire( "Questionnaire/x72638dsydu" ); + fhirQuestionnaireResponse.addBasedOn().setReference( "CarePlan/a73dhsjxhs8" ); + fhirQuestionnaireResponse.setStatus( QuestionnaireResponse.QuestionnaireResponseStatus.COMPLETED ); + EventStatusExtensionUtils.setValue( fhirQuestionnaireResponse, EventStatus.OVERDUE, ResourceFactory::createType ); + LocationExtensionUtils.setValue( fhirQuestionnaireResponse, new Reference( "Location/j82jdy28dusx" ) ); + DueDateExtensionUtils.setValue( fhirQuestionnaireResponse, Date.from( ZonedDateTime.parse( "2011-12-08T10:11:30+01:00" ).toInstant() ), ResourceFactory::createType ); + + fhirQuestionnaireResponse.addItem().setLinkId( "d0123456789" ).addAnswer().setValue( new StringType( "New Value 1x" ) ); + fhirQuestionnaireResponse.addItem().setLinkId( "f7123456789" ).addAnswer().setValue( new StringType( "Additional Value 3x" ) ); + + final Map scriptVariables = new HashMap<>(); + scriptVariables.put( "assignmentUtils", assignmentUtils ); + scriptVariables.put( "valueTypeUtils", new R4ValueTypeFhirToDhisTransformerUtils( scriptExecutionContext ) ); + + Mockito.doReturn( "a73dhsjxhs8" ).when( assignmentUtils ).getMappedDhisId( Mockito.same( transformerContext ), Mockito.any(), Mockito.any() ); + Mockito.doReturn( orgUnitExecutableScript ).when( fhirResourceMapping ) + .getImpEnrollmentOrgLookupScript(); + Mockito.doReturn( org.dhis2.fhir.adapter.dhis.model.Reference.createIdReference( "j82jdy28dusx" ) ) + .when( scriptExecutor ).execute( Mockito.same( orgUnitExecutableScript ), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any() ); + + transformer.transformInternal( transformerContext, new RuleInfo<>( new ProgramStageRule(), Collections.emptyList() ), scriptVariables, fhirResourceMapping, + program, programStage, fhirQuestionnaireResponse, scriptedTrackedEntityInstance, new WritableScriptedEvent( program, programStage, event, scriptedTrackedEntityInstance, scriptExecutionContext, valueConverter ) ); + + Assert.assertEquals( "x72638dsydu", event.getProgramId() ); + Assert.assertEquals( "j82jdy28dusx", event.getOrgUnitId() ); + Assert.assertEquals( "lyau3syaixys", event.getTrackedEntityInstanceId() ); + Assert.assertEquals( "a73dhsjxhs8", event.getEnrollmentId() ); + Assert.assertEquals( EventStatus.OVERDUE, event.getStatus() ); + Assert.assertEquals( ZonedDateTime.parse( "2011-12-03T10:15:30+01:00" ).toLocalDateTime(), event.getEventDate().toLocalDateTime() ); + Assert.assertEquals( ZonedDateTime.parse( "2011-12-08T10:11:30+01:00" ).toLocalDateTime(), event.getDueDate().toLocalDateTime() ); + + Assert.assertEquals( 2, event.getDataValues().size() ); + Assert.assertEquals( "d0123456789", event.getDataValues().get( 0 ).getDataElementId() ); + Assert.assertEquals( "New Value 1x", event.getDataValues().get( 0 ).getValue() ); + Assert.assertEquals( "f7123456789", event.getDataValues().get( 1 ).getDataElementId() ); + Assert.assertEquals( "Additional Value 3x", event.getDataValues().get( 1 ).getValue() ); + } + + @Test + public void getProgramStageRef() + { + final QuestionnaireResponse questionnaireResponse = new QuestionnaireResponse(); + questionnaireResponse.setQuestionnaire( "Questionnaire/sj87dhyui82" ); + + Assert.assertEquals( new org.dhis2.fhir.adapter.dhis.model.Reference( "sj87dhyui82", ReferenceType.ID ), + transformer.getProgramStageRef( transformerContext, new RuleInfo<>( new ProgramStageRule(), Collections.emptyList() ), new HashMap<>(), questionnaireResponse ) ); + } + + @Test + public void getProgramRefWithoutType() + { + final QuestionnaireResponse questionnaireResponse = new QuestionnaireResponse(); + questionnaireResponse.setQuestionnaire( "sj87dhyui82" ); + + Assert.assertEquals( new org.dhis2.fhir.adapter.dhis.model.Reference( "sj87dhyui82", ReferenceType.ID ), + transformer.getProgramStageRef( transformerContext, new RuleInfo<>( new ProgramStageRule(), Collections.emptyList() ), new HashMap<>(), questionnaireResponse ) ); + } + + @Test( expected = TransformerDataException.class ) + public void getProgramRefNull() + { + final QuestionnaireResponse questionnaireResponse = new QuestionnaireResponse(); + transformer.getProgramStageRef( transformerContext, new RuleInfo<>( new ProgramStageRule(), Collections.emptyList() ), new HashMap<>(), questionnaireResponse ); + } + + @Test + public void convertStatusCompleted() + { + final QuestionnaireResponse questionnaireResponse = new QuestionnaireResponse(); + questionnaireResponse.setStatus( QuestionnaireResponse.QuestionnaireResponseStatus.COMPLETED ); + + Assert.assertEquals( EventStatus.COMPLETED, transformer.convertStatus( questionnaireResponse ) ); + } + + @Test + public void convertStatusInProgress() + { + final QuestionnaireResponse questionnaireResponse = new QuestionnaireResponse(); + questionnaireResponse.setStatus( QuestionnaireResponse.QuestionnaireResponseStatus.INPROGRESS ); + + Assert.assertEquals( EventStatus.ACTIVE, transformer.convertStatus( questionnaireResponse ) ); + } + + @Test + public void convertStatusEventOverdue() + { + final QuestionnaireResponse questionnaireResponse = new QuestionnaireResponse(); + questionnaireResponse.setStatus( QuestionnaireResponse.QuestionnaireResponseStatus.COMPLETED ); + EventStatusExtensionUtils.setValue( questionnaireResponse, EventStatus.OVERDUE, ResourceFactory::createType ); + + Assert.assertEquals( EventStatus.OVERDUE, transformer.convertStatus( questionnaireResponse ) ); + } + + @Test( expected = TransformerDataException.class ) + public void convertStatusNull() + { + final QuestionnaireResponse questionnaireResponse = new QuestionnaireResponse(); + transformer.convertStatus( questionnaireResponse ); + } +} \ No newline at end of file diff --git a/fhir-r4/src/test/java/org/dhis2/fhir/adapter/fhir/transform/fhir/impl/util/r4/R4AssignmentDhisToFhirTransformerUtilsTest.java b/fhir-r4/src/test/java/org/dhis2/fhir/adapter/fhir/transform/fhir/impl/util/r4/R4AssignmentDhisToFhirTransformerUtilsTest.java new file mode 100644 index 00000000..a6dd62dd --- /dev/null +++ b/fhir-r4/src/test/java/org/dhis2/fhir/adapter/fhir/transform/fhir/impl/util/r4/R4AssignmentDhisToFhirTransformerUtilsTest.java @@ -0,0 +1,145 @@ +package org.dhis2.fhir.adapter.fhir.transform.fhir.impl.util.r4; + +/* + * Copyright (c) 2004-2019, 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. + */ + +import org.dhis2.fhir.adapter.dhis.model.DhisResourceId; +import org.dhis2.fhir.adapter.dhis.model.DhisResourceType; +import org.dhis2.fhir.adapter.fhir.data.repository.FhirDhisAssignmentRepository; +import org.dhis2.fhir.adapter.fhir.metadata.model.EnrollmentRule; +import org.dhis2.fhir.adapter.fhir.metadata.model.FhirClient; +import org.dhis2.fhir.adapter.fhir.metadata.model.FhirResourceType; +import org.dhis2.fhir.adapter.fhir.metadata.repository.FhirClientRepository; +import org.dhis2.fhir.adapter.fhir.model.FhirVersion; +import org.dhis2.fhir.adapter.fhir.script.ScriptExecutionContext; +import org.dhis2.fhir.adapter.fhir.transform.dhis.DhisToFhirTransformerContext; +import org.dhis2.fhir.adapter.fhir.transform.dhis.model.DhisRequest; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnit; +import org.mockito.junit.MockitoRule; + +import java.util.Objects; +import java.util.Optional; +import java.util.UUID; + +/** + * Unit tests for {@link R4AssignmentDhisToFhirTransformerUtils}. + * + * @author volsch + */ +public class R4AssignmentDhisToFhirTransformerUtilsTest +{ + @Mock + private ScriptExecutionContext scriptExecutionContext; + + @Mock + private FhirDhisAssignmentRepository assignmentRepository; + + @Mock + private FhirClientRepository fhirClientRepository; + + @Mock + private DhisToFhirTransformerContext transformerContext; + + @Mock + private DhisRequest dhisRequest; + + @Mock + private FhirClient fhirClient; + + @InjectMocks + private R4AssignmentDhisToFhirTransformerUtils utils; + + @Rule + public MockitoRule rule = MockitoJUnit.rule().silent(); + + @Before + public void setUp() + { + Mockito.doReturn( dhisRequest ).when( transformerContext ).getDhisRequest(); + } + + @Test + public void getFhirVersions() + { + Assert.assertEquals( FhirVersion.R4_ONLY, utils.getFhirVersions() ); + } + + @Test + public void getMappedFhirIdNull() + { + Assert.assertNull( utils.getMappedFhirId( transformerContext, new EnrollmentRule(), DhisResourceType.ENROLLMENT, null, FhirResourceType.CARE_PLAN ) ); + } + + @Test + public void getMappedFhirIdSync() + { + Assert.assertEquals( "PlanDefinition/hajdhcjsu89", + Objects.requireNonNull( utils.getMappedFhirId( transformerContext, new EnrollmentRule(), DhisResourceType.ENROLLMENT, "hajdhcjsu89", FhirResourceType.PLAN_DEFINITION ) ).getReferenceElement().getValue() ); + } + + @Test + public void getMappedFhirIdDhis() + { + Mockito.doReturn( true ).when( dhisRequest ).isDhisFhirId(); + + Assert.assertEquals( "CarePlan/hajdhcjsu89", + Objects.requireNonNull( utils.getMappedFhirId( transformerContext, new EnrollmentRule(), DhisResourceType.ENROLLMENT, "hajdhcjsu89", FhirResourceType.CARE_PLAN ) ).getReferenceElement().getValue() ); + } + + @Test + public void getMappedFhirId() + { + final UUID fhirClientId = UUID.randomUUID(); + + Mockito.doReturn( fhirClientId ).when( transformerContext ).getFhirClientId(); + Mockito.doReturn( Optional.of( fhirClient ) ).when( fhirClientRepository ).findOneByIdCached( Mockito.eq( fhirClientId ) ); + Mockito.doReturn( "4711" ).when( assignmentRepository ).findFirstFhirResourceId( Mockito.same( fhirClient ), Mockito.eq( new DhisResourceId( DhisResourceType.ENROLLMENT, "hajdhcjsu89" ) ) ); + + Assert.assertEquals( "CarePlan/4711", + Objects.requireNonNull( utils.getMappedFhirId( transformerContext, new EnrollmentRule(), DhisResourceType.ENROLLMENT, "hajdhcjsu89", FhirResourceType.CARE_PLAN ) ).getReferenceElement().getValue() ); + } + + @Test + public void getMappedFhirIdNullMapping() + { + final UUID fhirClientId = UUID.randomUUID(); + + Mockito.doReturn( fhirClientId ).when( transformerContext ).getFhirClientId(); + Mockito.doReturn( Optional.of( fhirClient ) ).when( fhirClientRepository ).findOneByIdCached( Mockito.eq( fhirClientId ) ); + Mockito.doReturn( null ).when( assignmentRepository ).findFirstFhirResourceId( Mockito.same( fhirClient ), Mockito.eq( new DhisResourceId( DhisResourceType.ENROLLMENT, "hajdhcjsu89" ) ) ); + + Assert.assertNull( utils.getMappedFhirId( transformerContext, new EnrollmentRule(), DhisResourceType.ENROLLMENT, "hajdhcjsu89", FhirResourceType.CARE_PLAN ) ); + } +} \ No newline at end of file diff --git a/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/metadata/model/ProgramStageRule.java b/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/metadata/model/ProgramStageRule.java index 80823bb9..3b2bb4da 100644 --- a/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/metadata/model/ProgramStageRule.java +++ b/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/metadata/model/ProgramStageRule.java @@ -62,13 +62,16 @@ @NamedQueries( { @NamedQuery( name = ProgramStageRule.FIND_ALL_EXP_NAMED_QUERY, query = "SELECT psr FROM ProgramStageRule psr " + - "LEFT JOIN psr.programStage ps ON (ps.enabled=true AND ps.expEnabled=true AND (ps.fhirCreateEnabled=true OR ps.fhirUpdateEnabled=true) AND ps.programStageReference IN (:programStageReferences)) " + - "LEFT JOIN ps.program p ON (p.enabled=true AND p.expEnabled=true AND (p.fhirCreateEnabled=true OR p.fhirUpdateEnabled=true) AND p.programReference IN (:programReferences)) WHERE " + + "JOIN psr.programStage ps ON (ps.enabled=true AND ps.expEnabled=true AND (ps.fhirCreateEnabled=true OR ps.fhirUpdateEnabled=true) AND ps.programStageReference IN (:programStageReferences)) " + + "JOIN ps.program p ON (p.enabled=true AND p.expEnabled=true AND (p.fhirCreateEnabled=true OR p.fhirUpdateEnabled=true) AND p.programReference IN (:programReferences)) WHERE " + + "psr.enabled=true AND psr.expEnabled=true AND (psr.fhirCreateEnabled=true OR psr.fhirUpdateEnabled=true)" ), + @NamedQuery( name = ProgramStageRule.FIND_ALL_EXP_WILDCARD_NAMED_QUERY, + query = "SELECT psr FROM ProgramStageRule psr WHERE psr.programStage IS NULL AND " + "psr.enabled=true AND psr.expEnabled=true AND (psr.fhirCreateEnabled=true OR psr.fhirUpdateEnabled=true)" ), @NamedQuery( name = ProgramStageRule.FIND_ALL_EXP_BY_DATA_REF_NAMED_QUERY, query = "SELECT psr FROM ProgramStageRule psr " + - "LEFT JOIN psr.programStage ps ON (ps.enabled=true AND ps.expEnabled=true AND (ps.fhirCreateEnabled=true OR ps.fhirUpdateEnabled=true) AND ps.programStageReference IN (:programStageReferences)) " + - "LEFT JOIN ps.program p ON (p.enabled=true AND p.expEnabled=true AND (p.fhirCreateEnabled=true OR p.fhirUpdateEnabled=true) AND p.programReference IN (:programReferences)) WHERE " + + "JOIN psr.programStage ps ON (ps.enabled=true AND ps.expEnabled=true AND (ps.fhirCreateEnabled=true OR ps.fhirUpdateEnabled=true) AND ps.programStageReference IN (:programStageReferences)) " + + "JOIN ps.program p ON (p.enabled=true AND p.expEnabled=true AND (p.fhirCreateEnabled=true OR p.fhirUpdateEnabled=true) AND p.programReference IN (:programReferences)) WHERE " + "psr.enabled=true AND psr.expEnabled=true AND (psr.fhirCreateEnabled=true OR psr.fhirUpdateEnabled=true) AND " + "EXISTS (SELECT 1 FROM RuleDhisDataReference edr WHERE edr.rule=psr AND edr.dataReference IN (:dataReferences))" ) } ) @@ -80,6 +83,8 @@ public class ProgramStageRule extends AbstractRule public static final String FIND_ALL_EXP_NAMED_QUERY = "ProgramStageRule.findAllExportedWithoutDataRef"; + public static final String FIND_ALL_EXP_WILDCARD_NAMED_QUERY = "ProgramStageRule.findAllExportedWithoutProgramStageAndDataRef"; + public static final String FIND_ALL_EXP_BY_DATA_REF_NAMED_QUERY = "ProgramStageRule.findAllExportedByDataRef"; private MappedTrackerProgramStage programStage; diff --git a/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/metadata/repository/impl/CustomProgramStageRuleRepositoryImpl.java b/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/metadata/repository/impl/CustomProgramStageRuleRepositoryImpl.java index c091cda4..c9914bf7 100644 --- a/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/metadata/repository/impl/CustomProgramStageRuleRepositoryImpl.java +++ b/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/metadata/repository/impl/CustomProgramStageRuleRepositoryImpl.java @@ -73,12 +73,13 @@ public CustomProgramStageRuleRepositoryImpl( @Nonnull EntityManager entityManage public Collection> findAllExp( @Nonnull Collection programReferences, @Nonnull Collection programStageReferences, @Nullable Collection dataReferences ) { final List rules; - if ( (dataReferences == null) || dataReferences.isEmpty() ) + if ( dataReferences == null || dataReferences.isEmpty() ) { rules = new ArrayList<>( entityManager.createNamedQuery( ProgramStageRule.FIND_ALL_EXP_NAMED_QUERY, ProgramStageRule.class ) .setParameter( "programReferences", programReferences ) .setParameter( "programStageReferences", programStageReferences ).getResultList() ); + rules.addAll( entityManager.createNamedQuery( ProgramStageRule.FIND_ALL_EXP_WILDCARD_NAMED_QUERY, ProgramStageRule.class ).getResultList() ); } else { diff --git a/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/transform/fhir/impl/util/AssignmentFhirToDhisTransformerUtilsTest.java b/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/transform/fhir/impl/util/AssignmentFhirToDhisTransformerUtilsTest.java new file mode 100644 index 00000000..42ad31b9 --- /dev/null +++ b/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/transform/fhir/impl/util/AssignmentFhirToDhisTransformerUtilsTest.java @@ -0,0 +1,140 @@ +package org.dhis2.fhir.adapter.fhir.transform.fhir.impl.util; + +/* + * Copyright (c) 2004-2019, 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. + */ + +import ca.uhn.fhir.model.primitive.IdDt; +import org.dhis2.fhir.adapter.fhir.data.repository.FhirDhisAssignmentRepository; +import org.dhis2.fhir.adapter.fhir.metadata.model.EnrollmentRule; +import org.dhis2.fhir.adapter.fhir.metadata.model.FhirClient; +import org.dhis2.fhir.adapter.fhir.metadata.repository.FhirClientRepository; +import org.dhis2.fhir.adapter.fhir.model.FhirVersion; +import org.dhis2.fhir.adapter.fhir.script.ScriptExecutionContext; +import org.dhis2.fhir.adapter.fhir.transform.fhir.FhirToDhisTransformerContext; +import org.dhis2.fhir.adapter.fhir.transform.fhir.model.FhirRequest; +import org.junit.Assert; +import org.junit.Before; +import org.junit.Rule; +import org.junit.Test; +import org.mockito.InjectMocks; +import org.mockito.Mock; +import org.mockito.Mockito; +import org.mockito.junit.MockitoJUnit; +import org.mockito.junit.MockitoRule; + +import java.util.Optional; +import java.util.UUID; + +/** + * Unit tests for {@link AssignmentFhirToDhisTransformerUtils}. + * + * @author volsch + */ +public class AssignmentFhirToDhisTransformerUtilsTest +{ + @Mock + private ScriptExecutionContext scriptExecutionContext; + + @Mock + private FhirDhisAssignmentRepository assignmentRepository; + + @Mock + private FhirClientRepository fhirClientRepository; + + @Mock + private FhirToDhisTransformerContext transformerContext; + + @Mock + private FhirRequest fhirRequest; + + @Mock + private FhirClient fhirClient; + + @InjectMocks + private AssignmentFhirToDhisTransformerUtils utils; + + @Rule + public MockitoRule rule = MockitoJUnit.rule().silent(); + + @Before + public void setUp() + { + Mockito.doReturn( fhirRequest ).when( transformerContext ).getFhirRequest(); + } + + @Test + public void getFhirVersions() + { + Assert.assertEquals( FhirVersion.ALL, utils.getFhirVersions() ); + } + + @Test + public void getMappedDhisIdNull() + { + Assert.assertNull( utils.getMappedDhisId( transformerContext, new EnrollmentRule(), null ) ); + } + + @Test + public void getMappedDhisIdSync() + { + Assert.assertEquals( "hajdhcjsu89", utils.getMappedDhisId( transformerContext, new EnrollmentRule(), new IdDt( "PlanDefinition/hajdhcjsu89" ) ) ); + } + + @Test + public void getMappedDhisIdDhis() + { + Mockito.doReturn( true ).when( fhirRequest ).isDhisFhirId(); + + Assert.assertEquals( "hajdhcjsu89", utils.getMappedDhisId( transformerContext, new EnrollmentRule(), new IdDt( "CarePlan/hajdhcjsu89" ) ) ); + } + + @Test + public void getMappedDhisId() + { + final UUID fhirClientId = UUID.randomUUID(); + + Mockito.doReturn( fhirClientId ).when( fhirRequest ).getFhirClientId(); + Mockito.doReturn( Optional.of( fhirClient ) ).when( fhirClientRepository ).findOneByIdCached( Mockito.eq( fhirClientId ) ); + Mockito.doReturn( "4711" ).when( assignmentRepository ).findFirstDhisResourceId( Mockito.same( fhirClient ), Mockito.any() ); + + Assert.assertEquals( "4711", + utils.getMappedDhisId( transformerContext, new EnrollmentRule(), new IdDt( "CarePlan/hajdhcjsu89" ) ) ); + } + + @Test + public void getMappedDhisIdNullMapping() + { + final UUID fhirClientId = UUID.randomUUID(); + + Mockito.doReturn( fhirClientId ).when( fhirRequest ).getFhirClientId(); + Mockito.doReturn( Optional.of( fhirClient ) ).when( fhirClientRepository ).findOneByIdCached( Mockito.eq( fhirClientId ) ); + Mockito.doReturn( null ).when( assignmentRepository ).findFirstDhisResourceId( Mockito.same( fhirClient ), Mockito.any() ); + + Assert.assertNull( utils.getMappedDhisId( transformerContext, new EnrollmentRule(), new IdDt( "CarePlan/hajdhcjsu89" ) ) ); + } +}