use of io.vertx.core.AsyncResult in project nem2-sdk-java by nemtech.
the class NamespaceRepositoryVertxImpl method search.
@Override
public Observable<Page<NamespaceInfo>> search(NamespaceSearchCriteria criteria) {
String ownerAddress = toDto(criteria.getOwnerAddress());
NamespaceRegistrationTypeEnum registrationType = criteria.getRegistrationType() == null ? null : NamespaceRegistrationTypeEnum.fromValue(criteria.getRegistrationType().getValue());
String level0 = criteria.getLevel0();
AliasTypeEnum aliasType = criteria.getAliasType() == null ? null : AliasTypeEnum.fromValue(criteria.getAliasType().getValue());
Integer pageSize = criteria.getPageSize();
Integer pageNumber = criteria.getPageNumber();
String offset = criteria.getOffset();
Order order = toDto(criteria.getOrder());
Consumer<Handler<AsyncResult<NamespacePage>>> callback = handler -> getClient().searchNamespaces(ownerAddress, registrationType, level0, aliasType, pageSize, pageNumber, offset, order, handler);
return exceptionHandling(call(callback).map(page -> this.toPage(page.getPagination(), page.getData().stream().map(this::toNamespaceInfo).collect(Collectors.toList()))));
}
use of io.vertx.core.AsyncResult in project nem2-sdk-java by nemtech.
the class RestrictionAccountRepositoryVertxImpl method search.
@Override
public Observable<Page<AccountRestrictions>> search(AccountRestrictionSearchCriteria criteria) {
String address = toDto(criteria.getAddress());
Integer pageSize = criteria.getPageSize();
Integer pageNumber = criteria.getPageNumber();
String offset = criteria.getOffset();
Order order = toDto(criteria.getOrder());
Consumer<Handler<AsyncResult<AccountRestrictionsPage>>> handlerConsumer = (h) -> getClient().searchAccountRestrictions(address, pageSize, pageNumber, offset, order, h);
return this.call(handlerConsumer, this::toPage);
}
use of io.vertx.core.AsyncResult in project nem2-sdk-java by nemtech.
the class AbstractVertxRespositoryTest method mockErrorCodeRawResponse.
/**
* Mocks the api client telling that the next time there is remote call, an error should be
* returned.
*
* @param statusCode the status code of the response (404 for example)
* @param errorResponse the raw response, it may or may not be a json string.
*/
protected void mockErrorCodeRawResponse(int statusCode, String errorResponse) {
String reasonPhrase = HttpStatus.valueOf(statusCode).getReasonPhrase();
VertxHttpHeaders headers = new VertxHttpHeaders();
ApiException exception = new ApiException(reasonPhrase, statusCode, headers, errorResponse);
Mockito.doAnswer((Answer<Void>) invocationOnMock -> {
Handler<AsyncResult<Object>> resultHandler = (Handler<AsyncResult<Object>>) invocationOnMock.getArguments()[invocationOnMock.getArguments().length - 1];
resultHandler.handle(Future.failedFuture(exception));
return null;
}).when(apiClientMock).invokeAPI(Mockito.anyString(), Mockito.anyString(), Mockito.anyList(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any());
}
use of io.vertx.core.AsyncResult in project vert.x by eclipse.
the class Http2ServerTest method testPushPromise.
private void testPushPromise(Http2Headers requestHeaders, BiConsumer<HttpServerResponse, Handler<AsyncResult<HttpServerResponse>>> pusher, Consumer<Http2Headers> headerChecker) throws Exception {
Context ctx = vertx.getOrCreateContext();
server.requestHandler(req -> {
Handler<AsyncResult<HttpServerResponse>> handler = ar -> {
assertSame(ctx, Vertx.currentContext());
assertTrue(ar.succeeded());
HttpServerResponse response = ar.result();
response.end("the_content");
assertIllegalStateException(() -> response.push(HttpMethod.GET, "/wibble2", resp -> {
}));
};
pusher.accept(req.response(), handler);
});
startServer(ctx);
TestClient client = new TestClient();
ChannelFuture fut = client.connect(DEFAULT_HTTPS_PORT, DEFAULT_HTTPS_HOST, request -> {
int id = request.nextStreamId();
Http2ConnectionEncoder encoder = request.encoder;
encoder.writeHeaders(request.context, id, requestHeaders, 0, true, request.context.newPromise());
Map<Integer, Http2Headers> pushed = new HashMap<>();
request.decoder.frameListener(new Http2FrameAdapter() {
@Override
public void onPushPromiseRead(ChannelHandlerContext ctx, int streamId, int promisedStreamId, Http2Headers headers, int padding) throws Http2Exception {
pushed.put(promisedStreamId, headers);
}
@Override
public int onDataRead(ChannelHandlerContext ctx, int streamId, ByteBuf data, int padding, boolean endOfStream) throws Http2Exception {
int delta = super.onDataRead(ctx, streamId, data, padding, endOfStream);
String content = data.toString(StandardCharsets.UTF_8);
vertx.runOnContext(v -> {
assertEquals(Collections.singleton(streamId), pushed.keySet());
assertEquals("the_content", content);
Http2Headers pushedHeaders = pushed.get(streamId);
headerChecker.accept(pushedHeaders);
testComplete();
});
return delta;
}
});
});
fut.sync();
await();
}
use of io.vertx.core.AsyncResult in project vert.x by eclipse.
the class FutureTest method testDefaultCompleter.
@Test
public void testDefaultCompleter() {
AsyncResult<Object> succeededAsyncResult = new AsyncResult<Object>() {
Object result = new Object();
public Object result() {
return result;
}
public Throwable cause() {
throw new UnsupportedOperationException();
}
public boolean succeeded() {
return true;
}
public boolean failed() {
throw new UnsupportedOperationException();
}
public <U> AsyncResult<U> map(Function<Object, U> mapper) {
throw new UnsupportedOperationException();
}
public <V> AsyncResult<V> map(V value) {
throw new UnsupportedOperationException();
}
};
AsyncResult<Object> failedAsyncResult = new AsyncResult<Object>() {
Throwable cause = new Throwable();
public Object result() {
throw new UnsupportedOperationException();
}
public Throwable cause() {
return cause;
}
public boolean succeeded() {
return false;
}
public boolean failed() {
throw new UnsupportedOperationException();
}
public <U> AsyncResult<U> map(Function<Object, U> mapper) {
throw new UnsupportedOperationException();
}
public <V> AsyncResult<V> map(V value) {
throw new UnsupportedOperationException();
}
};
class DefaultCompleterTestFuture<T> implements Future<T> {
boolean succeeded;
boolean failed;
T result;
Throwable cause;
public boolean isComplete() {
throw new UnsupportedOperationException();
}
public Future<T> setHandler(Handler<AsyncResult<T>> handler) {
throw new UnsupportedOperationException();
}
public void complete(T result) {
if (!tryComplete(result)) {
throw new IllegalStateException();
}
}
public void complete() {
if (!tryComplete()) {
throw new IllegalStateException();
}
}
public void fail(Throwable cause) {
if (!tryFail(cause)) {
throw new IllegalStateException();
}
}
public void fail(String failureMessage) {
if (!tryFail(failureMessage)) {
throw new IllegalStateException();
}
}
public boolean tryComplete(T result) {
if (succeeded || failed) {
return false;
}
succeeded = true;
this.result = result;
return true;
}
public boolean tryComplete() {
throw new UnsupportedOperationException();
}
public boolean tryFail(Throwable cause) {
if (succeeded || failed) {
return false;
}
failed = true;
this.cause = cause;
return true;
}
public boolean tryFail(String failureMessage) {
throw new UnsupportedOperationException();
}
public T result() {
throw new UnsupportedOperationException();
}
public Throwable cause() {
throw new UnsupportedOperationException();
}
public boolean succeeded() {
throw new UnsupportedOperationException();
}
public boolean failed() {
throw new UnsupportedOperationException();
}
public void handle(AsyncResult<T> asyncResult) {
if (asyncResult.succeeded()) {
complete(asyncResult.result());
} else {
fail(asyncResult.cause());
}
}
}
DefaultCompleterTestFuture<Object> successFuture = new DefaultCompleterTestFuture<>();
successFuture.completer().handle(succeededAsyncResult);
assertTrue(successFuture.succeeded);
assertEquals(succeededAsyncResult.result(), successFuture.result);
DefaultCompleterTestFuture<Object> failureFuture = new DefaultCompleterTestFuture<>();
failureFuture.completer().handle(failedAsyncResult);
assertTrue(failureFuture.failed);
assertEquals(failedAsyncResult.cause(), failureFuture.cause);
}
Aggregations