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());
@ -1270,6 +1270,25 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte
} }
} }
addServerValidationParameters(vs, pin, options);
if (txLog != null) {
txLog.clearLastId();
}
if (tcc.getClient() == null) {
throw new FHIRException(formatMessage(I18nConstants.ATTEMPT_TO_USE_TERMINOLOGY_SERVER_WHEN_NO_TERMINOLOGY_SERVER_IS_AVAILABLE));
}
Parameters pOut;
if (vs == null) {
pOut = tcc.getClient().validateCS(pin);
} else {
pOut = tcc.getClient().validateVS(pin);
}
return processValidationResult(pOut);
}
protected void addServerValidationParameters(ValueSet vs, Parameters pin, ValidationOptions options) {
boolean cache = false;
if (vs != null) { if (vs != null) {
if (tcc.isTxCaching() && tcc.getCacheId() != null && vs.getUrl() != null && tcc.getCached().contains(vs.getUrl()+"|"+ vs.getVersion())) { 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() : ""))); pin.addParameter().setName("url").setValue(new UriType(vs.getUrl()+(vs.hasVersion() ? "|"+ vs.getVersion() : "")));
@ -1300,20 +1319,6 @@ public abstract class BaseWorkerContext extends I18nBase implements IWorkerConte
if (options.isDisplayWarningMode()) { if (options.isDisplayWarningMode()) {
pin.addParameter("mode","lenient-display-validation"); pin.addParameter("mode","lenient-display-validation");
} }
if (txLog != null) {
txLog.clearLastId();
}
if (tcc.getClient() == null) {
throw new FHIRException(formatMessage(I18nConstants.ATTEMPT_TO_USE_TERMINOLOGY_SERVER_WHEN_NO_TERMINOLOGY_SERVER_IS_AVAILABLE));
}
Parameters pOut;
if (vs == null) {
pOut = tcc.getClient().validateCS(pin);
} else {
pOut = tcc.getClient().validateVS(pin);
}
return processValidationResult(pOut);
} }
private boolean addDependentResources(Parameters pin, ValueSet vs) { private boolean addDependentResources(Parameters pin, ValueSet vs) {

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