Refactor OperationParameter to try and improve test coverage

This commit is contained in:
James Agnew 2016-06-14 07:11:47 -05:00
parent 1166a2ee67
commit aac914df22
2 changed files with 141 additions and 140 deletions

View File

@ -73,7 +73,7 @@ import ca.uhn.fhir.rest.api.RestOperationTypeEnum;
import ca.uhn.fhir.rest.api.SummaryEnum;
import ca.uhn.fhir.rest.api.ValidationModeEnum;
import ca.uhn.fhir.rest.client.BaseHttpClientInvocation;
import ca.uhn.fhir.rest.method.OperationParameter.IConverter;
import ca.uhn.fhir.rest.method.OperationParameter.IOperationParamConverter;
import ca.uhn.fhir.rest.param.CollectionBinder;
import ca.uhn.fhir.rest.param.DateAndListParam;
import ca.uhn.fhir.rest.param.NumberAndListParam;
@ -499,7 +499,7 @@ public class MethodUtil {
if (parameterType.equals(ValidationModeEnum.class) == false) {
throw new ConfigurationException("Parameter annotated with @" + Validate.class.getSimpleName() + "." + Validate.Mode.class.getSimpleName() + " must be of type " + ValidationModeEnum.class.getName());
}
param = new OperationParameter(theContext, Constants.EXTOP_VALIDATE, Constants.EXTOP_VALIDATE_MODE, 0, 1).setConverter(new IConverter() {
param = new OperationParameter(theContext, Constants.EXTOP_VALIDATE, Constants.EXTOP_VALIDATE_MODE, 0, 1).setConverter(new IOperationParamConverter() {
@Override
public Object incomingServer(Object theObject) {
if (isNotBlank(theObject.toString())) {
@ -522,7 +522,7 @@ public class MethodUtil {
if (parameterType.equals(String.class) == false) {
throw new ConfigurationException("Parameter annotated with @" + Validate.class.getSimpleName() + "." + Validate.Profile.class.getSimpleName() + " must be of type " + String.class.getName());
}
param = new OperationParameter(theContext, Constants.EXTOP_VALIDATE, Constants.EXTOP_VALIDATE_PROFILE, 0, 1).setConverter(new IConverter() {
param = new OperationParameter(theContext, Constants.EXTOP_VALIDATE, Constants.EXTOP_VALIDATE_PROFILE, 0, 1).setConverter(new IOperationParamConverter() {
@Override
public Object incomingServer(Object theObject) {
return theObject.toString();

View File

@ -63,8 +63,9 @@ import ca.uhn.fhir.rest.server.exceptions.InvalidRequestException;
import ca.uhn.fhir.rest.server.exceptions.MethodNotAllowedException;
import ca.uhn.fhir.util.FhirTerser;
import ca.uhn.fhir.util.ParametersUtil;
import ca.uhn.fhir.util.ReflectionUtil;
public class OperationParameter implements IParameter {
class OperationParameter implements IParameter {
@SuppressWarnings("unchecked")
private static final Class<? extends IQueryParameterType>[] COMPOSITE_TYPES = new Class[0];
@ -74,7 +75,7 @@ public class OperationParameter implements IParameter {
private boolean myAllowGet;
private final FhirContext myContext;
private IConverter myConverter;
private IOperationParamConverter myConverter;
@SuppressWarnings("rawtypes")
private Class<? extends Collection> myInnerCollectionType;
private int myMax;
@ -203,7 +204,7 @@ public class OperationParameter implements IParameter {
mySearchParameterBinding = new SearchParameter(myName, myMin > 0);
mySearchParameterBinding.setCompositeTypes(COMPOSITE_TYPES);
mySearchParameterBinding.setType(myContext, theParameterType, theInnerCollectionType, theOuterCollectionType);
myConverter = new QueryParameterConverter();
myConverter = new OperationParamConverter();
} else {
throw new ConfigurationException("Invalid type for @OperationParam: " + myParameterType.getName());
}
@ -212,7 +213,7 @@ public class OperationParameter implements IParameter {
}
public OperationParameter setConverter(IConverter theConverter) {
public OperationParameter setConverter(IOperationParamConverter theConverter) {
myConverter = theConverter;
return this;
}
@ -242,6 +243,25 @@ public class OperationParameter implements IParameter {
List<Object> matchingParamValues = new ArrayList<Object>();
if (theRequest.getRequestType() == RequestTypeEnum.GET) {
translateQueryParametersIntoServerArgumentForGet(theRequest, matchingParamValues);
} else {
translateQueryParametersIntoServerArgumentForPost(theRequest, matchingParamValues);
}
if (matchingParamValues.isEmpty()) {
return null;
}
if (myInnerCollectionType == null) {
return matchingParamValues.get(0);
}
Collection<Object> retVal = ReflectionUtil.newInstance(myInnerCollectionType);
retVal.addAll(matchingParamValues);
return retVal;
}
private void translateQueryParametersIntoServerArgumentForGet(RequestDetails theRequest, List<Object> matchingParamValues) {
if (mySearchParameterBinding != null) {
List<QualifiedParamList> params = new ArrayList<QualifiedParamList>();
@ -318,9 +338,9 @@ public class OperationParameter implements IParameter {
}
}
}
}
} else {
private void translateQueryParametersIntoServerArgumentForPost(RequestDetails theRequest, List<Object> matchingParamValues) {
IBaseResource requestContents = (IBaseResource) theRequest.getUserData().get(REQUEST_CONTENTS_USERDATA_KEY);
RuntimeResourceDefinition def = myContext.getResourceDefinition(requestContents);
if (def.getName().equals("Parameters")) {
@ -365,29 +385,6 @@ public class OperationParameter implements IParameter {
}
}
if (matchingParamValues.isEmpty()) {
return null;
}
if (myInnerCollectionType == null) {
return matchingParamValues.get(0);
}
try {
Collection<Object> retVal = myInnerCollectionType.newInstance();
retVal.addAll(matchingParamValues);
return retVal;
} catch (InstantiationException e) {
throw new InternalErrorException("Failed to instantiate " + myInnerCollectionType, e);
} catch (IllegalAccessException e) {
throw new InternalErrorException("Failed to instantiate " + myInnerCollectionType, e);
}
}
public static void throwInvalidMode(String paramValues) {
throw new InvalidRequestException("Invalid mode value: \"" + paramValues + "\"");
}
@SuppressWarnings("unchecked")
private void tryToAddValues(List<IBase> theParamValues, List<Object> theMatchingParamValues) {
for (Object nextValue : theParamValues) {
@ -419,7 +416,11 @@ public class OperationParameter implements IParameter {
}
}
public interface IConverter {
public static void throwInvalidMode(String paramValues) {
throw new InvalidRequestException("Invalid mode value: \"" + paramValues + "\"");
}
interface IOperationParamConverter {
Object incomingServer(Object theObject);
@ -427,9 +428,9 @@ public class OperationParameter implements IParameter {
}
private class QueryParameterConverter implements IConverter {
class OperationParamConverter implements IOperationParamConverter {
public QueryParameterConverter() {
public OperationParamConverter() {
Validate.isTrue(mySearchParameterBinding != null);
}