Search in sources :

Example 11 with MessageConsumer

use of org.eclipse.hono.application.client.MessageConsumer in project hono by eclipse.

the class HttpTestBase method testUploadMessages.

/**
 * Uploads messages to the HTTP endpoint.
 *
 * @param ctx The test context to run on.
 * @param tenantId The tenant that the device belongs to.
 * @param messageConsumer Consumer that is invoked when a message was received.
 * @param requestSender The test device that will publish the data.
 * @param numberOfMessages The number of messages that are uploaded.
 * @param expectedQos The expected QoS level, may be {@code null} leading to expecting the default for event or telemetry.
 * @throws InterruptedException if the test is interrupted before it has finished.
 */
protected void testUploadMessages(final VertxTestContext ctx, final String tenantId, final Function<DownstreamMessage<? extends MessageContext>, Future<Void>> messageConsumer, final Function<Integer, Future<HttpResponse<Buffer>>> requestSender, final int numberOfMessages, final QoS expectedQos) throws InterruptedException {
    final VertxTestContext messageSending = new VertxTestContext();
    final Checkpoint messageSent = messageSending.checkpoint(numberOfMessages);
    final Checkpoint messageReceived = messageSending.laxCheckpoint(numberOfMessages);
    final AtomicInteger receivedMessageCount = new AtomicInteger(0);
    final VertxTestContext setup = new VertxTestContext();
    createConsumer(tenantId, msg -> {
        logger.trace("received {}", msg);
        ctx.verify(() -> {
            DownstreamMessageAssertions.assertTelemetryMessageProperties(msg, tenantId);
            assertThat(msg.getQos()).isEqualTo(getExpectedQoS(expectedQos));
            assertAdditionalMessageProperties(msg);
        });
        Optional.ofNullable(messageConsumer).map(consumer -> consumer.apply(msg)).orElseGet(() -> Future.succeededFuture()).onComplete(attempt -> {
            if (attempt.succeeded()) {
                receivedMessageCount.incrementAndGet();
                messageReceived.flag();
            } else {
                logger.error("failed to process message from device", attempt.cause());
                messageSending.failNow(attempt.cause());
            }
        });
        if (receivedMessageCount.get() % 20 == 0) {
            logger.info("messages received: {}", receivedMessageCount.get());
        }
    }).onComplete(setup.succeedingThenComplete());
    assertThat(setup.awaitCompletion(5, TimeUnit.SECONDS)).isTrue();
    if (setup.failed()) {
        ctx.failNow(setup.causeOfFailure());
        return;
    }
    final long start = System.currentTimeMillis();
    int messageCount = 0;
    while (messageCount < numberOfMessages && !messageSending.failed()) {
        messageCount++;
        final int currentMessage = messageCount;
        final CountDownLatch sending = new CountDownLatch(1);
        requestSender.apply(currentMessage).compose(this::assertHttpResponse).onComplete(attempt -> {
            try {
                if (attempt.succeeded()) {
                    logger.debug("sent message {}", currentMessage);
                    messageSent.flag();
                } else {
                    logger.info("failed to send message {}: {}", currentMessage, attempt.cause().getMessage());
                    messageSending.failNow(attempt.cause());
                }
            } finally {
                sending.countDown();
            }
        });
        sending.await();
        if (currentMessage % 20 == 0) {
            logger.info("messages sent: " + currentMessage);
        }
    }
    final long timeToWait = Math.max(TEST_TIMEOUT_MILLIS - 50 - (System.currentTimeMillis() - testStartTimeMillis), 1);
    assertThat(messageSending.awaitCompletion(timeToWait, TimeUnit.MILLISECONDS)).isTrue();
    if (messageSending.failed()) {
        logger.error("test execution failed", messageSending.causeOfFailure());
        ctx.failNow(messageSending.causeOfFailure());
    } else {
        logger.info("successfully sent {} and received {} messages after {} milliseconds", messageCount, receivedMessageCount.get(), System.currentTimeMillis() - start);
        ctx.completeNow();
    }
}
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) Checkpoint(io.vertx.junit5.Checkpoint) VertxTestContext(io.vertx.junit5.VertxTestContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch) Checkpoint(io.vertx.junit5.Checkpoint)

Example 12 with MessageConsumer

use of org.eclipse.hono.application.client.MessageConsumer in project hono by eclipse.

the class CommandAndControlMqttIT method testSendCommandViaKafkaFailsForMalformedMessage.

/**
 * Verifies that the adapter rejects malformed command messages sent by applications.
 * <p>
 * This test is applicable only if the messaging network type is Kafka.
 *
 * @param endpointConfig The endpoints to use for sending/receiving commands.
 * @param ctx The vert.x test context.
 * @throws InterruptedException if not all commands and responses are exchanged in time.
 */
