Fix unit tests
This commit is contained in:
parent
6b60703a74
commit
b8dd4fa9f2
|
@ -30,7 +30,7 @@ public class SearchMethodBinding extends BaseMethodBinding {
|
||||||
private Method method;
|
private Method method;
|
||||||
|
|
||||||
private Class<?> myDeclaredResourceType;
|
private Class<?> myDeclaredResourceType;
|
||||||
private List<SearchParameter> myParameters;
|
private List<IParameter> myParameters;
|
||||||
|
|
||||||
public SearchMethodBinding(MethodReturnTypeEnum theMethodReturnTypeEnum, Class<? extends IResource> theReturnResourceType, Method theMethod) {
|
public SearchMethodBinding(MethodReturnTypeEnum theMethodReturnTypeEnum, Class<? extends IResource> theReturnResourceType, Method theMethod) {
|
||||||
super(theMethodReturnTypeEnum, theReturnResourceType);
|
super(theMethodReturnTypeEnum, theReturnResourceType);
|
||||||
|
@ -48,7 +48,7 @@ public class SearchMethodBinding extends BaseMethodBinding {
|
||||||
return method;
|
return method;
|
||||||
}
|
}
|
||||||
|
|
||||||
public List<SearchParameter> getParameters() {
|
public List<IParameter> getParameters() {
|
||||||
return myParameters;
|
return myParameters;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,7 +65,7 @@ public class SearchMethodBinding extends BaseMethodBinding {
|
||||||
|
|
||||||
for (int idx = 0; idx < theArgs.length; idx++) {
|
for (int idx = 0; idx < theArgs.length; idx++) {
|
||||||
Object object = theArgs[idx];
|
Object object = theArgs[idx];
|
||||||
SearchParameter nextParam = myParameters.get(idx);
|
IParameter nextParam = myParameters.get(idx);
|
||||||
|
|
||||||
if (object == null) {
|
if (object == null) {
|
||||||
if (nextParam.isRequired()) {
|
if (nextParam.isRequired()) {
|
||||||
|
@ -156,7 +156,7 @@ public class SearchMethodBinding extends BaseMethodBinding {
|
||||||
|
|
||||||
Set<String> methodParamsTemp = new HashSet<String>();
|
Set<String> methodParamsTemp = new HashSet<String>();
|
||||||
for (int i = 0; i < this.myParameters.size(); i++) {
|
for (int i = 0; i < this.myParameters.size(); i++) {
|
||||||
SearchParameter temp = this.myParameters.get(i);
|
IParameter temp = this.myParameters.get(i);
|
||||||
methodParamsTemp.add(temp.getName());
|
methodParamsTemp.add(temp.getName());
|
||||||
if (temp.isRequired() && !theRequest.getParameterNames().contains(temp.getName())) {
|
if (temp.isRequired() && !theRequest.getParameterNames().contains(temp.getName())) {
|
||||||
ourLog.trace("Method {} doesn't match param '{}' is not present", method.getName(), temp.getName());
|
ourLog.trace("Method {} doesn't match param '{}' is not present", method.getName(), temp.getName());
|
||||||
|
@ -174,7 +174,7 @@ public class SearchMethodBinding extends BaseMethodBinding {
|
||||||
this.method = method;
|
this.method = method;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setParameters(List<SearchParameter> parameters) {
|
public void setParameters(List<IParameter> parameters) {
|
||||||
this.myParameters = parameters;
|
this.myParameters = parameters;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -13,4 +13,6 @@ public interface IParameter {
|
||||||
|
|
||||||
public abstract Object parse(List<List<String>> theString) throws InternalErrorException, InvalidRequestException;
|
public abstract Object parse(List<List<String>> theString) throws InternalErrorException, InvalidRequestException;
|
||||||
|
|
||||||
|
public abstract boolean isRequired();
|
||||||
|
|
||||||
}
|
}
|
|
@ -25,4 +25,10 @@ public class IncludeParameter implements IParameter {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isRequired() {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,11 +10,14 @@ import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
|
||||||
|
import ch.qos.logback.core.joran.action.ParamAction;
|
||||||
|
import ca.uhn.fhir.context.ConfigurationException;
|
||||||
import ca.uhn.fhir.rest.annotation.Include;
|
import ca.uhn.fhir.rest.annotation.Include;
|
||||||
import ca.uhn.fhir.rest.annotation.Optional;
|
import ca.uhn.fhir.rest.annotation.Optional;
|
||||||
import ca.uhn.fhir.rest.annotation.Read;
|
import ca.uhn.fhir.rest.annotation.Read;
|
||||||
import ca.uhn.fhir.rest.annotation.Required;
|
import ca.uhn.fhir.rest.annotation.Required;
|
||||||
import ca.uhn.fhir.rest.param.IParameter;
|
import ca.uhn.fhir.rest.param.IParameter;
|
||||||
|
import ca.uhn.fhir.rest.param.IncludeParameter;
|
||||||
import ca.uhn.fhir.rest.param.SearchParameter;
|
import ca.uhn.fhir.rest.param.SearchParameter;
|
||||||
import ca.uhn.fhir.util.ReflectionUtil;
|
import ca.uhn.fhir.util.ReflectionUtil;
|
||||||
|
|
||||||
|
@ -44,25 +47,25 @@ public class Util {
|
||||||
List<IParameter> parameters = new ArrayList<IParameter>();
|
List<IParameter> parameters = new ArrayList<IParameter>();
|
||||||
|
|
||||||
Class<?>[] parameterTypes = method.getParameterTypes();
|
Class<?>[] parameterTypes = method.getParameterTypes();
|
||||||
|
int paramIndex = 0;
|
||||||
for (Annotation[] annotations : method.getParameterAnnotations()) {
|
for (Annotation[] annotations : method.getParameterAnnotations()) {
|
||||||
|
boolean haveHandledMethod = false;
|
||||||
for (int i = 0; i < annotations.length; i++) {
|
for (int i = 0; i < annotations.length; i++) {
|
||||||
Annotation nextAnnotation = annotations[i];
|
Annotation nextAnnotation = annotations[i];
|
||||||
Class<?> parameterType = parameterTypes[i];
|
Class<?> parameterType = parameterTypes[paramIndex];
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
Class<? extends java.util.Collection<?>> outerCollectionType = null;
|
Class<? extends java.util.Collection<?>> outerCollectionType = null;
|
||||||
Class<? extends java.util.Collection<?>> innerCollectionType = null;
|
Class<? extends java.util.Collection<?>> innerCollectionType = null;
|
||||||
|
|
||||||
if (Collection.class.isAssignableFrom(parameterType)) {
|
if (Collection.class.isAssignableFrom(parameterType)) {
|
||||||
innerCollectionType = (Class<? extends java.util.Collection<?>>) parameterType;
|
innerCollectionType = (Class<? extends java.util.Collection<?>>) parameterType;
|
||||||
parameterType = ReflectionUtil.getGenericCollectionTypeOfMethodParameter(method, i);
|
parameterType = ReflectionUtil.getGenericCollectionTypeOfMethodParameter(method, paramIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (Collection.class.isAssignableFrom(parameterType)) {
|
if (Collection.class.isAssignableFrom(parameterType)) {
|
||||||
outerCollectionType = innerCollectionType;
|
outerCollectionType = innerCollectionType;
|
||||||
innerCollectionType = (Class<? extends java.util.Collection<?>>) parameterType;
|
innerCollectionType = (Class<? extends java.util.Collection<?>>) parameterType;
|
||||||
parameterType = ReflectionUtil.getGenericCollectionTypeOfMethodParameter(method, i);
|
parameterType = ReflectionUtil.getGenericCollectionTypeOfMethodParameter(method, paramIndex);
|
||||||
}
|
}
|
||||||
|
|
||||||
IParameter param;
|
IParameter param;
|
||||||
|
@ -71,17 +74,33 @@ public class Util {
|
||||||
parameter.setName(((Required) nextAnnotation).name());
|
parameter.setName(((Required) nextAnnotation).name());
|
||||||
parameter.setRequired(true);
|
parameter.setRequired(true);
|
||||||
parameter.setType(parameterType, innerCollectionType, outerCollectionType);
|
parameter.setType(parameterType, innerCollectionType, outerCollectionType);
|
||||||
|
param = parameter;
|
||||||
} else if (nextAnnotation instanceof Optional) {
|
} else if (nextAnnotation instanceof Optional) {
|
||||||
SearchParameter parameter = new SearchParameter();
|
SearchParameter parameter = new SearchParameter();
|
||||||
parameter.setName(((Optional) nextAnnotation).name());
|
parameter.setName(((Optional) nextAnnotation).name());
|
||||||
parameter.setRequired(false);
|
parameter.setRequired(false);
|
||||||
parameter.setType(parameterType, innerCollectionType, innerCollectionType);
|
parameter.setType(parameterType, innerCollectionType, innerCollectionType);
|
||||||
|
param = parameter;
|
||||||
} else if (nextAnnotation instanceof Include) {
|
} else if (nextAnnotation instanceof Include) {
|
||||||
|
if (parameterType != String.class) {
|
||||||
|
throw new ConfigurationException("Method '" + method.getName() + "' is annotated with @" + Include.class.getSimpleName() + " but has a type other than Collection<String>");
|
||||||
|
}
|
||||||
|
// if (innerCollectionType)
|
||||||
|
|
||||||
|
param = new IncludeParameter();
|
||||||
|
} else {
|
||||||
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
haveHandledMethod= true;
|
||||||
parameters.add(param);
|
parameters.add(param);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!haveHandledMethod) {
|
||||||
|
throw new ConfigurationException("Parameter # " + paramIndex + " of method '" + method.getName() + "' has no recognized FHIR interface parameter annotations. Don't know how to handle this parameter!");
|
||||||
|
}
|
||||||
|
|
||||||
|
paramIndex++;
|
||||||
}
|
}
|
||||||
return parameters;
|
return parameters;
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,6 +15,7 @@ import ca.uhn.fhir.rest.method.BaseMethodBinding.MethodReturnTypeEnum;
|
||||||
import ca.uhn.fhir.rest.method.Request;
|
import ca.uhn.fhir.rest.method.Request;
|
||||||
import ca.uhn.fhir.rest.method.SearchMethodBinding;
|
import ca.uhn.fhir.rest.method.SearchMethodBinding;
|
||||||
import ca.uhn.fhir.rest.method.SearchMethodBinding.RequestType;
|
import ca.uhn.fhir.rest.method.SearchMethodBinding.RequestType;
|
||||||
|
import ca.uhn.fhir.rest.param.IParameter;
|
||||||
import ca.uhn.fhir.rest.param.SearchParameter;
|
import ca.uhn.fhir.rest.param.SearchParameter;
|
||||||
|
|
||||||
public class ResourceMethodTest {
|
public class ResourceMethodTest {
|
||||||
|
@ -28,7 +29,7 @@ public class ResourceMethodTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRequiredParamsMissing() {
|
public void testRequiredParamsMissing() {
|
||||||
List<SearchParameter> methodParams = new ArrayList<SearchParameter>();
|
List<IParameter> methodParams = new ArrayList<IParameter>();
|
||||||
|
|
||||||
methodParams.add(new SearchParameter("firstName", false));
|
methodParams.add(new SearchParameter("firstName", false));
|
||||||
methodParams.add(new SearchParameter("lastName", false));
|
methodParams.add(new SearchParameter("lastName", false));
|
||||||
|
@ -45,7 +46,7 @@ public class ResourceMethodTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testRequiredParamsOnly() {
|
public void testRequiredParamsOnly() {
|
||||||
List<SearchParameter> methodParams = new ArrayList<SearchParameter>();
|
List<IParameter> methodParams = new ArrayList<IParameter>();
|
||||||
|
|
||||||
methodParams.add(new SearchParameter("firstName", false));
|
methodParams.add(new SearchParameter("firstName", false));
|
||||||
methodParams.add(new SearchParameter("lastName", false));
|
methodParams.add(new SearchParameter("lastName", false));
|
||||||
|
@ -60,7 +61,7 @@ public class ResourceMethodTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testMixedParams() {
|
public void testMixedParams() {
|
||||||
List<SearchParameter> methodParams = new ArrayList<SearchParameter>();
|
List<IParameter> methodParams = new ArrayList<IParameter>();
|
||||||
|
|
||||||
methodParams.add(new SearchParameter("firstName", false));
|
methodParams.add(new SearchParameter("firstName", false));
|
||||||
methodParams.add(new SearchParameter("lastName", false));
|
methodParams.add(new SearchParameter("lastName", false));
|
||||||
|
@ -77,7 +78,7 @@ public class ResourceMethodTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAllParams() {
|
public void testAllParams() {
|
||||||
List<SearchParameter> methodParams = new ArrayList<SearchParameter>();
|
List<IParameter> methodParams = new ArrayList<IParameter>();
|
||||||
|
|
||||||
methodParams.add(new SearchParameter("firstName", false));
|
methodParams.add(new SearchParameter("firstName", false));
|
||||||
methodParams.add(new SearchParameter("lastName", false));
|
methodParams.add(new SearchParameter("lastName", false));
|
||||||
|
@ -95,7 +96,7 @@ public class ResourceMethodTest {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testAllParamsWithExtra() {
|
public void testAllParamsWithExtra() {
|
||||||
List<SearchParameter> methodParams = new ArrayList<SearchParameter>();
|
List<IParameter> methodParams = new ArrayList<IParameter>();
|
||||||
|
|
||||||
methodParams.add(new SearchParameter("firstName", false));
|
methodParams.add(new SearchParameter("firstName", false));
|
||||||
methodParams.add(new SearchParameter("lastName", false));
|
methodParams.add(new SearchParameter("lastName", false));
|
||||||
|
|
Loading…
Reference in New Issue