FlashInterceptor implements Interceptor { public void intercept(InterceptorStack stack, ResourceMethod method, Object resourceInstance) throws InterceptionException { @SuppressWarnings("unchecked") Map<String, Object> parameters = (Map<String, Object>) session.getAttribute(FLASH_INCLUDED_PARAMETERS); if (parameters != null) { session.removeAttribute(FLASH_INCLUDED_PARAMETERS); for (Entry<String, Object> parameter : parameters.entrySet()) { result.include(parameter.getKey(), parameter.getValue()); } } response.addRedirectListener(new RedirectListener() { public void beforeRedirect() { Map<String, Object> included = result.included(); if (!included.isEmpty()) { try { session.setAttribute(FLASH_INCLUDED_PARAMETERS, included); } catch (IllegalStateException e) { LOGGER.info("HTTP Session was invalidated. It is not possible to include " + "Result parameters on Flash Scope"); } } } }); stack.next(method, resourceInstance); } FlashInterceptor(HttpSession session, Result result, MutableResponse response); boolean accepts(ResourceMethod method); void intercept(InterceptorStack stack, ResourceMethod method, Object resourceInstance);  }
@Test public void shouldDoNothingWhenThereIsNoFlashParameters() throws Exception { when(session.getAttribute(FLASH_INCLUDED_PARAMETERS)).thenReturn(null); interceptor.intercept(stack, null, null); verifyZeroInteractions(result); }
@Test public void shouldAddAllFlashParametersToResult() throws Exception { when(session.getAttribute(FLASH_INCLUDED_PARAMETERS)).thenReturn(singletonMap("Abc", 1002)); interceptor.intercept(stack, null, null); verify(result).include("Abc", 1002); }
@Test public void shouldRemoveFlashIncludedParameters() throws Exception { when(session.getAttribute(FLASH_INCLUDED_PARAMETERS)).thenReturn(singletonMap("Abc", 1002)); interceptor.intercept(stack, null, null); verify(session).removeAttribute(FLASH_INCLUDED_PARAMETERS); }
@Test public void shouldIncludeFlashParametersWhenARedirectHappens() throws Exception { Map<String, Object> parameters = Collections.<String, Object>singletonMap("Abc", 1002); when(result.included()).thenReturn(parameters); when(session.getAttribute(FLASH_INCLUDED_PARAMETERS)).thenReturn(null); interceptor.intercept(stack, null, null); response.sendRedirect("Anything"); verify(session).setAttribute(FLASH_INCLUDED_PARAMETERS, parameters); }
@Test public void shouldNotIncludeFlashParametersWhenThereIsNoIncludedParameter() throws Exception { Map<String, Object> parameters = Collections.emptyMap(); when(result.included()).thenReturn(parameters); when(session.getAttribute(FLASH_INCLUDED_PARAMETERS)).thenReturn(null); interceptor.intercept(stack, null, null); response.sendRedirect("Anything"); verify(session, never()).setAttribute(anyString(), anyObject()); }
@Test public void shouldNotCrashWhenSessionIsInvalid() throws Exception { Map<String, Object> parameters = Collections.<String, Object>singletonMap("Abc", 1002); when(result.included()).thenReturn(parameters); doThrow(new IllegalStateException()).when(session).setAttribute(FLASH_INCLUDED_PARAMETERS, parameters); when(session.getAttribute(FLASH_INCLUDED_PARAMETERS)).thenReturn(null); interceptor.intercept(stack, null, null); response.sendRedirect("Anything"); }