Search in sources :

Example 11 with HttpResponse

use of io.vertx.ext.web.client.HttpResponse in project vertx-web by vert-x3.

the class OpenAPI3ParametersUnitTest method testPathMultiSimpleMatrix.

/**
 * Test: path_multi_simple_matrix
 * Expected parameters sent:
 * color_simple: blue
 * color_matrix: ;color=blue,black,brown
 * Expected response: {"color_simple":"blue","color_matrix":["blue","black","brown"]}
 * @throws Exception
 */
@Test
public void testPathMultiSimpleMatrix() throws Exception {
    routerFactory.addHandlerByOperationId("path_multi_simple_matrix", routingContext -> {
        RequestParameters params = routingContext.get("parsedParameters");
        JsonObject res = new JsonObject();
        RequestParameter colorSimple_path = params.pathParameter("color_simple");
        assertNotNull(colorSimple_path);
        assertTrue(colorSimple_path.isString());
        assertEquals(colorSimple_path.getString(), "blue");
        res.put("color_simple", colorSimple_path.getString());
        RequestParameter colorMatrix_path = params.pathParameter("color_matrix");
        assertNotNull(colorMatrix_path);
        assertTrue(colorMatrix_path.isArray());
        res.put("color_matrix", new JsonArray(colorMatrix_path.getArray().stream().map(param -> param.getString()).collect(Collectors.toList())));
        routingContext.response().setStatusCode(200).setStatusMessage("OK").putHeader("content-type", "application/json; charset=utf-8").end(res.encode());
    });
    CountDownLatch latch = new CountDownLatch(1);
    String colorSimple_path;
    colorSimple_path = "blue";
    List<Object> colorMatrix_path;
    colorMatrix_path = new ArrayList<>();
    colorMatrix_path.add("blue");
    colorMatrix_path.add("black");
    colorMatrix_path.add("brown");
    startServer();
    apiClient.pathMultiSimpleMatrix(colorSimple_path, colorMatrix_path, (AsyncResult<HttpResponse> ar) -> {
        if (ar.succeeded()) {
            assertEquals(200, ar.result().statusCode());
            assertTrue("Expected: " + new JsonObject("{\"color_simple\":\"blue\",\"color_matrix\":[\"blue\",\"black\",\"brown\"]}").encode() + " Actual: " + ar.result().bodyAsJsonObject().encode(), new JsonObject("{\"color_simple\":\"blue\",\"color_matrix\":[\"blue\",\"black\",\"brown\"]}").equals(ar.result().bodyAsJsonObject()));
        } else {
            assertTrue(ar.cause().getMessage(), false);
        }
        latch.countDown();
    });
    awaitLatch(latch);
}
Also used : JsonArray(io.vertx.core.json.JsonArray) HttpResponse(io.vertx.ext.web.client.HttpResponse) HashMap(java.util.HashMap) RoutingContext(io.vertx.ext.web.RoutingContext) OpenAPI3RouterFactoryImpl(io.vertx.ext.web.api.contract.openapi3.impl.OpenAPI3RouterFactoryImpl) ArrayList(java.util.ArrayList) OpenAPI(io.swagger.v3.oas.models.OpenAPI) Map(java.util.Map) RequestParameters(io.vertx.ext.web.api.RequestParameters) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) Files(java.nio.file.Files) RequestParameter(io.vertx.ext.web.api.RequestParameter) Test(org.junit.Test) IOException(java.io.IOException) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) RouterFactoryOptions(io.vertx.ext.web.api.contract.RouterFactoryOptions) WebTestValidationBase(io.vertx.ext.web.api.validation.WebTestValidationBase) ParseOptions(io.swagger.v3.parser.core.models.ParseOptions) JsonArray(io.vertx.core.json.JsonArray) CountDownLatch(java.util.concurrent.CountDownLatch) List(java.util.List) Rule(org.junit.Rule) ExternalResource(org.junit.rules.ExternalResource) Paths(java.nio.file.Paths) HttpServerOptions(io.vertx.core.http.HttpServerOptions) Handler(io.vertx.core.Handler) OpenAPIV3Parser(io.swagger.v3.parser.OpenAPIV3Parser) 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 12 with HttpResponse

