### Question:
CharsetResponseFilter implements ContainerResponseFilter { @Override public void filter(final ContainerRequestContext requestContext, final ContainerResponseContext responseContext) throws IOException { final MediaType type = responseContext.getMediaType(); if (type != null && !type.getParameters().containsKey(MediaType.CHARSET_PARAMETER)) { responseContext.getHeaders().putSingle(HttpHeaders.CONTENT_TYPE, type.withCharset(DEFAULT_CHARSET)); } }  @Override void filter(final ContainerRequestContext requestContext, final ContainerResponseContext responseContext);  }### Answer:
@Test public void shouldAddCharsetParameter_onFilter() throws Exception { final ContainerRequestContext mockContainerRequestContext = mock(ContainerRequestContext.class); final ContainerResponseContext mockContainerResponseContext = mock(ContainerResponseContext.class); final MediaType mockMediaType = mock(MediaType.class); when(mockContainerResponseContext.getMediaType()).thenReturn(mockMediaType); final MediaType mockTypeWithCharset = mock(MediaType.class); when(mockMediaType.withCharset("utf-8")).thenReturn(mockTypeWithCharset); final HashMap<String, String> parameters = new HashMap<String, String>(); when(mockMediaType.getParameters()).thenReturn(parameters); final MultivaluedMap<String, Object> mockHeadersMap = mock(MultivaluedMap.class); when(mockContainerResponseContext.getHeaders()).thenReturn(mockHeadersMap); final CharsetResponseFilter charsetResponseFilter = new CharsetResponseFilter(); charsetResponseFilter.filter(mockContainerRequestContext, mockContainerResponseContext); verify(mockHeadersMap).putSingle(HttpHeaders.CONTENT_TYPE, mockTypeWithCharset); }
@Test public void shouldNotOverrideCharsetParameter_onFilter() throws Exception { final ContainerRequestContext mockContainerRequestContext = mock(ContainerRequestContext.class); final ContainerResponseContext mockContainerResponseContext = mock(ContainerResponseContext.class); final MediaType mockMediaType = mock(MediaType.class); when(mockContainerResponseContext.getMediaType()).thenReturn(mockMediaType); final HashMap<String, String> parameters = new HashMap<String, String>(); parameters.put(MediaType.CHARSET_PARAMETER, randomString(10)); when(mockMediaType.getParameters()).thenReturn(parameters); final MultivaluedMap<String, Object> mockHeadersMap = mock(MultivaluedMap.class); when(mockContainerResponseContext.getHeaders()).thenReturn(mockHeadersMap); final CharsetResponseFilter charsetResponseFilter = new CharsetResponseFilter(); charsetResponseFilter.filter(mockContainerRequestContext, mockContainerResponseContext); verifyZeroInteractions(mockHeadersMap); }
@Test public void shouldNotSetContentTypeOnResponseWithoutMediaType_onFilter() throws Exception { final ContainerRequestContext mockContainerRequestContext = mock(ContainerRequestContext.class); final ContainerResponseContext mockContainerResponseContext = mock(ContainerResponseContext.class); when(mockContainerResponseContext.getMediaType()).thenReturn(null); final MultivaluedMap<String, Object> mockHeadersMap = mock(MultivaluedMap.class); when(mockContainerResponseContext.getHeaders()).thenReturn(mockHeadersMap); final CharsetResponseFilter charsetResponseFilter = new CharsetResponseFilter(); charsetResponseFilter.filter(mockContainerRequestContext, mockContainerResponseContext); verifyZeroInteractions(mockHeadersMap); }