Search in sources :

Example 11 with ResourceDownloadManager

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

the class ResourceRequestHandlerTest method handleWithDrainExceptionWhenCallTest.

@SuppressWarnings("unchecked")
@Test
public void handleWithDrainExceptionWhenCallTest() throws Exception {
    // given
    String resourcePath = "static/index.html";
    String resourceURI = "/index.html";
    String resourceExtension = "html";
    ResourceDownloadManager downloadManager = mock(ResourceDownloadManager.class);
    doThrow(IllegalStateException.class).when(downloadManager).drainAsync(any(InputStream.class), any(OutputStream.class), any(EzyResultCallback.class));
    int timeout = RandomUtil.randomSmallInt() + 1;
    ResourceRequestHandler sut = new ResourceRequestHandler(resourcePath, resourceURI, resourceExtension, downloadManager, timeout);
    RequestArguments arguments = mock(RequestArguments.class);
    AsyncContext asyncContext = mock(AsyncContext.class);
    when(arguments.getAsyncContext()).thenReturn(asyncContext);
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(asyncContext.getResponse()).thenReturn(response);
    ServletOutputStream outputStream = mock(ServletOutputStream.class);
    when(response.getOutputStream()).thenReturn(outputStream);
    doThrow(IOException.class).when(outputStream).write(any(byte[].class), anyInt(), anyInt());
    when(asyncContext.getResponse()).thenReturn(response);
    // when
    sut.handle(arguments);
    // then
    verify(arguments, times(1)).getAsyncContext();
    verify(response, times(1)).getOutputStream();
    verify(response, times(1)).setStatus(StatusCodes.INTERNAL_SERVER_ERROR);
    verify(asyncContext, times(1)).setTimeout(timeout);
    verify(asyncContext, times(1)).getResponse();
    verify(asyncContext, times(1)).complete();
}
Also used : EzyResultCallback(com.tvd12.ezyfox.concurrent.callback.EzyResultCallback) ResourceDownloadManager(com.tvd12.ezyhttp.core.resources.ResourceDownloadManager) ResourceRequestHandler(com.tvd12.ezyhttp.server.core.handler.ResourceRequestHandler) ServletOutputStream(javax.servlet.ServletOutputStream) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) ServletOutputStream(javax.servlet.ServletOutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) RequestArguments(com.tvd12.ezyhttp.server.core.request.RequestArguments) Test(org.testng.annotations.Test)

Example 12 with ResourceDownloadManager

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

the class ResourceRequestHandlerTest method handleAsyncTest.

@Test
public void handleAsyncTest() throws Exception {
    // given
    String resourcePath = "static/index.html";
    String resourceURI = "/index.html";
    String resourceExtension = "html";
    ResourceDownloadManager downloadManager = new ResourceDownloadManager();
    ResourceRequestHandler sut = new ResourceRequestHandler(resourcePath, resourceURI, resourceExtension, downloadManager);
    RequestArguments arguments = mock(RequestArguments.class);
    AsyncContext asyncContext = mock(AsyncContext.class);
    when(arguments.getAsyncContext()).thenReturn(asyncContext);
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(asyncContext.getResponse()).thenReturn(response);
    ServletOutputStream outputStream = mock(ServletOutputStream.class);
    when(response.getOutputStream()).thenReturn(outputStream);
    when(asyncContext.getResponse()).thenReturn(response);
    // when
    sut.handle(arguments);
    // then
    Asserts.assertTrue(sut.isAsync());
    Asserts.assertEquals(HttpMethod.GET, sut.getMethod());
    Asserts.assertEquals("/index.html", sut.getRequestURI());
    Asserts.assertEquals(ContentTypes.TEXT_HTML_UTF8, sut.getResponseContentType());
    Thread.sleep(300);
    downloadManager.stop();
    verify(arguments, times(1)).getAsyncContext();
    verify(response, times(1)).getOutputStream();
    verify(response, times(1)).setStatus(StatusCodes.OK);
    verify(asyncContext, times(1)).getResponse();
    verify(asyncContext, times(1)).complete();
}
Also used : ResourceDownloadManager(com.tvd12.ezyhttp.core.resources.ResourceDownloadManager) ResourceRequestHandler(com.tvd12.ezyhttp.server.core.handler.ResourceRequestHandler) ServletOutputStream(javax.servlet.ServletOutputStream) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) RequestArguments(com.tvd12.ezyhttp.server.core.request.RequestArguments) Test(org.testng.annotations.Test)

