Skip to content

Commit

Permalink
Adds tests for program to plan definition transformation.
Browse files Browse the repository at this point in the history
  • Loading branch information
volsch committed Aug 2, 2019
1 parent 3ff40b1 commit 9a81552
Show file tree
Hide file tree
Showing 16 changed files with 624 additions and 73 deletions.
1 change: 1 addition & 0 deletions app/src/main/resources/default-application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,7 @@ dhis2.fhir-adapter:
# DHIS 2 resource types that will be synchronized.
resource-types:
- ORGANIZATION_UNIT
- PROGRAM_METADATA
- TRACKED_ENTITY
- PROGRAM_STAGE_EVENT
# The queue that is used to store distributed requests to synchronize the data from
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"pager": { "page": 1, "pageCount": 1, "total": 0, "pageSize": 1 },
"programs": [
{
"name": "Child Programme",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package org.dhis2.fhir.adapter.fhir.transform.dhis.impl.metadata.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.tracker.program.ProgramMetadataService;
import org.dhis2.fhir.adapter.fhir.metadata.model.ProgramMetadataRule;
import org.dhis2.fhir.adapter.fhir.metadata.model.RuleInfo;
import org.dhis2.fhir.adapter.fhir.model.FhirVersion;
import org.dhis2.fhir.adapter.fhir.script.ScriptExecutor;
import org.dhis2.fhir.adapter.fhir.transform.dhis.search.SearchFilter;
import org.dhis2.fhir.adapter.fhir.transform.dhis.search.SearchFilterCollector;
import org.junit.Assert;
import org.junit.Rule;
import org.junit.Test;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnit;
import org.mockito.junit.MockitoRule;
import org.springframework.web.util.DefaultUriBuilderFactory;
import org.springframework.web.util.UriBuilder;

import java.net.URI;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* Unit tests for {@link R4ProgramMetadataToFhirDataProvider}.
*
* @author volsch
*/
public class R4ProgramMetadataToFhirDataProviderTest
{
@Mock
private ScriptExecutor scriptExecutor;

@Mock
private ProgramMetadataService programMetadataService;

@InjectMocks
private R4ProgramMetadataToFhirDataProvider provider;

@Rule
public MockitoRule rule = MockitoJUnit.rule();

@Test
public void getFhirVersions()
{
Assert.assertEquals( FhirVersion.R4_ONLY, provider.getFhirVersions() );
}

@Test
public void searchFilterName() throws URISyntaxException
{
final SearchFilterCollector searchFilterCollector = new SearchFilterCollector( Collections.singletonMap( "name", Collections.singletonList( "Child Programme" ) ) );
final SearchFilter searchFilter = new SearchFilter( searchFilterCollector, false );

provider.initSearchFilter( FhirVersion.R4, new RuleInfo<>( new ProgramMetadataRule(), Collections.emptyList() ), searchFilter );

final List<String> variables = new ArrayList<>();
UriBuilder uriBuilder = new DefaultUriBuilderFactory().builder();
uriBuilder = searchFilterCollector.add( uriBuilder, variables );

Assert.assertEquals( new URI( "?filter=%5Bname:$ilike:Child%20Programme%5D" ), uriBuilder.build( variables ) );
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,171 @@
package org.dhis2.fhir.adapter.fhir.transform.dhis.impl.metadata.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.Reference;
import org.dhis2.fhir.adapter.dhis.model.ReferenceType;
import org.dhis2.fhir.adapter.dhis.orgunit.OrganizationUnitService;
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.trackedentity.TrackedEntityMetadataService;
import org.dhis2.fhir.adapter.dhis.tracker.trackedentity.WritableTrackedEntityType;
import org.dhis2.fhir.adapter.fhir.data.repository.FhirDhisAssignmentRepository;
import org.dhis2.fhir.adapter.fhir.metadata.model.FhirClient;
import org.dhis2.fhir.adapter.fhir.metadata.model.ProgramMetadataRule;
import org.dhis2.fhir.adapter.fhir.metadata.model.RuleInfo;
import org.dhis2.fhir.adapter.fhir.metadata.repository.SystemRepository;
import org.dhis2.fhir.adapter.fhir.metadata.repository.TrackedEntityRuleRepository;
import org.dhis2.fhir.adapter.fhir.model.FhirVersion;
import org.dhis2.fhir.adapter.fhir.repository.FhirResourceRepository;
import org.dhis2.fhir.adapter.fhir.script.ScriptExecutionContext;
import org.dhis2.fhir.adapter.fhir.script.ScriptExecutor;
import org.dhis2.fhir.adapter.fhir.transform.dhis.DhisToFhirTransformerContext;
import org.dhis2.fhir.adapter.fhir.transform.scripted.WritableScriptedDhisMetadata;
import org.dhis2.fhir.adapter.lock.LockManager;
import org.hl7.fhir.r4.model.PlanDefinition;
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.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Optional;

/**
* Unit tests for {@link R4ProgramMetadataToFhirPlanDefinitionTransformer}.
*
* @author volsch
*/
public class R4ProgramMetadataToFhirPlanDefinitionTransformerTest
{
@Mock
private ScriptExecutor scriptExecutor;

@Mock
private LockManager lockManager;

@Mock
private SystemRepository systemRepository;

@Mock
private FhirResourceRepository fhirResourceRepository;

@Mock
private FhirDhisAssignmentRepository fhirDhisAssignmentRepository;

@Mock
private OrganizationUnitService organizationUnitService;

@Mock
private TrackedEntityMetadataService trackedEntityMetadataService;

@Mock
private TrackedEntityRuleRepository trackedEntityRuleRepository;

@Mock
private DhisToFhirTransformerContext context;

@Mock
private FhirClient fhirClient;

@Mock
private ScriptExecutionContext scriptExecutionContext;

@InjectMocks
private R4ProgramMetadataToFhirPlanDefinitionTransformer transformer;

@Rule
public MockitoRule rule = MockitoJUnit.rule();

@Test
public void getFhirVersions()
{
Assert.assertEquals( FhirVersion.R4_ONLY, transformer.getFhirVersions() );
}

@Test
public void transformInternal()
{
Mockito.doReturn( Optional.of( new WritableTrackedEntityType( "a1234567890", "Test", Collections.emptyList() ) ) )
.when( trackedEntityMetadataService ).findTypeByReference( Mockito.eq( new Reference( "a1234567890", ReferenceType.ID ) ) );
Mockito.doReturn( Collections.singletonList( new RuleInfo<>( new ProgramMetadataRule(), Collections.emptyList() ) ) )
.when( trackedEntityRuleRepository ).findByTypeRefs( Mockito.eq( new HashSet<>(
Arrays.asList( new Reference( "a1234567890", ReferenceType.ID ), new Reference( "Test", ReferenceType.NAME ) ) ) ) );

final WritableProgram program = new WritableProgram();
program.setName( "Test Program" );
program.setDescription( "Test Description" );
program.setWithoutRegistration( false );
program.setTrackedEntityTypeId( "a1234567890" );
program.setStages( new ArrayList<>() );

WritableProgramStage programStage = new WritableProgramStage();
programStage.setId( "b1234567890" );
programStage.setName( "Test Stage 1" );
programStage.setDescription( "Test Description 1" );
programStage.setRepeatable( false );
program.getStages().add( programStage );

programStage = new WritableProgramStage();
programStage.setId( "b1234567891" );
programStage.setName( "Test Stage 2" );
programStage.setDescription( "Test Description 2" );
programStage.setRepeatable( true );
program.getStages().add( programStage );

final PlanDefinition fhirPlanDefinition = new PlanDefinition();

transformer.transformInternal( fhirClient, context, new RuleInfo<>( new ProgramMetadataRule(), Collections.emptyList() ), new HashMap<>(),
new WritableScriptedDhisMetadata( program, scriptExecutionContext ), fhirPlanDefinition );

Assert.assertEquals( "Test Program", fhirPlanDefinition.getName() );
Assert.assertEquals( "Test Description", fhirPlanDefinition.getDescription() );
Assert.assertEquals( 2, fhirPlanDefinition.getAction().size() );

Assert.assertEquals( "b1234567890", fhirPlanDefinition.getAction().get( 0 ).getId() );
Assert.assertEquals( "Test Stage 1", fhirPlanDefinition.getAction().get( 0 ).getTitle() );
Assert.assertEquals( "Test Description 1", fhirPlanDefinition.getAction().get( 0 ).getDescription() );
Assert.assertEquals( PlanDefinition.ActionCardinalityBehavior.SINGLE, fhirPlanDefinition.getAction().get( 0 ).getCardinalityBehavior() );
Assert.assertEquals( "Questionnaire/b1234567890", fhirPlanDefinition.getAction().get( 0 ).getDefinition().primitiveValue() );

Assert.assertEquals( "b1234567891", fhirPlanDefinition.getAction().get( 1 ).getId() );
Assert.assertEquals( "Test Stage 2", fhirPlanDefinition.getAction().get( 1 ).getTitle() );
Assert.assertEquals( "Test Description 2", fhirPlanDefinition.getAction().get( 1 ).getDescription() );
Assert.assertEquals( PlanDefinition.ActionCardinalityBehavior.MULTIPLE, fhirPlanDefinition.getAction().get( 1 ).getCardinalityBehavior() );
Assert.assertEquals( "Questionnaire/b1234567891", fhirPlanDefinition.getAction().get( 1 ).getDefinition().primitiveValue() );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -110,8 +110,8 @@ public DhisResource findByDhisFhirIdentifierCasted( @Nonnull FhirClient fhirClie
public PreparedDhisToFhirSearch prepareSearchCasted( @Nonnull FhirVersion fhirVersion, @Nonnull List<RuleInfo<? extends AbstractRule>> ruleInfos, @Nullable Map<String, List<String>> filter, @Nullable DateRangeParam lastUpdatedDateRange, int count ) throws DhisToFhirDataProviderException
{
final Class<R> ruleClass = getRuleClass();
final List<RuleInfo<R>> castedRuleInfos =
ruleInfos.stream().map( r -> new RuleInfo<>( getRuleClass().cast( r.getRule() ), r.getDhisDataReferences() ) ).collect( Collectors.toList() );
final List<RuleInfo<R>> castedRuleInfos = ruleInfos.stream().map( r -> new RuleInfo<>( getRuleClass().cast( r.getRule() ), r.getDhisDataReferences() ) ).collect( Collectors.toList() );

return prepareSearch( fhirVersion, castedRuleInfos, filter, lastUpdatedDateRange, count );
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -58,11 +58,13 @@ public AbstractDhisToFhirRequestResolver( @Nonnull FhirClientRepository fhirClie
@Nonnull
public Optional<FhirClient> resolveFhirClient( @Nonnull ScriptedDhisResource scriptedDhisResource )
{
Optional<FhirClient> fhirClient = fhirClientRepository.findExpEnabledOnly();
final Optional<FhirClient> fhirClient = fhirClientRepository.findExpEnabledOnly();

if ( fhirClient.isPresent() && fhirClient.get().isEnabled() && fhirClient.get().isExpEnabled() )
{
return fhirClient;
}

return Optional.empty();
}
}
Loading

0 comments on commit 9a81552

Please sign in to comment.