Search in sources :

Example 1 with ResourceUploadManager

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();
}
Also used : FileUploadCallback(com.tvd12.ezyhttp.server.core.resources.FileUploadCallback) FileUploader(com.tvd12.ezyhttp.server.core.resources.FileUploader) Part(javax.servlet.http.Part) ResourceUploadManager(com.tvd12.ezyhttp.core.resources.ResourceUploadManager) AsyncContext(javax.servlet.AsyncContext) File(java.io.File) Test(org.testng.annotations.Test)

Example 2 with ResourceUploadManager

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();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) EzyExceptionVoid(com.tvd12.ezyfox.function.EzyExceptionVoid) ServletOutputStream(javax.servlet.ServletOutputStream) FileUploader(com.tvd12.ezyhttp.server.core.resources.FileUploader) Part(javax.servlet.http.Part) MaxUploadSizeException(com.tvd12.ezyhttp.core.exception.MaxUploadSizeException) HttpServletResponse(javax.servlet.http.HttpServletResponse) ResourceUploadManager(com.tvd12.ezyhttp.core.resources.ResourceUploadManager) AsyncContext(javax.servlet.AsyncContext) File(java.io.File) Test(org.testng.annotations.Test)

Example 3 with ResourceUploadManager

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();
}
Also used : FileUploadCallback(com.tvd12.ezyhttp.server.core.resources.FileUploadCallback) FileUploader(com.tvd12.ezyhttp.server.core.resources.FileUploader) InputStream(java.io.InputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) ResourceUploadManager(com.tvd12.ezyhttp.core.resources.ResourceUploadManager) AsyncContext(javax.servlet.AsyncContext) File(java.io.File) Test(org.testng.annotations.Test)

Example 4 with ResourceUploadManager

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();
}
Also used : EzyExceptionVoid(com.tvd12.ezyfox.function.EzyExceptionVoid) FileUploader(com.tvd12.ezyhttp.server.core.resources.FileUploader) Part(javax.servlet.http.Part) HttpServletResponse(javax.servlet.http.HttpServletResponse) ResourceUploadManager(com.tvd12.ezyhttp.core.resources.ResourceUploadManager) AsyncContext(javax.servlet.AsyncContext) File(java.io.File) Test(org.testng.annotations.Test)

Example 5 with ResourceUploadManager

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();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ResourceUploadManager(com.tvd12.ezyhttp.core.resources.ResourceUploadManager) IOException(java.io.IOException) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Aggregations

ResourceUploadManager (com.tvd12.ezyhttp.core.resources.ResourceUploadManager)20 Test (org.testng.annotations.Test)19 InputStream (java.io.InputStream)14 BaseTest (com.tvd12.test.base.BaseTest)12 ByteArrayInputStream (java.io.ByteArrayInputStream)12 ByteArrayOutputStream (java.io.ByteArrayOutputStream)12 OutputStream (java.io.OutputStream)9 FileUploader (com.tvd12.ezyhttp.server.core.resources.FileUploader)7 AsyncContext (javax.servlet.AsyncContext)7 File (java.io.File)6 HttpServletResponse (javax.servlet.http.HttpServletResponse)6 Part (javax.servlet.http.Part)5 EzyExceptionVoid (com.tvd12.ezyfox.function.EzyExceptionVoid)4 MaxUploadSizeException (com.tvd12.ezyhttp.core.exception.MaxUploadSizeException)4 IOException (java.io.IOException)4 FileUploadCallback (com.tvd12.ezyhttp.server.core.resources.FileUploadCallback)3 HttpServletRequest (javax.servlet.http.HttpServletRequest)3 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 ServletOutputStream (javax.servlet.ServletOutputStream)2 EzyFutureMap (com.tvd12.ezyfox.concurrent.EzyFutureMap)1