Search in sources :

Example 11 with ResourceUploadManager

use of com.tvd12.ezyhttp.core.resources.ResourceUploadManager 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();
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) 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 12 with ResourceUploadManager

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

Example 13 with ResourceUploadManager

use of com.tvd12.ezyhttp.core.resources.ResourceUploadManager 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);
    }
}
Also used : ResourceUploadManager(com.tvd12.ezyhttp.core.resources.ResourceUploadManager)

Example 14 with ResourceUploadManager

use of com.tvd12.ezyhttp.core.resources.ResourceUploadManager in project ezyhttp by youngmonkeys.

the class ResourceUploadManagerTest method activeFalse.

@Test
public void activeFalse() throws Exception {
    // given
    ResourceUploadManager sut = new ResourceUploadManager();
    FieldUtil.setFieldValue(sut, "active", false);
    int size = ResourceUploadManager.DEFAULT_BUFFER_SIZE * 3;
    byte[] inputBytes = RandomUtil.randomByteArray(size);
    InputStream inputStream = new ByteArrayInputStream(inputBytes);
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream(size);
    // when
    sut.drainAsync(inputStream, outputStream, it -> {
    });
    Thread.sleep(100);
    // then
    byte[] outputBytes = outputStream.toByteArray();
    Asserts.assertEquals(inputBytes, outputBytes);
    sut.stop();
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ResourceUploadManager(com.tvd12.ezyhttp.core.resources.ResourceUploadManager) ByteArrayOutputStream(java.io.ByteArrayOutputStream) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Example 15 with ResourceUploadManager

use of com.tvd12.ezyhttp.core.resources.ResourceUploadManager in project ezyhttp by youngmonkeys.

the class ResourceUploadManagerTest method drainOfferAgainButErrorTest.

@Test
public void drainOfferAgainButErrorTest() throws Exception {
    // given
    ResourceUploadManager sut = new ResourceUploadManager();
    InputStream inputStream = mock(InputStream.class);
    OutputStream outputStream = mock(OutputStream.class);
    // when
    AtomicBoolean finished = new AtomicBoolean();
    sut.drainAsync(inputStream, outputStream, response -> {
        finished.set(true);
        throw new RuntimeException("just test");
    });
    // then
    while (!finished.get()) {
        EzyThreads.sleep(3);
    }
    sut.stop();
    sut.destroy();
    verify(inputStream, times(1)).read(any());
}
Also used : AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) ResourceUploadManager(com.tvd12.ezyhttp.core.resources.ResourceUploadManager) 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