From b8dd4fa9f2ff1e0b6b15fcd922d6b1b4fa72e554 Mon Sep 17 00:00:00 2001 From: jamesagnew Date: Tue, 18 Mar 2014 11:54:20 -0400 Subject: [PATCH] Fix unit tests --- .../fhir/rest/method/SearchMethodBinding.java | 10 +++---- .../ca/uhn/fhir/rest/param/IParameter.java | 2 ++ .../uhn/fhir/rest/param/IncludeParameter.java | 6 ++++ .../java/ca/uhn/fhir/rest/server/Util.java | 29 +++++++++++++++---- .../fhir/rest/server/ResourceMethodTest.java | 11 +++---- 5 files changed, 43 insertions(+), 15 deletions(-) diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/SearchMethodBinding.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/SearchMethodBinding.java index 9970973d771..a16271547fa 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/SearchMethodBinding.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/method/SearchMethodBinding.java @@ -30,7 +30,7 @@ public class SearchMethodBinding extends BaseMethodBinding { private Method method; private Class myDeclaredResourceType; - private List myParameters; + private List myParameters; public SearchMethodBinding(MethodReturnTypeEnum theMethodReturnTypeEnum, Class theReturnResourceType, Method theMethod) { super(theMethodReturnTypeEnum, theReturnResourceType); @@ -48,7 +48,7 @@ public class SearchMethodBinding extends BaseMethodBinding { return method; } - public List getParameters() { + public List 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 methodParamsTemp = new HashSet(); 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 parameters) { + public void setParameters(List parameters) { this.myParameters = parameters; } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/IParameter.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/IParameter.java index 7531185bf0e..95e40f8904d 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/IParameter.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/IParameter.java @@ -13,4 +13,6 @@ public interface IParameter { public abstract Object parse(List> theString) throws InternalErrorException, InvalidRequestException; + public abstract boolean isRequired(); + } \ No newline at end of file diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/IncludeParameter.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/IncludeParameter.java index d13f3e76f8c..a27725447d5 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/IncludeParameter.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/param/IncludeParameter.java @@ -25,4 +25,10 @@ public class IncludeParameter implements IParameter { return null; } + @Override + public boolean isRequired() { + // TODO Auto-generated method stub + return false; + } + } diff --git a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/Util.java b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/Util.java index c92e74fecc7..1f7b5ce4383 100644 --- a/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/Util.java +++ b/hapi-fhir-base/src/main/java/ca/uhn/fhir/rest/server/Util.java @@ -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 parameters = new ArrayList(); 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> outerCollectionType = null; Class> innerCollectionType = null; if (Collection.class.isAssignableFrom(parameterType)) { innerCollectionType = (Class>) parameterType; - parameterType = ReflectionUtil.getGenericCollectionTypeOfMethodParameter(method, i); + parameterType = ReflectionUtil.getGenericCollectionTypeOfMethodParameter(method, paramIndex); } if (Collection.class.isAssignableFrom(parameterType)) { outerCollectionType = innerCollectionType; innerCollectionType = (Class>) 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"); + } +// 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; } diff --git a/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/server/ResourceMethodTest.java b/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/server/ResourceMethodTest.java index 3e2797f29ad..b35fd182f9c 100644 --- a/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/server/ResourceMethodTest.java +++ b/hapi-fhir-base/src/test/java/ca/uhn/fhir/rest/server/ResourceMethodTest.java @@ -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 methodParams = new ArrayList(); + List methodParams = new ArrayList(); methodParams.add(new SearchParameter("firstName", false)); methodParams.add(new SearchParameter("lastName", false)); @@ -45,7 +46,7 @@ public class ResourceMethodTest { @Test public void testRequiredParamsOnly() { - List methodParams = new ArrayList(); + List methodParams = new ArrayList(); methodParams.add(new SearchParameter("firstName", false)); methodParams.add(new SearchParameter("lastName", false)); @@ -60,7 +61,7 @@ public class ResourceMethodTest { @Test public void testMixedParams() { - List methodParams = new ArrayList(); + List methodParams = new ArrayList(); methodParams.add(new SearchParameter("firstName", false)); methodParams.add(new SearchParameter("lastName", false)); @@ -77,7 +78,7 @@ public class ResourceMethodTest { @Test public void testAllParams() { - List methodParams = new ArrayList(); + List methodParams = new ArrayList(); methodParams.add(new SearchParameter("firstName", false)); methodParams.add(new SearchParameter("lastName", false)); @@ -95,7 +96,7 @@ public class ResourceMethodTest { @Test public void testAllParamsWithExtra() { - List methodParams = new ArrayList(); + List methodParams = new ArrayList(); methodParams.add(new SearchParameter("firstName", false)); methodParams.add(new SearchParameter("lastName", false));