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();
}
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();
}
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();
}
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();
}
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());
}
Aggregations