MultiPartFileParser { public String parseMultiPartFiles(final List<FileItem> fields) throws IOException { String msg = ""; Iterator<FileItem> it = fields.iterator(); if (!fields.isEmpty() && it.hasNext()) { while (it.hasNext()) { FileItem fileItem = it.next(); if (!fileItem.isFormField()) { String uploadedFileName = fileItem.getName(); InputStream uploadedResource = fileItem.getInputStream(); String contentType = fileItem.getContentType(); String fieldName = fileItem.getFieldName(); msg += multiPartFileProcessor.processUploadedFile(uploadedResource, contentType, fieldName, uploadedFileName); } } } else { msg = "No file uploaded"; } return msg; } @Autowired  MultiPartFileParser(final MultiPartFileProcessor multiPartFileProcessor); String parseMultiPartFiles(final List<FileItem> fields);  }
@Test public void testParseMultiPartFilesShouldReturnWithMsgWhenParamListIsEmpty() throws IOException { String actual = underTest.parseMultiPartFiles(uploadedFiles); assertEquals(actual, "No file uploaded"); }
@Test public void testParseMultiPartFilesShouldReturnWithEmptyMsgWhenParameterIsFormField() throws IOException { given(fileItem.isFormField()).willReturn(true); uploadedFiles.add(fileItem); String actual = underTest.parseMultiPartFiles(uploadedFiles); assertEquals(actual, ""); }
@Test public void testParseMultiPartFilesShouldReturnWithProcessingResultMsgWhenParameterIsNotFormField() throws IOException { String stringValue = "STRING_VALUE"; given(fileItem.isFormField()).willReturn(false); given(fileItem.getName()).willReturn(stringValue); given(fileItem.getContentType()).willReturn(stringValue); given(fileItem.getFieldName()).willReturn(stringValue); given(fileItem.getInputStream()).willReturn(uploadedFileStream); given(multiPartFileProcessor.processUploadedFile(uploadedFileStream, stringValue, stringValue, stringValue)).willReturn( "processing result message"); uploadedFiles.add(fileItem); String actual = underTest.parseMultiPartFiles(uploadedFiles); assertEquals(actual, "processing result message"); }
@Test(expectedExceptions = IOException.class) public void testParseMultiPartFilesShouldThrowExceptionWhenFileCanNotBeParsed() throws IOException { given(fileItem.isFormField()).willReturn(false); given(fileItem.getInputStream()).willThrow(new IOException()); uploadedFiles.add(fileItem); underTest.parseMultiPartFiles(uploadedFiles); }