Search in sources :

Example 1 with EzyWrap

use of com.tvd12.ezyfox.util.EzyWrap in project ezyhttp by youngmonkeys.

the class HttpClientProxyTest method executeJsonTest.

@Test
public void executeJsonTest() throws Exception {
    // given
    HttpClientProxy sut = newClientProxy();
    GetRequest request = new GetRequest().setConnectTimeout(15000).setResponseType(TestResponse.class).setResponseType(StatusCodes.OK, TestResponse.class).setURL("http://127.0.0.1:18081/greet?who=Monkey");
    // when
    CountDownLatch countDownLatch = new CountDownLatch(1);
    EzyWrap<TestResponse> wrap = new EzyWrap<>();
    sut.execute(request, new RequestCallback<ResponseEntity>() {

        @Override
        public void onResponse(ResponseEntity response) {
            wrap.setValue(response.getBody());
            countDownLatch.countDown();
        }

        @Override
        public void onException(Exception e) {
        }
    });
    countDownLatch.await();
    // then
    TestResponse expectation = new TestResponse("Greet Monkey!");
    Asserts.assertEquals(expectation, wrap.getValue());
    sut.close();
    sut.stop();
}
Also used : EzyWrap(com.tvd12.ezyfox.util.EzyWrap) ResponseEntity(com.tvd12.ezyhttp.core.response.ResponseEntity) HttpClientProxy(com.tvd12.ezyhttp.client.HttpClientProxy) CountDownLatch(java.util.concurrent.CountDownLatch) DownloadCancelledException(com.tvd12.ezyhttp.client.exception.DownloadCancelledException) RequestQueueFullException(com.tvd12.ezyhttp.client.exception.RequestQueueFullException) UnknownHostException(java.net.UnknownHostException) BadRequestException(com.tvd12.ezyfox.exception.BadRequestException) ClientNotActiveException(com.tvd12.ezyhttp.client.exception.ClientNotActiveException) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) BaseTest(com.tvd12.test.base.BaseTest)

Example 2 with EzyWrap

use of com.tvd12.ezyfox.util.EzyWrap in project ezyhttp by youngmonkeys.

the class HttpClientProxyTest method fireJsonButExceptionInCallbackTest.

@Test
public void fireJsonButExceptionInCallbackTest() throws Exception {
    // given
    HttpClientProxy sut = newClientProxy();
    GetRequest request = new GetRequest().setConnectTimeout(15000).setResponseType(TestResponse.class).setResponseType(StatusCodes.OK, TestResponse.class).setURL("http://127.0.0.1:18081/greet?who=Monkey");
    // when
    CountDownLatch countDownLatch = new CountDownLatch(1);
    EzyWrap<TestResponse> wrap = new EzyWrap<>();
    sut.fire(request, new RequestCallback<TestResponse>() {

        @Override
        public void onResponse(TestResponse response) {
            wrap.setValue(response);
            countDownLatch.countDown();
            throw new RuntimeException("just test");
        }

        @Override
        public void onException(Exception e) {
        }
    });
    countDownLatch.await();
    Thread.sleep(100);
    // then
    TestResponse expectation = new TestResponse("Greet Monkey!");
    Asserts.assertEquals(expectation, wrap.getValue());
    sut.close();
    sut.stop();
}
Also used : EzyWrap(com.tvd12.ezyfox.util.EzyWrap) HttpClientProxy(com.tvd12.ezyhttp.client.HttpClientProxy) CountDownLatch(java.util.concurrent.CountDownLatch) DownloadCancelledException(com.tvd12.ezyhttp.client.exception.DownloadCancelledException) RequestQueueFullException(com.tvd12.ezyhttp.client.exception.RequestQueueFullException) UnknownHostException(java.net.UnknownHostException) BadRequestException(com.tvd12.ezyfox.exception.BadRequestException) ClientNotActiveException(com.tvd12.ezyhttp.client.exception.ClientNotActiveException) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest) BaseTest(com.tvd12.test.base.BaseTest)

Example 3 with EzyWrap

use of com.tvd12.ezyfox.util.EzyWrap in project ezyhttp by youngmonkeys.

