use of org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput in project microservice_framework by CJSCommonPlatform.
the class DefaultFileInputDetailsFactoryTest method shouldThrowBadRequestExceptionIfNoInputPartFoundWithTheNameSpecifiedInTheRaml.
@Test
public void shouldThrowBadRequestExceptionIfNoInputPartFoundWithTheNameSpecifiedInTheRaml() throws Exception {
final String fileName = "the-file-name.jpeg";
final String fieldName = "myFieldName";
final MultipartFormDataInput multipartFormDataInput = mock(MultipartFormDataInput.class);
final InputPart inputPart = mock(InputPart.class);
final InputStream inputStream = mock(InputStream.class);
final Map<String, List<InputPart>> formDataMap = new HashMap<>();
when(multipartFormDataInput.getFormDataMap()).thenReturn(formDataMap);
when(inputPartFileNameExtractor.extractFileName(inputPart)).thenReturn(fileName);
when(inputPart.getBody(InputStream.class, null)).thenReturn(inputStream);
try {
fileInputDetailsFactory.createFileInputDetailsFrom(multipartFormDataInput, singletonList(fieldName));
fail();
} catch (final BadRequestException expected) {
assertThat(expected.getMessage(), is("Failed to find input part named 'myFieldName' as specified in the raml"));
}
}
use of org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput in project microservice_framework by CJSCommonPlatform.
the class DefaultFileInputDetailsFactoryTest method shouldThrowFileStoreFailedExceptionIfGettingFileInputStreamFails.
@Test
public void shouldThrowFileStoreFailedExceptionIfGettingFileInputStreamFails() throws Exception {
final IOException ioException = new IOException("bunnies");
final String fileName = "the-file-name.jpeg";
final String fieldName = "myFieldName";
final MultipartFormDataInput multipartFormDataInput = mock(MultipartFormDataInput.class);
final InputPart inputPart = mock(InputPart.class);
final Map<String, List<InputPart>> formDataMap = ImmutableMap.of(fieldName, singletonList(inputPart));
when(multipartFormDataInput.getFormDataMap()).thenReturn(formDataMap);
when(inputPartFileNameExtractor.extractFileName(inputPart)).thenReturn(fileName);
when(inputPart.getMediaType()).thenReturn(TEXT_XML_TYPE);
when(inputPart.getBody(InputStream.class, null)).thenThrow(ioException);
try {
fileInputDetailsFactory.createFileInputDetailsFrom(multipartFormDataInput, singletonList(fieldName));
fail();
} catch (final FileStoreFailedException expected) {
assertThat(expected.getCause(), is(ioException));
assertThat(expected.getMessage(), is("Failed to store file 'the-file-name.jpeg'"));
}
}
use of org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput in project syndesis by syndesisio.
the class DriverUploadTest method upload.
@Test
public void upload() throws IOException, URISyntaxException {
URL driverClasspath = DriverUploadTest.class.getClassLoader().getResource(FILE_CLASSPATH);
assertThat(driverClasspath).isNotNull();
MultipartFormDataInput multiPart = mock(MultipartFormDataInput.class);
InputPart inputPartFileContent = mock(InputPart.class);
InputPart inputPartFileName = mock(InputPart.class);
List<InputPart> parts = new ArrayList<>();
parts.add(inputPartFileContent);
parts.add(inputPartFileName);
when(multiPart.getParts()).thenReturn(parts);
when(multiPart.getFormDataPart("file", InputStream.class, null)).thenReturn(driverClasspath.openStream());
when(multiPart.getFormDataPart("fileName", String.class, null)).thenReturn(FILE_NAME);
String tempDir = System.getProperty("java.io.tmpdir");
System.setProperty("LOADER_HOME", tempDir);
DriverUploadEndpoint endpoint = new DriverUploadEndpoint();
Boolean reply = endpoint.upload(multiPart);
assertThat(reply).isTrue();
// check equality with the original file
// File originalFile = new File(driverClasspath.toURI());
// File uploadedFile = new File(tempDir + File.separator + FILE_NAME);
// Boolean isEquals = FileUtils.contentEquals(originalFile, uploadedFile);
// assertThat(isEquals).isTrue();
}
use of org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput in project microservice_framework by CJSCommonPlatform.
the class DefaultFileInputDetailsFactoryTest method shouldCreateFileInputDetailsFromFilePart.
@Test
public void shouldCreateFileInputDetailsFromFilePart() throws Exception {
final String fileName = "the-file-name.jpeg";
final String fieldName = "myFieldName";
final MultipartFormDataInput multipartFormDataInput = mock(MultipartFormDataInput.class);
final InputPart inputPart = mock(InputPart.class);
final InputStream inputStream = mock(InputStream.class);
final Map<String, List<InputPart>> formDataMap = ImmutableMap.of(fieldName, singletonList(inputPart));
when(multipartFormDataInput.getFormDataMap()).thenReturn(formDataMap);
when(inputPartFileNameExtractor.extractFileName(inputPart)).thenReturn(fileName);
when(inputPart.getBody(InputStream.class, null)).thenReturn(inputStream);
final List<FileInputDetails> fileInputDetails = fileInputDetailsFactory.createFileInputDetailsFrom(multipartFormDataInput, singletonList(fieldName));
final FileInputDetails inputDetails = fileInputDetails.get(0);
assertThat(inputDetails.getFileName(), is(fileName));
assertThat(inputDetails.getFieldName(), is(fieldName));
assertThat(inputDetails.getInputStream(), is(inputStream));
}
use of org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput in project microservice_framework by CJSCommonPlatform.
the class DefaultFileInputDetailsFactoryTest method shouldHandleMoreThanOneMultipart.
@Test
public void shouldHandleMoreThanOneMultipart() throws Exception {
final String fileName_1 = "the-file-name_1.jpeg";
final String fieldName_1 = "myFieldName_1";
final String fileName_2 = "the-file-name_2.jpeg";
final String fieldName_2 = "myFieldName_2";
final MultipartFormDataInput multipartFormDataInput = mock(MultipartFormDataInput.class);
final InputPart inputPart_1 = mock(InputPart.class);
final InputPart inputPart_2 = mock(InputPart.class);
final InputStream inputStream_1 = mock(InputStream.class);
final InputStream inputStream_2 = mock(InputStream.class);
final Map<String, List<InputPart>> formDataMap = ImmutableMap.of(fieldName_1, singletonList(inputPart_1), fieldName_2, singletonList(inputPart_2));
when(multipartFormDataInput.getFormDataMap()).thenReturn(formDataMap);
when(inputPartFileNameExtractor.extractFileName(inputPart_1)).thenReturn(fileName_1);
when(inputPartFileNameExtractor.extractFileName(inputPart_2)).thenReturn(fileName_2);
when(inputPart_1.getBody(InputStream.class, null)).thenReturn(inputStream_1);
when(inputPart_2.getBody(InputStream.class, null)).thenReturn(inputStream_2);
final List<FileInputDetails> fileInputDetails = fileInputDetailsFactory.createFileInputDetailsFrom(multipartFormDataInput, asList(fieldName_1, fieldName_2));
final FileInputDetails inputDetails_1 = fileInputDetails.get(0);
assertThat(inputDetails_1.getFileName(), is(fileName_1));
assertThat(inputDetails_1.getFieldName(), is(fieldName_1));
assertThat(inputDetails_1.getInputStream(), is(inputStream_1));
final FileInputDetails inputDetails_2 = fileInputDetails.get(1);
assertThat(inputDetails_2.getFileName(), is(fileName_2));
assertThat(inputDetails_2.getFieldName(), is(fieldName_2));
assertThat(inputDetails_2.getInputStream(), is(inputStream_2));
}
Aggregations