Search in sources :

Example 6 with InputPart

use of org.jboss.resteasy.plugins.providers.multipart.InputPart in project microservice_framework by CJSCommonPlatform.

the class InputPartFileNameExtractorTest method shouldThrowABadRequestExceptionIfNoHeadersFound.

@Test
public void shouldThrowABadRequestExceptionIfNoHeadersFound() throws Exception {
    final InputPart inputPart = mock(InputPart.class);
    when(inputPart.getHeaders()).thenReturn(new MultivaluedHashMap<>());
    try {
        inputPartFileNameExtractor.extractFileName(inputPart);
    } catch (final BadRequestException expected) {
        assertThat(expected.getMessage(), is("No header found named 'Content-Disposition'"));
    }
}
Also used : InputPart(org.jboss.resteasy.plugins.providers.multipart.InputPart) BadRequestException(uk.gov.justice.services.adapter.rest.exception.BadRequestException) Test(org.junit.Test)

Example 7 with InputPart

use of org.jboss.resteasy.plugins.providers.multipart.InputPart in project microservice_framework by CJSCommonPlatform.

the class InputPartFileNameExtractorTest method shouldThrowABadRequestExceptionIfNoFilenameFoundInContentDispositionHeader.

@Test
public void shouldThrowABadRequestExceptionIfNoFilenameFoundInContentDispositionHeader() throws Exception {
    final String headerName = "Content-Disposition";
    final String headerValue = "form-data; name=\"file\"";
    final Map<String, String> headers = of(headerName, headerValue);
    final InputPart inputPart = mock(InputPart.class);
    when(inputPart.getHeaders()).thenReturn(new MultivaluedHashMap<>(headers));
    try {
        inputPartFileNameExtractor.extractFileName(inputPart);
    } catch (final BadRequestException expected) {
        assertThat(expected.getMessage(), is("Failed to find 'filename' in 'Content-Disposition' header"));
    }
}
Also used : InputPart(org.jboss.resteasy.plugins.providers.multipart.InputPart) BadRequestException(uk.gov.justice.services.adapter.rest.exception.BadRequestException) Test(org.junit.Test)

Example 8 with InputPart

use of org.jboss.resteasy.plugins.providers.multipart.InputPart in project microservice_framework by CJSCommonPlatform.

the class DefaultFileInputDetailsFactory method fileInputDetailsFrom.

private FileInputDetails fileInputDetailsFrom(final Map<String, List<InputPart>> formDataMap, final String fieldName) {
    if (!formDataMap.containsKey(fieldName)) {
        throw new BadRequestException(format("Failed to find input part named '%s' as specified in the raml", fieldName));
    }
    final List<InputPart> inputParts = formDataMap.get(fieldName);
    if (inputParts.isEmpty()) {
        throw new BadRequestException(format("The list of input parts named '%s' is empty", fieldName));
    }
    final InputPart inputPart = inputParts.get(0);
    final String fileName = inputPartFileNameExtractor.extractFileName(inputPart);
    return new DefaultFileInputDetails(fileName, fieldName, inputStreamFrom(inputPart, fileName));
}
Also used : InputPart(org.jboss.resteasy.plugins.providers.multipart.InputPart) BadRequestException(uk.gov.justice.services.adapter.rest.exception.BadRequestException)

Example 9 with InputPart

use of org.jboss.resteasy.plugins.providers.multipart.InputPart in project candlepin by candlepin.

the class OwnerResourceTest method testImportManifestFailure.

@Test
public void testImportManifestFailure() throws IOException, ImporterException {
    ManifestManager manifestManager = mock(ManifestManager.class);
    EventSink es = mock(EventSink.class);
    OwnerResource thisOwnerResource = new OwnerResource(ownerCurator, productCurator, null, null, i18n, es, eventFactory, null, null, manifestManager, null, null, null, null, importRecordCurator, null, null, null, null, null, null, null, contentOverrideValidator, serviceLevelValidator, null, null, null, null, null, this.modelTranslator);
    MultipartInput input = mock(MultipartInput.class);
    InputPart part = mock(InputPart.class);
    File archive = mock(File.class);
    List<InputPart> parts = new ArrayList<>();
    parts.add(part);
    MultivaluedMap<String, String> mm = new MultivaluedMapImpl<>();
    List<String> contDis = new ArrayList<>();
    contDis.add("form-data; name=\"upload\"; filename=\"test_file.zip\"");
    mm.put("Content-Disposition", contDis);
    when(input.getParts()).thenReturn(parts);
    when(part.getHeaders()).thenReturn(mm);
    when(part.getBody(any(GenericType.class))).thenReturn(archive);
    ImporterException expectedException = new ImporterException("Bad import");
    when(manifestManager.importManifest(eq(owner), any(File.class), any(String.class), any(ConflictOverrides.class))).thenThrow(expectedException);
    try {
        thisOwnerResource.importManifest(owner.getKey(), new String[] {}, input);
        fail("Expected IseException was not thrown");
    } catch (IseException ise) {
    // expected, so we catch and go on.
    }
    verify(manifestManager).recordImportFailure(eq(owner), eq(expectedException), eq("test_file.zip"));
}
Also used : ImporterException(org.candlepin.sync.ImporterException) ConflictOverrides(org.candlepin.sync.ConflictOverrides) GenericType(org.jboss.resteasy.util.GenericType) MultipartInput(org.jboss.resteasy.plugins.providers.multipart.MultipartInput) ArrayList(java.util.ArrayList) MultivaluedMapImpl(org.jboss.resteasy.specimpl.MultivaluedMapImpl) Matchers.anyString(org.mockito.Matchers.anyString) ManifestManager(org.candlepin.controller.ManifestManager) InputPart(org.jboss.resteasy.plugins.providers.multipart.InputPart) IseException(org.candlepin.common.exceptions.IseException) EventSink(org.candlepin.audit.EventSink) File(java.io.File) Test(org.junit.Test)

