Testing Part 1

This commit is contained in:
dotasek 2022-01-21 15:29:17 -05:00
parent 14eac3e2a0
commit f9561f2685
13 changed files with 8558 additions and 40 deletions

View File

@ -666,6 +666,7 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte
return expandVS(vs, cacheOk, heirarchical);
}
@Override
public ValueSetExpansionOutcome expandVS(ConceptSetComponent inc, boolean hierarchical) throws TerminologyServiceException {
ValueSet vs = new ValueSet();
@ -713,6 +714,7 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte
return res;
}
//TESTME
@Override
public ValueSetExpansionOutcome expandVS(ValueSet vs, boolean cacheOk, boolean heirarchical) {
if (expParameters == null)
@ -721,6 +723,7 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte
return expandVS(vs, cacheOk, heirarchical, false, p);
}
//TESTME
@Override
public ValueSetExpansionOutcome expandVS(ValueSet vs, boolean cacheOk, boolean heirarchical, boolean incompleteOk) {
if (expParameters == null)
@ -729,6 +732,7 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte
return expandVS(vs, cacheOk, heirarchical, incompleteOk, p);
}
//TESTME
public ValueSetExpansionOutcome expandVS(ValueSet vs, boolean cacheOk, boolean heirarchical, boolean incompleteOk, Parameters p) {
if (p == null) {
throw new Error(formatMessage(I18nConstants.NO_PARAMETERS_PROVIDED_TO_EXPANDVS));
@ -947,11 +951,11 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte
return code.hasVersion() ? code.getSystem()+"|"+code.getVersion() : code.getSystem();
}
//TESTME
@Override
public ValidationResult validateCode(ValidationOptions options, Coding code, ValueSet vs, ValidationContextCarrier ctxt) {
if (options == null) {
options = ValidationOptions.defaults();
}
public ValidationResult validateCode(final ValidationOptions optionsArg, final Coding code, final ValueSet vs, final ValidationContextCarrier ctxt) {
ValidationOptions options = optionsArg != null ? optionsArg : ValidationOptions.defaults();
if (code.hasSystem()) {
codeSystemsUsed.add(code.getSystem());
@ -1036,6 +1040,7 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte
}
}
//TESTME
@Override
public ValidationResult validateCode(ValidationOptions options, CodeableConcept code, ValueSet vs) {
CacheToken cacheToken = txCache.generateValidationToken(options, code, vs);
@ -1084,7 +1089,7 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte
return res;
}
private ValidationResult validateOnServer(ValueSet vs, Parameters pin, ValidationOptions options) throws FHIRException {
protected ValidationResult validateOnServer(ValueSet vs, Parameters pin, ValidationOptions options) throws FHIRException {
boolean cache = false;
if (vs != null) {
for (ConceptSetComponent inc : vs.getCompose().getInclude()) {

View File

@ -94,6 +94,7 @@ public class TerminologyCache {
public class CacheToken {
private String name;
private String key;
@Getter
private String request;
private boolean hasVersion;
@ -124,7 +125,6 @@ public class TerminologyCache {
private CapabilityStatement capabilityStatementCache = null;
public boolean hasCapabilityStatement() {
return capabilityStatementCache != null;
}
@ -183,6 +183,7 @@ public class TerminologyCache {
public void clear() {
caches.clear();
}
public CacheToken generateValidationToken(ValidationOptions options, Coding code, ValueSet vs) {
CacheToken ct = new CacheToken();
if (code.hasSystem())
@ -198,7 +199,7 @@ public class TerminologyCache {
} catch (IOException e) {
throw new Error(e);
}
ct.key = String.valueOf(hashNWS(ct.request));
ct.key = String.valueOf(hashJson(ct.request));
return ct;
}
@ -227,7 +228,7 @@ public class TerminologyCache {
} catch (IOException e) {
throw new Error(e);
}
ct.key = String.valueOf(hashNWS(ct.request));
ct.key = String.valueOf(hashJson(ct.request));
return ct;
}
@ -268,7 +269,7 @@ public class TerminologyCache {
} catch (IOException e) {
throw new Error(e);
}
ct.key = String.valueOf(hashNWS(ct.request));
ct.key = String.valueOf(hashJson(ct.request));
return ct;
}
@ -500,33 +501,15 @@ public class TerminologyCache {
}
}
private void loadNamedCache(String fn) {
int c = 0;
try {
String src = TextFile.fileToString(Utilities.path(folder, fn));
String title = fn.substring(0, fn.lastIndexOf("."));
NamedCache nc = new NamedCache();
nc.name = title;
caches.put(title, nc);
if (src.startsWith("?"))
src = src.substring(1);
int i = src.indexOf(ENTRY_MARKER);
while (i > -1) {
c++;
String s = src.substring(0, i);
src = src.substring(i + ENTRY_MARKER.length() + 1);
i = src.indexOf(ENTRY_MARKER);
if (!Utilities.noString(s)) {
int j = s.indexOf(BREAK);
String q = s.substring(0, j);
String p = s.substring(j + BREAK.length() + 1).trim();
private CacheEntry getCacheEntry(String request, String resultString) throws IOException {
CacheEntry ce = new CacheEntry();
ce.persistent = true;
ce.request = q;
boolean e = p.charAt(0) == 'e';
p = p.substring(3);
JsonObject o = (JsonObject) new com.google.gson.JsonParser().parse(p);
ce.request = request;
boolean e = resultString.charAt(0) == 'e';
resultString = resultString.substring(3);
JsonObject o = (JsonObject) new com.google.gson.JsonParser().parse(resultString);
String error = loadJS(o.get("error"));
if (e) {
if (o.has("valueSet"))
@ -544,9 +527,37 @@ public class TerminologyCache {
TerminologyServiceErrorClass errorClass = t == null ? null : TerminologyServiceErrorClass.valueOf(t);
ce.v = new ValidationResult(severity, error, system, new ConceptDefinitionComponent().setDisplay(display).setDefinition(definition).setCode(code)).setErrorClass(errorClass);
}
nc.map.put(String.valueOf(hashNWS(ce.request)), ce);
nc.list.add(ce);
return ce;
}
private void loadNamedCache(String fn) {
int c = 0;
try {
String src = TextFile.fileToString(Utilities.path(folder, fn));
String title = fn.substring(0, fn.lastIndexOf("."));
NamedCache nc = new NamedCache();
nc.name = title;
if (src.startsWith("?"))
src = src.substring(1);
int i = src.indexOf(ENTRY_MARKER);
while (i > -1) {
c++;
String s = src.substring(0, i);
src = src.substring(i + ENTRY_MARKER.length() + 1);
i = src.indexOf(ENTRY_MARKER);
if (!Utilities.noString(s)) {
int j = s.indexOf(BREAK);
String request = s.substring(0, j);
String p = s.substring(j + BREAK.length() + 1).trim();
CacheEntry cacheEntry = getCacheEntry(request, p);
nc.map.put(String.valueOf(hashJson(cacheEntry.request)), cacheEntry);
nc.list.add(cacheEntry);
}
caches.put(nc.name, nc);
}
} catch (Exception e) {
throw new FHIRException("Error loading " + fn + ": " + e.getMessage() + " entry " + c, e);
@ -580,7 +591,7 @@ public class TerminologyCache {
return s;
}
private String hashNWS(String s) {
private String hashJson(String s) {
s = StringUtils.remove(s, ' ');
s = StringUtils.remove(s, '\n');
s = StringUtils.remove(s, '\r');

View File

@ -0,0 +1,8 @@
package org.hl7.fhir.r5.context;
import org.hl7.fhir.utilities.validation.ValidationOptions;
public class CacheTestUtils {
public static final ValidationOptions validationOptions = new ValidationOptions().guessSystem().setVersionFlexible(false);
}

View File

@ -0,0 +1,52 @@
package org.hl7.fhir.r5.context;
import org.hl7.fhir.exceptions.DefinitionException;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.r5.formats.IParser;
import org.hl7.fhir.r5.formats.ParserType;
import org.hl7.fhir.r5.model.CodeableConcept;
import org.hl7.fhir.r5.model.StructureDefinition;
import org.hl7.fhir.r5.model.ValueSet;
import org.hl7.fhir.r5.utils.validation.IResourceValidator;
import org.hl7.fhir.r5.utils.validation.ValidationContextCarrier;
import org.hl7.fhir.utilities.npm.BasePackageCacheManager;
import org.hl7.fhir.utilities.npm.NpmPackage;
import org.hl7.fhir.r5.model.Coding;
import org.hl7.fhir.utilities.validation.ValidationOptions;
import org.junit.jupiter.api.Test;
import org.mockito.ArgumentMatchers;
import org.mockito.Mockito;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import static org.mockito.Mockito.mock;
public class SimpleWorkerContextTests {
@Test
public void testValidateCodingOnServer() throws IOException {
SimpleWorkerContext context = Mockito.spy(new SimpleWorkerContext());
IWorkerContext.ValidationResult result = mock(IWorkerContext.ValidationResult.class);
Mockito.doReturn(result).when(context).validateOnServer(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any());
ValidationOptions validationOptions = new ValidationOptions().guessSystem().setVersionFlexible(false);
ValueSet valueSet = new ValueSet();
ValidationContextCarrier ctxt = null;
context.validateCode(validationOptions, new Coding(), valueSet, ctxt);
}
@Test
public void testValidateCodableConceptOnServer() throws IOException {
SimpleWorkerContext context = Mockito.spy(new SimpleWorkerContext());
IWorkerContext.ValidationResult result = mock(IWorkerContext.ValidationResult.class);
Mockito.doReturn(result).when(context).validateOnServer(ArgumentMatchers.any(), ArgumentMatchers.any(), ArgumentMatchers.any());
ValidationOptions validationOptions = new ValidationOptions().guessSystem().setVersionFlexible(false);
ValueSet valueSet = new ValueSet();
CodeableConcept codeableConcept = new CodeableConcept();
context.validateCode(validationOptions, codeableConcept, valueSet);
}
}

View File

@ -0,0 +1,74 @@
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.model.CodeableConcept;
import org.hl7.fhir.r5.model.Coding;
import org.hl7.fhir.r5.model.ValueSet;
import org.junit.jupiter.api.Test;
import java.io.IOException;
import java.net.URISyntaxException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class TerminologyCacheTests {
private JsonParser jsonParser = new JsonParser();
private JsonElement getJsonFromFile(String filename) throws URISyntaxException, IOException {
final Path path = Paths.get("src","test","resources", "context", filename);
final String stringValue = new String ( Files.readAllBytes(path));
return jsonParser.parse(stringValue);
};
@Test
public void testCodingCacheTokenGeneration() throws IOException, URISyntaxException {
Object lock = new Object();
TerminologyCache terminologyCache = new TerminologyCache(lock, null);
ValueSet valueSet = new ValueSet();
Coding coding = new Coding();
coding.setCode("dummyCode");
TerminologyCache.CacheToken cacheToken = terminologyCache.generateValidationToken(CacheTestUtils.validationOptions,
coding, valueSet );
JsonElement actual = jsonParser.parse(cacheToken.getRequest());
JsonElement expected = getJsonFromFile("codingEmptyValueSet.json");
assertEquals(expected, actual);
}
@Test
public void testCodableConceptCacheTokenGeneration() throws IOException, URISyntaxException {
Object lock = new Object();
TerminologyCache terminologyCache = new TerminologyCache(lock, null);
CodeableConcept concept = new CodeableConcept();
concept.addCoding(new Coding().setCode("dummyCode"));
ValueSet valueSet = new ValueSet();
TerminologyCache.CacheToken cacheToken = terminologyCache.generateValidationToken(CacheTestUtils.validationOptions,
concept, valueSet );
JsonElement actual = jsonParser.parse(cacheToken.getRequest());
JsonElement expected = getJsonFromFile("codableConceptEmptyValueSet.json");
assertEquals(expected, actual);
}
@Test
public void testDummyCache() throws IOException {
Object lock = new Object();
Path path = Paths.get("src","test","resources", "context", "dummyCache");
TerminologyCache cache = new TerminologyCache(lock, path.toString());
assertTrue(cache.hasTerminologyCapabilities());
assertTrue(cache.hasCapabilityStatement());
}
}

View File

@ -0,0 +1,18 @@
{
"code": {
"coding": [
{
"code": "dummyCode"
}
]
},
"valueSet": {
"resourceType": "ValueSet"
},
"lang": "null",
"useServer": "true",
"useClient": "true",
"guessSystem": "true",
"valueSetMode": "ALL_CHECKS",
"versionFlexible": "false"
}

View File

@ -0,0 +1,14 @@
{
"code": {
"code": "dummyCode"
},
"valueSet": {
"resourceType": "ValueSet"
},
"lang": "null",
"useServer": "true",
"useClient": "true",
"guessSystem": "true",
"valueSetMode": "ALL_CHECKS",
"versionFlexible": "false"
}

View File

@ -0,0 +1,62 @@
{
"resourceType" : "CapabilityStatement",
"id" : "FhirServer",
"meta" : {
"tag" : [{
"system" : "http://hl7.org/fhir/v3/ObservationValue",
"code" : "SUBSETTED",
"display" : "Subsetted"
}]
},
"url" : "http://fhir.healthintersections.com.au/open/metadata",
"version" : "4.0.1-2.0.12-SNAPSHOT",
"name" : "FHIR Reference Server Conformance Statement",
"status" : "active",
"date" : "2022-01-10T11:07:19.254Z",
"contact" : [{
"telecom" : [{
"system" : "other",
"value" : "http://healthintersections.com.au/"
}]
}],
"kind" : "instance",
"instantiates" : ["http://hl7.org/fhir/CapabilityStatement/terminology-server"],
"software" : {
"name" : "Reference Server",
"version" : "2.0.12-SNAPSHOT",
"releaseDate" : "2021-12-20T02:28:03.769Z"
},
"fhirVersion" : "4.0.1",
"format" : ["application/fhir+xml",
"application/fhir+json"],
"rest" : [{
"mode" : "server",
"security" : {
"cors" : true
},
"operation" : [{
"name" : "expand",
"definition" : "http://hl7.org/fhir/OperationDefinition/ValueSet-expand"
},
{
"name" : "lookup",
"definition" : "http://hl7.org/fhir/OperationDefinition/ValueSet-lookup"
},
{
"name" : "validate-code",
"definition" : "http://hl7.org/fhir/OperationDefinition/Resource-validate"
},
{
"name" : "translate",
"definition" : "http://hl7.org/fhir/OperationDefinition/ConceptMap-translate"
},
{
"name" : "closure",
"definition" : "http://hl7.org/fhir/OperationDefinition/ConceptMap-closure"
},
{
"name" : "versions",
"definition" : "/OperationDefinition/fso-versions"
}]
}]
}

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,11 @@
-------------------------------------------------------------------------------------
{"code" : {
"system" : "http://dummysite.org/fhir/CodeSystem/dummy",
"code" : "Every 4 weeks"
}, "valueSet" :null, "lang":"null", "useServer":"true", "useClient":"true", "guessSystem":"false", "valueSetMode":"ALL_CHECKS", "versionFlexible":"true"}####
v: {
"severity" : "error",
"error" : "The code system 'http://dummysite.org/fhir/CodeSystem/dummy' is not known (encountered paired with code = 'Every 4 weeks'); The code provided (http://dummysite.org/fhir/CodeSystem/dummy#Every 4 weeks) is not valid in the value set 'All codes known to the system' (from http://tx.fhir.org/r4)",
"class" : "CODESYSTEM_UNSUPPORTED"
}
-------------------------------------------------------------------------------------

View File

@ -0,0 +1,49 @@
-------------------------------------------------------------------------------------
{"code" : {
"system" : "http://unitsofmeasure.org",
"code" : "L/min"
}, "valueSet" :null, "lang":"fi", "useServer":"true", "useClient":"true", "guessSystem":"false", "valueSetMode":"ALL_CHECKS", "versionFlexible":"false"}####
v: {
"display" : "L/min",
"code" : "L/min",
"system" : "http://unitsofmeasure.org"
}
-------------------------------------------------------------------------------------
{"code" : {
"system" : "http://unitsofmeasure.org",
"code" : "21612-7"
}, "valueSet" :null, "lang":"fi", "useServer":"true", "useClient":"true", "guessSystem":"false", "valueSetMode":"ALL_CHECKS", "versionFlexible":"false"}####
v: {
"severity" : "error",
"error" : "Error processing Unit: '21612-7': Expected \"/\" or \".\" at character 6; The code \"21612-7\" is not valid in the system http://unitsofmeasure.org; The code provided (http://unitsofmeasure.org#21612-7) is not valid in the value set 'All codes known to the system' (from http://tx.fhir.org/r3)"
}
-------------------------------------------------------------------------------------
{"code" : {
"system" : "http://unitsofmeasure.org",
"code" : "tbl"
}, "valueSet" :null, "lang":"null", "useServer":"true", "useClient":"true", "guessSystem":"false", "valueSetMode":"ALL_CHECKS", "versionFlexible":"false"}####
v: {
"severity" : "error",
"error" : "Error processing Unit: 'tbl': The unit \"tbl\" is unknown at character 1; The code \"tbl\" is not valid in the system http://unitsofmeasure.org; The code provided (http://unitsofmeasure.org#tbl) is not valid in the value set 'All codes known to the system' (from http://tx.fhir.org/r3)"
}
-------------------------------------------------------------------------------------
{"code" : {
"system" : "http://unitsofmeasure.org",
"code" : "mmol/L"
}, "valueSet" :null, "lang":"null", "useServer":"true", "useClient":"true", "guessSystem":"false", "valueSetMode":"ALL_CHECKS", "versionFlexible":"false"}####
v: {
"display" : "mmol/L",
"code" : "mmol/L",
"system" : "http://unitsofmeasure.org"
}
-------------------------------------------------------------------------------------
{"code" : {
"system" : "http://unitsofmeasure.org",
"code" : "[lb_av]"
}, "valueSet" :null, "lang":"null", "useServer":"true", "useClient":"true", "guessSystem":"false", "valueSetMode":"ALL_CHECKS", "versionFlexible":"true"}####
v: {
"display" : "[lb_av]",
"code" : "[lb_av]",
"system" : "http://unitsofmeasure.org"
}
-------------------------------------------------------------------------------------

View File

@ -287,7 +287,7 @@ public class ValidationService {
System.out.println("No such cached session exists for session id " + sessionId + ", re-instantiating validator.");
}
System.out.print(" Load FHIR v" + cliContext.getSv() + " from " + definitions);
ValidationEngine validator = new ValidationEngine(definitions, cliContext.getSv(), cliContext.getTxCache(), tt, "fhir/validator");
ValidationEngine validator = new ValidationEngine(definitions, cliContext.getSv(), tt, "fhir/validator");
sessionId = sessionCache.cacheSession(validator);
FhirPublication ver = FhirPublication.fromCode(cliContext.getSv());