MultiPartFileProcessor { public String processUploadedFile(final InputStream resource, final String contentType, final String fieldName, final String fileName) { String result; String resFileName = extractFileNameFromAbsolutePath(fileName); String classUploadResult = processUploadedClasses(resource, contentType, fieldName, resFileName); if (classUploadResult != null) { result = classUploadResult; } else { if ("stub-configuration".equals(fieldName) && JSON_CONTENT_TYPE.equals(contentType)) { try { routingService.performModification(new NewStubDescriptorCommand(resource, stubConfigurationJsonBuilder, sequenceDescriptorHolder)); serviceMap.detectServices(); } catch (ClassNotFoundException e) { result = "Uploading " + fileName + " failed with ClassNotFoundException."; throw new CannotUploadExternalResourceException(result, e); } result = "New stub configuration was uploaded to Wilma."; } else if ("stub-template".equals(fieldName)) { writeResourceToFile(resource, resFileName, stubResourcePathProvider.getTemplatesPathAsString()); result = "External template '" + resFileName + "' was uploaded to Wilma."; } else { result = "Uploading " + fileName + " failed: wrong content type or tried to upload file from unauthorized form!"; throw new CannotUploadExternalResourceException(result); } } return result; }  String processUploadedFile(final InputStream resource, final String contentType, final String fieldName, final String fileName);  }
@Test public void testProcessUploadedFileShouldReturnWhenStubConfigIsValid() throws ClassNotFoundException { String actual = underTest.processUploadedFile(resource, JSON_CONTENT_TYPE, "stub-configuration", FILE_PATH); verify(routingService).performModification(Mockito.any(NewStubDescriptorCommand.class)); assertEquals(actual, "New stub configuration was uploaded to Wilma."); }
@Test(expectedExceptions = CannotUploadExternalResourceException.class) public void testProcessUploadedFileShouldThrowExceptionWhenUploadedStubConfigContentTypeIsWrong() { underTest.processUploadedFile(resource, OCTET_STREAM_CONTENT_TYPE, "stub-configuration", FILE_PATH); }
@Test public void testProcessUploadedFileShouldReturnWhenConditionCheckerUploaded() { given(stubResourcePathProvider.getConditionCheckerPathAsString()).willReturn(""); String actual = underTest.processUploadedFile(resource, APPLICATION_JAVA, "stub-condition-checker", FILE_PATH); verify(fileWriter).write(resource, "/" + FILE_PATH, EXCEPTION_MESSAGE); assertEquals(actual, "External condition checker class '" + FILE_PATH + "' was uploaded to Wilma."); }
@Test(expectedExceptions = CannotUploadExternalResourceException.class) public void testProcessUploadedFileShouldThrowExceptionWhenUploadedFileContentTypeNotJavaOrOctetStream() { underTest.processUploadedFile(resource, JSON_CONTENT_TYPE, "stub-condition-checker", FILE_PATH); }
@Test public void testProcessUploadedFileShouldReturnWhenResponseFormatterUploaded() { given(stubResourcePathProvider.getResponseFormattersPathAsString()).willReturn(""); String actual = underTest.processUploadedFile(resource, OCTET_STREAM_CONTENT_TYPE, "stub-response-formatter", FILE_PATH); verify(fileWriter).write(resource, "/" + FILE_PATH, EXCEPTION_MESSAGE); assertEquals(actual, "External response formatter class '" + FILE_PATH + "' was uploaded to Wilma."); }
@Test(expectedExceptions = CannotUploadExternalResourceException.class) public void testProcessUploadedFileShouldThrowExceptionWhenUploadedResponseFormatterContentTypeNotJavaOrOctetStream() { underTest.processUploadedFile(resource, JSON_CONTENT_TYPE, "stub-response-formatter", FILE_PATH); }
@Test public void testProcessUploadedFileShouldReturnWhenTemplateUploaded() { given(stubResourcePathProvider.getTemplatesPathAsString()).willReturn(""); String actual = underTest.processUploadedFile(resource, "some-content-type", "stub-template", FILE_PATH); verify(fileWriter).write(resource, "/" + FILE_PATH, EXCEPTION_MESSAGE); assertEquals(actual, "External template '" + FILE_PATH + "' was uploaded to Wilma."); }
@Test(expectedExceptions = CannotUploadExternalResourceException.class) public void testProcessUploadedFileShouldReturnWhenUploadedingFileFromUnknownInputField() { underTest.processUploadedFile(resource, JSON_CONTENT_TYPE, "asdfasdf", FILE_PATH); }
@Test public void testProcessUploadedFileShouldReturnWhenInterceptorUploaded() { given(stubResourcePathProvider.getInterceptorPathAsString()).willReturn(""); String actual = underTest.processUploadedFile(resource, "some-content-type", "stub-interceptor", FILE_PATH); verify(fileWriter).write(resource, "/" + FILE_PATH, EXCEPTION_MESSAGE); assertEquals(actual, "External interceptor '" + FILE_PATH + "' was uploaded to Wilma."); }
@Test public void testProcessUploadedFileShouldReturnWhenJarUploaded() { given(stubResourcePathProvider.getJarPathAsString()).willReturn(""); String actual = underTest.processUploadedFile(resource, "some-content-type", "stub-jar", FILE_PATH); verify(fileWriter).write(resource, "/" + FILE_PATH, EXCEPTION_MESSAGE); assertEquals(actual, "External jar '" + FILE_PATH + "' was uploaded to Wilma."); }
@Test public void testProcessUploadedFileShouldGetFileNameFromPathWhichContainsSlash() { given(stubResourcePathProvider.getTemplatesPathAsString()).willReturn(""); underTest.processUploadedFile(resource, "some-content-type", "stub-template", FILE_PATH_W_SLASH); verify(fileWriter).write(resource, "/" + FILE_PATH, EXCEPTION_MESSAGE); }
@Test public void testProcessUploadedFileShouldGetFileNameFromPathWhichContainsBackSlash() { given(stubResourcePathProvider.getTemplatesPathAsString()).willReturn(""); underTest.processUploadedFile(resource, "some-content-type", "stub-template", FILE_PATH_W_BACK_SLASH); verify(fileWriter).write(resource, "/" + FILE_PATH, EXCEPTION_MESSAGE); }