use of uk.gov.justice.services.adapter.rest.exception.BadRequestException in project microservice_framework by CJSCommonPlatform.
the class InputPartFileNameExtractor method extractFileName.
public String extractFileName(final InputPart filePart) {
final String headerValue = filePart.getHeaders().getFirst(CONTENT_DISPOSITION_HEADER_NAME);
if (headerValue == null) {
throw new BadRequestException("No header found named '" + CONTENT_DISPOSITION_HEADER_NAME + "'");
}
final Matcher matcher = FIND_FILENAME_PATTERN.matcher(headerValue);
if (matcher.find()) {
return matcher.group(FILENAME_MATCHER_GROUP);
}
throw new BadRequestException("Failed to find 'filename' in '" + CONTENT_DISPOSITION_HEADER_NAME + "' header");
}
use of uk.gov.justice.services.adapter.rest.exception.BadRequestException in project microservice_framework by CJSCommonPlatform.
the class DefaultFileInputDetailsFactoryTest method shouldThrowBadRequestExceptionIfTheListOfFilePartsForAFieldNameIsEmpty.
@Test
public void shouldThrowBadRequestExceptionIfTheListOfFilePartsForAFieldNameIsEmpty() 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, emptyList());
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));
} catch (final BadRequestException expected) {
assertThat(expected.getMessage(), is("The list of input parts named 'myFieldName' is empty"));
}
}
use of uk.gov.justice.services.adapter.rest.exception.BadRequestException in project microservice_framework by CJSCommonPlatform.
the class InputPartFileNameExtractorTest method shouldThrowABadRequestExceptionIfNoContentDispositionHeaderFound.
@Test
public void shouldThrowABadRequestExceptionIfNoContentDispositionHeaderFound() throws Exception {
final String headerName = "Some-Other-Header-Name";
final String headerValue = "form-data; name=\"file\"; filename=\"your_file.zip\"";
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("No header found named 'Content-Disposition'"));
}
}
use of uk.gov.justice.services.adapter.rest.exception.BadRequestException in project microservice_framework by CJSCommonPlatform.
the class BadRequestExceptionMapperTest method shouldReturn400ResponseForBadRequestException.
@Test
public void shouldReturn400ResponseForBadRequestException() throws Exception {
final Response response = exceptionMapper.toResponse(new BadRequestException(TEST_ERROR_MESSAGE));
assertThat(response.getStatus(), is(BAD_REQUEST.getStatusCode()));
assertThat(response.getEntity(), notNullValue());
assertThat(response.getEntity().toString(), hasJsonPath("$.error", equalTo(TEST_ERROR_MESSAGE)));
}
use of uk.gov.justice.services.adapter.rest.exception.BadRequestException in project microservice_framework by CJSCommonPlatform.
the class BadRequestExceptionMapperTest method shouldAddJsonValidationErrorsToResponse.
@Test
public void shouldAddJsonValidationErrorsToResponse() {
final ValidationException validationException = new ValidationException(schema, "Test Json");
final JsonSchemaValidationException jsonSchemaValidationException = new JsonSchemaValidationException(validationException.getMessage(), validationException);
final BadRequestException badRequestException = new BadRequestException(TEST_ERROR_MESSAGE, jsonSchemaValidationException);
final Response response = exceptionMapper.toResponse(badRequestException);
final String body = response.getEntity().toString();
assertThat(body, hasJsonPath("$.validationErrors.message", equalTo("#: Test Json")));
}
Aggregations