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

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

View File

@ -94,6 +94,7 @@ public class TerminologyCache {
public class CacheToken { public class CacheToken {
private String name; private String name;
private String key; private String key;
@Getter
private String request; private String request;
private boolean hasVersion; private boolean hasVersion;
@ -124,7 +125,6 @@ public class TerminologyCache {
private CapabilityStatement capabilityStatementCache = null; private CapabilityStatement capabilityStatementCache = null;
public boolean hasCapabilityStatement() { public boolean hasCapabilityStatement() {
return capabilityStatementCache != null; return capabilityStatementCache != null;
} }
@ -183,6 +183,7 @@ public class TerminologyCache {
public void clear() { public void clear() {
caches.clear(); caches.clear();
} }
public CacheToken generateValidationToken(ValidationOptions options, Coding code, ValueSet vs) { public CacheToken generateValidationToken(ValidationOptions options, Coding code, ValueSet vs) {
CacheToken ct = new CacheToken(); CacheToken ct = new CacheToken();
if (code.hasSystem()) if (code.hasSystem())
@ -198,7 +199,7 @@ public class TerminologyCache {
} catch (IOException e) { } catch (IOException e) {
throw new Error(e); throw new Error(e);
} }
ct.key = String.valueOf(hashNWS(ct.request)); ct.key = String.valueOf(hashJson(ct.request));
return ct; return ct;
} }
@ -227,7 +228,7 @@ public class TerminologyCache {
} catch (IOException e) { } catch (IOException e) {
throw new Error(e); throw new Error(e);
} }
ct.key = String.valueOf(hashNWS(ct.request)); ct.key = String.valueOf(hashJson(ct.request));
return ct; return ct;
} }
@ -268,7 +269,7 @@ public class TerminologyCache {
} catch (IOException e) { } catch (IOException e) {
throw new Error(e); throw new Error(e);
} }
ct.key = String.valueOf(hashNWS(ct.request)); ct.key = String.valueOf(hashJson(ct.request));
return ct; return ct;
} }
@ -500,6 +501,35 @@ public class TerminologyCache {
} }
} }
private CacheEntry getCacheEntry(String request, String resultString) throws IOException {
CacheEntry ce = new CacheEntry();
ce.persistent = true;
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"))
ce.e = new ValueSetExpansionOutcome((ValueSet) new JsonParser().parse(o.getAsJsonObject("valueSet")), error, TerminologyServiceErrorClass.UNKNOWN);
else
ce.e = new ValueSetExpansionOutcome(error, TerminologyServiceErrorClass.UNKNOWN);
} else {
String t = loadJS(o.get("severity"));
IssueSeverity severity = t == null ? null : IssueSeverity.fromCode(t);
String display = loadJS(o.get("display"));
String code = loadJS(o.get("code"));
String system = loadJS(o.get("system"));
String definition = loadJS(o.get("definition"));
t = loadJS(o.get("class"));
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);
}
return ce;
}
private void loadNamedCache(String fn) { private void loadNamedCache(String fn) {
int c = 0; int c = 0;
try { try {
@ -508,7 +538,7 @@ public class TerminologyCache {
NamedCache nc = new NamedCache(); NamedCache nc = new NamedCache();
nc.name = title; nc.name = title;
caches.put(title, nc);
if (src.startsWith("?")) if (src.startsWith("?"))
src = src.substring(1); src = src.substring(1);
int i = src.indexOf(ENTRY_MARKER); int i = src.indexOf(ENTRY_MARKER);
@ -519,34 +549,15 @@ public class TerminologyCache {
i = src.indexOf(ENTRY_MARKER); i = src.indexOf(ENTRY_MARKER);
if (!Utilities.noString(s)) { if (!Utilities.noString(s)) {
int j = s.indexOf(BREAK); int j = s.indexOf(BREAK);
String q = s.substring(0, j); String request = s.substring(0, j);
String p = s.substring(j + BREAK.length() + 1).trim(); String p = s.substring(j + BREAK.length() + 1).trim();
CacheEntry ce = new CacheEntry();
ce.persistent = true; CacheEntry cacheEntry = getCacheEntry(request, p);
ce.request = q;
boolean e = p.charAt(0) == 'e'; nc.map.put(String.valueOf(hashJson(cacheEntry.request)), cacheEntry);
p = p.substring(3); nc.list.add(cacheEntry);
JsonObject o = (JsonObject) new com.google.gson.JsonParser().parse(p);
String error = loadJS(o.get("error"));
if (e) {
if (o.has("valueSet"))
ce.e = new ValueSetExpansionOutcome((ValueSet) new JsonParser().parse(o.getAsJsonObject("valueSet")), error, TerminologyServiceErrorClass.UNKNOWN);
else
ce.e = new ValueSetExpansionOutcome(error, TerminologyServiceErrorClass.UNKNOWN);
} else {
String t = loadJS(o.get("severity"));
IssueSeverity severity = t == null ? null : IssueSeverity.fromCode(t);
String display = loadJS(o.get("display"));
String code = loadJS(o.get("code"));
String system = loadJS(o.get("system"));
String definition = loadJS(o.get("definition"));
t = loadJS(o.get("class"));
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);
} }
caches.put(nc.name, nc);
} }
} catch (Exception e) { } catch (Exception e) {
throw new FHIRException("Error loading " + fn + ": " + e.getMessage() + " entry " + c, e); throw new FHIRException("Error loading " + fn + ": " + e.getMessage() + " entry " + c, e);
@ -580,7 +591,7 @@ public class TerminologyCache {
return s; return s;
} }
private String hashNWS(String s) { private String hashJson(String s) {
s = StringUtils.remove(s, ' '); s = StringUtils.remove(s, ' ');
s = StringUtils.remove(s, '\n'); s = StringUtils.remove(s, '\n');
s = StringUtils.remove(s, '\r'); 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.println("No such cached session exists for session id " + sessionId + ", re-instantiating validator.");
} }
System.out.print(" Load FHIR v" + cliContext.getSv() + " from " + definitions); 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); sessionId = sessionCache.cacheSession(validator);
FhirPublication ver = FhirPublication.fromCode(cliContext.getSv()); FhirPublication ver = FhirPublication.fromCode(cliContext.getSv());