Merge pull request #699 from paul-robson-validic/bug_check_loop_variable

Bug: Loop condition is always true
This commit is contained in:
dotasek 2022-01-06 11:24:14 -05:00 committed by GitHub
commit 83679d758c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 122 additions and 30 deletions

View File

@ -1,33 +1,33 @@
package org.hl7.fhir.r4.terminologies;
/*
Copyright (c) 2011+, HL7, Inc.
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 HL7 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 HOLDER 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.
*/
/*
Copyright (c) 2011+, HL7, Inc.
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 HL7 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 HOLDER 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.
*/
@ -64,7 +64,7 @@ public class ConceptMapEngine {
for (ConceptMapGroupComponent g : cm.getGroup()) {
for (SourceElementComponent e : g.getElement()) {
if (code.equals(e.getCode())) {
if (e != null)
if (ct != null)
throw new FHIRException("Unable to process translate "+code+" because multiple candidate matches were found in concept map "+cm.getUrl());
ct = e;
cg = g;
@ -94,4 +94,4 @@ public class ConceptMapEngine {
throw new Error("Not done yet");
}
}
}

View File

@ -0,0 +1,92 @@
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 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 {
private static final String CONCEPT_MAP_URL = "https://test-fhir.com/ConceptMap/fake";
public static final String SOURCE_CODE_STRING = "body-weight";
public static final String TARGET_CODE_STRING = "vital-signs";
@Test
@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);
assertEquals(TARGET_CODE_STRING, actual.getCode());
}
@Test
@DisplayName("Coding fails to translate due to multiple candidate matches in ConceptMap")
void codingTranslateFailsForMultipleCandidateMatches() throws IOException {
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<ConceptMap.SourceElementComponent> 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);
}
@Nonnull
private ConceptMap.SourceElementComponent getSourceElementComponent() {
ConceptMap.TargetElementComponent targetElementComponent = new ConceptMap.TargetElementComponent();
targetElementComponent.setCode(TARGET_CODE_STRING);
targetElementComponent.setEquivalence(Enumerations.ConceptMapEquivalence.EQUIVALENT);
ConceptMap.SourceElementComponent sourceElementComponent = new ConceptMap.SourceElementComponent();
sourceElementComponent.setCode(SOURCE_CODE_STRING);
sourceElementComponent.setTarget(Collections.singletonList(targetElementComponent));
return sourceElementComponent;
}
@Nonnull
private ConceptMap getConceptMap(Collection<ConceptMap.SourceElementComponent> elements) {
ConceptMap.ConceptMapGroupComponent conceptMapGroupComponent = new ConceptMap.ConceptMapGroupComponent();
for (ConceptMap.SourceElementComponent element : elements) {
conceptMapGroupComponent.addElement(element);
}
return new ConceptMap()
.addGroup(conceptMapGroupComponent)
.setUrl(CONCEPT_MAP_URL);
}
}