mirror of
https://github.com/hapifhir/hapi-fhir.git
synced 2025-02-08 22:14:50 +00:00
Work on tests
This commit is contained in:
parent
c5f48b6b47
commit
541fbf3ec1
@ -106,6 +106,38 @@ public class SearchParamExtractorDstu3Test {
|
|||||||
assertEquals("15", params.iterator().next().getValue().toPlainString());
|
assertEquals("15", params.iterator().next().getValue().toPlainString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testEmptyPath() {
|
||||||
|
|
||||||
|
MySearchParamRegistry searchParamRegistry = new MySearchParamRegistry();
|
||||||
|
SearchParamExtractorDstu3 extractor = new SearchParamExtractorDstu3(new ModelConfig(), ourCtx, ourValidationSupport, searchParamRegistry);
|
||||||
|
extractor.start();
|
||||||
|
|
||||||
|
searchParamRegistry.addSearchParam(new RuntimeSearchParam("foo", "foo", "", RestSearchParameterTypeEnum.STRING, Sets.newHashSet(), Sets.newHashSet(), RuntimeSearchParam.RuntimeSearchParamStatusEnum.ACTIVE));
|
||||||
|
Patient resource = new Patient();
|
||||||
|
extractor.extractSearchParamStrings(resource);
|
||||||
|
|
||||||
|
searchParamRegistry.addSearchParam(new RuntimeSearchParam("foo", "foo", null, RestSearchParameterTypeEnum.STRING, Sets.newHashSet(), Sets.newHashSet(), RuntimeSearchParam.RuntimeSearchParamStatusEnum.ACTIVE));
|
||||||
|
extractor.extractSearchParamStrings(resource);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testStringMissingResourceType() {
|
||||||
|
|
||||||
|
MySearchParamRegistry searchParamRegistry = new MySearchParamRegistry();
|
||||||
|
SearchParamExtractorDstu3 extractor = new SearchParamExtractorDstu3(new ModelConfig(), ourCtx, ourValidationSupport, searchParamRegistry);
|
||||||
|
extractor.start();
|
||||||
|
|
||||||
|
searchParamRegistry.addSearchParam(new RuntimeSearchParam("foo", "foo", "communication.language.coding.system | communication.language.coding.code", RestSearchParameterTypeEnum.STRING, Sets.newHashSet(), Sets.newHashSet(), RuntimeSearchParam.RuntimeSearchParamStatusEnum.ACTIVE));
|
||||||
|
Patient resource = new Patient();
|
||||||
|
resource.getCommunicationFirstRep().getLanguage().getCodingFirstRep().setCode("blah");
|
||||||
|
Set<ResourceIndexedSearchParamString> strings = extractor.extractSearchParamStrings(resource);
|
||||||
|
assertEquals(1, strings.size());
|
||||||
|
assertEquals("BLAH", strings.iterator().next().getValueNormalized());
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInvalidType() {
|
public void testInvalidType() {
|
||||||
|
@ -1,7 +1,10 @@
|
|||||||
package ca.uhn.fhir.jpa.searchparam.extractor;
|
package ca.uhn.fhir.jpa.searchparam.extractor;
|
||||||
|
|
||||||
import ca.uhn.fhir.context.*;
|
import ca.uhn.fhir.context.*;
|
||||||
|
import ca.uhn.fhir.parser.DataFormatException;
|
||||||
import ca.uhn.fhir.rest.api.RestSearchParameterTypeEnum;
|
import ca.uhn.fhir.rest.api.RestSearchParameterTypeEnum;
|
||||||
|
import org.apache.commons.lang3.Validate;
|
||||||
|
import org.hl7.fhir.instance.model.api.IBase;
|
||||||
import org.slf4j.Logger;
|
import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
@ -9,12 +12,14 @@ import java.util.ArrayList;
|
|||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
|
import static org.apache.commons.lang3.StringUtils.isBlank;
|
||||||
|
|
||||||
public class SearchParamFinder {
|
public class SearchParamFinder {
|
||||||
private static final Logger ourLog = LoggerFactory.getLogger(SearchParamFinder.class);
|
private static final Logger ourLog = LoggerFactory.getLogger(SearchParamFinder.class);
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
|
|
||||||
RestSearchParameterTypeEnum type = RestSearchParameterTypeEnum.TOKEN;
|
RestSearchParameterTypeEnum type = RestSearchParameterTypeEnum.STRING;
|
||||||
process(FhirContext.forDstu2(), type);
|
process(FhirContext.forDstu2(), type);
|
||||||
process(FhirContext.forDstu3(), type);
|
process(FhirContext.forDstu3(), type);
|
||||||
process(FhirContext.forR4(), type);
|
process(FhirContext.forR4(), type);
|
||||||
@ -26,8 +31,29 @@ public class SearchParamFinder {
|
|||||||
for (String nextResourceName : theCtx.getResourceNames()) {
|
for (String nextResourceName : theCtx.getResourceNames()) {
|
||||||
RuntimeResourceDefinition nextResDef = theCtx.getResourceDefinition(nextResourceName);
|
RuntimeResourceDefinition nextResDef = theCtx.getResourceDefinition(nextResourceName);
|
||||||
for (RuntimeSearchParam nextSp : nextResDef.getSearchParams()) {
|
for (RuntimeSearchParam nextSp : nextResDef.getSearchParams()) {
|
||||||
|
if (nextSp.getName().equals("_id")){
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
if (nextSp.getParamType() == theWantType) {
|
if (nextSp.getParamType() == theWantType) {
|
||||||
for (String nextPath : nextSp.getPathsSplit()) {
|
for (String nextPath : nextSp.getPathsSplit()) {
|
||||||
|
if (isBlank(nextPath)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (nextPath.startsWith("(") && nextPath.endsWith(")")) {
|
||||||
|
nextPath = nextPath.substring(1, nextPath.length()-1);
|
||||||
|
}
|
||||||
|
if (nextPath.startsWith("(")) {
|
||||||
|
ourLog.warn("Skipping unparseable path: " + nextPath);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
if (Character.isLowerCase(nextPath.charAt(0))) {
|
||||||
|
nextPath = nextResourceName + "." + nextPath;
|
||||||
|
}
|
||||||
|
|
||||||
List<String> pathsPart = new ArrayList<>(Arrays.asList(nextPath.split("\\.")));
|
List<String> pathsPart = new ArrayList<>(Arrays.asList(nextPath.split("\\.")));
|
||||||
BaseRuntimeElementCompositeDefinition def = null;
|
BaseRuntimeElementCompositeDefinition def = null;
|
||||||
traverse(theCtx, nextSp, pathsPart, theCtx, pathsPart, def);
|
traverse(theCtx, nextSp, pathsPart, theCtx, pathsPart, def);
|
||||||
@ -38,22 +64,37 @@ public class SearchParamFinder {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static void traverse(FhirContext theCtx, RuntimeSearchParam theSearchParam, List<String> theFullPath, FhirContext theContext, List<String> thePathsPart, BaseRuntimeElementCompositeDefinition theDef) {
|
private static void traverse(FhirContext theCtx, RuntimeSearchParam theSearchParam, List<String> theFullPath, FhirContext theContext, List<String> thePathsPart, BaseRuntimeElementCompositeDefinition theDef) {
|
||||||
|
Validate.notNull(theSearchParam);
|
||||||
|
|
||||||
if (thePathsPart.size() == 0) {
|
if (thePathsPart.size() == 0) {
|
||||||
ourLog.info("Found {} - {} - {} - {}", theCtx.getVersion().getVersion(), theSearchParam.getName(), String.join(".", theFullPath), theDef.getName());
|
ourLog.info("Found {} - {} - {} - {}", theCtx.getVersion().getVersion(), theSearchParam.getName(), String.join(".", theFullPath), theDef.getName());
|
||||||
|
handle(theCtx, theSearchParam, theDef.getName());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
String nextName = thePathsPart.get(0);
|
String nextName = thePathsPart.get(0);
|
||||||
|
if (nextName.endsWith("[x]")) {
|
||||||
|
nextName = nextName.substring(0, nextName.length() - 3);
|
||||||
|
}
|
||||||
|
|
||||||
if (theDef == null) {
|
if (theDef == null) {
|
||||||
RuntimeResourceDefinition def = theContext.getResourceDefinition(nextName);
|
Validate.notBlank(nextName, "Invalid path: " + theFullPath + " for " + theSearchParam);
|
||||||
|
RuntimeResourceDefinition def;
|
||||||
|
try {
|
||||||
|
def = theContext.getResourceDefinition(nextName);
|
||||||
|
} catch (DataFormatException e) {
|
||||||
|
throw new IllegalArgumentException("Unknown resource: " + theFullPath + " for " + theSearchParam);
|
||||||
|
}
|
||||||
traverse(theCtx, theSearchParam, theFullPath, theContext, thePathsPart.subList(1, thePathsPart.size()), def);
|
traverse(theCtx, theSearchParam, theFullPath, theContext, thePathsPart.subList(1, thePathsPart.size()), def);
|
||||||
} else {
|
} else {
|
||||||
BaseRuntimeChildDefinition child = theDef.getChildByName(nextName);
|
BaseRuntimeChildDefinition child = theDef.getChildByName(nextName);
|
||||||
if (child != null) {
|
if (child != null) {
|
||||||
BaseRuntimeElementDefinition<?> subChild = child.getChildByName(nextName);
|
BaseRuntimeElementDefinition<?> subChild = child.getChildByName(nextName);
|
||||||
|
Validate.notNull(subChild, "Unknown child: " + nextName + " in path: " + theFullPath);
|
||||||
if (subChild instanceof RuntimePrimitiveDatatypeDefinition) {
|
if (subChild instanceof RuntimePrimitiveDatatypeDefinition) {
|
||||||
assert thePathsPart.size() == 1;
|
assert thePathsPart.size() == 1;
|
||||||
ourLog.info("Found {} - {} - {} - {}", theCtx.getVersion().getVersion(), theSearchParam.getName(), String.join(".", theFullPath), subChild.getName());
|
ourLog.info("Found {} - {} - {} - {}", theCtx.getVersion().getVersion(), theSearchParam.getName(), String.join(".", theFullPath), subChild.getName());
|
||||||
|
handle(theCtx, theSearchParam, subChild.getName());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -69,6 +110,7 @@ public class SearchParamFinder {
|
|||||||
if (subChild instanceof RuntimePrimitiveDatatypeDefinition) {
|
if (subChild instanceof RuntimePrimitiveDatatypeDefinition) {
|
||||||
assert thePathsPart.size() == 1;
|
assert thePathsPart.size() == 1;
|
||||||
ourLog.info("Found {} - {} - {} - {}", theCtx.getVersion().getVersion(), theSearchParam.getName(), String.join(".", theFullPath), subChild.getName());
|
ourLog.info("Found {} - {} - {} - {}", theCtx.getVersion().getVersion(), theSearchParam.getName(), String.join(".", theFullPath), subChild.getName());
|
||||||
|
handle(theCtx, theSearchParam, subChild.getName());
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
BaseRuntimeElementCompositeDefinition def = (BaseRuntimeElementCompositeDefinition) subChild;
|
BaseRuntimeElementCompositeDefinition def = (BaseRuntimeElementCompositeDefinition) subChild;
|
||||||
@ -79,5 +121,21 @@ public class SearchParamFinder {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void handle(FhirContext theCtx, RuntimeSearchParam theSearchParam, String theName) {
|
||||||
|
BaseSearchParamExtractor extractor;
|
||||||
|
switch (theCtx.getVersion().getVersion()) {
|
||||||
|
case DSTU2:
|
||||||
|
extractor = new SearchParamExtractorDstu2();
|
||||||
|
break;
|
||||||
|
default:
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
IBase element = theCtx.getElementDefinition(theName).newInstance();
|
||||||
|
// extractor.extractSearchParamDates()
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user