Search in sources :

Example 46 with AsyncResult

use of io.vertx.core.AsyncResult in project vertx-web by vert-x3.

the class OpenAPI3ParametersUnitTest method testQueryFormNoexplodeString.

/**
 * Test: query_form_noexplode_string
 * Expected parameters sent:
 * color: color=blue
 * Expected response: {"color":"blue"}
 * @throws Exception
 */
@Test
public void testQueryFormNoexplodeString() throws Exception {
    routerFactory.addHandlerByOperationId("query_form_noexplode_string", routingContext -> {
        RequestParameters params = routingContext.get("parsedParameters");
        JsonObject res = new JsonObject();
        RequestParameter color_query = params.queryParameter("color");
        assertNotNull(color_query);
        assertTrue(color_query.isString());
        assertEquals(color_query.getString(), "blue");
        res.put("color", color_query.getString());
        routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("content-type", "application/json; charset=utf-8").end(res.encode());
    });
    CountDownLatch latch = new CountDownLatch(1);
    String color_query;
    color_query = "blue";
    startServer();
    apiClient.queryFormNoexplodeString(color_query, (AsyncResult<HttpResponse> ar) -> {
        if (ar.succeeded()) {
            assertEquals(200, ar.result().statusCode());
            assertTrue("Expected: " + new JsonObject("{\"color\":\"blue\"}").encode() + " Actual: " + ar.result().bodyAsJsonObject().encode(), new JsonObject("{\"color\":\"blue\"}").equals(ar.result().bodyAsJsonObject()));
        } else {
            assertTrue(ar.cause().getMessage(), false);
        }
        latch.countDown();
    });
    awaitLatch(latch);
}
Also used : RequestParameter(io.vertx.ext.web.api.RequestParameter) JsonObject(io.vertx.core.json.JsonObject) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncResult(io.vertx.core.AsyncResult) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Example 47 with AsyncResult

use of io.vertx.core.AsyncResult in project vertx-web by vert-x3.

the class OpenAPI3ParametersUnitTest method testHeaderSimpleNoexplodeObject.

/**
 * Test: header_simple_noexplode_object
 * Expected parameters sent:
 * color: R,100,G,200,B,150
 * Expected response: {"color":{"R":"100","G":"200","B":"150"}}
 * @throws Exception
 */
@Test
public void testHeaderSimpleNoexplodeObject() throws Exception {
    routerFactory.addHandlerByOperationId("header_simple_noexplode_object", routingContext -> {
        RequestParameters params = routingContext.get("parsedParameters");
        JsonObject res = new JsonObject();
        RequestParameter color_header = params.headerParameter("color");
        assertNotNull(color_header);
        assertTrue(color_header.isObject());
        Map<String, String> map = new HashMap<>();
        for (String key : color_header.getObjectKeys()) map.put(key, color_header.getObjectValue(key).getString());
        res.put("color", map);
        routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("content-type", "application/json; charset=utf-8").end(res.encode());
    });
    CountDownLatch latch = new CountDownLatch(1);
    Map<String, Object> color_header;
    color_header = new HashMap<>();
    color_header.put("R", "100");
    color_header.put("G", "200");
    color_header.put("B", "150");
    startServer();
    apiClient.headerSimpleNoexplodeObject(color_header, (AsyncResult<HttpResponse> ar) -> {
        if (ar.succeeded()) {
            assertEquals(200, ar.result().statusCode());
            assertTrue("Expected: " + new JsonObject("{\"color\":{\"R\":\"100\",\"G\":\"200\",\"B\":\"150\"}}").encode() + " Actual: " + ar.result().bodyAsJsonObject().encode(), new JsonObject("{\"color\":{\"R\":\"100\",\"G\":\"200\",\"B\":\"150\"}}").equals(ar.result().bodyAsJsonObject()));
        } else {
            assertTrue(ar.cause().getMessage(), false);
        }
        latch.countDown();
    });
    awaitLatch(latch);
}
Also used : HashMap(java.util.HashMap) RequestParameter(io.vertx.ext.web.api.RequestParameter) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncResult(io.vertx.core.AsyncResult) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Example 48 with AsyncResult

use of io.vertx.core.AsyncResult in project vertx-web by vert-x3.

the class OpenAPI3ParametersUnitTest method testQueryPipeDelimitedNoexplodeObject.

/**
 * Test: query_pipeDelimited_noexplode_object
 * Expected parameters sent:
 * color: R|100|G|200|B|150
 * Expected response: {"color":{"R":"100","G":"200","B":"150"}}
 * @throws Exception
 */
@Test
public void testQueryPipeDelimitedNoexplodeObject() throws Exception {
    routerFactory.addHandlerByOperationId("query_pipeDelimited_noexplode_object", routingContext -> {
        RequestParameters params = routingContext.get("parsedParameters");
        JsonObject res = new JsonObject();
        RequestParameter color_query = params.queryParameter("color");
        assertNotNull(color_query);
        assertTrue(color_query.isObject());
        Map<String, String> map = new HashMap<>();
        for (String key : color_query.getObjectKeys()) map.put(key, color_query.getObjectValue(key).getString());
        res.put("color", map);
        routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("content-type", "application/json; charset=utf-8").end(res.encode());
    });
    CountDownLatch latch = new CountDownLatch(1);
    Map<String, Object> color_query;
    color_query = new HashMap<>();
    color_query.put("R", "100");
    color_query.put("G", "200");
    color_query.put("B", "150");
    startServer();
    apiClient.queryPipeDelimitedNoexplodeObject(color_query, (AsyncResult<HttpResponse> ar) -> {
        if (ar.succeeded()) {
            assertEquals(200, ar.result().statusCode());
            assertTrue("Expected: " + new JsonObject("{\"color\":{\"R\":\"100\",\"G\":\"200\",\"B\":\"150\"}}").encode() + " Actual: " + ar.result().bodyAsJsonObject().encode(), new JsonObject("{\"color\":{\"R\":\"100\",\"G\":\"200\",\"B\":\"150\"}}").equals(ar.result().bodyAsJsonObject()));
        } else {
            assertTrue(ar.cause().getMessage(), false);
        }
        latch.countDown();
    });
    awaitLatch(latch);
}
Also used : HashMap(java.util.HashMap) RequestParameter(io.vertx.ext.web.api.RequestParameter) JsonObject(io.vertx.core.json.JsonObject) JsonObject(io.vertx.core.json.JsonObject) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncResult(io.vertx.core.AsyncResult) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Example 49 with AsyncResult

