Refactor and introduce test

This commit is contained in:
dotasek 2023-06-15 10:38:03 -04:00
parent 10c50a14f0
commit f4b6ff2f6e
2 changed files with 135 additions and 31 deletions

View File

@ -1260,7 +1260,7 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte
} }
protected 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) { if (vs != null) {
for (ConceptSetComponent inc : vs.getCompose().getInclude()) { for (ConceptSetComponent inc : vs.getCompose().getInclude()) {
codeSystemsUsed.add(inc.getSystem()); codeSystemsUsed.add(inc.getSystem());
@ -1269,37 +1269,8 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte
codeSystemsUsed.add(inc.getSystem()); codeSystemsUsed.add(inc.getSystem());
} }
} }
if (vs != null) {
if (tcc.isTxCaching() && tcc.getCacheId() != null && vs.getUrl() != null && tcc.getCached().contains(vs.getUrl()+"|"+vs.getVersion())) {
pin.addParameter().setName("url").setValue(new UriType(vs.getUrl()+(vs.hasVersion() ? "|"+vs.getVersion() : "")));
} else if (options.getVsAsUrl()){
pin.addParameter().setName("url").setValue(new UriType(vs.getUrl()));
} else {
pin.addParameter().setName("valueSet").setResource(vs);
if (vs.getUrl() != null) {
tcc.getCached().add(vs.getUrl()+"|"+vs.getVersion());
}
}
cache = true;
addDependentResources(pin, vs);
}
if (cache) {
pin.addParameter().setName("cache-id").setValue(new IdType(tcc.getCacheId()));
}
for (ParametersParameterComponent pp : pin.getParameter()) {
if (pp.getName().equals("profile")) {
throw new Error(formatMessage(I18nConstants.CAN_ONLY_SPECIFY_PROFILE_IN_THE_CONTEXT));
}
}
if (expParameters == null) {
throw new Error(formatMessage(I18nConstants.NO_EXPANSIONPROFILE_PROVIDED));
}
pin.addParameter().setName("profile").setResource(expParameters);
if (options.isDisplayWarningMode()) { addServerValidationParameters(vs, pin, options);
pin.addParameter("mode","lenient-display-validation");
}
if (txLog != null) { if (txLog != null) {
txLog.clearLastId(); txLog.clearLastId();
@ -1316,6 +1287,40 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte
return processValidationResult(pOut); return processValidationResult(pOut);
} }
protected void addServerValidationParameters(ValueSet vs, Parameters pin, ValidationOptions options) {
boolean cache = false;
if (vs != null) {
if (tcc.isTxCaching() && tcc.getCacheId() != null && vs.getUrl() != null && tcc.getCached().contains(vs.getUrl()+"|"+ vs.getVersion())) {
pin.addParameter().setName("url").setValue(new UriType(vs.getUrl()+(vs.hasVersion() ? "|"+ vs.getVersion() : "")));
} else if (options.getVsAsUrl()){
pin.addParameter().setName("url").setValue(new UriType(vs.getUrl()));
} else {
pin.addParameter().setName("valueSet").setResource(vs);
if (vs.getUrl() != null) {
tcc.getCached().add(vs.getUrl()+"|"+ vs.getVersion());
}
}
cache = true;
addDependentResources(pin, vs);
}
if (cache) {
pin.addParameter().setName("cache-id").setValue(new IdType(tcc.getCacheId()));
}
for (ParametersParameterComponent pp : pin.getParameter()) {
if (pp.getName().equals("profile")) {
throw new Error(formatMessage(I18nConstants.CAN_ONLY_SPECIFY_PROFILE_IN_THE_CONTEXT));
}
}
if (expParameters == null) {
throw new Error(formatMessage(I18nConstants.NO_EXPANSIONPROFILE_PROVIDED));
}
pin.addParameter().setName("profile").setResource(expParameters);
if (options.isDisplayWarningMode()) {
pin.addParameter("mode","lenient-display-validation");
}
}
private boolean addDependentResources(Parameters pin, ValueSet vs) { private boolean addDependentResources(Parameters pin, ValueSet vs) {
boolean cache = false; boolean cache = false;
for (ConceptSetComponent inc : vs.getCompose().getInclude()) { for (ConceptSetComponent inc : vs.getCompose().getInclude()) {

View File

@ -0,0 +1,99 @@
package org.hl7.fhir.r5.context;
import org.hl7.fhir.exceptions.FHIRException;
import org.hl7.fhir.r5.model.PackageInformation;
import org.hl7.fhir.r5.model.Parameters;
import org.hl7.fhir.r5.model.ValueSet;
import org.hl7.fhir.r5.utils.validation.IResourceValidator;
import org.hl7.fhir.utilities.npm.BasePackageCacheManager;
import org.hl7.fhir.utilities.npm.NpmPackage;
import org.hl7.fhir.utilities.validation.ValidationOptions;
import org.junit.jupiter.api.Test;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNull;
public class BaseWorkerContextTests {
private BaseWorkerContext getBaseWorkerContext() throws IOException {
BaseWorkerContext baseWorkerContext = new BaseWorkerContext() {
@Override
public String getVersion() {
return null;
}
@Override
public IResourceValidator newValidator() throws FHIRException {
return null;
}
@Override
public void cachePackage(PackageInformation packageInfo) {
}
@Override
public List<String> getResourceNames() {
return null;
}
@Override
public int loadFromPackage(NpmPackage pi, IContextResourceLoader loader) throws FileNotFoundException, IOException, FHIRException {
return 0;
}
@Override
public int loadFromPackage(NpmPackage pi, IContextResourceLoader loader, List<String> types) throws FileNotFoundException, IOException, FHIRException {
return 0;
}
@Override
public int loadFromPackageAndDependencies(NpmPackage pi, IContextResourceLoader loader, BasePackageCacheManager pcm) throws FileNotFoundException, IOException, FHIRException {
return 0;
}
@Override
public boolean hasPackage(String id, String ver) {
return false;
}
@Override
public boolean hasPackage(PackageInformation pack) {
return false;
}
@Override
public PackageInformation getPackage(String id, String ver) {
return null;
}
@Override
public String getSpecUrl() {
return null;
}
};
baseWorkerContext.expParameters = new Parameters();
return baseWorkerContext;
}
@Test
public void testAddServerValidationParametersDisplayWarning() throws IOException {
BaseWorkerContext baseWorkerContext = getBaseWorkerContext();
Parameters pin = new Parameters();
baseWorkerContext.addServerValidationParameters(new ValueSet(), pin, new ValidationOptions().setDisplayWarningMode(true));
assertEquals("lenient-display-validation", pin.getParameter("mode").getValue().primitiveValue());
}
@Test
public void testAddServerValidationParametersDisplayError() throws IOException {
BaseWorkerContext baseWorkerContext = getBaseWorkerContext();
Parameters pin = new Parameters();
baseWorkerContext.addServerValidationParameters(new ValueSet(), pin, new ValidationOptions());
assertNull(pin.getParameter("mode"));
}
}