use of io.vertx.ext.web.client.HttpResponse in project vertx-web by vert-x3.

the class HttpContext method sendRequest.

private void sendRequest() {
    Future<HttpClientResponse> responseFuture = Future.<HttpClientResponse>future().setHandler(ar -> {
        Context context = Vertx.currentContext();
        if (ar.succeeded()) {
            HttpClientResponse resp = ar.result();
            Future<HttpResponse<Object>> fut = Future.future();
            fut.setHandler(r -> {
                // We are running on a context (the HTTP client mandates it)
                context.runOnContext(v -> currentResponseHandler.handle(r));
            });
            resp.exceptionHandler(err -> {
                if (!fut.isComplete()) {
                    fut.fail(err);
                }
            });
            resp.pause();
            ((BodyCodec<Object>) request.codec).create(ar2 -> {
                resp.resume();
                if (ar2.succeeded()) {
                    BodyStream<Object> stream = ar2.result();
                    stream.exceptionHandler(err -> {
                        if (!fut.isComplete()) {
                            fut.fail(err);
                        }
                    });
                    resp.endHandler(v -> {
                        if (!fut.isComplete()) {
                            stream.end();
                            if (stream.result().succeeded()) {
                                fut.complete(new HttpResponseImpl<>(resp, null, stream.result().result()));
                            } else {
                                fut.fail(stream.result().cause());
                            }
                        }
                    });
                    Pump responsePump = Pump.pump(resp, stream);
                    responsePump.start();
                } else {
                    currentResponseHandler.handle(Future.failedFuture(ar2.cause()));
                }
            });
        } else {
            currentResponseHandler.handle(Future.failedFuture(ar.cause()));
        }
    });
    HttpClientRequest req;
    String requestURI;
    if (request.queryParams() != null && request.queryParams().size() > 0) {
        QueryStringEncoder enc = new QueryStringEncoder(request.uri);
        request.queryParams().forEach(param -> enc.addParam(param.getKey(), param.getValue()));
        requestURI = enc.toString();
    } else {
        requestURI = request.uri;
    }
    int port = request.port;
    String host = request.host;
    if (request.ssl != request.options.isSsl()) {
        req = request.client.client.request(request.method, new RequestOptions().setSsl(request.ssl).setHost(host).setPort(port).setURI(requestURI));
    } else {
        if (request.protocol != null && !request.protocol.equals("http") && !request.protocol.equals("https")) {
            // we have to create an abs url again to parse it in HttpClient
            try {
                URI uri = new URI(request.protocol, null, host, port, requestURI, null, null);
                req = request.client.client.requestAbs(request.method, uri.toString());
            } catch (URISyntaxException ex) {
                currentResponseHandler.handle(Future.failedFuture(ex));
                return;
            }
        } else {
            req = request.client.client.request(request.method, port, host, requestURI);
        }
    }
    if (request.virtualHost != null) {
        String virtalHost = request.virtualHost;
        if (port != 80) {
            virtalHost += ":" + port;
        }
        req.setHost(virtalHost);
    }
    req.setFollowRedirects(request.followRedirects);
    if (request.headers != null) {
        req.headers().addAll(request.headers);
    }
    req.handler(responseFuture::tryComplete);
    if (request.timeout > 0) {
        req.setTimeout(request.timeout);
    }
    if (body != null) {
        if (contentType != null) {
            String prev = req.headers().get(HttpHeaders.CONTENT_TYPE);
            if (prev == null) {
                req.putHeader(HttpHeaders.CONTENT_TYPE, contentType);
            } else {
                contentType = prev;
            }
        }
        if (body instanceof ReadStream<?>) {
            ReadStream<Buffer> stream = (ReadStream<Buffer>) body;
            if (request.headers == null || !request.headers.contains(HttpHeaders.CONTENT_LENGTH)) {
                req.setChunked(true);
            }
            Pump pump = Pump.pump(stream, req);
            req.exceptionHandler(err -> {
                pump.stop();
                stream.endHandler(null);
                stream.resume();
                responseFuture.tryFail(err);
            });
            stream.exceptionHandler(err -> {
                req.reset();
                responseFuture.tryFail(err);
            });
            stream.endHandler(v -> {
                req.exceptionHandler(responseFuture::tryFail);
                req.end();
                pump.stop();
            });
            pump.start();
        } else {
            Buffer buffer;
            if (body instanceof Buffer) {
                buffer = (Buffer) body;
            } else if (body instanceof MultiMap) {
                try {
                    MultiMap attributes = (MultiMap) body;
                    boolean multipart = "multipart/form-data".equals(contentType);
                    DefaultFullHttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, io.netty.handler.codec.http.HttpMethod.POST, "/");
                    HttpPostRequestEncoder encoder = new HttpPostRequestEncoder(request, multipart);
                    for (Map.Entry<String, String> attribute : attributes) {
                        encoder.addBodyAttribute(attribute.getKey(), attribute.getValue());
                    }
                    encoder.finalizeRequest();
                    for (String headerName : request.headers().names()) {
                        req.putHeader(headerName, request.headers().get(headerName));
                    }
                    if (encoder.isChunked()) {
                        buffer = Buffer.buffer();
                        while (true) {
                            HttpContent chunk = encoder.readChunk(new UnpooledByteBufAllocator(false));
                            ByteBuf content = chunk.content();
                            if (content.readableBytes() == 0) {
                                break;
                            }
                            buffer.appendBuffer(Buffer.buffer(content));
                        }
                    } else {
                        ByteBuf content = request.content();
                        buffer = Buffer.buffer(content);
                    }
                } catch (Exception e) {
                    throw new VertxException(e);
                }
            } else if (body instanceof JsonObject) {
                buffer = Buffer.buffer(((JsonObject) body).encode());
            } else {
                buffer = Buffer.buffer(Json.encode(body));
            }
            req.exceptionHandler(responseFuture::tryFail);
            req.end(buffer);
        }
    } else {
        req.exceptionHandler(responseFuture::tryFail);
        req.end();
    }
}
Also used : RequestOptions(io.vertx.core.http.RequestOptions) HttpPostRequestEncoder(io.netty.handler.codec.http.multipart.HttpPostRequestEncoder) JsonObject(io.vertx.core.json.JsonObject) URISyntaxException(java.net.URISyntaxException) ByteBuf(io.netty.buffer.ByteBuf) URI(java.net.URI) MultiMap(io.vertx.core.MultiMap) UnpooledByteBufAllocator(io.netty.buffer.UnpooledByteBufAllocator) VertxException(io.vertx.core.VertxException) HttpClientResponse(io.vertx.core.http.HttpClientResponse) ReadStream(io.vertx.core.streams.ReadStream) Context(io.vertx.core.Context) BodyCodec(io.vertx.ext.web.codec.BodyCodec) Buffer(io.vertx.core.buffer.Buffer) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) HttpResponse(io.vertx.ext.web.client.HttpResponse) Pump(io.vertx.core.streams.Pump) VertxException(io.vertx.core.VertxException) URISyntaxException(java.net.URISyntaxException) HttpClientRequest(io.vertx.core.http.HttpClientRequest) JsonObject(io.vertx.core.json.JsonObject) QueryStringEncoder(io.netty.handler.codec.http.QueryStringEncoder) HttpContent(io.netty.handler.codec.http.HttpContent)

