From 9ec63bc1b9f4f4f056a834d963d40c5e26edcd4a Mon Sep 17 00:00:00 2001 From: dotasek Date: Tue, 25 Jan 2022 16:36:13 -0500 Subject: [PATCH] Test capability caching. Just when I think I'm done... --- .../r5/context/SimpleWorkerContextTests.java | 59 ++++++++++++++++++- .../r5/context/TerminologyCacheTests.java | 26 +++++++- 2 files changed, 80 insertions(+), 5 deletions(-) diff --git a/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/context/SimpleWorkerContextTests.java b/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/context/SimpleWorkerContextTests.java index 803d00990..a032328fc 100644 --- a/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/context/SimpleWorkerContextTests.java +++ b/org.hl7.fhir.r5/src/test/java/org/hl7/fhir/r5/context/SimpleWorkerContextTests.java @@ -19,13 +19,13 @@ import org.mockito.Spy; import org.mockito.junit.jupiter.MockitoExtension; import java.io.IOException; +import java.util.Arrays; import java.util.HashMap; import java.util.Map; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.mockito.ArgumentMatchers.*; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.times; +import static org.mockito.Mockito.*; @ExtendWith(MockitoExtension.class) public class SimpleWorkerContextTests { @@ -64,6 +64,15 @@ public class SimpleWorkerContextTests { @Mock Parameters expParameters; + public static final TerminologyCapabilities terminologyCapabilities = new TerminologyCapabilities(); + static { terminologyCapabilities.getExpansion().setParameter(Arrays.asList());} + + public static final CapabilityStatement.CapabilityStatementSoftwareComponent software = new CapabilityStatement.CapabilityStatementSoftwareComponent(); + static { software.setVersion("dummyVersion"); } + + public static final CapabilityStatement capabilitiesStatement = new CapabilityStatement(); + static { capabilitiesStatement.setSoftware(software);} + @BeforeEach public void beforeEach() { context.txCache = terminologyCache; @@ -357,4 +366,50 @@ public class SimpleWorkerContextTests { Mockito.verify(terminologyCache).getExpansion(cacheToken); Mockito.verify(terminologyCache).cacheExpansion(cacheToken, actualExpansionResult, true); } + + + @Test + public void testInitializationWithCache() { + + Mockito.doReturn(true).when(terminologyCache).hasTerminologyCapabilities(); + Mockito.doReturn(true).when(terminologyCache).hasCapabilityStatement(); + + Mockito.doReturn(terminologyCapabilities).when(terminologyCache).getTerminologyCapabilities(); + Mockito.doReturn(capabilitiesStatement).when(terminologyCache).getCapabilityStatement(); + + String actual = context.connectToTSServer(terminologyClient, null); + + assertEquals("dummyVersion", actual); + + Mockito.verify(terminologyCache).getTerminologyCapabilities(); + Mockito.verify(terminologyCache).getCapabilityStatement(); + + Mockito.verify(terminologyClient, times(0)).getTerminologyCapabilities(); + Mockito.verify(terminologyClient, times(0)).getCapabilitiesStatementQuick(); + + Mockito.verify(context).setTxCaps(terminologyCapabilities); + } + + @Test + public void testInitializationWithClient() { + + Mockito.doReturn(false).when(terminologyCache).hasTerminologyCapabilities(); + Mockito.doReturn(false).when(terminologyCache).hasCapabilityStatement(); + + Mockito.doReturn(terminologyCapabilities).when(terminologyClient).getTerminologyCapabilities(); + Mockito.doReturn(capabilitiesStatement).when(terminologyClient).getCapabilitiesStatementQuick(); + + String actual = context.connectToTSServer(terminologyClient, null); + + assertEquals("dummyVersion", actual); + + Mockito.verify(terminologyCache, times(0)).getTerminologyCapabilities(); + Mockito.verify(terminologyCache, times(0)).getCapabilityStatement(); + + Mockito.verify(terminologyClient).getTerminologyCapabilities(); + Mockito.verify(terminologyClient).getCapabilitiesStatementQuick(); + + Mockito.verify(context).setTxCaps(terminologyCapabilities); + } + } 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 39f6a6ada..665c0fe21 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 @@ -3,9 +3,7 @@ package org.hl7.fhir.r5.context; import com.google.gson.JsonElement; import com.google.gson.JsonParser; 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.model.*; import org.hl7.fhir.r5.terminologies.ValueSetExpander; import org.hl7.fhir.utilities.validation.ValidationMessage; import org.junit.jupiter.api.Test; @@ -84,6 +82,15 @@ public class TerminologyCacheTests { ValueSet valueSet = new ValueSet(); valueSet.setUrl("dummyValueSetURL"); + TerminologyCapabilities terminologyCapabilities = new TerminologyCapabilities(); + terminologyCapabilities.getExpansion().setParameter(Arrays.asList()); + + CapabilityStatement.CapabilityStatementSoftwareComponent software = new CapabilityStatement.CapabilityStatementSoftwareComponent(); + software.setVersion("dummyVersion"); + + CapabilityStatement capabilityStatement = new CapabilityStatement(); + capabilityStatement.setSoftware(software); + Coding coding = new Coding(); coding.setCode("dummyCode"); @@ -95,6 +102,9 @@ public class TerminologyCacheTests { // Add dummy results to the cache TerminologyCache terminologyCacheA = new TerminologyCache(lock, tempCacheDirectory.toString()); + terminologyCacheA.cacheTerminologyCapabilities(terminologyCapabilities); + terminologyCacheA.cacheCapabilityStatement(capabilityStatement); + IWorkerContext.ValidationResult codingResultA = new IWorkerContext.ValidationResult(ValidationMessage.IssueSeverity.INFORMATION, "dummyInfo"); TerminologyCache.CacheToken codingTokenA = terminologyCacheA.generateValidationToken(CacheTestUtils.validationOptions, coding, valueSet); @@ -111,6 +121,9 @@ public class TerminologyCacheTests { terminologyCacheA.cacheExpansion(expansionTokenA, expansionOutcomeA, true); // Check that the in-memory cache is returning what we put in { + assertEquals(terminologyCapabilities, terminologyCacheA.getTerminologyCapabilities()); + assertEquals(capabilityStatement, terminologyCacheA.getCapabilityStatement()); + assertValidationResultEquals(codingResultA, terminologyCacheA.getValidation(codingTokenA)); assertValidationResultEquals(codeableConceptResultA, terminologyCacheA.getValidation(codeableConceptTokenA)); assertExpansionOutcomeEquals(expansionOutcomeA,terminologyCacheA.getExpansion(expansionTokenA)); @@ -120,6 +133,9 @@ public class TerminologyCacheTests { { TerminologyCache terminologyCacheB = new TerminologyCache(lock, tempCacheDirectory.toString()); + assertCanonicalResourceEquals(terminologyCapabilities, terminologyCacheB.getTerminologyCapabilities()); + assertCanonicalResourceEquals(capabilityStatement, terminologyCacheB.getCapabilityStatement()); + assertValidationResultEquals(codingResultA, terminologyCacheB.getValidation(terminologyCacheA.generateValidationToken(CacheTestUtils.validationOptions, coding, valueSet))); assertValidationResultEquals(codeableConceptResultA, terminologyCacheB.getValidation(terminologyCacheA.generateValidationToken(CacheTestUtils.validationOptions, @@ -129,6 +145,10 @@ public class TerminologyCacheTests { deleteTempCacheDirectory(tempCacheDirectory); } + private void assertCanonicalResourceEquals(CanonicalResource a, CanonicalResource b) { + assertTrue(a.equalsDeep(b)); + } + private void assertValidationResultEquals(IWorkerContext.ValidationResult a, IWorkerContext.ValidationResult b) { assertEquals(a.getSeverity(), b.getSeverity()); assertEquals(a.getMessage(), b.getMessage());