Example 13 with ResourceDownloadManager

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

the class ApplicationContextBuilderTest method testNotEnable.

@Test
public void testNotEnable() {
    // given
    System.setProperty(EzyBeanContext.ACTIVE_PROFILES_KEY, "disable");
    ApplicationContext applicationContext = new ApplicationContextBuilder().scan("i_dont_know").build();
    System.setProperty(EzyBeanContext.ACTIVE_PROFILES_KEY, "enable");
    EzyBeanContext beanContext = applicationContext.getBeanContext();
    // when
    Boolean managementEnable = beanContext.getProperty("management.enable", boolean.class);
    Boolean resourceEnable = beanContext.getProperty("resources.enable", boolean.class);
    Boolean resourceUploadEnable = beanContext.getProperty("resources.upload.enable", boolean.class);
    ResourceResolver resourceResolver = beanContext.getSingleton(ResourceResolver.class);
    ResourceDownloadManager resourceDownloadManager = beanContext.getSingleton(ResourceDownloadManager.class);
    Set<String> packagesToScan = beanContext.getPackagesToScan();
    // then
    Asserts.assertFalse(managementEnable);
    Asserts.assertFalse(resourceEnable);
    Asserts.assertFalse(resourceUploadEnable);
    Asserts.assertNull(resourceResolver);
    Asserts.assertNull(resourceDownloadManager);
    Asserts.assertEquals(Sets.newHashSet("i_dont_know", "com.tvd12.ezyhttp.server", "com.tvd12.ezyhttp.server.core.test", "com.tvd12.ezyhttp.server.core.test.config", "com.tvd12.ezyhttp.server.core.test.event", "com.tvd12.ezyhttp.server.core.test.api"), packagesToScan);
    applicationContext.destroy();
}
Also used : ApplicationContext(com.tvd12.ezyhttp.server.core.ApplicationContext) ResourceDownloadManager(com.tvd12.ezyhttp.core.resources.ResourceDownloadManager) EzyBeanContext(com.tvd12.ezyfox.bean.EzyBeanContext) ResourceResolver(com.tvd12.ezyhttp.server.core.resources.ResourceResolver) ApplicationContextBuilder(com.tvd12.ezyhttp.server.core.ApplicationContextBuilder) Test(org.testng.annotations.Test)

Example 14 with ResourceDownloadManager

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

the class EzyHttpApplicationTest method test.

@Test
public void test() throws Exception {
    // given
    EzyHttpApplication sut = EzyHttpApplication.start(EzyHttpApplicationTest.class);
    ApplicationContext applicationContext = sut.getApplicationContext();
    EzyBeanContext beanContext = applicationContext.getBeanContext();
    // when
    int actualOneProp = beanContext.getProperty("one", int.class);
    boolean managementEnable = beanContext.getProperty("management.enable", boolean.class);
    UserService userService = beanContext.getSingleton(UserService.class);
    ViewContextBuilder viewContextBuilder = beanContext.getSingleton(ViewContextBuilder.class);
    ResourceResolver resourceResolver = beanContext.getSingleton(ResourceResolver.class);
    ResourceDownloadManager resourceDownloadManager = beanContext.getSingleton(ResourceDownloadManager.class);
    // then
    Asserts.assertEquals(1, actualOneProp);
    Asserts.assertTrue(managementEnable);
    Asserts.assertNotNull(userService);
    Asserts.assertNotNull(viewContextBuilder);
    Asserts.assertNotNull(resourceDownloadManager);
    Asserts.assertEquals(4, resourceResolver.getResources().size());
    sut.stop();
}
Also used : ApplicationContext(com.tvd12.ezyhttp.server.core.ApplicationContext) ResourceDownloadManager(com.tvd12.ezyhttp.core.resources.ResourceDownloadManager) EzyHttpApplication(com.tvd12.ezyhttp.server.core.EzyHttpApplication) EzyBeanContext(com.tvd12.ezyfox.bean.EzyBeanContext) UserService(com.tvd12.ezyhttp.server.core.test.service.UserService) ResourceResolver(com.tvd12.ezyhttp.server.core.resources.ResourceResolver) ViewContextBuilder(com.tvd12.ezyhttp.server.core.view.ViewContextBuilder) Test(org.testng.annotations.Test)

