Fix unit tests

This commit is contained in:
jamesagnew 2014-03-18 11:54:20 -04:00
parent 6b60703a74
commit b8dd4fa9f2
5 changed files with 43 additions and 15 deletions

View File

@ -30,7 +30,7 @@ public class SearchMethodBinding extends BaseMethodBinding {
private Method method;
private Class<?> myDeclaredResourceType;
private List<SearchParameter> myParameters;
private List<IParameter> myParameters;
public SearchMethodBinding(MethodReturnTypeEnum theMethodReturnTypeEnum, Class<? extends IResource> theReturnResourceType, Method theMethod) {
super(theMethodReturnTypeEnum, theReturnResourceType);
@ -48,7 +48,7 @@ public class SearchMethodBinding extends BaseMethodBinding {
return method;
}
public List<SearchParameter> getParameters() {
public List<IParameter> getParameters() {
return myParameters;
}
@ -65,7 +65,7 @@ public class SearchMethodBinding extends BaseMethodBinding {
for (int idx = 0; idx < theArgs.length; idx++) {
Object object = theArgs[idx];
SearchParameter nextParam = myParameters.get(idx);
IParameter nextParam = myParameters.get(idx);
if (object == null) {
if (nextParam.isRequired()) {
@ -156,7 +156,7 @@ public class SearchMethodBinding extends BaseMethodBinding {
Set<String> methodParamsTemp = new HashSet<String>();
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());
if (temp.isRequired() && !theRequest.getParameterNames().contains(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;
}
public void setParameters(List<SearchParameter> parameters) {
public void setParameters(List<IParameter> parameters) {
this.myParameters = parameters;
}

View File

@ -13,4 +13,6 @@ public interface IParameter {
public abstract Object parse(List<List<String>> theString) throws InternalErrorException, InvalidRequestException;
public abstract boolean isRequired();
}

View File

@ -25,4 +25,10 @@ public class IncludeParameter implements IParameter {
return null;
}
@Override
public boolean isRequired() {
// TODO Auto-generated method stub
return false;
}
}

View File

@ -10,11 +10,14 @@ import java.util.HashMap;
import java.util.List;
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.Optional;
import ca.uhn.fhir.rest.annotation.Read;
import ca.uhn.fhir.rest.annotation.Required;
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.util.ReflectionUtil;
@ -44,25 +47,25 @@ public class Util {
List<IParameter> parameters = new ArrayList<IParameter>();
Class<?>[] parameterTypes = method.getParameterTypes();
int paramIndex = 0;
for (Annotation[] annotations : method.getParameterAnnotations()) {
boolean haveHandledMethod = false;
for (int i = 0; i < annotations.length; 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<?>> innerCollectionType = null;
if (Collection.class.isAssignableFrom(parameterType)) {
innerCollectionType = (Class<? extends java.util.Collection<?>>) parameterType;
parameterType = ReflectionUtil.getGenericCollectionTypeOfMethodParameter(method, i);
parameterType = ReflectionUtil.getGenericCollectionTypeOfMethodParameter(method, paramIndex);
}
if (Collection.class.isAssignableFrom(parameterType)) {
outerCollectionType = innerCollectionType;
innerCollectionType = (Class<? extends java.util.Collection<?>>) parameterType;
parameterType = ReflectionUtil.getGenericCollectionTypeOfMethodParameter(method, i);
parameterType = ReflectionUtil.getGenericCollectionTypeOfMethodParameter(method, paramIndex);
}
IParameter param;
@ -71,17 +74,33 @@ public class Util {
parameter.setName(((Required) nextAnnotation).name());
parameter.setRequired(true);
parameter.setType(parameterType, innerCollectionType, outerCollectionType);
param = parameter;
} else if (nextAnnotation instanceof Optional) {
SearchParameter parameter = new SearchParameter();
parameter.setName(((Optional) nextAnnotation).name());
parameter.setRequired(false);
parameter.setType(parameterType, innerCollectionType, innerCollectionType);
param = parameter;
} 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);
}
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;
}

View File

@ -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.SearchMethodBinding;
import ca.uhn.fhir.rest.method.SearchMethodBinding.RequestType;
import ca.uhn.fhir.rest.param.IParameter;
import ca.uhn.fhir.rest.param.SearchParameter;
public class ResourceMethodTest {
@ -28,7 +29,7 @@ public class ResourceMethodTest {
@Test
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("lastName", false));
@ -45,7 +46,7 @@ public class ResourceMethodTest {
@Test
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("lastName", false));
@ -60,7 +61,7 @@ public class ResourceMethodTest {
@Test
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("lastName", false));
@ -77,7 +78,7 @@ public class ResourceMethodTest {
@Test
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("lastName", false));
@ -95,7 +96,7 @@ public class ResourceMethodTest {
@Test
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("lastName", false));