From 5930c6377e8526ed22c88a54c9e9d7b76746dc1c Mon Sep 17 00:00:00 2001 From: Volker Schmidt Date: Wed, 31 Jul 2019 17:54:47 +0200 Subject: [PATCH] Adds value type extension for Questionnaire items. --- ...ourceTypeExtension.StructureDefinition.xml | 2 +- ...ogramQuestionnaire.StructureDefinition.xml | 20 ++++++ ...ValueTypeExtension.StructureDefinition.xml | 57 ++++++++++++++++ ...etadataToFhirQuestionnaireTransformer.java | 13 ++++ ...ataToFhirQuestionnaireTransformerTest.java | 12 ++++ .../fhir/extension/BaseExtensionUtils.java | 65 ++++++++++++++++++ .../extension/ResourceTypeExtensionUtils.java | 13 +--- .../extension/ValueTypeExtensionUtils.java | 56 ++++++++++++++++ ...etadataToFhirQuestionnaireTransformer.java | 5 ++ .../extension/BaseExtensionUtilsTest.java | 64 ++++++++++++++++++ .../ResourceTypeExtensionUtilsTest.java | 47 +------------ .../fhir/extension/TestPlanDefinition.java | 66 +++++++++++++++++++ .../adapter/fhir/extension/TypeFactory.java | 48 ++++++++++++++ .../ValueTypeExtensionUtilsTest.java | 53 +++++++++++++++ 14 files changed, 463 insertions(+), 58 deletions(-) create mode 100644 docs/fhir/profiles/ValueTypeExtension.StructureDefinition.xml create mode 100644 fhir/src/main/java/org/dhis2/fhir/adapter/fhir/extension/BaseExtensionUtils.java create mode 100644 fhir/src/main/java/org/dhis2/fhir/adapter/fhir/extension/ValueTypeExtensionUtils.java create mode 100644 fhir/src/test/java/org/dhis2/fhir/adapter/fhir/extension/BaseExtensionUtilsTest.java create mode 100644 fhir/src/test/java/org/dhis2/fhir/adapter/fhir/extension/TestPlanDefinition.java create mode 100644 fhir/src/test/java/org/dhis2/fhir/adapter/fhir/extension/TypeFactory.java create mode 100644 fhir/src/test/java/org/dhis2/fhir/adapter/fhir/extension/ValueTypeExtensionUtilsTest.java diff --git a/docs/fhir/profiles/ResourceTypeExtension.StructureDefinition.xml b/docs/fhir/profiles/ResourceTypeExtension.StructureDefinition.xml index 76f90869..fc42e483 100644 --- a/docs/fhir/profiles/ResourceTypeExtension.StructureDefinition.xml +++ b/docs/fhir/profiles/ResourceTypeExtension.StructureDefinition.xml @@ -36,7 +36,7 @@ - + diff --git a/docs/fhir/profiles/TrackerProgramQuestionnaire.StructureDefinition.xml b/docs/fhir/profiles/TrackerProgramQuestionnaire.StructureDefinition.xml index eeef7fc0..f7fa6224 100644 --- a/docs/fhir/profiles/TrackerProgramQuestionnaire.StructureDefinition.xml +++ b/docs/fhir/profiles/TrackerProgramQuestionnaire.StructureDefinition.xml @@ -164,6 +164,26 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/docs/fhir/profiles/ValueTypeExtension.StructureDefinition.xml b/docs/fhir/profiles/ValueTypeExtension.StructureDefinition.xml new file mode 100644 index 00000000..5fae7e76 --- /dev/null +++ b/docs/fhir/profiles/ValueTypeExtension.StructureDefinition.xml @@ -0,0 +1,57 @@ + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/fhir-r4/src/main/java/org/dhis2/fhir/adapter/fhir/transform/dhis/impl/metadata/r4/R4ProgramStageMetadataToFhirQuestionnaireTransformer.java b/fhir-r4/src/main/java/org/dhis2/fhir/adapter/fhir/transform/dhis/impl/metadata/r4/R4ProgramStageMetadataToFhirQuestionnaireTransformer.java index 2575b8ee..8b113b97 100644 --- a/fhir-r4/src/main/java/org/dhis2/fhir/adapter/fhir/transform/dhis/impl/metadata/r4/R4ProgramStageMetadataToFhirQuestionnaireTransformer.java +++ b/fhir-r4/src/main/java/org/dhis2/fhir/adapter/fhir/transform/dhis/impl/metadata/r4/R4ProgramStageMetadataToFhirQuestionnaireTransformer.java @@ -28,9 +28,11 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +import ca.uhn.fhir.model.api.IElement; import org.dhis2.fhir.adapter.dhis.orgunit.OrganizationUnitService; import org.dhis2.fhir.adapter.dhis.tracker.program.ProgramStage; import org.dhis2.fhir.adapter.fhir.data.repository.FhirDhisAssignmentRepository; +import org.dhis2.fhir.adapter.fhir.extension.ValueTypeExtensionUtils; import org.dhis2.fhir.adapter.fhir.metadata.model.FhirClient; import org.dhis2.fhir.adapter.fhir.metadata.model.ProgramStageMetadataRule; import org.dhis2.fhir.adapter.fhir.metadata.model.RuleInfo; @@ -49,12 +51,14 @@ import org.hl7.fhir.r4.model.Questionnaire; import org.hl7.fhir.r4.model.Questionnaire.QuestionnaireItemComponent; import org.hl7.fhir.r4.model.Questionnaire.QuestionnaireItemType; +import org.hl7.fhir.r4.model.ResourceFactory; import org.springframework.stereotype.Component; import javax.annotation.Nonnull; import javax.annotation.Nullable; import java.util.Map; import java.util.Set; +import java.util.function.Function; import static org.hl7.fhir.r4.model.Questionnaire.QuestionnaireItemType.*; @@ -104,6 +108,8 @@ protected boolean transformInternal( @Nonnull FhirClient fhirClient, @Nonnull Dh itemComponent.setRequired( dataElement.isCompulsory() ); itemComponent.setType( type ); + ValueTypeExtensionUtils.setValue( itemComponent, dataElement.getElement().getValueType(), getTypeFactory() ); + if ( dataElement.getElement().isOptionSetValue() ) { dataElement.getElement().getOptionSet().getOptions().forEach( option -> itemComponent.addAnswerOption().setValue( @@ -155,4 +161,11 @@ protected QuestionnaireItemType convertValueType( @Nonnull ValueType valueType ) // unhandled data type return null; } + + @Nonnull + @Override + protected Function getTypeFactory() + { + return ResourceFactory::createType; + } } diff --git a/fhir-r4/src/test/java/org/dhis2/fhir/adapter/fhir/transform/dhis/impl/metadata/r4/R4ProgramStageMetadataToFhirQuestionnaireTransformerTest.java b/fhir-r4/src/test/java/org/dhis2/fhir/adapter/fhir/transform/dhis/impl/metadata/r4/R4ProgramStageMetadataToFhirQuestionnaireTransformerTest.java index 3cf0b25c..c27859e3 100644 --- a/fhir-r4/src/test/java/org/dhis2/fhir/adapter/fhir/transform/dhis/impl/metadata/r4/R4ProgramStageMetadataToFhirQuestionnaireTransformerTest.java +++ b/fhir-r4/src/test/java/org/dhis2/fhir/adapter/fhir/transform/dhis/impl/metadata/r4/R4ProgramStageMetadataToFhirQuestionnaireTransformerTest.java @@ -35,6 +35,7 @@ import org.dhis2.fhir.adapter.dhis.tracker.program.WritableProgramStage; import org.dhis2.fhir.adapter.dhis.tracker.program.WritableProgramStageDataElement; import org.dhis2.fhir.adapter.fhir.data.repository.FhirDhisAssignmentRepository; +import org.dhis2.fhir.adapter.fhir.extension.ValueTypeExtensionUtils; import org.dhis2.fhir.adapter.fhir.metadata.model.FhirClient; import org.dhis2.fhir.adapter.fhir.metadata.model.ProgramStageMetadataRule; import org.dhis2.fhir.adapter.fhir.metadata.model.RuleInfo; @@ -49,6 +50,7 @@ import org.dhis2.fhir.adapter.model.ValueType; import org.hl7.fhir.r4.model.Coding; import org.hl7.fhir.r4.model.Questionnaire; +import org.hl7.fhir.r4.model.StringType; import org.junit.Assert; import org.junit.Rule; import org.junit.Test; @@ -157,6 +159,11 @@ public void transformInternal() Assert.assertEquals( 0, fhirQuestionnaire.getItem().get( 0 ).getAnswerOption().size() ); Assert.assertFalse( fhirQuestionnaire.getItem().get( 0 ).getRequired() ); + Assert.assertEquals( 1, fhirQuestionnaire.getItem().get( 0 ).getExtension().size() ); + Assert.assertEquals( ValueTypeExtensionUtils.URL, fhirQuestionnaire.getItem().get( 0 ).getExtension().get( 0 ).getUrl() ); + Assert.assertTrue( fhirQuestionnaire.getItem().get( 0 ).getExtension().get( 0 ).getValue() instanceof StringType ); + Assert.assertEquals( "TEXT", ( (StringType) fhirQuestionnaire.getItem().get( 0 ).getExtension().get( 0 ).getValue() ).getValue() ); + Assert.assertEquals( "d1123456789", fhirQuestionnaire.getItem().get( 1 ).getLinkId() ); Assert.assertEquals( "Value 2", fhirQuestionnaire.getItem().get( 1 ).getText() ); Assert.assertEquals( 2, fhirQuestionnaire.getItem().get( 1 ).getAnswerOption().size() ); @@ -166,5 +173,10 @@ public void transformInternal() Assert.assertEquals( "7", ( (Coding) fhirQuestionnaire.getItem().get( 1 ).getAnswerOption().get( 1 ).getValue() ).getCode() ); Assert.assertEquals( "Test Value 2", ( (Coding) fhirQuestionnaire.getItem().get( 1 ).getAnswerOption().get( 1 ).getValue() ).getDisplay() ); Assert.assertTrue( fhirQuestionnaire.getItem().get( 1 ).getRequired() ); + + Assert.assertEquals( 1, fhirQuestionnaire.getItem().get( 1 ).getExtension().size() ); + Assert.assertEquals( ValueTypeExtensionUtils.URL, fhirQuestionnaire.getItem().get( 1 ).getExtension().get( 0 ).getUrl() ); + Assert.assertTrue( fhirQuestionnaire.getItem().get( 1 ).getExtension().get( 0 ).getValue() instanceof StringType ); + Assert.assertEquals( "INTEGER", ( (StringType) fhirQuestionnaire.getItem().get( 1 ).getExtension().get( 0 ).getValue() ).getValue() ); } } diff --git a/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/extension/BaseExtensionUtils.java b/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/extension/BaseExtensionUtils.java new file mode 100644 index 00000000..6811ec79 --- /dev/null +++ b/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/extension/BaseExtensionUtils.java @@ -0,0 +1,65 @@ +package org.dhis2.fhir.adapter.fhir.extension; + +/* + * 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.api.IElement; +import org.hl7.fhir.instance.model.api.IBaseExtension; +import org.hl7.fhir.instance.model.api.IBaseHasExtensions; +import org.hl7.fhir.instance.model.api.IPrimitiveType; + +import javax.annotation.Nonnull; +import javax.annotation.Nullable; +import java.util.function.Function; + +/** + * Utility class to process FHIR resource extensions. + * + * @author volsch + */ +abstract class BaseExtensionUtils +{ + @SuppressWarnings( "unchecked" ) + protected static void setStringValue( @Nonnull String url, @Nonnull IBaseHasExtensions resource, @Nullable String value, @Nonnull Function typeFactory ) + { + resource.getExtension().removeIf( e -> url.equals( e.getUrl() ) ); + + if ( value != null ) + { + final IBaseExtension extension = resource.addExtension(); + + extension.setUrl( url ); + extension.setValue( ( (IPrimitiveType) typeFactory.apply( "string" ) ).setValue( value ) ); + } + } + + private BaseExtensionUtils() + { + super(); + } +} diff --git a/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/extension/ResourceTypeExtensionUtils.java b/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/extension/ResourceTypeExtensionUtils.java index 595cc28f..6a316e7d 100644 --- a/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/extension/ResourceTypeExtensionUtils.java +++ b/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/extension/ResourceTypeExtensionUtils.java @@ -30,9 +30,7 @@ import ca.uhn.fhir.model.api.IElement; import org.dhis2.fhir.adapter.fhir.metadata.model.FhirResourceType; -import org.hl7.fhir.instance.model.api.IBaseExtension; import org.hl7.fhir.instance.model.api.IBaseHasExtensions; -import org.hl7.fhir.instance.model.api.IPrimitiveType; import javax.annotation.Nonnull; import javax.annotation.Nullable; @@ -47,18 +45,9 @@ public abstract class ResourceTypeExtensionUtils { public static final String URL = "http://www.dhis2.org/dhis2-fhir-adapter/fhir/extensions/resource-type"; - @SuppressWarnings( "unchecked" ) public static void setValue( @Nonnull IBaseHasExtensions resource, @Nullable FhirResourceType fhirResourceType, @Nonnull Function typeFactory ) { - resource.getExtension().removeIf( e -> URL.equals( e.getUrl() ) ); - - if ( fhirResourceType != null ) - { - final IBaseExtension extension = resource.addExtension(); - - extension.setUrl( URL ); - extension.setValue( ( (IPrimitiveType) typeFactory.apply( "string" ) ).setValue( fhirResourceType.getResourceTypeName() ) ); - } + BaseExtensionUtils.setStringValue( URL, resource, fhirResourceType == null ? null : fhirResourceType.getResourceTypeName(), typeFactory ); } private ResourceTypeExtensionUtils() diff --git a/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/extension/ValueTypeExtensionUtils.java b/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/extension/ValueTypeExtensionUtils.java new file mode 100644 index 00000000..127b9c7c --- /dev/null +++ b/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/extension/ValueTypeExtensionUtils.java @@ -0,0 +1,56 @@ +package org.dhis2.fhir.adapter.fhir.extension; + +/* + * 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.api.IElement; +import org.dhis2.fhir.adapter.model.ValueType; +import org.hl7.fhir.instance.model.api.IBaseHasExtensions; + +import javax.annotation.Nonnull; +import java.util.function.Function; + +/** + * Utility class to process FHIR value type extension. + * + * @author volsch + */ +public abstract class ValueTypeExtensionUtils +{ + public static final String URL = "http://www.dhis2.org/dhis2-fhir-adapter/fhir/extensions/value-type"; + + public static void setValue( @Nonnull IBaseHasExtensions resource, @Nonnull ValueType valueType, @Nonnull Function typeFactory ) + { + BaseExtensionUtils.setStringValue( URL, resource, valueType.name(), typeFactory ); + } + + private ValueTypeExtensionUtils() + { + super(); + } +} diff --git a/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/transform/dhis/impl/metadata/program/AbstractProgramStageMetadataToFhirQuestionnaireTransformer.java b/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/transform/dhis/impl/metadata/program/AbstractProgramStageMetadataToFhirQuestionnaireTransformer.java index 0bc8ba45..2252165a 100644 --- a/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/transform/dhis/impl/metadata/program/AbstractProgramStageMetadataToFhirQuestionnaireTransformer.java +++ b/fhir/src/main/java/org/dhis2/fhir/adapter/fhir/transform/dhis/impl/metadata/program/AbstractProgramStageMetadataToFhirQuestionnaireTransformer.java @@ -28,6 +28,7 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +import ca.uhn.fhir.model.api.IElement; import org.dhis2.fhir.adapter.dhis.model.DhisResourceType; import org.dhis2.fhir.adapter.dhis.orgunit.OrganizationUnitService; import org.dhis2.fhir.adapter.fhir.data.repository.FhirDhisAssignmentRepository; @@ -45,6 +46,7 @@ import org.slf4j.LoggerFactory; import javax.annotation.Nonnull; +import java.util.function.Function; /** * Implementation of {@link DhisToFhirTransformer} for transforming DHIS2 @@ -90,4 +92,7 @@ public Class getRuleClass() { return ProgramStageMetadataRule.class; } + + @Nonnull + protected abstract Function getTypeFactory(); } diff --git a/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/extension/BaseExtensionUtilsTest.java b/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/extension/BaseExtensionUtilsTest.java new file mode 100644 index 00000000..b6c8758a --- /dev/null +++ b/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/extension/BaseExtensionUtilsTest.java @@ -0,0 +1,64 @@ +package org.dhis2.fhir.adapter.fhir.extension; + +/* + * 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.junit.Assert; +import org.junit.Test; + +/** + * Unit tests for {@link BaseExtensionUtils}. + * + * @author volsch + */ +public class BaseExtensionUtilsTest +{ + @Test + public void resetValue() + { + TestPlanDefinition planDefinition = new TestPlanDefinition(); + + BaseExtensionUtils.setStringValue( "testUrl", planDefinition, "testValue", TypeFactory::createType ); + BaseExtensionUtils.setStringValue( "testUrl", planDefinition, null, TypeFactory::createType ); + + Assert.assertTrue( planDefinition.getExtension().isEmpty() ); + } + + @Test + public void setValue() + { + TestPlanDefinition planDefinition = new TestPlanDefinition(); + + BaseExtensionUtils.setStringValue( "testUrl", planDefinition, "testValue", TypeFactory::createType ); + + Assert.assertEquals( 1, planDefinition.getExtension().size() ); + Assert.assertEquals( "testUrl", planDefinition.getExtension().get( 0 ).getUrl() ); + Assert.assertEquals( "testValue", planDefinition.getExtension().get( 0 ).getValue().toString() ); + } + +} \ No newline at end of file diff --git a/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/extension/ResourceTypeExtensionUtilsTest.java b/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/extension/ResourceTypeExtensionUtilsTest.java index d245ec1d..b45787a9 100644 --- a/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/extension/ResourceTypeExtensionUtilsTest.java +++ b/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/extension/ResourceTypeExtensionUtilsTest.java @@ -28,18 +28,10 @@ * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -import ca.uhn.fhir.model.api.ExtensionDt; -import ca.uhn.fhir.model.api.IElement; -import ca.uhn.fhir.model.primitive.StringDt; import org.dhis2.fhir.adapter.fhir.metadata.model.FhirResourceType; -import org.hl7.fhir.instance.model.api.IBaseExtension; -import org.hl7.fhir.instance.model.api.IBaseHasExtensions; import org.junit.Assert; import org.junit.Test; -import java.util.ArrayList; -import java.util.List; - /** * Unit tests for {@link ResourceTypeExtensionUtils}. * @@ -50,7 +42,7 @@ public class ResourceTypeExtensionUtilsTest @Test public void resetValue() { - PlanDefinition planDefinition = new PlanDefinition(); + TestPlanDefinition planDefinition = new TestPlanDefinition(); ResourceTypeExtensionUtils.setValue( planDefinition, FhirResourceType.PATIENT, TypeFactory::createType ); ResourceTypeExtensionUtils.setValue( planDefinition, null, TypeFactory::createType ); @@ -61,7 +53,7 @@ public void resetValue() @Test public void setValue() { - PlanDefinition planDefinition = new PlanDefinition(); + TestPlanDefinition planDefinition = new TestPlanDefinition(); ResourceTypeExtensionUtils.setValue( planDefinition, FhirResourceType.PATIENT, TypeFactory::createType ); @@ -69,39 +61,4 @@ public void setValue() Assert.assertEquals( ResourceTypeExtensionUtils.URL, planDefinition.getExtension().get( 0 ).getUrl() ); Assert.assertEquals( "Patient", planDefinition.getExtension().get( 0 ).getValue().toString() ); } - - public static class PlanDefinition implements IBaseHasExtensions - { - private final List> extensions = new ArrayList<>(); - - @Override - public IBaseExtension addExtension() - { - extensions.add( new ExtensionDt() ); - - return extensions.get( extensions.size() - 1 ); - } - - @Override - public List> getExtension() - { - return extensions; - } - - @Override - public boolean hasExtension() - { - return !extensions.isEmpty(); - } - } - - public static class TypeFactory - { - public static IElement createType( String name ) - { - Assert.assertEquals( "string", name ); - - return new StringDt(); - } - } } \ No newline at end of file diff --git a/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/extension/TestPlanDefinition.java b/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/extension/TestPlanDefinition.java new file mode 100644 index 00000000..b2f01a17 --- /dev/null +++ b/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/extension/TestPlanDefinition.java @@ -0,0 +1,66 @@ +package org.dhis2.fhir.adapter.fhir.extension; + +/* + * 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.api.ExtensionDt; +import org.hl7.fhir.instance.model.api.IBaseExtension; +import org.hl7.fhir.instance.model.api.IBaseHasExtensions; + +import java.util.ArrayList; +import java.util.List; + +/** + * Test plan definition FHIR resource. + * + * @author volsch + */ +class TestPlanDefinition implements IBaseHasExtensions +{ + private final List> extensions = new ArrayList<>(); + + @Override + public IBaseExtension addExtension() + { + extensions.add( new ExtensionDt() ); + + return extensions.get( extensions.size() - 1 ); + } + + @Override + public List> getExtension() + { + return extensions; + } + + @Override + public boolean hasExtension() + { + return !extensions.isEmpty(); + } +} diff --git a/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/extension/TypeFactory.java b/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/extension/TypeFactory.java new file mode 100644 index 00000000..32329ee4 --- /dev/null +++ b/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/extension/TypeFactory.java @@ -0,0 +1,48 @@ +package org.dhis2.fhir.adapter.fhir.extension; + +/* + * 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.api.IElement; +import ca.uhn.fhir.model.primitive.StringDt; +import org.junit.Assert; + +/** + * Test type factory. + * + * @author volsch + */ +class TypeFactory +{ + public static IElement createType( String name ) + { + Assert.assertEquals( "string", name ); + + return new StringDt(); + } +} diff --git a/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/extension/ValueTypeExtensionUtilsTest.java b/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/extension/ValueTypeExtensionUtilsTest.java new file mode 100644 index 00000000..6593491a --- /dev/null +++ b/fhir/src/test/java/org/dhis2/fhir/adapter/fhir/extension/ValueTypeExtensionUtilsTest.java @@ -0,0 +1,53 @@ +package org.dhis2.fhir.adapter.fhir.extension; + +/* + * 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.model.ValueType; +import org.junit.Assert; +import org.junit.Test; + +/** + * Unit tests for {@link ValueTypeExtensionUtils}. + * + * @author volsch + */ +public class ValueTypeExtensionUtilsTest +{ + @Test + public void setValue() + { + TestPlanDefinition planDefinition = new TestPlanDefinition(); + + ValueTypeExtensionUtils.setValue( planDefinition, ValueType.INTEGER_POSITIVE, TypeFactory::createType ); + + Assert.assertEquals( 1, planDefinition.getExtension().size() ); + Assert.assertEquals( ValueTypeExtensionUtils.URL, planDefinition.getExtension().get( 0 ).getUrl() ); + Assert.assertEquals( "INTEGER_POSITIVE", planDefinition.getExtension().get( 0 ).getValue().toString() ); + } +} \ No newline at end of file