Example 13 with HttpResponse

use of io.vertx.ext.web.client.HttpResponse in project hono by eclipse.

the class HttpTestBase method testUploadMessagesWithTtdThatReplyWithCommand.

private void testUploadMessagesWithTtdThatReplyWithCommand(final HttpCommandEndpointConfiguration endpointConfig, final Tenant tenant, final VertxTestContext ctx) throws InterruptedException {
    final VertxTestContext setup = new VertxTestContext();
    final MultiMap requestHeaders = MultiMap.caseInsensitiveMultiMap().add(HttpHeaders.CONTENT_TYPE, "text/plain").add(HttpHeaders.AUTHORIZATION, authorization).add(HttpHeaders.ORIGIN, ORIGIN_URI).add(Constants.HEADER_TIME_TILL_DISCONNECT, "5");
    final MultiMap cmdResponseRequestHeaders = MultiMap.caseInsensitiveMultiMap().add(HttpHeaders.CONTENT_TYPE, "text/plain").add(HttpHeaders.AUTHORIZATION, authorization).add(HttpHeaders.ORIGIN, ORIGIN_URI).add(Constants.HEADER_COMMAND_RESPONSE_STATUS, "200");
    helper.registry.addDeviceForTenant(tenantId, tenant, deviceId, PWD).onComplete(setup.succeedingThenComplete());
    assertThat(setup.awaitCompletion(5, TimeUnit.SECONDS)).isTrue();
    if (setup.failed()) {
        ctx.failNow(setup.causeOfFailure());
        return;
    }
    final String commandTargetDeviceId = endpointConfig.isSubscribeAsGateway() ? helper.setupGatewayDeviceBlocking(tenantId, deviceId, 5) : deviceId;
    final String subscribingDeviceId = endpointConfig.isSubscribeAsGatewayForSingleDevice() ? commandTargetDeviceId : deviceId;
    testUploadMessages(ctx, tenantId, msg -> {
        return msg.getTimeUntilDisconnectNotification().map(notification -> {
            logger.trace("received piggy backed message [ttd: {}]: {}", notification.getTtd(), msg);
            ctx.verify(() -> {
                assertThat(notification.getTenantId()).isEqualTo(tenantId);
                assertThat(notification.getDeviceId()).isEqualTo(subscribingDeviceId);
            });
            // now ready to send a command
            final JsonObject inputData = new JsonObject().put(COMMAND_JSON_KEY, (int) (Math.random() * 100));
            return helper.sendCommand(tenantId, commandTargetDeviceId, COMMAND_TO_SEND, "application/json", inputData.toBuffer(), notification.getMillisecondsUntilExpiry()).map(response -> {
                ctx.verify(() -> {
                    assertThat(response.getContentType()).isEqualTo("text/plain");
                    assertThat(response.getDeviceId()).isEqualTo(commandTargetDeviceId);
                    assertThat(response.getTenantId()).isEqualTo(tenantId);
                    assertThat(response.getCreationTime()).isNotNull();
                });
                return (Void) null;
            });
        }).orElseGet(Future::succeededFuture);
    }, count -> {
        final Buffer buffer = Buffer.buffer("hello " + count);
        return sendHttpRequestForGatewayOrDevice(buffer, requestHeaders, endpointConfig, commandTargetDeviceId, true).map(httpResponse -> {
            final String requestId = httpResponse.getHeader(Constants.HEADER_COMMAND_REQUEST_ID);
            ctx.verify(() -> {
                // assert that the response contains a command
                assertWithMessage("response no. %s '%s' header", count, Constants.HEADER_COMMAND).that(httpResponse.getHeader(Constants.HEADER_COMMAND)).isNotNull();
                assertThat(httpResponse.getHeader(Constants.HEADER_COMMAND)).isEqualTo(COMMAND_TO_SEND);
                assertThat(httpResponse.getHeader(HttpHeaders.CONTENT_TYPE.toString())).isEqualTo("application/json");
                assertThat(requestId).isNotNull();
                assertThat(httpResponse.getHeader(HttpHeaders.CONTENT_LENGTH.toString())).isNotEqualTo("0");
            });
            return requestId;
        }).compose(receivedCommandRequestId -> {
            // send a response to the command now
            final String responseUri = endpointConfig.getCommandResponseUri(tenantId, commandTargetDeviceId, receivedCommandRequestId);
            logger.debug("sending response to command [uri: {}]", responseUri);
            final Buffer body = Buffer.buffer("ok");
            final Future<HttpResponse<Buffer>> result;
            if (endpointConfig.isSubscribeAsGateway()) {
                // GW uses PUT when acting on behalf of a device
                result = httpClient.update(responseUri, body, cmdResponseRequestHeaders, ResponsePredicate.status(HttpURLConnection.HTTP_ACCEPTED));
            } else {
                result = httpClient.create(responseUri, body, cmdResponseRequestHeaders, ResponsePredicate.status(HttpURLConnection.HTTP_ACCEPTED));
            }
            return result.recover(thr -> {
                // wrap exception, making clear it occurred when sending the command response, not the preceding telemetry/event message
                final String msg = "Error sending command response: " + thr.getMessage();
                return Future.failedFuture(new RuntimeException(msg, thr));
            });
        });
    });
}
Also used : HttpURLConnection(java.net.HttpURLConnection) KeyPair(java.security.KeyPair) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) ResponsePredicate(io.vertx.ext.web.client.predicate.ResponsePredicate) DownstreamMessage(org.eclipse.hono.application.client.DownstreamMessage) LoggerFactory(org.slf4j.LoggerFactory) MultiMap(io.vertx.core.MultiMap) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Timeout(io.vertx.junit5.Timeout) GeneralSecurityException(java.security.GeneralSecurityException) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) BeforeAll(org.junit.jupiter.api.BeforeAll) JsonObject(io.vertx.core.json.JsonObject) Tenants(org.eclipse.hono.tests.Tenants) MethodSource(org.junit.jupiter.params.provider.MethodSource) Device(org.eclipse.hono.service.management.device.Device) MessageContext(org.eclipse.hono.application.client.MessageContext) SubscriberRole(org.eclipse.hono.tests.CommandEndpointConfiguration.SubscriberRole) Truth.assertWithMessage(com.google.common.truth.Truth.assertWithMessage) MetricsTags(org.eclipse.hono.service.metric.MetricsTags) Set(java.util.Set) HttpHeaders(io.vertx.core.http.HttpHeaders) DownstreamMessageAssertions(org.eclipse.hono.tests.DownstreamMessageAssertions) UUID(java.util.UUID) Future(io.vertx.core.Future) StandardCharsets(java.nio.charset.StandardCharsets) TestInfo(org.junit.jupiter.api.TestInfo) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) Base64(java.util.Base64) Stream(java.util.stream.Stream) Buffer(io.vertx.core.buffer.Buffer) Optional(java.util.Optional) Checkpoint(io.vertx.junit5.Checkpoint) QoS(org.eclipse.hono.util.QoS) VertxTestContext(io.vertx.junit5.VertxTestContext) X500Principal(javax.security.auth.x500.X500Principal) HttpResponse(io.vertx.ext.web.client.HttpResponse) SelfSignedCertificate(io.vertx.core.net.SelfSignedCertificate) Function(java.util.function.Function) Constants(org.eclipse.hono.util.Constants) CompositeFuture(io.vertx.core.CompositeFuture) HttpClientOptions(io.vertx.core.http.HttpClientOptions) PemTrustOptions(io.vertx.core.net.PemTrustOptions) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) Logger(org.slf4j.Logger) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) Truth.assertThat(com.google.common.truth.Truth.assertThat) TimeUnit(java.util.concurrent.TimeUnit) Adapter(org.eclipse.hono.util.Adapter) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) CrudHttpClient(org.eclipse.hono.tests.CrudHttpClient) MessageConsumer(org.eclipse.hono.application.client.MessageConsumer) Handler(io.vertx.core.Handler) Collections(java.util.Collections) Buffer(io.vertx.core.buffer.Buffer) MultiMap(io.vertx.core.MultiMap) VertxTestContext(io.vertx.junit5.VertxTestContext) JsonObject(io.vertx.core.json.JsonObject) Future(io.vertx.core.Future) CompositeFuture(io.vertx.core.CompositeFuture) HttpResponse(io.vertx.ext.web.client.HttpResponse)

