Object[] args = invocation.getArguments();
FilterRequestContext requestContext = (FilterRequestContext) args[0];
requestContext.getFilterScratchpad().put(spKey, spValue);
if (requestContext.getMethodType() == ResourceMethod.CREATE)
{
RecordTemplate entity = requestContext.getRequestData().getEntity();
if (entity != null && entity instanceof Greeting)
{
Greeting greeting = (Greeting) entity;
if (greeting.hasTone())
{
Tone tone = greeting.getTone();
if (tone == Tone.INSULTING)
{
throw new RestLiServiceException(REQ_FILTER_ERROR_STATUS, REQ_FILTER_ERROR_MESSAGE);
}
greeting.setTone(mapToneForIncomingRequest(tone));
}
}
}
return null;
}
}).when(_requestFilter).onRequest(any(FilterRequestContext.class));
List<RequestFilter> reqFilters = Arrays.asList(_requestFilter);
List<ResponseFilter> respFilters = null;
if (responseFilter)
{
reset(_responseFilter);
doAnswer(new Answer<Object>()
{
@Override
public Object answer(InvocationOnMock invocation) throws Throwable
{
Object[] args = invocation.getArguments();
FilterRequestContext requestContext = (FilterRequestContext) args[0];
FilterResponseContext responseContext = (FilterResponseContext) args[1];
// Verify the scratch pad value.
assertTrue(requestContext.getFilterScratchpad().get(spKey) == spValue);
RecordTemplate entity = responseContext.getResponseData().getEntityResponse();
if (entity != null && requestContext.getMethodType() == ResourceMethod.GET
&& responseContext.getHttpStatus() == HttpStatus.S_200_OK)
{
Greeting greeting = new Greeting(entity.data());
if (greeting.hasTone())
{
greeting.setTone(mapToneForOutgoingResponse(greeting.getTone()));
responseContext.getResponseData().setEntityResponse(greeting);
}