Search in sources :

Example 1 with MultipartFormDataInput

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"));
    }
}
Also used : InputPart(org.jboss.resteasy.plugins.providers.multipart.InputPart) MultipartFormDataInput(org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput) HashMap(java.util.HashMap) InputStream(java.io.InputStream) BadRequestException(uk.gov.justice.services.adapter.rest.exception.BadRequestException) Collections.emptyList(java.util.Collections.emptyList) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) Arrays.asList(java.util.Arrays.asList) Test(org.junit.Test)

Example 2 with MultipartFormDataInput

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'"));
    }
}
Also used : FileStoreFailedException(uk.gov.justice.services.adapter.rest.interceptor.FileStoreFailedException) InputPart(org.jboss.resteasy.plugins.providers.multipart.InputPart) MultipartFormDataInput(org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput) Collections.emptyList(java.util.Collections.emptyList) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) Arrays.asList(java.util.Arrays.asList) IOException(java.io.IOException) Test(org.junit.Test)

Example 3 with MultipartFormDataInput

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();
}
Also used : InputPart(org.jboss.resteasy.plugins.providers.multipart.InputPart) MultipartFormDataInput(org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput) ArrayList(java.util.ArrayList) URL(java.net.URL) Test(org.junit.Test)

Example 4 with MultipartFormDataInput

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));
}
Also used : InputPart(org.jboss.resteasy.plugins.providers.multipart.InputPart) MultipartFormDataInput(org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput) InputStream(java.io.InputStream) Collections.emptyList(java.util.Collections.emptyList) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) Arrays.asList(java.util.Arrays.asList) Test(org.junit.Test)

Example 5 with MultipartFormDataInput

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));
}
Also used : InputPart(org.jboss.resteasy.plugins.providers.multipart.InputPart) MultipartFormDataInput(org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput) InputStream(java.io.InputStream) Collections.emptyList(java.util.Collections.emptyList) Collections.singletonList(java.util.Collections.singletonList) List(java.util.List) Arrays.asList(java.util.Arrays.asList) Test(org.junit.Test)

Aggregations

InputPart (org.jboss.resteasy.plugins.providers.multipart.InputPart)6 MultipartFormDataInput (org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput)6 Test (org.junit.Test)6 Arrays.asList (java.util.Arrays.asList)5 Collections.emptyList (java.util.Collections.emptyList)5 Collections.singletonList (java.util.Collections.singletonList)5 List (java.util.List)5 InputStream (java.io.InputStream)4 BadRequestException (uk.gov.justice.services.adapter.rest.exception.BadRequestException)2 IOException (java.io.IOException)1 URL (java.net.URL)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 FileStoreFailedException (uk.gov.justice.services.adapter.rest.interceptor.FileStoreFailedException)1