use of com.tvd12.ezyfox.function.EzyExceptionVoid 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.ezyfox.function.EzyExceptionVoid 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.ezyfox.function.EzyExceptionVoid in project ezyhttp by youngmonkeys.
the class FileUploaderTest method acceptFirstFailed.
@SuppressWarnings("unchecked")
@Test
public void acceptFirstFailed() 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);
ResourceUploadManager resourceUploadManager = mock(ResourceUploadManager.class);
doAnswer(it -> {
EzyResultCallback<Boolean> cb = it.getArgumentAt(3, EzyResultCallback.class);
cb.onException(new Exception("just test"));
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(0)).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.ezyfox.function.EzyExceptionVoid 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.ezyfox.function.EzyExceptionVoid in project ezyhttp by youngmonkeys.
the class FileUploader method accept.
public void accept(AsyncContext asyncContext, Part part, File outputFile, long maxUploadSize, EzyExceptionVoid callback) {
HttpServletResponse response = (HttpServletResponse) asyncContext.getResponse();
accept(asyncContext, part, outputFile, maxUploadSize, new FileUploadCallback() {
@Override
public void onSuccess() {
try {
callback.apply();
response.setStatus(StatusCodes.OK);
} catch (Exception e) {
onFailure(e);
}
}
@Override
public void onFailure(Exception e) {
if (e instanceof MaxUploadSizeException) {
processWithLogException(() -> response.getOutputStream().write(OVER_UPLOAD_SIZE_MESSAGE));
response.setStatus(StatusCodes.BAD_REQUEST);
} else {
response.setStatus(StatusCodes.INTERNAL_SERVER_ERROR);
}
HttpServletRequest request = (HttpServletRequest) asyncContext.getRequest();
logger.info("accept request: {} error", request.getRequestURI(), e);
}
});
}
Aggregations