use of com.tvd12.ezyhttp.server.core.resources.FileUploader in project ezyhttp by youngmonkeys.
the class FileUploaderTest method acceptFirstException.
@SuppressWarnings("unchecked")
@Test
public void acceptFirstException() 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);
Part part = mock(Part.class);
File outputFile = new File("test-output/files");
EzyExceptionVoid callback = mock(EzyExceptionVoid.class);
doThrow(IllegalStateException.class).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.INTERNAL_SERVER_ERROR);
verify(request, times(1)).getRequestURI();
verify(asyncContext, times(1)).getRequest();
verify(asyncContext, times(1)).complete();
}
use of com.tvd12.ezyhttp.server.core.resources.FileUploader in project ezyhttp by youngmonkeys.
the class FileUploaderTest method acceptFourthFailed.
@Test
public void acceptFourthFailed() {
// given
HttpServletResponse response = mock(HttpServletResponse.class);
AsyncContext asyncContext = mock(AsyncContext.class);
when(asyncContext.getResponse()).thenReturn(response);
InputStream inputStream = mock(InputStream.class);
OutputStream outputStream = mock(OutputStream.class);
FileUploadCallback callback = mock(FileUploadCallback.class);
ResourceUploadManager resourceUploadManager = mock(ResourceUploadManager.class);
doThrow(IllegalStateException.class).when(resourceUploadManager).drainAsync(any(), any(), any(long.class), any());
int timeout = RandomUtil.randomSmallInt() + 1;
FileUploader sut = new FileUploader(resourceUploadManager, timeout);
// when
sut.accept(asyncContext, inputStream, outputStream, callback);
// then
verify(callback, times(1)).onFailure(any());
verify(resourceUploadManager, times(1)).drainAsync(any(), any(), any(long.class), any());
verify(asyncContext, times(1)).setTimeout(timeout);
verify(asyncContext, times(1)).complete();
}
use of com.tvd12.ezyhttp.server.core.resources.FileUploader in project ezyhttp by youngmonkeys.
the class ResourceUploadManagerConfiguration method config.
@Override
public void config() {
if (resourceUploadEnable) {
ResourceUploadManager resourceUploadManager = createUploadManager(context);
FileUploader fileUploader = new FileUploader(resourceUploadManager);
context.getSingletonFactory().addSingleton(resourceUploadManager);
context.getSingletonFactory().addSingleton(fileUploader);
}
}
Aggregations