Skip to content

Commit

Permalink
Adds unit tests for FHIR to DHIS2 care related resources.
Browse files Browse the repository at this point in the history
  • Loading branch information
volsch committed Aug 18, 2019
1 parent e8388ae commit bf11160
Show file tree
Hide file tree
Showing 8 changed files with 1,025 additions and 15 deletions.
Original file line number Diff line number Diff line change
@@ -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 ) );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 ) );
Expand Down Expand Up @@ -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 )
{
Expand Down
Loading

0 comments on commit bf11160

Please sign in to comment.