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'"));
}
}
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"));
}
}
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));
}
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"));
}
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);
}
Aggregations