use of com.tvd12.ezyhttp.core.resources.ResourceUploadManager in project ezyhttp by youngmonkeys.
the class FileUploaderTest method acceptSecondFailed.
@SuppressWarnings("unchecked")
@Test
public void acceptSecondFailed() throws IOException {
// given
AsyncContext asyncContext = mock(AsyncContext.class);
Part part = mock(Part.class);
when(part.getInputStream()).thenThrow(IOException.class);
File outputFile = new File("test-output/files");
FileUploadCallback callback = mock(FileUploadCallback.class);
ResourceUploadManager resourceUploadManager = mock(ResourceUploadManager.class);
FileUploader sut = new FileUploader(resourceUploadManager);
// when
sut.accept(asyncContext, part, outputFile, callback);
// then
verify(callback, times(1)).onFailure(any());
verify(asyncContext, times(1)).complete();
}
use of com.tvd12.ezyhttp.core.resources.ResourceUploadManager in project ezyhttp by youngmonkeys.
the class FileUploaderTest method acceptFirstMaxUploadSizeException.
@SuppressWarnings("unchecked")
@Test
public void acceptFirstMaxUploadSizeException() throws Exception {
// given
AsyncContext asyncContext = mock(AsyncContext.class);
HttpServletRequest request = mock(HttpServletRequest.class);
when(request.getRequestURI()).thenReturn("hello-world");
when(asyncContext.getRequest()).thenReturn(request);
HttpServletResponse response = mock(HttpServletResponse.class);
when(asyncContext.getResponse()).thenReturn(response);
ServletOutputStream outputStream = mock(ServletOutputStream.class);
when(response.getOutputStream()).thenReturn(outputStream);
Part part = mock(Part.class);
File outputFile = new File("test-output/files");
EzyExceptionVoid callback = mock(EzyExceptionVoid.class);
doThrow(new MaxUploadSizeException(100)).when(callback).apply();
ResourceUploadManager resourceUploadManager = mock(ResourceUploadManager.class);
doAnswer(it -> {
EzyResultCallback<Boolean> cb = it.getArgumentAt(3, EzyResultCallback.class);
cb.onResponse(Boolean.TRUE);
return null;
}).when(resourceUploadManager).drainAsync(any(), any(), any(long.class), any());
FileUploader sut = new FileUploader(resourceUploadManager);
// when
sut.accept(asyncContext, part, outputFile, callback);
// then
verify(callback, times(1)).apply();
verify(resourceUploadManager, times(1)).drainAsync(any(), any(), any(long.class), any());
verify(response, times(1)).setStatus(StatusCodes.BAD_REQUEST);
verify(response, times(1)).getOutputStream();
verify(outputStream, times(1)).write(any(byte[].class));
verify(request, times(1)).getRequestURI();
verify(asyncContext, times(1)).getRequest();
verify(asyncContext, times(1)).complete();
}
use of com.tvd12.ezyhttp.core.resources.ResourceUploadManager in project ezyhttp by youngmonkeys.
the class FileUploaderTest method acceptThirdFileNotFound.
@Test
public void acceptThirdFileNotFound() {
// given
HttpServletResponse response = mock(HttpServletResponse.class);
AsyncContext asyncContext = mock(AsyncContext.class);
when(asyncContext.getResponse()).thenReturn(response);
InputStream inputStream = mock(InputStream.class);
File outputFile = new File("");
FileUploadCallback callback = mock(FileUploadCallback.class);
ResourceUploadManager resourceUploadManager = mock(ResourceUploadManager.class);
FileUploader sut = new FileUploader(resourceUploadManager);
// when
sut.accept(asyncContext, inputStream, outputFile, callback);
// then
verify(callback, times(1)).onFailure(any());
verify(asyncContext, times(1)).complete();
}
use of com.tvd12.ezyhttp.core.resources.ResourceUploadManager in project ezyhttp by youngmonkeys.
the class FileUploaderTest method acceptFirst.
@SuppressWarnings("unchecked")
@Test
public void acceptFirst() throws Exception {
// given
AsyncContext asyncContext = mock(AsyncContext.class);
HttpServletResponse response = mock(HttpServletResponse.class);
when(asyncContext.getResponse()).thenReturn(response);
Part part = mock(Part.class);
File outputFile = new File("test-output/files");
EzyExceptionVoid callback = mock(EzyExceptionVoid.class);
ResourceUploadManager resourceUploadManager = mock(ResourceUploadManager.class);
doAnswer(it -> {
EzyResultCallback<Boolean> cb = it.getArgumentAt(3, EzyResultCallback.class);
cb.onResponse(Boolean.TRUE);
return null;
}).when(resourceUploadManager).drainAsync(any(), any(), any(long.class), any());
FileUploader sut = new FileUploader(resourceUploadManager);
// when
sut.accept(asyncContext, part, outputFile, callback);
// then
verify(callback, times(1)).apply();
verify(resourceUploadManager, times(1)).drainAsync(any(), any(), any(long.class), any());
verify(response, times(1)).setStatus(StatusCodes.OK);
verify(asyncContext, times(1)).complete();
}
use of com.tvd12.ezyhttp.core.resources.ResourceUploadManager in project ezyhttp by youngmonkeys.
the class ResourceUploadManagerTest method drainFailedDueToOutputStream.
@Test
public void drainFailedDueToOutputStream() throws Exception {
// given
ResourceUploadManager sut = new ResourceUploadManager();
int size = ResourceUploadManager.DEFAULT_BUFFER_SIZE * 3;
byte[] inputBytes = RandomUtil.randomByteArray(size);
InputStream inputStream = new ByteArrayInputStream(inputBytes);
OutputStream outputStream = mock(OutputStream.class);
IOException exception = new IOException("just test");
doThrow(exception).when(outputStream).write(any(byte[].class), anyInt(), anyInt());
// when
Throwable e = Asserts.assertThrows(() -> sut.drain(inputStream, outputStream));
// then
Asserts.assertThat(e).isEqualsType(IOException.class);
sut.stop();
}
Aggregations