the class BlockingServletTest method doGetResponseContentTypeNull.

@Test
public void doGetResponseContentTypeNull() throws Exception {
    // given
    ComponentManager componentManager = ComponentManager.getInstance();
    componentManager.setServerPort(PORT);
    BlockingServlet sut = new BlockingServlet();
    sut.init();
    String requestURI = "/get";
    HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getMethod()).thenReturn(HttpMethod.GET.toString());
    when(request.getRequestURI()).thenReturn(requestURI);
    when(request.getServerPort()).thenReturn(PORT);
    when(request.getParameterNames()).thenReturn(Collections.enumeration(Collections.singletonList("param")));
    when(request.getParameter("param")).thenReturn("ParameterValue");
    when(request.getParameterValues("param")).thenReturn(new String[] { "ParameterValue" });
    when(request.getHeaderNames()).thenReturn(Collections.enumeration(Collections.singletonList("header")));
    when(request.getHeader("header")).thenReturn("HeaderValue");
    when(request.getCookies()).thenReturn(new Cookie[] { new Cookie("cookie", "CookieValue") });
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getContentType()).thenReturn(ContentTypes.APPLICATION_JSON);
    ServletOutputStream outputStream = mock(ServletOutputStream.class);
    when(response.getOutputStream()).thenReturn(outputStream);
    AsyncContext asyncContext = mock(AsyncContext.class);
    when(request.startAsync(request, response)).thenReturn(asyncContext);
    when(request.isAsyncStarted()).thenReturn(true);
    EzyWrap<AsyncListener> asyncListener = new EzyWrap<>();
    doAnswer(it -> {
        asyncListener.setValue(it.getArgumentAt(0, AsyncListener.class));
        return null;
    }).when(asyncContext).addListener(any(AsyncListener.class));
    RequestHandlerManager requestHandlerManager = componentManager.getRequestHandlerManager();
    GetRequestHandlerContentTypeNull requestHandler = new GetRequestHandlerContentTypeNull();
    requestHandlerManager.addHandler(new RequestURI(HttpMethod.GET, requestURI, false), requestHandler);
    // when
    sut.service(request, response);
    asyncListener.getValue().onComplete(new AsyncEvent(asyncContext));
    // then
    verify(request, times(1)).getMethod();
    verify(request, times(1)).getRequestURI();
    verify(asyncContext, times(1)).addListener(any(AsyncListener.class));
    componentManager.destroy();
}
Also used : Cookie(javax.servlet.http.Cookie) RequestCookie(com.tvd12.ezyhttp.server.core.annotation.RequestCookie) EzyWrap(com.tvd12.ezyfox.util.EzyWrap) ServletOutputStream(javax.servlet.ServletOutputStream) BlockingServlet(com.tvd12.ezyhttp.server.core.servlet.BlockingServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) ToString(lombok.ToString) AsyncEvent(javax.servlet.AsyncEvent) HttpServletRequest(javax.servlet.http.HttpServletRequest) AsyncListener(javax.servlet.AsyncListener) RequestHandlerManager(com.tvd12.ezyhttp.server.core.manager.RequestHandlerManager) ComponentManager(com.tvd12.ezyhttp.server.core.manager.ComponentManager) RequestURI(com.tvd12.ezyhttp.server.core.request.RequestURI) Test(org.testng.annotations.Test)

Example 4 with EzyWrap

use of com.tvd12.ezyfox.util.EzyWrap in project ezyhttp by youngmonkeys.

the class BlockingServletTest method doGetResponseContentTypeNullAndPostHandleRequestError.

