From 97c11a9be0f2479febe8fe7d6caba989ce40be92 Mon Sep 17 00:00:00 2001 From: dotasek Date: Sun, 23 Jan 2022 12:06:44 -0500 Subject: [PATCH] Testing Part 3 --- .../hl7/fhir/r5/context/TerminologyCache.java | 4 +- .../r5/context/TerminologyCacheTests.java | 140 ++++++++++++++++-- .../src/test/resources/context/expansion.json | 3 + .../context/expansionHierarchical.json | 3 + 4 files changed, 137 insertions(+), 13 deletions(-) create mode 100644 org.hl7.fhir.r5/src/test/resources/context/expansion.json create mode 100644 org.hl7.fhir.r5/src/test/resources/context/expansionHierarchical.json diff --git a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/TerminologyCache.java b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/TerminologyCache.java index da0ea7227..af246c22d 100644 --- a/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/TerminologyCache.java +++ b/org.hl7.fhir.r5/src/main/java/org/hl7/fhir/r5/context/TerminologyCache.java @@ -402,7 +402,7 @@ public class TerminologyCache { } - private void save(K resource, String title) { + private void save(K resource, String title) { if (folder == null) return; @@ -591,7 +591,7 @@ public class TerminologyCache { return s; } - private String hashJson(String s) { + protected String hashJson(String s) { s = StringUtils.remove(s, ' '); s = StringUtils.remove(s, '\n'); s = StringUtils.remove(s, '\r'); diff --git a/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/context/TerminologyCacheTests.java b/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/context/TerminologyCacheTests.java index bac88508f..342ff96b8 100644 --- a/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/context/TerminologyCacheTests.java +++ b/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/context/TerminologyCacheTests.java @@ -2,14 +2,16 @@ package org.hl7.fhir.r5.context; import com.google.gson.JsonElement; import com.google.gson.JsonParser; -import org.checkerframework.checker.units.qual.C; +import org.hl7.fhir.r5.formats.IParser; import org.hl7.fhir.r5.model.CodeableConcept; import org.hl7.fhir.r5.model.Coding; import org.hl7.fhir.r5.model.ValueSet; import org.hl7.fhir.r5.terminologies.ValueSetExpander; import org.hl7.fhir.utilities.validation.ValidationMessage; -import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.Arguments; +import org.junit.jupiter.params.provider.MethodSource; import java.io.File; import java.io.IOException; @@ -17,9 +19,15 @@ import java.net.URISyntaxException; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.Arrays; +import java.util.Date; +import java.util.List; +import java.util.Random; +import java.util.stream.Collectors; +import java.util.stream.IntStream; +import java.util.stream.Stream; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; import static org.mockito.Mockito.mock; public class TerminologyCacheTests { @@ -32,6 +40,12 @@ public class TerminologyCacheTests { return jsonParser.parse(stringValue); }; + private TerminologyCache createTerminologyCache() throws IOException { + Object lock = new Object(); + TerminologyCache terminologyCache = new TerminologyCache(lock, null); + return terminologyCache; + } + public Path createTempCacheDirectory() throws IOException { Path tmp = Files.createTempDirectory("integrationTestCache"); tmp.toFile().deleteOnExit(); @@ -72,7 +86,7 @@ public class TerminologyCacheTests { assertValidationResultEquals(validationResultA, terminologyCacheA.getValidation(codingTokenA)); assertExpansionOutcomeEquals(expansionOutcomeA,terminologyCacheA.getExpansion(expansionTokenA)); } - + //Create another cache using the same directory, and check that it gives the same results. { TerminologyCache terminologyCacheB = new TerminologyCache(lock, tempCacheDirectory.toString()); @@ -95,9 +109,8 @@ public class TerminologyCacheTests { @Test public void testCodingCacheTokenGeneration() throws IOException, URISyntaxException { - Object lock = new Object(); - TerminologyCache terminologyCache = new TerminologyCache(lock, null); + TerminologyCache terminologyCache = createTerminologyCache(); ValueSet valueSet = new ValueSet(); Coding coding = new Coding(); @@ -109,13 +122,13 @@ public class TerminologyCacheTests { JsonElement expected = getJsonFromFile("codingEmptyValueSet.json"); assertEquals(expected, actual); - + assertEquals(terminologyCache.hashJson(expected.toString()), terminologyCache.hashJson(actual.toString())); } @Test public void testCodableConceptCacheTokenGeneration() throws IOException, URISyntaxException { - Object lock = new Object(); - TerminologyCache terminologyCache = new TerminologyCache(lock, null); + + TerminologyCache terminologyCache = createTerminologyCache(); CodeableConcept concept = new CodeableConcept(); concept.addCoding(new Coding().setCode("dummyCode")); ValueSet valueSet = new ValueSet(); @@ -126,6 +139,112 @@ public class TerminologyCacheTests { JsonElement expected = getJsonFromFile("codableConceptEmptyValueSet.json"); assertEquals(expected, actual); + assertEquals(terminologyCache.hashJson(expected.toString()), terminologyCache.hashJson(actual.toString())); + } + + @Test + public void testExpansionToken() throws IOException, URISyntaxException { + TerminologyCache terminologyCache = createTerminologyCache(); + ValueSet valueSet = new ValueSet(); + + TerminologyCache.CacheToken expansionToken = terminologyCache.generateExpandToken(valueSet, false); + TerminologyCache.CacheToken expansionTokenHierarchical = terminologyCache.generateExpandToken(valueSet, true); + + JsonElement actualExpansion = jsonParser.parse(expansionToken.getRequest()); + JsonElement expectedExpansion = getJsonFromFile("expansion.json"); + + assertEquals(expectedExpansion, actualExpansion); + + JsonElement actualExpansionHierarchical = jsonParser.parse(expansionTokenHierarchical.getRequest()); + JsonElement expectedExpansionHierarchical = getJsonFromFile("expansionHierarchical.json"); + + assertEquals(expectedExpansionHierarchical, actualExpansionHierarchical); + assertEquals(terminologyCache.hashJson(expectedExpansion.toString()), + terminologyCache.hashJson(actualExpansion.toString())); + assertEquals(terminologyCache.hashJson(expectedExpansionHierarchical.toString()), + terminologyCache.hashJson(actualExpansionHierarchical.toString())); + + } + + @Test + public void testGetVSEssence() throws IOException { + ValueSet.ValueSetExpansionParameterComponent vsepc = new ValueSet.ValueSetExpansionParameterComponent().setName("dummyValueSetExpansionParameterComponent"); + + ValueSet vs = new ValueSet(); + vs.getExpansion().setParameter(Arrays.asList(vsepc)); + vs.getExpansion().setContains(Arrays.asList(new ValueSet.ValueSetExpansionContainsComponent().setCode("dummyVSExpansionContainsComponent"))); + vs.getExpansion().setIdentifier("dummyIdentifier"); + vs.getExpansion().setTimestamp(new Date()); + + assertTrue(vs.getExpansion().hasIdentifier()); + + TerminologyCache cache = createTerminologyCache(); + ValueSet vse = cache.getVSEssense(vs); + + assertEquals(vs.getExpansion().getParameter(), vse.getExpansion().getParameter()); + assertEquals(vs.getExpansion().getContains(), vse.getExpansion().getContains()); + + assertFalse(vse.getExpansion().hasIdentifier()); + assertFalse(vse.getExpansion().hasTimestamp()); + } + + private List createContainsArray(int size) { + return IntStream.range(0, size).boxed() + .map(value -> new ValueSet.ValueSetExpansionContainsComponent().setCode("dummyVSExpansionContainsComponent" + + value)).collect(Collectors.toList()); + } + + private static Stream under1000IntParams() { + return getIntParams(0, 1000); + } + + private static Stream over1000IntParams() { + return getIntParams(1000, 1100); + } + + private static Stream getIntParams(int min, int max) { + return new Random().ints(5, min, max).boxed().map( value -> + Arguments.of(value) + ); + } + + @ParameterizedTest + @MethodSource("under1000IntParams") + public void testExtractedUnder1000(int max) throws IOException { + TerminologyCache cache = createTerminologyCache(); + ValueSet vs = new ValueSet(); + + List list = createContainsArray(max); + + vs.setUrl("http://dummy.org"); + vs.getExpansion().setContains(list); + + org.hl7.fhir.r5.formats.JsonParser json = new org.hl7.fhir.r5.formats.JsonParser(); + json.setOutputStyle(IParser.OutputStyle.PRETTY); + String extracted = cache.extracted(json, vs); + + JsonElement element = jsonParser.parse(extracted); + assertEquals(max, element.getAsJsonObject().getAsJsonObject("expansion").getAsJsonArray("contains").size()); + + } + + @ParameterizedTest + @MethodSource("over1000IntParams") + public void testExtractedOver1000(int max) throws IOException { + + TerminologyCache cache = createTerminologyCache(); + ValueSet vs = new ValueSet(); + + List list = createContainsArray(max); + + vs.setUrl("http://dummy.org"); + vs.getExpansion().setContains(list); + + org.hl7.fhir.r5.formats.JsonParser json = new org.hl7.fhir.r5.formats.JsonParser(); + json.setOutputStyle(IParser.OutputStyle.PRETTY); + String extracted = cache.extracted(json, vs); + + assertEquals("http://dummy.org", extracted); } @Test @@ -137,6 +256,5 @@ public class TerminologyCacheTests { assertTrue(cache.hasTerminologyCapabilities()); assertTrue(cache.hasCapabilityStatement()); - } } diff --git a/org.hl7.fhir.r5/src/test/resources/context/expansion.json b/org.hl7.fhir.r5/src/test/resources/context/expansion.json new file mode 100644 index 000000000..1d5951ee4 --- /dev/null +++ b/org.hl7.fhir.r5/src/test/resources/context/expansion.json @@ -0,0 +1,3 @@ +{"hierarchical" : false, "valueSet" :{ + "resourceType" : "ValueSet" +}} diff --git a/org.hl7.fhir.r5/src/test/resources/context/expansionHierarchical.json b/org.hl7.fhir.r5/src/test/resources/context/expansionHierarchical.json new file mode 100644 index 000000000..b08c86f36 --- /dev/null +++ b/org.hl7.fhir.r5/src/test/resources/context/expansionHierarchical.json @@ -0,0 +1,3 @@ +{"hierarchical" : true, "valueSet" :{ + "resourceType" : "ValueSet" +}}