Test capability caching.

Just when I think I'm done...
This commit is contained in:
dotasek 2022-01-25 16:36:13 -05:00
parent 320042a0ad
commit 9ec63bc1b9
2 changed files with 80 additions and 5 deletions

View File

@ -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);
}
}

View File

@ -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());