@Test
public void doGetResponseContentTypeNullAndPostHandleRequestError() throws Exception {
    // given
    ComponentManager componentManager = ComponentManager.getInstance();
    componentManager.setServerPort(PORT);
    int defaultAsyncTimeout = RandomUtil.randomSmallInt() + 1;
    componentManager.setAsyncDefaultTimeout(defaultAsyncTimeout);
    BlockingServlet sut = new BlockingServlet();
    sut.init();
    String requestURI = "/get";
    HttpServletRequest request = mock(HttpServletRequest.class);
    when(request.getMethod()).thenReturn(HttpMethod.GET.toString());
    when(request.getRequestURI()).thenReturn(requestURI);
    when(request.getServerPort()).thenReturn(PORT);
    when(request.getParameterNames()).thenReturn(Collections.enumeration(Collections.singletonList("param")));
    when(request.getParameter("param")).thenReturn("ParameterValue");
    when(request.getParameterValues("param")).thenReturn(new String[] { "ParameterValue" });
    when(request.getHeaderNames()).thenReturn(Collections.enumeration(Collections.singletonList("header")));
    when(request.getHeader("header")).thenReturn("HeaderValue");
    when(request.getCookies()).thenReturn(new Cookie[] { new Cookie("cookie", "CookieValue") });
    HttpServletResponse response = mock(HttpServletResponse.class);
    when(response.getContentType()).thenReturn(ContentTypes.APPLICATION_JSON);
    ServletOutputStream outputStream = mock(ServletOutputStream.class);
    when(response.getOutputStream()).thenReturn(outputStream);
    AsyncContext asyncContext = mock(AsyncContext.class);
    when(request.startAsync(request, response)).thenReturn(asyncContext);
    when(request.isAsyncStarted()).thenReturn(true);
    EzyWrap<AsyncListener> asyncListener = new EzyWrap<>();
    doAnswer(it -> {
        asyncListener.setValue(it.getArgumentAt(0, AsyncListener.class));
        return null;
    }).when(asyncContext).addListener(any(AsyncListener.class));
    RequestHandlerManager requestHandlerManager = componentManager.getRequestHandlerManager();
    GetRequestHandlerContentTypeNull requestHandler = new GetRequestHandlerContentTypeNull();
    requestHandlerManager.addHandler(new RequestURI(HttpMethod.GET, requestURI, false), requestHandler);
    // when
    sut.service(request, response);
    RequestInterceptor interceptor = mock(RequestInterceptor.class);
    doThrow(IllegalStateException.class).when(interceptor).postHandle(any(), any());
    componentManager.getInterceptorManager().addRequestInterceptors(Collections.singletonList(interceptor));
    asyncListener.getValue().onComplete(new AsyncEvent(asyncContext));
    // then
    verify(request, times(1)).getMethod();
    verify(request, times(2)).getRequestURI();
    verify(asyncContext, times(1)).addListener(any(AsyncListener.class));
    verify(asyncContext, times(1)).setTimeout(defaultAsyncTimeout);
    componentManager.destroy();
}
Also used : Cookie(javax.servlet.http.Cookie) RequestCookie(com.tvd12.ezyhttp.server.core.annotation.RequestCookie) EzyWrap(com.tvd12.ezyfox.util.EzyWrap) ServletOutputStream(javax.servlet.ServletOutputStream) BlockingServlet(com.tvd12.ezyhttp.server.core.servlet.BlockingServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) AsyncContext(javax.servlet.AsyncContext) ToString(lombok.ToString) RequestInterceptor(com.tvd12.ezyhttp.server.core.interceptor.RequestInterceptor) AsyncEvent(javax.servlet.AsyncEvent) HttpServletRequest(javax.servlet.http.HttpServletRequest) AsyncListener(javax.servlet.AsyncListener) RequestHandlerManager(com.tvd12.ezyhttp.server.core.manager.RequestHandlerManager) ComponentManager(com.tvd12.ezyhttp.server.core.manager.ComponentManager) RequestURI(com.tvd12.ezyhttp.server.core.request.RequestURI) Test(org.testng.annotations.Test)

Example 5 with EzyWrap

use of com.tvd12.ezyfox.util.EzyWrap in project dahlia by youngmonkeys.

the class CommandFindHandler method handle.

