From 36cc6defc271d9b7b41c06fa65362d50b8b5513f Mon Sep 17 00:00:00 2001 From: dotasek Date: Thu, 6 Jan 2022 10:10:45 -0500 Subject: [PATCH] Refactor with a mock + new test to catch exception --- .../terminologies/ConceptMapEngineTest.java | 58 ++++++++++++++----- 1 file changed, 45 insertions(+), 13 deletions(-) diff --git a/org.hl7.fhir.r4/src/test/java/org/hl7/fhir/r4/terminologies/ConceptMapEngineTest.java b/org.hl7.fhir.r4/src/test/java/org/hl7/fhir/r4/terminologies/ConceptMapEngineTest.java index d9e065500..6e2992bc2 100644 --- a/org.hl7.fhir.r4/src/test/java/org/hl7/fhir/r4/terminologies/ConceptMapEngineTest.java +++ b/org.hl7.fhir.r4/src/test/java/org/hl7/fhir/r4/terminologies/ConceptMapEngineTest.java @@ -1,17 +1,23 @@ package org.hl7.fhir.r4.terminologies; +import org.hl7.fhir.exceptions.FHIRException; import org.hl7.fhir.r4.context.SimpleWorkerContext; import org.hl7.fhir.r4.model.Coding; import org.hl7.fhir.r4.model.ConceptMap; import org.hl7.fhir.r4.model.Enumerations; -import org.jetbrains.annotations.NotNull; +import javax.annotation.Nonnull; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; import java.io.IOException; +import java.util.Arrays; +import java.util.Collection; import java.util.Collections; import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; class ConceptMapEngineTest { @@ -20,9 +26,12 @@ class ConceptMapEngineTest { public static final String TARGET_CODE_STRING = "vital-signs"; @Test - @DisplayName("Coding is converted according to ConceptMap") - void translate() throws IOException { - ConceptMapEngine conceptMapEngine = getConceptMapEngine(); + @DisplayName("Coding is translated according to ConceptMap") + void codingTranslate() throws IOException { + + final ConceptMap.SourceElementComponent sourceElementComponent = getSourceElementComponent(); + + final ConceptMapEngine conceptMapEngine = getConceptMapEngine(Arrays.asList(sourceElementComponent)); Coding coding = new Coding(null, SOURCE_CODE_STRING, "Body Weight"); Coding actual = conceptMapEngine.translate(coding, CONCEPT_MAP_URL); @@ -30,18 +39,34 @@ class ConceptMapEngineTest { assertEquals(TARGET_CODE_STRING, actual.getCode()); } - @NotNull - private ConceptMapEngine getConceptMapEngine() throws IOException { - ConceptMap conceptMap = getConceptMap(); + @Test + @DisplayName("Coding fails to translate due to multiple candidate matches in ConceptMap") + void codingTranslateFailsForMultipleCandidateMatches() throws IOException { - SimpleWorkerContext simpleWorkerContext = new SimpleWorkerContext(); - simpleWorkerContext.cacheResource(conceptMap); + final ConceptMap.SourceElementComponent sourceElementComponent = getSourceElementComponent(); + + final ConceptMapEngine conceptMapEngine = getConceptMapEngine(Arrays.asList(sourceElementComponent, + sourceElementComponent + )); + Coding coding = new Coding(null, SOURCE_CODE_STRING, "Body Weight"); + + assertThrows(FHIRException.class, () -> { + conceptMapEngine.translate(coding, CONCEPT_MAP_URL); + }); + } + + @Nonnull + private ConceptMapEngine getConceptMapEngine(Collection elements) throws IOException { + ConceptMap conceptMap = getConceptMap(elements); + + SimpleWorkerContext simpleWorkerContext = mock(SimpleWorkerContext.class); + when(simpleWorkerContext.fetchResource(ConceptMap.class, CONCEPT_MAP_URL)).thenReturn(conceptMap); return new ConceptMapEngine(simpleWorkerContext); } - @NotNull - private ConceptMap getConceptMap() { + @Nonnull + private ConceptMap.SourceElementComponent getSourceElementComponent() { ConceptMap.TargetElementComponent targetElementComponent = new ConceptMap.TargetElementComponent(); targetElementComponent.setCode(TARGET_CODE_STRING); targetElementComponent.setEquivalence(Enumerations.ConceptMapEquivalence.EQUIVALENT); @@ -50,9 +75,16 @@ class ConceptMapEngineTest { sourceElementComponent.setCode(SOURCE_CODE_STRING); sourceElementComponent.setTarget(Collections.singletonList(targetElementComponent)); - ConceptMap.ConceptMapGroupComponent conceptMapGroupComponent = new ConceptMap.ConceptMapGroupComponent(); - conceptMapGroupComponent.addElement(sourceElementComponent); + return sourceElementComponent; + } + @Nonnull + private ConceptMap getConceptMap(Collection elements) { + + ConceptMap.ConceptMapGroupComponent conceptMapGroupComponent = new ConceptMap.ConceptMapGroupComponent(); + for (ConceptMap.SourceElementComponent element : elements) { + conceptMapGroupComponent.addElement(element); + } return new ConceptMap() .addGroup(conceptMapGroupComponent) .setUrl(CONCEPT_MAP_URL);