@ParameterizedTest(name = IntegrationTestSupport.PARAMETERIZED_TEST_NAME_PATTERN)
@MethodSource("allCombinations")
@Timeout(timeUnit = TimeUnit.SECONDS, value = 20)
@AssumeMessagingSystem(type = MessagingType.kafka)
public void testSendCommandViaKafkaFailsForMalformedMessage(final MqttCommandEndpointConfiguration endpointConfig, final VertxTestContext ctx) throws InterruptedException {
    final String commandTargetDeviceId = endpointConfig.isSubscribeAsGateway() ? helper.setupGatewayDeviceBlocking(tenantId, deviceId, 5) : deviceId;
    final AtomicReference<GenericKafkaSender> kafkaSenderRef = new AtomicReference<>();
    final CountDownLatch expectedCommandResponses = new CountDownLatch(1);
    final VertxTestContext setup = new VertxTestContext();
    final Checkpoint ready = setup.checkpoint(2);
    final Future<MessageConsumer> kafkaAsyncErrorResponseConsumer = helper.createDeliveryFailureCommandResponseConsumer(ctx, tenantId, HttpURLConnection.HTTP_BAD_REQUEST, response -> expectedCommandResponses.countDown(), null);
    createConsumer(tenantId, msg -> {
        // expect empty notification with TTD -1
        setup.verify(() -> assertThat(msg.getContentType()).isEqualTo(EventConstants.CONTENT_TYPE_EMPTY_NOTIFICATION));
        final TimeUntilDisconnectNotification notification = msg.getTimeUntilDisconnectNotification().orElse(null);
        LOGGER.info("received notification [{}]", notification);
        if (notification.getTtd() == -1) {
            ready.flag();
        }
    }).compose(consumer -> helper.registry.addDeviceToTenant(tenantId, deviceId, password)).compose(ok -> connectToAdapter(IntegrationTestSupport.getUsername(deviceId, tenantId), password)).compose(conAck -> subscribeToCommands(commandTargetDeviceId, msg -> {
        // all commands should get rejected because they fail to pass the validity check
        ctx.failNow(new IllegalStateException("should not have received command"));
    }, endpointConfig, MqttQoS.AT_MOST_ONCE)).compose(ok -> helper.createGenericKafkaSender().onSuccess(kafkaSenderRef::set).mapEmpty()).compose(ok -> kafkaAsyncErrorResponseConsumer).onComplete(setup.succeeding(v -> ready.flag()));
    assertWithMessage("setup of adapter finished within %s seconds", IntegrationTestSupport.getTestSetupTimeout()).that(setup.awaitCompletion(IntegrationTestSupport.getTestSetupTimeout(), TimeUnit.SECONDS)).isTrue();
    if (setup.failed()) {
        ctx.failNow(setup.causeOfFailure());
        return;
    }
    final String commandTopic = new HonoTopic(HonoTopic.Type.COMMAND, tenantId).toString();
    LOGGER.debug("sending command message lacking subject and correlation ID - no failure response expected here");
    final Map<String, Object> properties1 = Map.of(MessageHelper.APP_PROPERTY_DEVICE_ID, deviceId, MessageHelper.SYS_PROPERTY_CONTENT_TYPE, MessageHelper.CONTENT_TYPE_OCTET_STREAM, KafkaRecordHelper.HEADER_RESPONSE_REQUIRED, true);
    kafkaSenderRef.get().sendAndWaitForOutcome(commandTopic, tenantId, deviceId, Buffer.buffer(), properties1).onComplete(ctx.succeeding(ok -> {
    }));
    LOGGER.debug("sending command message lacking subject");
    final String correlationId = "1";
    final Map<String, Object> properties2 = Map.of(MessageHelper.SYS_PROPERTY_CORRELATION_ID, correlationId, MessageHelper.APP_PROPERTY_DEVICE_ID, deviceId, MessageHelper.SYS_PROPERTY_CONTENT_TYPE, MessageHelper.CONTENT_TYPE_OCTET_STREAM, KafkaRecordHelper.HEADER_RESPONSE_REQUIRED, true);
    kafkaSenderRef.get().sendAndWaitForOutcome(commandTopic, tenantId, deviceId, Buffer.buffer(), properties2).onComplete(ctx.succeeding(ok -> {
    }));
    final long timeToWait = 2500;
    if (!expectedCommandResponses.await(timeToWait, TimeUnit.MILLISECONDS)) {
        LOGGER.info("Timeout of {} milliseconds reached, stop waiting for command response", timeToWait);
    }
    kafkaAsyncErrorResponseConsumer.result().close().onComplete(ar -> {
        if (expectedCommandResponses.getCount() == 0) {
            ctx.completeNow();
        } else {
            ctx.failNow(new java.lang.IllegalStateException("did not receive command response"));
        }
    });
}
Also used : GenericKafkaSender(org.eclipse.hono.tests.GenericKafkaSender) HttpURLConnection(java.net.HttpURLConnection) BeforeEach(org.junit.jupiter.api.BeforeEach) DownstreamMessage(org.eclipse.hono.application.client.DownstreamMessage) MqttPublishMessage(io.vertx.mqtt.messages.MqttPublishMessage) Timeout(io.vertx.junit5.Timeout) MessagingType(org.eclipse.hono.util.MessagingType) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ChannelPromise(io.netty.channel.ChannelPromise) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) MqttClientImpl(io.vertx.mqtt.impl.MqttClientImpl) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) Method(java.lang.reflect.Method) MethodSource(org.junit.jupiter.params.provider.MethodSource) ChannelOutboundHandlerAdapter(io.netty.channel.ChannelOutboundHandlerAdapter) MessageContext(org.eclipse.hono.application.client.MessageContext) SubscriberRole(org.eclipse.hono.tests.CommandEndpointConfiguration.SubscriberRole) Truth.assertWithMessage(com.google.common.truth.Truth.assertWithMessage) IllegalStateException(javax.jms.IllegalStateException) MessageHelper(org.eclipse.hono.util.MessageHelper) VertxExtension(io.vertx.junit5.VertxExtension) EventConstants(org.eclipse.hono.util.EventConstants) Future(io.vertx.core.Future) CountDownLatch(java.util.concurrent.CountDownLatch) Stream(java.util.stream.Stream) Buffer(io.vertx.core.buffer.Buffer) Checkpoint(io.vertx.junit5.Checkpoint) NetSocketInternal(io.vertx.core.net.impl.NetSocketInternal) VertxTestContext(io.vertx.junit5.VertxTestContext) GenericKafkaSender(org.eclipse.hono.tests.GenericKafkaSender) MqttQoS(io.netty.handler.codec.mqtt.MqttQoS) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClientErrorException(org.eclipse.hono.client.ClientErrorException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) TimeUntilDisconnectNotification(org.eclipse.hono.util.TimeUntilDisconnectNotification) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) Message(org.apache.qpid.proton.message.Message) MqttPubAckMessage(io.netty.handler.codec.mqtt.MqttPubAckMessage) Promise(io.vertx.core.Promise) ServerErrorException(org.eclipse.hono.client.ServerErrorException) ProtonHelper(io.vertx.proton.ProtonHelper) KafkaRecordHelper(org.eclipse.hono.client.kafka.KafkaRecordHelper) Truth.assertThat(com.google.common.truth.Truth.assertThat) AssumeMessagingSystem(org.eclipse.hono.tests.AssumeMessagingSystem) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) HonoTopic(org.eclipse.hono.client.kafka.HonoTopic) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MessageConsumer(org.eclipse.hono.application.client.MessageConsumer) SendMessageTimeoutException(org.eclipse.hono.client.SendMessageTimeoutException) NoopSpan(io.opentracing.noop.NoopSpan) GenericSenderLink(org.eclipse.hono.client.amqp.GenericSenderLink) Handler(io.vertx.core.Handler) IllegalStateException(javax.jms.IllegalStateException) MessageConsumer(org.eclipse.hono.application.client.MessageConsumer) VertxTestContext(io.vertx.junit5.VertxTestContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) HonoTopic(org.eclipse.hono.client.kafka.HonoTopic) CountDownLatch(java.util.concurrent.CountDownLatch) Checkpoint(io.vertx.junit5.Checkpoint) TimeUntilDisconnectNotification(org.eclipse.hono.util.TimeUntilDisconnectNotification) AssumeMessagingSystem(org.eclipse.hono.tests.AssumeMessagingSystem) Timeout(io.vertx.junit5.Timeout) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

Buffer (io.vertx.core.buffer.Buffer)12 MessageConsumer (org.eclipse.hono.application.client.MessageConsumer)12 Future (io.vertx.core.Future)11 Handler (io.vertx.core.Handler)11 DownstreamMessage (org.eclipse.hono.application.client.DownstreamMessage)11 VertxTestContext (io.vertx.junit5.VertxTestContext)10 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)10 MessageContext (org.eclipse.hono.application.client.MessageContext)10 Truth.assertThat (com.google.common.truth.Truth.assertThat)9 Promise (io.vertx.core.Promise)9 Optional (java.util.Optional)9 CountDownLatch (java.util.concurrent.CountDownLatch)9 TimeUnit (java.util.concurrent.TimeUnit)9 HttpURLConnection (java.net.HttpURLConnection)8 Map (java.util.Map)8 Function (java.util.function.Function)8 Truth.assertWithMessage (com.google.common.truth.Truth.assertWithMessage)7 Checkpoint (io.vertx.junit5.Checkpoint)7 Timeout (io.vertx.junit5.Timeout)7 AtomicLong (java.util.concurrent.atomic.AtomicLong)7