@Override
public Object handle(CommandFind command) {
    int collectionId = command.getCollectionId();
    Collection collection = databases.getCollection(collectionId);
    if (collection == null)
        throw new CollectionNotFoundException(collectionId);
    EzyObject query = command.getQuery();
    Predicate<EzyObject> predicate = queryToPredicate.toPredicate(query);
    CollectionSetting setting = collection.getSetting();
    CollectionStorage collectionStorage = storage.getCollectionStorage(collectionId);
    FieldSetting sId = setting.getId();
    Map<String, FieldSetting> sFields = setting.getFields();
    EzyObject options = command.getOptions();
    int skip = options.get(OptionFields.SKIP, int.class, 0);
    int limit = options.get(OptionFields.LIMIT, int.class, 25);
    EzyObject sortBy = options.get(OptionFields.SORT, EzyObject.class, EMPTY_OBJECT);
    if (sortBy.isEmpty()) {
        EzyArray answer = EzyEntityFactory.newArray();
        EzyWrap<Integer> count = new EzyWrap<>(0);
        synchronized (collection) {
            collection.forEach(new RecordConsumer() {

                @Override
                public void accept(Record r) {
                    EzyObject value = collectionStorage.readRecord(r, sId, sFields);
                    boolean accepted = predicate.test(value);
                    if (accepted) {
                        int currentCount = count.getValue();
                        if (currentCount >= skip)
                            answer.add(value);
                        count.setValue(currentCount + 1);
                    }
                }

                @Override
                public boolean next() {
                    int currentSize = answer.size();
                    return currentSize < limit;
                }
            });
        }
        return answer;
    } else {
        List<EzyObject> found = new ArrayList<>();
        synchronized (collection) {
            collection.forEach(new RecordConsumer() {

                @Override
                public void accept(Record r) {
                    EzyObject value = collectionStorage.readRecord(r, sId, sFields);
                    boolean accepted = predicate.test(value);
                    if (accepted)
                        found.add(value);
                }
            });
        }
        found.sort(sortByComparator(sortBy));
        return getResult(found, skip, limit);
    }
}
Also used : EzyWrap(com.tvd12.ezyfox.util.EzyWrap) ArrayList(java.util.ArrayList) CollectionStorage(com.tvd12.dahlia.core.storage.CollectionStorage) RecordConsumer(com.tvd12.dahlia.core.function.RecordConsumer) CollectionNotFoundException(com.tvd12.dahlia.exception.CollectionNotFoundException) FieldSetting(com.tvd12.dahlia.core.setting.FieldSetting) CollectionSetting(com.tvd12.dahlia.core.setting.CollectionSetting) EzyArray(com.tvd12.ezyfox.entity.EzyArray) Collection(com.tvd12.dahlia.core.entity.Collection) Record(com.tvd12.dahlia.core.entity.Record) EzyObject(com.tvd12.ezyfox.entity.EzyObject)

Aggregations

EzyWrap (com.tvd12.ezyfox.util.EzyWrap)9 Test (org.testng.annotations.Test)7 BadRequestException (com.tvd12.ezyfox.exception.BadRequestException)5 HttpClientProxy (com.tvd12.ezyhttp.client.HttpClientProxy)5 ClientNotActiveException (com.tvd12.ezyhttp.client.exception.ClientNotActiveException)5 DownloadCancelledException (com.tvd12.ezyhttp.client.exception.DownloadCancelledException)5 RequestQueueFullException (com.tvd12.ezyhttp.client.exception.RequestQueueFullException)5 BaseTest (com.tvd12.test.base.BaseTest)5 UnknownHostException (java.net.UnknownHostException)5 CountDownLatch (java.util.concurrent.CountDownLatch)5 BeforeTest (org.testng.annotations.BeforeTest)5 Collection (com.tvd12.dahlia.core.entity.Collection)2 Record (com.tvd12.dahlia.core.entity.Record)2 RecordConsumer (com.tvd12.dahlia.core.function.RecordConsumer)2 CollectionSetting (com.tvd12.dahlia.core.setting.CollectionSetting)2 FieldSetting (com.tvd12.dahlia.core.setting.FieldSetting)2 CollectionStorage (com.tvd12.dahlia.core.storage.CollectionStorage)2 CollectionNotFoundException (com.tvd12.dahlia.exception.CollectionNotFoundException)2 EzyObject (com.tvd12.ezyfox.entity.EzyObject)2 ResponseEntity (com.tvd12.ezyhttp.core.response.ResponseEntity)2