Testing Part 2

This commit is contained in:
dotasek 2022-01-21 17:39:43 -05:00
parent f9561f2685
commit 6416ccce36
1 changed files with 68 additions and 0 deletions

View File

@ -6,8 +6,12 @@ import org.checkerframework.checker.units.qual.C;
import org.hl7.fhir.r5.model.CodeableConcept; import org.hl7.fhir.r5.model.CodeableConcept;
import org.hl7.fhir.r5.model.Coding; import org.hl7.fhir.r5.model.Coding;
import org.hl7.fhir.r5.model.ValueSet; 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.api.Test;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.nio.file.Files; import java.nio.file.Files;
@ -16,6 +20,7 @@ import java.nio.file.Paths;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.mock;
public class TerminologyCacheTests { public class TerminologyCacheTests {
@ -27,9 +32,71 @@ public class TerminologyCacheTests {
return jsonParser.parse(stringValue); return jsonParser.parse(stringValue);
}; };
public Path createTempCacheDirectory() throws IOException {
Path tmp = Files.createTempDirectory("integrationTestCache");
tmp.toFile().deleteOnExit();
return tmp;
}
public void deleteTempCacheDirectory(Path path) {
File directory = new File(path.toUri());
for (File file : directory.listFiles()) {
file.delete();
}
}
@Test
public void testCachePersistence() throws IOException, URISyntaxException {
Object lock = new Object();
Path tempCacheDirectory = createTempCacheDirectory();
ValueSet valueSet = new ValueSet();
valueSet.setUrl("dummyValueSetURL");
Coding coding = new Coding();
coding.setCode("dummyCode");
// Add dummy results to the cache
TerminologyCache terminologyCacheA = new TerminologyCache(lock, tempCacheDirectory.toString());
IWorkerContext.ValidationResult validationResultA = new IWorkerContext.ValidationResult(ValidationMessage.IssueSeverity.INFORMATION, "dummyInfo");
TerminologyCache.CacheToken codingTokenA = terminologyCacheA.generateValidationToken(CacheTestUtils.validationOptions,
coding, valueSet);
terminologyCacheA.cacheValidation(codingTokenA, validationResultA, true);
TerminologyCache.CacheToken expansionTokenA = terminologyCacheA.generateExpandToken(valueSet, true);
ValueSetExpander.ValueSetExpansionOutcome expansionOutcomeA = new ValueSetExpander.ValueSetExpansionOutcome(valueSet);
terminologyCacheA.cacheExpansion(expansionTokenA, expansionOutcomeA, true);
// Check that the in-memory cache is returning what we put in
{
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());
assertValidationResultEquals(validationResultA, terminologyCacheB.getValidation(terminologyCacheA.generateValidationToken(CacheTestUtils.validationOptions,
coding, valueSet)));
assertExpansionOutcomeEquals(expansionOutcomeA,terminologyCacheB.getExpansion(terminologyCacheA.generateExpandToken(valueSet, true)));
}
deleteTempCacheDirectory(tempCacheDirectory);
}
private void assertValidationResultEquals(IWorkerContext.ValidationResult a, IWorkerContext.ValidationResult b) {
assertEquals(a.getSeverity(), b.getSeverity());
assertEquals(a.getMessage(), b.getMessage());
}
private void assertExpansionOutcomeEquals(ValueSetExpander.ValueSetExpansionOutcome a, ValueSetExpander.ValueSetExpansionOutcome b) {
assertEquals(a.getValueset().getUrl(), b.getValueset().getUrl());
}
@Test @Test
public void testCodingCacheTokenGeneration() throws IOException, URISyntaxException { public void testCodingCacheTokenGeneration() throws IOException, URISyntaxException {
Object lock = new Object(); Object lock = new Object();
TerminologyCache terminologyCache = new TerminologyCache(lock, null); TerminologyCache terminologyCache = new TerminologyCache(lock, null);
ValueSet valueSet = new ValueSet(); ValueSet valueSet = new ValueSet();
@ -42,6 +109,7 @@ public class TerminologyCacheTests {
JsonElement expected = getJsonFromFile("codingEmptyValueSet.json"); JsonElement expected = getJsonFromFile("codingEmptyValueSet.json");
assertEquals(expected, actual); assertEquals(expected, actual);
} }
@Test @Test