|
| 1 | +package org.dhis2.fhir.adapter.fhir.transform.dhis.impl.metadata.r4; |
| 2 | + |
| 3 | +/* |
| 4 | + * Copyright (c) 2004-2019, University of Oslo |
| 5 | + * All rights reserved. |
| 6 | + * |
| 7 | + * Redistribution and use in source and binary forms, with or without |
| 8 | + * modification, are permitted provided that the following conditions are met: |
| 9 | + * Redistributions of source code must retain the above copyright notice, this |
| 10 | + * list of conditions and the following disclaimer. |
| 11 | + * |
| 12 | + * Redistributions in binary form must reproduce the above copyright notice, |
| 13 | + * this list of conditions and the following disclaimer in the documentation |
| 14 | + * and/or other materials provided with the distribution. |
| 15 | + * Neither the name of the HISP project nor the names of its contributors may |
| 16 | + * be used to endorse or promote products derived from this software without |
| 17 | + * specific prior written permission. |
| 18 | + * |
| 19 | + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
| 20 | + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
| 21 | + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
| 22 | + * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR |
| 23 | + * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES |
| 24 | + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 25 | + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON |
| 26 | + * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 27 | + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS |
| 28 | + * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 29 | + */ |
| 30 | + |
| 31 | +import org.dhis2.fhir.adapter.dhis.model.Reference; |
| 32 | +import org.dhis2.fhir.adapter.dhis.model.ReferenceType; |
| 33 | +import org.dhis2.fhir.adapter.dhis.orgunit.OrganizationUnitService; |
| 34 | +import org.dhis2.fhir.adapter.dhis.tracker.program.WritableProgram; |
| 35 | +import org.dhis2.fhir.adapter.dhis.tracker.program.WritableProgramStage; |
| 36 | +import org.dhis2.fhir.adapter.dhis.tracker.trackedentity.TrackedEntityMetadataService; |
| 37 | +import org.dhis2.fhir.adapter.dhis.tracker.trackedentity.WritableTrackedEntityType; |
| 38 | +import org.dhis2.fhir.adapter.fhir.data.repository.FhirDhisAssignmentRepository; |
| 39 | +import org.dhis2.fhir.adapter.fhir.metadata.model.FhirClient; |
| 40 | +import org.dhis2.fhir.adapter.fhir.metadata.model.ProgramMetadataRule; |
| 41 | +import org.dhis2.fhir.adapter.fhir.metadata.model.RuleInfo; |
| 42 | +import org.dhis2.fhir.adapter.fhir.metadata.repository.SystemRepository; |
| 43 | +import org.dhis2.fhir.adapter.fhir.metadata.repository.TrackedEntityRuleRepository; |
| 44 | +import org.dhis2.fhir.adapter.fhir.model.FhirVersion; |
| 45 | +import org.dhis2.fhir.adapter.fhir.repository.FhirResourceRepository; |
| 46 | +import org.dhis2.fhir.adapter.fhir.script.ScriptExecutionContext; |
| 47 | +import org.dhis2.fhir.adapter.fhir.script.ScriptExecutor; |
| 48 | +import org.dhis2.fhir.adapter.fhir.transform.dhis.DhisToFhirTransformerContext; |
| 49 | +import org.dhis2.fhir.adapter.fhir.transform.scripted.WritableScriptedDhisMetadata; |
| 50 | +import org.dhis2.fhir.adapter.lock.LockManager; |
| 51 | +import org.hl7.fhir.r4.model.PlanDefinition; |
| 52 | +import org.junit.Assert; |
| 53 | +import org.junit.Rule; |
| 54 | +import org.junit.Test; |
| 55 | +import org.mockito.InjectMocks; |
| 56 | +import org.mockito.Mock; |
| 57 | +import org.mockito.Mockito; |
| 58 | +import org.mockito.junit.MockitoJUnit; |
| 59 | +import org.mockito.junit.MockitoRule; |
| 60 | + |
| 61 | +import java.util.ArrayList; |
| 62 | +import java.util.Arrays; |
| 63 | +import java.util.Collections; |
| 64 | +import java.util.HashMap; |
| 65 | +import java.util.HashSet; |
| 66 | +import java.util.Optional; |
| 67 | + |
| 68 | +/** |
| 69 | + * Unit tests for {@link R4ProgramMetadataToFhirPlanDefinitionTransformer}. |
| 70 | + * |
| 71 | + * @author volsch |
| 72 | + */ |
| 73 | +public class R4ProgramMetadataToFhirPlanDefinitionTransformerTest |
| 74 | +{ |
| 75 | + @Mock |
| 76 | + private ScriptExecutor scriptExecutor; |
| 77 | + |
| 78 | + @Mock |
| 79 | + private LockManager lockManager; |
| 80 | + |
| 81 | + @Mock |
| 82 | + private SystemRepository systemRepository; |
| 83 | + |
| 84 | + @Mock |
| 85 | + private FhirResourceRepository fhirResourceRepository; |
| 86 | + |
| 87 | + @Mock |
| 88 | + private FhirDhisAssignmentRepository fhirDhisAssignmentRepository; |
| 89 | + |
| 90 | + @Mock |
| 91 | + private OrganizationUnitService organizationUnitService; |
| 92 | + |
| 93 | + @Mock |
| 94 | + private TrackedEntityMetadataService trackedEntityMetadataService; |
| 95 | + |
| 96 | + @Mock |
| 97 | + private TrackedEntityRuleRepository trackedEntityRuleRepository; |
| 98 | + |
| 99 | + @Mock |
| 100 | + private DhisToFhirTransformerContext context; |
| 101 | + |
| 102 | + @Mock |
| 103 | + private FhirClient fhirClient; |
| 104 | + |
| 105 | + @Mock |
| 106 | + private ScriptExecutionContext scriptExecutionContext; |
| 107 | + |
| 108 | + @InjectMocks |
| 109 | + private R4ProgramMetadataToFhirPlanDefinitionTransformer transformer; |
| 110 | + |
| 111 | + @Rule |
| 112 | + public MockitoRule rule = MockitoJUnit.rule(); |
| 113 | + |
| 114 | + @Test |
| 115 | + public void getFhirVersions() |
| 116 | + { |
| 117 | + Assert.assertEquals( FhirVersion.R4_ONLY, transformer.getFhirVersions() ); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void transformInternal() |
| 122 | + { |
| 123 | + Mockito.doReturn( Optional.of( new WritableTrackedEntityType( "a1234567890", "Test", Collections.emptyList() ) ) ) |
| 124 | + .when( trackedEntityMetadataService ).findTypeByReference( Mockito.eq( new Reference( "a1234567890", ReferenceType.ID ) ) ); |
| 125 | + Mockito.doReturn( Collections.singletonList( new RuleInfo<>( new ProgramMetadataRule(), Collections.emptyList() ) ) ) |
| 126 | + .when( trackedEntityRuleRepository ).findByTypeRefs( Mockito.eq( new HashSet<>( |
| 127 | + Arrays.asList( new Reference( "a1234567890", ReferenceType.ID ), new Reference( "Test", ReferenceType.NAME ) ) ) ) ); |
| 128 | + |
| 129 | + final WritableProgram program = new WritableProgram(); |
| 130 | + program.setName( "Test Program" ); |
| 131 | + program.setDescription( "Test Description" ); |
| 132 | + program.setWithoutRegistration( false ); |
| 133 | + program.setTrackedEntityTypeId( "a1234567890" ); |
| 134 | + program.setStages( new ArrayList<>() ); |
| 135 | + |
| 136 | + WritableProgramStage programStage = new WritableProgramStage(); |
| 137 | + programStage.setId( "b1234567890" ); |
| 138 | + programStage.setName( "Test Stage 1" ); |
| 139 | + programStage.setDescription( "Test Description 1" ); |
| 140 | + programStage.setRepeatable( false ); |
| 141 | + program.getStages().add( programStage ); |
| 142 | + |
| 143 | + programStage = new WritableProgramStage(); |
| 144 | + programStage.setId( "b1234567891" ); |
| 145 | + programStage.setName( "Test Stage 2" ); |
| 146 | + programStage.setDescription( "Test Description 2" ); |
| 147 | + programStage.setRepeatable( true ); |
| 148 | + program.getStages().add( programStage ); |
| 149 | + |
| 150 | + final PlanDefinition fhirPlanDefinition = new PlanDefinition(); |
| 151 | + |
| 152 | + transformer.transformInternal( fhirClient, context, new RuleInfo<>( new ProgramMetadataRule(), Collections.emptyList() ), new HashMap<>(), |
| 153 | + new WritableScriptedDhisMetadata( program, scriptExecutionContext ), fhirPlanDefinition ); |
| 154 | + |
| 155 | + Assert.assertEquals( "Test Program", fhirPlanDefinition.getName() ); |
| 156 | + Assert.assertEquals( "Test Description", fhirPlanDefinition.getDescription() ); |
| 157 | + Assert.assertEquals( 2, fhirPlanDefinition.getAction().size() ); |
| 158 | + |
| 159 | + Assert.assertEquals( "b1234567890", fhirPlanDefinition.getAction().get( 0 ).getId() ); |
| 160 | + Assert.assertEquals( "Test Stage 1", fhirPlanDefinition.getAction().get( 0 ).getTitle() ); |
| 161 | + Assert.assertEquals( "Test Description 1", fhirPlanDefinition.getAction().get( 0 ).getDescription() ); |
| 162 | + Assert.assertEquals( PlanDefinition.ActionCardinalityBehavior.SINGLE, fhirPlanDefinition.getAction().get( 0 ).getCardinalityBehavior() ); |
| 163 | + Assert.assertEquals( "Questionnaire/b1234567890", fhirPlanDefinition.getAction().get( 0 ).getDefinition().primitiveValue() ); |
| 164 | + |
| 165 | + Assert.assertEquals( "b1234567891", fhirPlanDefinition.getAction().get( 1 ).getId() ); |
| 166 | + Assert.assertEquals( "Test Stage 2", fhirPlanDefinition.getAction().get( 1 ).getTitle() ); |
| 167 | + Assert.assertEquals( "Test Description 2", fhirPlanDefinition.getAction().get( 1 ).getDescription() ); |
| 168 | + Assert.assertEquals( PlanDefinition.ActionCardinalityBehavior.MULTIPLE, fhirPlanDefinition.getAction().get( 1 ).getCardinalityBehavior() ); |
| 169 | + Assert.assertEquals( "Questionnaire/b1234567891", fhirPlanDefinition.getAction().get( 1 ).getDefinition().primitiveValue() ); |
| 170 | + } |
| 171 | +} |
0 commit comments