DocumentTypeResolver { public static DocumentType documentTypeFromString(String type) { Optional<DocumentType> documentType = stream(DocumentType.class.getEnumConstants()) .filter(dt -> dt.name().equalsIgnoreCase(type)) .findAny(); if (!documentType.isPresent()) { throw new UnsupportedTypeException("type = " + type + " is unsupported"); } return documentType.get(); }  static DocumentType documentTypeFromString(String type);  }
@Test public void pdfTypeReturnsPdfMimeType() { DocumentType documentType = DocumentTypeResolver.documentTypeFromString("pdf"); assertEquals(documentType, DocumentType.PDF); }
@Test public void ignoreCaseForType() { DocumentType documentType = DocumentTypeResolver.documentTypeFromString("PdF"); assertEquals(documentType, DocumentType.PDF); documentType = DocumentTypeResolver.documentTypeFromString("PDF"); assertEquals(documentType, DocumentType.PDF); documentType = DocumentTypeResolver.documentTypeFromString("pdf"); assertEquals(documentType, DocumentType.PDF); }
@Test public void unsupportedTypeThrowsException() throws Exception { String unsupportedType = "some not supported type"; expectedException.expect(UnsupportedTypeException.class); expectedException.expectMessage("type = " + unsupportedType + " is unsupported"); DocumentTypeResolver.documentTypeFromString(unsupportedType); }
@Test public void extraWhiteSpaceInOtherwiseSupportedTypeIsIllegal() { String supportedTypeWithExtraWhiteSpace = " pdf "; expectedException.expect(UnsupportedTypeException.class); expectedException.expectMessage("type = " + supportedTypeWithExtraWhiteSpace + " is unsupported"); DocumentTypeResolver.documentTypeFromString(supportedTypeWithExtraWhiteSpace); }