Search in sources :

Example 1 with EzyAnywayInputStreamLoader

use of com.tvd12.ezyfox.stream.EzyAnywayInputStreamLoader in project ezyfox-server-example by tvd12.

the class WheelService method loadWheelFromJsonFile.

private Wheel loadWheelFromJsonFile() {
    ObjectMapper objectMapper = new ObjectMapper();
    InputStream inputStream = new EzyAnywayInputStreamLoader().load("wheel.json");
    try {
        Wheel res = objectMapper.readValue(inputStream, Wheel.class);
        res.setId("wheel");
        return res;
    } catch (IOException e) {
        throw new IllegalStateException("can not load wheel", e);
    }
}
Also used : Wheel(com.tvd12.example.lucky_wheel.entity.Wheel) EzyAnywayInputStreamLoader(com.tvd12.ezyfox.stream.EzyAnywayInputStreamLoader) InputStream(java.io.InputStream) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 2 with EzyAnywayInputStreamLoader

use of com.tvd12.ezyfox.stream.EzyAnywayInputStreamLoader in project ezyhttp by youngmonkeys.

the class ResourceRequestHandlerTest method handleWithDrainExceptionTest.

@Test
public void handleWithDrainExceptionTest() 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, new EzyAnywayInputStreamLoader(), 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);
    doThrow(IOException.class).when(outputStream).write(any(byte[].class), anyInt(), anyInt());
    when(asyncContext.getResponse()).thenReturn(response);
    // when
    sut.handle(arguments);
    // then
    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.INTERNAL_SERVER_ERROR);
    verify(asyncContext, times(1)).getResponse();
    verify(asyncContext, times(1)).complete();
}
Also used : EzyAnywayInputStreamLoader(com.tvd12.ezyfox.stream.EzyAnywayInputStreamLoader) 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)

Aggregations

EzyAnywayInputStreamLoader (com.tvd12.ezyfox.stream.EzyAnywayInputStreamLoader)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 Wheel (com.tvd12.example.lucky_wheel.entity.Wheel)1 ResourceDownloadManager (com.tvd12.ezyhttp.core.resources.ResourceDownloadManager)1 ResourceRequestHandler (com.tvd12.ezyhttp.server.core.handler.ResourceRequestHandler)1 RequestArguments (com.tvd12.ezyhttp.server.core.request.RequestArguments)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 AsyncContext (javax.servlet.AsyncContext)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 Test (org.testng.annotations.Test)1