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