Example 10 with InputPart

use of org.jboss.resteasy.plugins.providers.multipart.InputPart in project candlepin by candlepin.

the class OwnerResourceTest method testImportManifestSynchronousSuccess.

@Test
public void testImportManifestSynchronousSuccess() throws IOException, ImporterException {
    ManifestManager manifestManager = mock(ManifestManager.class);
    EventSink es = mock(EventSink.class);
    OwnerResource thisOwnerResource = new OwnerResource(ownerCurator, productCurator, null, null, i18n, es, eventFactory, null, null, manifestManager, null, null, null, null, importRecordCurator, null, null, null, null, null, null, null, contentOverrideValidator, serviceLevelValidator, null, null, null, null, null, this.modelTranslator);
    MultipartInput input = mock(MultipartInput.class);
    InputPart part = mock(InputPart.class);
    File archive = mock(File.class);
    List<InputPart> parts = new ArrayList<>();
    parts.add(part);
    MultivaluedMap<String, String> mm = new MultivaluedMapImpl<>();
    List<String> contDis = new ArrayList<>();
    contDis.add("form-data; name=\"upload\"; filename=\"test_file.zip\"");
    mm.put("Content-Disposition", contDis);
    when(input.getParts()).thenReturn(parts);
    when(part.getHeaders()).thenReturn(mm);
    when(part.getBody(any(GenericType.class))).thenReturn(archive);
    ImportRecord ir = new ImportRecord(owner);
    when(manifestManager.importManifest(eq(owner), any(File.class), eq("test_file.zip"), any(ConflictOverrides.class))).thenReturn(ir);
    ImportRecord response = thisOwnerResource.importManifest(owner.getKey(), new String[] {}, input);
    assertNotNull(response);
    assertEquals(ir, response);
}
Also used : ConflictOverrides(org.candlepin.sync.ConflictOverrides) GenericType(org.jboss.resteasy.util.GenericType) MultipartInput(org.jboss.resteasy.plugins.providers.multipart.MultipartInput) ArrayList(java.util.ArrayList) MultivaluedMapImpl(org.jboss.resteasy.specimpl.MultivaluedMapImpl) Matchers.anyString(org.mockito.Matchers.anyString) ManifestManager(org.candlepin.controller.ManifestManager) ImportRecord(org.candlepin.model.ImportRecord) InputPart(org.jboss.resteasy.plugins.providers.multipart.InputPart) EventSink(org.candlepin.audit.EventSink) File(java.io.File) Test(org.junit.Test)

Aggregations

InputPart (org.jboss.resteasy.plugins.providers.multipart.InputPart)26 Test (org.junit.Test)13 InputStream (java.io.InputStream)12 List (java.util.List)12 IOException (java.io.IOException)9 ArrayList (java.util.ArrayList)8 File (java.io.File)6 MultipartFormDataInput (org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput)6 BadRequestException (uk.gov.justice.services.adapter.rest.exception.BadRequestException)6 Arrays.asList (java.util.Arrays.asList)5 Collections.emptyList (java.util.Collections.emptyList)5 Collections.singletonList (java.util.Collections.singletonList)5 GenericType (org.jboss.resteasy.util.GenericType)4 BufferedInputStream (java.io.BufferedInputStream)3 Consumes (javax.ws.rs.Consumes)3 POST (javax.ws.rs.POST)3 Path (javax.ws.rs.Path)3 EventSink (org.candlepin.audit.EventSink)3 ManifestManager (org.candlepin.controller.ManifestManager)3 ConflictOverrides (org.candlepin.sync.ConflictOverrides)3