### Question:
ContentTypeNormaliserImpl implements ContentTypeNormaliser { @Override public MediaType getNormalisedRequestMediaType(HttpServletRequest request) { String contentType = request.getContentType(); if (request.getMethod().equals("POST")) { if (contentType == null) { throw new CougarValidationException(ServerFaultCode.ContentTypeNotValid, "Input content type was not specified for deserialisable response"); } MediaType requestMT; try { requestMT = MediaType.valueOf(contentType); } catch (Exception e) { throw new CougarValidationException(ServerFaultCode.MediaTypeParseFailure, "Input content type cannot be parsed: " + contentType,e); } if (requestMT.isWildcardType() || requestMT.isWildcardSubtype()) { throw new CougarValidationException(ServerFaultCode.InvalidInputMediaType, "Input content type may not be wildcard: " + requestMT); } if (!MediaTypeUtils.isValid(allContentTypes, requestMT)) { throw new CougarValidationException(ServerFaultCode.ContentTypeNotValid, "Input content type is not valid: " + requestMT); } String candidateContentType = requestMT.getType() + "/" + requestMT.getSubtype(); MediaType normalizedMediaType = validContentTypes.get(candidateContentType); if (normalizedMediaType == null) { throw new CougarValidationException(ServerFaultCode.FrameworkError, "Input content type " + contentType + " failed to find a normalized type using key " + candidateContentType); } return normalizedMediaType; } return null; }  @Override void addValidContentTypes(final Set<String> vct, MediaType normalisedContentType); @Override void addValidEncodings(final Set<String> validEncodings); @Override MediaType getNormalisedResponseMediaType(HttpServletRequest request); @Override MediaType getNormalisedRequestMediaType(HttpServletRequest request); void setDefaultResponseFormat(String defaultResponseFormat); @Override String getNormalisedEncoding(HttpServletRequest request); static final String DEFAULT_ENCODING; }### Answer:
@Test public void testGetNormalisedRequestMediaType() { when(request.getMethod()).thenReturn("POST"); when(request.getContentType()).thenReturn("application/xml"); MediaType mediaType = ctn.getNormalisedRequestMediaType(request); assertNotNull(mediaType); assertEquals(MediaType.APPLICATION_XML_TYPE, mediaType); }
@Test public void testGetNormalisedRequestMediaType_Invalid() { when(request.getMethod()).thenReturn("POST"); when(request.getContentType()).thenReturn("application/text"); try { ctn.getNormalisedRequestMediaType(request); fail("CougarValidationException should have been thrown"); } catch (CougarValidationException cve) { assertEquals(ServerFaultCode.ContentTypeNotValid, cve.getServerFaultCode()); assertEquals(ResponseCode.UnsupportedMediaType, cve.getResponseCode()); } }
@Test public void testGetNormalisedRequestMediaType_ParseFailure() { when(request.getMethod()).thenReturn("POST"); when(request.getContentType()).thenReturn("soxml"); try { ctn.getNormalisedRequestMediaType(request); fail("CougarValidationException should have been thrown"); } catch (CougarValidationException cve) { assertEquals(ServerFaultCode.MediaTypeParseFailure, cve.getServerFaultCode()); assertEquals(ResponseCode.UnsupportedMediaType, cve.getResponseCode()); } }
@Test public void testGetNormalisedRequestMediaType_Wildcard() { when(request.getMethod()).thenReturn("POST"); when(request.getContentType()).thenReturn("application/*"); try { ctn.getNormalisedRequestMediaType(request); fail("CougarValidationException should have been thrown"); } catch (CougarValidationException cve) { assertEquals(ServerFaultCode.InvalidInputMediaType, cve.getServerFaultCode()); assertEquals(ResponseCode.UnsupportedMediaType, cve.getResponseCode()); } }