Example 14 with HttpResponse

use of io.vertx.ext.web.client.HttpResponse in project hono by eclipse.

the class TelemetryHttpIT method testUploadQos1MessageFailsIfDeliveryStateNotUpdated.

/**
 * Verifies that the upload of a QoS 1 telemetry message fails with a 503 status code
 * when the consumer doesn't update the message delivery state and the
 * <em>sendMessageTimeout</em> has elapsed.
 *
 * @param vertx The vert.x instance.
 * @param ctx The test context
 * @throws InterruptedException if test is interrupted while running.
 */
@Test
@AssumeMessagingSystem(type = MessagingType.amqp)
public void testUploadQos1MessageFailsIfDeliveryStateNotUpdated(final Vertx vertx, final VertxTestContext ctx) throws InterruptedException {
    final AmqpApplicationClient amqpApplicationClient = (AmqpApplicationClient) helper.applicationClient;
    // GIVEN a device and a north bound message consumer that doesn't update the message delivery state
    final Tenant tenant = new Tenant();
    final Checkpoint messageReceived = ctx.checkpoint();
    final Checkpoint deliveryStateCheckDone = ctx.checkpoint();
    final Checkpoint httpResponseReceived = ctx.checkpoint();
    final VertxTestContext setup = new VertxTestContext();
    final AtomicReference<ProtonDelivery> deliveryRef = new AtomicReference<>();
    helper.registry.addDeviceForTenant(tenantId, tenant, deviceId, PWD).compose(ok -> amqpApplicationClient.createTelemetryConsumer(tenantId, msg -> {
        final Promise<Void> result = Promise.promise();
        final var delivery = msg.getMessageContext().getDelivery();
        deliveryRef.set(delivery);
        logger.debug("received message: {}", msg.getMessageContext().getRawMessage());
        ctx.verify(() -> {
            assertThat(delivery.remotelySettled()).isFalse();
            assertThat(delivery.getRemoteState()).isNull();
        });
        messageReceived.flag();
        // don't update the delivery state here
        return result.future();
    }, remoteClose -> {
    })).onComplete(setup.succeedingThenComplete());
    assertThat(setup.awaitCompletion(IntegrationTestSupport.getTestSetupTimeout(), TimeUnit.SECONDS)).isTrue();
    if (setup.failed()) {
        ctx.failNow(setup.causeOfFailure());
        return;
    }
    // WHEN the device tries to upload a telemetry message
    final MultiMap requestHeaders = MultiMap.caseInsensitiveMultiMap().add(HttpHeaders.CONTENT_TYPE, "binary/octet-stream").add(HttpHeaders.AUTHORIZATION, authorization).add(HttpHeaders.ORIGIN, ORIGIN_URI).add(Constants.HEADER_QOS_LEVEL, "1");
    final Future<HttpResponse<Buffer>> httpResponseFuture = httpClient.create(getEndpointUri(), Buffer.buffer("hello"), requestHeaders, // THEN the message gets rejected by the HTTP adapter with a 503
    ResponsePredicate.status(HttpURLConnection.HTTP_UNAVAILABLE));
    httpResponseFuture.onComplete(ctx.succeeding(response -> {
        ctx.verify(() -> {
            final var body = response.bodyAsJsonObject();
            assertThat(body.getString(RequestResponseApiConstants.FIELD_ERROR)).isEqualTo(ServiceInvocationException.getLocalizedMessage(SendMessageTimeoutException.CLIENT_FACING_MESSAGE_KEY));
        });
        httpResponseReceived.flag();
        // verify that the telemetry message delivery is remotely settled via the timeout handling in the adapter
        vertx.setTimer(50, tid -> {
            ctx.verify(() -> {
                final ProtonDelivery delivery = deliveryRef.get();
                assertThat(delivery).isNotNull();
                assertThat(delivery.remotelySettled()).isTrue();
                assertThat(delivery.getRemoteState()).isNotNull();
                assertThat(delivery.getRemoteState().getType()).isEqualTo(DeliveryState.DeliveryStateType.Released);
            });
            deliveryStateCheckDone.flag();
        });
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) ResponsePredicate(io.vertx.ext.web.client.predicate.ResponsePredicate) DownstreamMessage(org.eclipse.hono.application.client.DownstreamMessage) HttpResponse(io.vertx.ext.web.client.HttpResponse) ProtonDelivery(io.vertx.proton.ProtonDelivery) MultiMap(io.vertx.core.MultiMap) RequestOptions(io.vertx.core.http.RequestOptions) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Constants(org.eclipse.hono.util.Constants) NoConsumerException(org.eclipse.hono.client.NoConsumerException) Tenant(org.eclipse.hono.service.management.tenant.Tenant) TelemetryConstants(org.eclipse.hono.util.TelemetryConstants) MessagingType(org.eclipse.hono.util.MessagingType) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Assumptions.assumeTrue(org.junit.jupiter.api.Assumptions.assumeTrue) DeliveryState(org.apache.qpid.proton.amqp.transport.DeliveryState) Tenants(org.eclipse.hono.tests.Tenants) MessageContext(org.eclipse.hono.application.client.MessageContext) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) HttpHeaders(io.vertx.core.http.HttpHeaders) Truth.assertThat(com.google.common.truth.Truth.assertThat) RequestResponseApiConstants(org.eclipse.hono.util.RequestResponseApiConstants) VertxExtension(io.vertx.junit5.VertxExtension) AssumeMessagingSystem(org.eclipse.hono.tests.AssumeMessagingSystem) Future(io.vertx.core.Future) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Buffer(io.vertx.core.buffer.Buffer) AmqpApplicationClient(org.eclipse.hono.application.client.amqp.AmqpApplicationClient) MessageConsumer(org.eclipse.hono.application.client.MessageConsumer) SendMessageTimeoutException(org.eclipse.hono.client.SendMessageTimeoutException) Checkpoint(io.vertx.junit5.Checkpoint) Handler(io.vertx.core.Handler) QoS(org.eclipse.hono.util.QoS) ProtonDelivery(io.vertx.proton.ProtonDelivery) VertxTestContext(io.vertx.junit5.VertxTestContext) HttpResponse(io.vertx.ext.web.client.HttpResponse) AtomicReference(java.util.concurrent.atomic.AtomicReference) AmqpApplicationClient(org.eclipse.hono.application.client.amqp.AmqpApplicationClient) MultiMap(io.vertx.core.MultiMap) Checkpoint(io.vertx.junit5.Checkpoint) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Test(org.junit.jupiter.api.Test) AssumeMessagingSystem(org.eclipse.hono.tests.AssumeMessagingSystem)

Example 15 with HttpResponse

use of io.vertx.ext.web.client.HttpResponse in project hono by eclipse.

the class HttpBasedMessageMappingTest method testMapCommandSucceeds.

/**
 * Verifies that the result returned by the upstream mapping service contains the
 * mapped payload.
 *
 * @param ctx The helper to use for running tests on vert.x.
 */
@SuppressWarnings("unchecked")
@Test
public void testMapCommandSucceeds(final VertxTestContext ctx) {
    config.setMapperEndpoints(Map.of("mapper", MapperEndpoint.from("host", 1234, "/uri", false)));
    final HttpRequest<Buffer> httpRequest = mock(HttpRequest.class, withSettings().defaultAnswer(RETURNS_SELF));
    final Buffer payload = Buffer.buffer("payload");
    final Buffer responseBody = Buffer.buffer("changed");
    final HttpResponse<Buffer> httpResponse = mock(HttpResponse.class);
    when(httpResponse.bodyAsBuffer()).thenReturn(responseBody);
    when(httpResponse.statusCode()).thenReturn(HttpURLConnection.HTTP_OK);
    when(mapperWebClient.post(anyInt(), anyString(), anyString())).thenReturn(httpRequest);
    final Command command = mock(Command.class);
    when(command.getPayload()).thenReturn(payload);
    final RegistrationAssertion assertion = new RegistrationAssertion("gateway").setUpstreamMessageMapper("mapper");
    messageMapping.mapUpstreamMessage(assertion, command).onComplete(ctx.succeeding(mappedBuffer -> {
        ctx.verify(() -> {
            assertThat(mappedBuffer).isEqualTo(responseBody);
            verify(mapperWebClient, times(1)).post(anyInt(), anyString(), anyString());
        });
        ctx.completeNow();
    }));
    final ArgumentCaptor<Handler<AsyncResult<HttpResponse<Buffer>>>> handleCaptor = VertxMockSupport.argumentCaptorHandler();
    verify(httpRequest).sendBuffer(any(Buffer.class), handleCaptor.capture());
    handleCaptor.getValue().handle(Future.succeededFuture(httpResponse));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) MqttQoS(io.netty.handler.codec.mqtt.MqttQoS) HttpResponse(io.vertx.ext.web.client.HttpResponse) WebClient(io.vertx.ext.web.client.WebClient) Command(org.eclipse.hono.client.command.Command) MultiMap(io.vertx.core.MultiMap) MqttEndpoint(io.vertx.mqtt.MqttEndpoint) MqttPublishMessage(io.vertx.mqtt.messages.MqttPublishMessage) Constants(org.eclipse.hono.util.Constants) RETURNS_SELF(org.mockito.Mockito.RETURNS_SELF) TelemetryConstants(org.eclipse.hono.util.TelemetryConstants) ArgumentCaptor(org.mockito.ArgumentCaptor) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) MqttContext(org.eclipse.hono.adapter.mqtt.MqttContext) TracingMockSupport(org.eclipse.hono.test.TracingMockSupport) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) AsyncResult(io.vertx.core.AsyncResult) ArgumentMatchers.anyInt(org.mockito.ArgumentMatchers.anyInt) MapperEndpoint(org.eclipse.hono.config.MapperEndpoint) ServerErrorException(org.eclipse.hono.client.ServerErrorException) HttpHeaders(io.vertx.core.http.HttpHeaders) Mockito.times(org.mockito.Mockito.times) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) MessageHelper(org.eclipse.hono.util.MessageHelper) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) StandardCharsets(java.nio.charset.StandardCharsets) Device(org.eclipse.hono.auth.Device) Mockito.verify(org.mockito.Mockito.verify) HttpRequest(io.vertx.ext.web.client.HttpRequest) Test(org.junit.jupiter.api.Test) Mockito.never(org.mockito.Mockito.never) URLEncoder(java.net.URLEncoder) Buffer(io.vertx.core.buffer.Buffer) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) MqttProtocolAdapterProperties(org.eclipse.hono.adapter.mqtt.MqttProtocolAdapterProperties) Span(io.opentracing.Span) Mockito.withSettings(org.mockito.Mockito.withSettings) Handler(io.vertx.core.Handler) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) Command(org.eclipse.hono.client.command.Command) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) Handler(io.vertx.core.Handler) HttpResponse(io.vertx.ext.web.client.HttpResponse) Test(org.junit.jupiter.api.Test)

Aggregations

HttpResponse (io.vertx.ext.web.client.HttpResponse)34 JsonObject (io.vertx.core.json.JsonObject)28 Handler (io.vertx.core.Handler)27 Map (java.util.Map)27 StandardCharsets (java.nio.charset.StandardCharsets)25 AsyncResult (io.vertx.core.AsyncResult)22 List (java.util.List)22 CountDownLatch (java.util.concurrent.CountDownLatch)21 HashMap (java.util.HashMap)20 ArrayList (java.util.ArrayList)19 Test (org.junit.Test)18 OpenAPI (io.swagger.v3.oas.models.OpenAPI)17 OpenAPIV3Parser (io.swagger.v3.parser.OpenAPIV3Parser)17 ParseOptions (io.swagger.v3.parser.core.models.ParseOptions)17 Buffer (io.vertx.core.buffer.Buffer)17 HttpServerOptions (io.vertx.core.http.HttpServerOptions)17 JsonArray (io.vertx.core.json.JsonArray)17 RoutingContext (io.vertx.ext.web.RoutingContext)17 RequestParameter (io.vertx.ext.web.api.RequestParameter)17 RequestParameters (io.vertx.ext.web.api.RequestParameters)17