Example 15 with ResourceDownloadManager

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

the class ResourceDownloadManagerTest method drainOfferAgainGetMaxQueueCapacityAndFutureNullTest.

@Test
public void drainOfferAgainGetMaxQueueCapacityAndFutureNullTest() throws Exception {
    // given
    ResourceDownloadManager sut = new ResourceDownloadManager(1, 1, 1);
    InputStream inputStream = mock(InputStream.class);
    when(inputStream.read(any())).thenReturn(1);
    OutputStream outputStream = mock(OutputStream.class);
    EzyFutureMap<?> futureMap = FieldUtil.getFieldValue(sut, "futureMap");
    // when
    for (int i = 0; i < 1000; ++i) {
        try {
            sut.drainAsync(inputStream, outputStream, response -> {
            });
        } catch (Exception ignored) {
        }
        futureMap.clear();
    }
    // then
    sut.stop();
    sut.destroy();
    verify(inputStream, atLeastOnce()).read(any());
    verify(outputStream, atLeastOnce()).write(any(), anyInt(), anyInt());
}
Also used : ResourceDownloadManager(com.tvd12.ezyhttp.core.resources.ResourceDownloadManager) Test(org.testng.annotations.Test) BaseTest(com.tvd12.test.base.BaseTest)

Aggregations

ResourceDownloadManager (com.tvd12.ezyhttp.core.resources.ResourceDownloadManager)18 Test (org.testng.annotations.Test)17 BaseTest (com.tvd12.test.base.BaseTest)10 ResourceRequestHandler (com.tvd12.ezyhttp.server.core.handler.ResourceRequestHandler)5 ResourceResolver (com.tvd12.ezyhttp.server.core.resources.ResourceResolver)5 EzyBeanContext (com.tvd12.ezyfox.bean.EzyBeanContext)4 ApplicationContext (com.tvd12.ezyhttp.server.core.ApplicationContext)4 RequestArguments (com.tvd12.ezyhttp.server.core.request.RequestArguments)3 UserService (com.tvd12.ezyhttp.server.core.test.service.UserService)3 ViewContextBuilder (com.tvd12.ezyhttp.server.core.view.ViewContextBuilder)3 AsyncContext (javax.servlet.AsyncContext)3 ServletOutputStream (javax.servlet.ServletOutputStream)3 HttpServletResponse (javax.servlet.http.HttpServletResponse)3 ApplicationContextBuilder (com.tvd12.ezyhttp.server.core.ApplicationContextBuilder)2 EzyHttpApplication (com.tvd12.ezyhttp.server.core.EzyHttpApplication)2 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)2 EzyFutureMap (com.tvd12.ezyfox.concurrent.EzyFutureMap)1 EzyResultCallback (com.tvd12.ezyfox.concurrent.callback.EzyResultCallback)1 EzyAnywayInputStreamLoader (com.tvd12.ezyfox.stream.EzyAnywayInputStreamLoader)1 RequestHandler (com.tvd12.ezyhttp.server.core.handler.RequestHandler)1