TracingFilter implements Filter { @Override public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException { if (request instanceof HttpServletRequest) { HttpServletRequest req = (HttpServletRequest) request; doFilterHttp(req, response, chain); } else { chain.doFilter(request, response); } }  @Override void init(FilterConfig filterConfig); @Override void doFilter(ServletRequest request, ServletResponse response, FilterChain chain); @Override void destroy(); static final String RATEL_HEADER_PROCESS_ID; }
@Test public void shouldFilterNonHttpRequests() throws Exception { filter.doFilter(request, response, chainMock); verify(chainMock).doFilter(eq(request), eq(response)); }
@Test public void shouldRewriteProcessIdFromHeaderToProcessContext() throws Exception { when(httpRequest.getHeader(eq(TracingFilter.RATEL_HEADER_PROCESS_ID))).thenReturn("123"); assertThat(ProcessContext.getInstance().getProcessIdentifier()).isNull(); filter.doFilter(httpRequest, httpResponse, chainStub); assertThat(ProcessContext.getInstance().getProcessIdentifier()).isNull(); assertThat(processIdInsideFilter).isEqualTo("123"); }
@Test public void shouldGenerateProcessIdWhenNotSetInHeader() throws Exception { assertThat(ProcessContext.getInstance().getProcessIdentifier()).isNull(); filter.doFilter(httpRequest, httpResponse, chainStub); assertThat(ProcessContext.getInstance().getProcessIdentifier()).isNull(); assertThat(processIdInsideFilter).isNotNull(); }
@Test public void shouldClearProcessIdAfterExceptionInFilter() throws Exception { assertThat(ProcessContext.getInstance().getProcessIdentifier()).isNull(); Mockito.doThrow(NumberFormatException.class).when(chainMock).doFilter(any(ServletRequest.class), any(ServletResponse.class)); try { filter.doFilter(httpRequest, httpResponse, chainMock); } catch (NumberFormatException e) { } assertThat(ProcessContext.getInstance().getProcessIdentifier()).isNull(); }