use of io.vertx.core.AsyncResult in project vertx-web by vert-x3.

the class OpenAPI3ParametersUnitTest method testPathSimpleExplodeString.

/**
 * Test: path_simple_explode_string
 * Expected parameters sent:
 * color: blue
 * Expected response: {"color":"blue"}
 * @throws Exception
 */
@Test
public void testPathSimpleExplodeString() throws Exception {
    routerFactory.addHandlerByOperationId("path_simple_explode_string", routingContext -> {
        RequestParameters params = routingContext.get("parsedParameters");
        JsonObject res = new JsonObject();
        RequestParameter color_path = params.pathParameter("color");
        assertNotNull(color_path);
        assertTrue(color_path.isString());
        assertEquals(color_path.getString(), "blue");
        res.put("color", color_path.getString());
        routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("content-type", "application/json; charset=utf-8").end(res.encode());
    });
    CountDownLatch latch = new CountDownLatch(1);
    String color_path;
    color_path = "blue";
    startServer();
    apiClient.pathSimpleExplodeString(color_path, (AsyncResult<HttpResponse> ar) -> {
        if (ar.succeeded()) {
            assertEquals(200, ar.result().statusCode());
            assertTrue("Expected: " + new JsonObject("{\"color\":\"blue\"}").encode() + " Actual: " + ar.result().bodyAsJsonObject().encode(), new JsonObject("{\"color\":\"blue\"}").equals(ar.result().bodyAsJsonObject()));
        } else {
            assertTrue(ar.cause().getMessage(), false);
        }
        latch.countDown();
    });
    awaitLatch(latch);
}
Also used : RequestParameter(io.vertx.ext.web.api.RequestParameter) JsonObject(io.vertx.core.json.JsonObject) CountDownLatch(java.util.concurrent.CountDownLatch) AsyncResult(io.vertx.core.AsyncResult) RequestParameters(io.vertx.ext.web.api.RequestParameters) Test(org.junit.Test)

Example 50 with AsyncResult

use of io.vertx.core.AsyncResult in project gravitee-gateway by gravitee-io.

the class FailoverInvoker method invoke.

@Override
public Request invoke(ExecutionContext executionContext, Request serverRequest, ReadStream<Buffer> stream, Handler<ProxyConnection> connectionHandler) {
    final Request failoverServerRequest = new FailoverRequest(serverRequest);
    circuitBreaker.execute(new io.vertx.core.Handler<Future<ProxyConnection>>() {

        @Override
        public void handle(Future<ProxyConnection> event) {
            invoker.invoke(executionContext, failoverServerRequest, stream, proxyConnection -> {
                proxyConnection.exceptionHandler(event::fail);
                proxyConnection.responseHandler(response -> event.complete(new FailoverProxyConnection(proxyConnection, response)));
            });
        }
    }).setHandler(new io.vertx.core.Handler<AsyncResult<ProxyConnection>>() {

        @Override
        public void handle(AsyncResult<ProxyConnection> event) {
            if (event.failed()) {
                FailoverConnection connection = new FailoverConnection();
                connectionHandler.handle(connection);
                connection.sendBadGatewayResponse();
            } else {
                FailoverProxyConnection proxyConnection = (FailoverProxyConnection) event.result();
                connectionHandler.handle(proxyConnection);
                proxyConnection.sendResponse();
            }
        }
    });
    return failoverServerRequest;
}
Also used : Request(io.gravitee.gateway.api.Request) Handler(io.gravitee.gateway.api.handler.Handler) ProxyConnection(io.gravitee.gateway.api.proxy.ProxyConnection) Future(io.vertx.core.Future) AsyncResult(io.vertx.core.AsyncResult)

Aggregations

AsyncResult (io.vertx.core.AsyncResult)162 Handler (io.vertx.core.Handler)106 Test (org.junit.Test)72 JsonObject (io.vertx.core.json.JsonObject)68 CountDownLatch (java.util.concurrent.CountDownLatch)62 Future (io.vertx.core.Future)59 List (java.util.List)49 RequestParameter (io.vertx.ext.web.api.RequestParameter)48 RequestParameters (io.vertx.ext.web.api.RequestParameters)48 HashMap (java.util.HashMap)45 Map (java.util.Map)42 IOException (java.io.IOException)41 ArrayList (java.util.ArrayList)40 Vertx (io.vertx.core.Vertx)35 Collectors (java.util.stream.Collectors)35 RoutingContext (io.vertx.ext.web.RoutingContext)28 Buffer (io.vertx.core.buffer.Buffer)24 StandardCharsets (java.nio.charset.StandardCharsets)23 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)23 Consumer (java.util.function.Consumer)23