Search in sources :

Example 6 with QoS

use of org.eclipse.hono.util.QoS in project hono by eclipse.

the class KafkaRecordHelperTest method testGetQoS.

/**
 * Verifies that {@link KafkaRecordHelper#getQoS(List)} returns the QoS.
 */
@Test
public void testGetQoS() {
    final QoS qos = QoS.AT_MOST_ONCE;
    headers.add(KafkaRecordHelper.createKafkaHeader("qos", qos.ordinal()));
    assertThat(KafkaRecordHelper.getQoS(headers)).isEqualTo(Optional.of(qos));
}
Also used : QoS(org.eclipse.hono.util.QoS) Test(org.junit.jupiter.api.Test)

Example 7 with QoS

use of org.eclipse.hono.util.QoS in project hono by eclipse.

the class CoapTestBase method testUploadMessages.

/**
 * Uploads messages to the CoAP endpoint.
 *
 * @param ctx The test context to run on.
 * @param tenantId The tenant that the device belongs to.
 * @param warmUp A sender of messages used to warm up the adapter before running the test itself or {@code null} if
 *            no warm up should be performed.
 * @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 Supplier<Future<Void>> warmUp, final Consumer<DownstreamMessage<? extends MessageContext>> messageConsumer, final Function<Integer, Future<OptionSet>> requestSender, final int numberOfMessages, final QoS expectedQos) throws InterruptedException {
    final CountDownLatch received = new CountDownLatch(numberOfMessages);
    final VertxTestContext setup = new VertxTestContext();
    createConsumer(tenantId, msg -> {
        ctx.verify(() -> {
            logger.trace("received {}", msg);
            DownstreamMessageAssertions.assertTelemetryMessageProperties(msg, tenantId);
            assertThat(msg.getQos()).isEqualTo(getExpectedQoS(expectedQos));
            assertAdditionalMessageProperties(msg);
            if (messageConsumer != null) {
                messageConsumer.accept(msg);
            }
        });
        received.countDown();
        if (received.getCount() % 20 == 0) {
            logger.info("messages received: {}", numberOfMessages - received.getCount());
        }
    }).compose(ok -> Optional.ofNullable(warmUp).map(w -> w.get()).orElseGet(() -> Future.succeededFuture())).onComplete(setup.succeedingThenComplete());
    ctx.verify(() -> assertThat(setup.awaitCompletion(5, TimeUnit.SECONDS)).isTrue());
    final long start = System.currentTimeMillis();
    final AtomicInteger messageCount = new AtomicInteger(0);
    while (messageCount.get() < numberOfMessages && !ctx.failed()) {
        final CountDownLatch sending = new CountDownLatch(1);
        requestSender.apply(messageCount.getAndIncrement()).compose(this::assertCoapResponse).onComplete(attempt -> {
            if (attempt.succeeded()) {
                logger.debug("sent message {}", messageCount.get());
            } else {
                logger.info("failed to send message {}: {}", messageCount.get(), attempt.cause().getMessage());
                ctx.failNow(attempt.cause());
            }
            sending.countDown();
        });
        if (messageCount.get() % 20 == 0) {
            logger.info("messages sent: {}", messageCount.get());
        }
        sending.await();
    }
    if (ctx.failed()) {
        return;
    }
    final long timeToWait = Math.max(TEST_TIMEOUT_MILLIS - 1000, Math.round(numberOfMessages * 20));
    if (received.await(timeToWait, TimeUnit.MILLISECONDS)) {
        logger.info("sent {} and received {} messages after {} milliseconds", messageCount, numberOfMessages - received.getCount(), System.currentTimeMillis() - start);
        ctx.completeNow();
    } else {
        logger.info("sent {} and received {} messages after {} milliseconds", messageCount, numberOfMessages - received.getCount(), System.currentTimeMillis() - start);
        ctx.failNow(new AssertionError("did not receive all messages sent"));
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) X509Certificate(java.security.cert.X509Certificate) ResponseCode(org.eclipse.californium.core.coap.CoAP.ResponseCode) KeyPair(java.security.KeyPair) BeforeEach(org.junit.jupiter.api.BeforeEach) Arrays(java.util.Arrays) DownstreamMessage(org.eclipse.hono.application.client.DownstreamMessage) AdvancedPskStore(org.eclipse.californium.scandium.dtls.pskstore.AdvancedPskStore) URISyntaxException(java.net.URISyntaxException) LoggerFactory(org.slf4j.LoggerFactory) CertificateType(org.eclipse.californium.scandium.dtls.CertificateType) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Timeout(io.vertx.junit5.Timeout) CoapResponse(org.eclipse.californium.core.CoapResponse) NetworkConfig(org.eclipse.californium.core.network.config.NetworkConfig) GeneralSecurityException(java.security.GeneralSecurityException) DTLSConnector(org.eclipse.californium.scandium.DTLSConnector) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Utils(org.eclipse.californium.core.Utils) JsonObject(io.vertx.core.json.JsonObject) URI(java.net.URI) Tenants(org.eclipse.hono.tests.Tenants) MethodSource(org.junit.jupiter.params.provider.MethodSource) Device(org.eclipse.hono.service.management.device.Device) DtlsConnectorConfig(org.eclipse.californium.scandium.config.DtlsConnectorConfig) MessageContext(org.eclipse.hono.application.client.MessageContext) SubscriberRole(org.eclipse.hono.tests.CommandEndpointConfiguration.SubscriberRole) Truth.assertWithMessage(com.google.common.truth.Truth.assertWithMessage) DownstreamMessageAssertions(org.eclipse.hono.tests.DownstreamMessageAssertions) InetSocketAddress(java.net.InetSocketAddress) 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) List(java.util.List) Stream(java.util.stream.Stream) Request(org.eclipse.californium.core.coap.Request) Buffer(io.vertx.core.buffer.Buffer) Optional(java.util.Optional) QoS(org.eclipse.hono.util.QoS) VertxTestContext(io.vertx.junit5.VertxTestContext) CoapClient(org.eclipse.californium.core.CoapClient) X500Principal(javax.security.auth.x500.X500Principal) KeyLoader(org.eclipse.hono.config.KeyLoader) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Function(java.util.function.Function) Supplier(java.util.function.Supplier) Constants(org.eclipse.hono.util.Constants) CoapEndpoint(org.eclipse.californium.core.network.CoapEndpoint) MediaTypeRegistry(org.eclipse.californium.core.coap.MediaTypeRegistry) StaticNewAdvancedCertificateVerifier(org.eclipse.californium.scandium.dtls.x509.StaticNewAdvancedCertificateVerifier) AsyncResult(io.vertx.core.AsyncResult) CommandConstants(org.eclipse.hono.util.CommandConstants) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) Type(org.eclipse.californium.core.coap.CoAP.Type) Logger(org.slf4j.Logger) AdvancedSinglePskStore(org.eclipse.californium.scandium.dtls.pskstore.AdvancedSinglePskStore) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) Truth.assertThat(com.google.common.truth.Truth.assertThat) Inet4Address(java.net.Inet4Address) UnknownHostException(java.net.UnknownHostException) TimeUnit(java.util.concurrent.TimeUnit) Consumer(java.util.function.Consumer) Code(org.eclipse.californium.core.coap.CoAP.Code) Adapter(org.eclipse.hono.util.Adapter) AfterEach(org.junit.jupiter.api.AfterEach) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) CoapHandler(org.eclipse.californium.core.CoapHandler) MessageConsumer(org.eclipse.hono.application.client.MessageConsumer) OptionSet(org.eclipse.californium.core.coap.OptionSet) Handler(io.vertx.core.Handler) Collections(java.util.Collections) VertxTestContext(io.vertx.junit5.VertxTestContext) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 8 with QoS

use of org.eclipse.hono.util.QoS 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 9 with QoS

use of org.eclipse.hono.util.QoS in project hono by eclipse.

the class AbstractVertxBasedMqttProtocolAdapterTest method testUploadQoS1MessageSendsPubAckOnSuccess.

private void testUploadQoS1MessageSendsPubAckOnSuccess(final Promise<Void> outcome, final EndpointType type, final BiConsumer<AbstractVertxBasedMqttProtocolAdapter<?>, MqttContext> upload) {
    // WHEN a device publishes a message using QoS 1
    final MqttEndpoint endpoint = mockEndpoint();
    when(endpoint.isConnected()).thenReturn(Boolean.TRUE);
    final Buffer payload = Buffer.buffer("some payload");
    final MqttPublishMessage messageFromDevice = mock(MqttPublishMessage.class);
    when(messageFromDevice.qosLevel()).thenReturn(MqttQoS.AT_LEAST_ONCE);
    when(messageFromDevice.messageId()).thenReturn(5555555);
    when(messageFromDevice.payload()).thenReturn(payload);
    when(messageFromDevice.topicName()).thenReturn(String.format("%s/my-tenant/4712", type.getCanonicalName()));
    final MqttContext context = newMqttContext(messageFromDevice, endpoint, span);
    upload.accept(adapter, context);
    // THEN the device does not receive a PUBACK
    verify(endpoint, never()).publishAcknowledge(anyInt());
    // and the message has not been reported as forwarded
    verify(metrics, never()).reportTelemetry(any(MetricsTags.EndpointType.class), anyString(), any(), eq(MetricsTags.ProcessingOutcome.FORWARDED), any(MetricsTags.QoS.class), anyInt(), any());
    // until the message has been settled and accepted
    outcome.complete();
    verify(endpoint).publishAcknowledge(5555555);
    verify(metrics).reportTelemetry(eq(type), eq("my-tenant"), any(), eq(MetricsTags.ProcessingOutcome.FORWARDED), eq(MetricsTags.QoS.AT_LEAST_ONCE), eq(payload.length()), any());
}
Also used : Buffer(io.vertx.core.buffer.Buffer) QoS(org.eclipse.hono.util.QoS) MqttQoS(io.netty.handler.codec.mqtt.MqttQoS) MqttEndpoint(io.vertx.mqtt.MqttEndpoint) MqttPublishMessage(io.vertx.mqtt.messages.MqttPublishMessage) EndpointType(org.eclipse.hono.service.metric.MetricsTags.EndpointType)

Example 10 with QoS

use of org.eclipse.hono.util.QoS in project hono by eclipse.

the class KafkaBasedTelemetrySenderTest method testThatSendTelemetryThrowsOnMissingMandatoryParameter.

/**
 * Verifies that
 * {@link KafkaBasedTelemetrySender#sendTelemetry(TenantObject, RegistrationAssertion, QoS, String, Buffer, Map, io.opentracing.SpanContext)}
 * throws an NPE if a mandatory parameter is {@code null}.
 */
@Test
public void testThatSendTelemetryThrowsOnMissingMandatoryParameter() {
    final QoS qos = QoS.AT_LEAST_ONCE;
    final MockProducer<String, Buffer> mockProducer = KafkaClientUnitTestHelper.newMockProducer(true);
    final CachingKafkaProducerFactory<String, Buffer> factory = CachingKafkaProducerFactory.testFactory(vertxMock, (n, c) -> KafkaClientUnitTestHelper.newKafkaProducer(mockProducer));
    final Tracer tracer = NoopTracerFactory.create();
    final KafkaBasedTelemetrySender sender = new KafkaBasedTelemetrySender(vertxMock, factory, kafkaProducerConfig, true, tracer);
    assertThrows(NullPointerException.class, () -> sender.sendTelemetry(null, device, qos, "the-content-type", null, null, null));
    assertThrows(NullPointerException.class, () -> sender.sendTelemetry(tenant, null, qos, "the-content-type", null, null, null));
    assertThrows(NullPointerException.class, () -> sender.sendTelemetry(tenant, device, null, "the-content-type", null, null, null));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) QoS(org.eclipse.hono.util.QoS) Tracer(io.opentracing.Tracer) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

QoS (org.eclipse.hono.util.QoS)10 Test (org.junit.jupiter.api.Test)8 Buffer (io.vertx.core.buffer.Buffer)7 Vertx (io.vertx.core.Vertx)6 VertxTestContext (io.vertx.junit5.VertxTestContext)6 Truth.assertThat (com.google.common.truth.Truth.assertThat)5 Future (io.vertx.core.Future)5 HttpURLConnection (java.net.HttpURLConnection)5 Constants (org.eclipse.hono.util.Constants)5 BeforeEach (org.junit.jupiter.api.BeforeEach)5 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)5 Handler (io.vertx.core.Handler)4 Promise (io.vertx.core.Promise)4 VertxExtension (io.vertx.junit5.VertxExtension)4 TimeUnit (java.util.concurrent.TimeUnit)4 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)4 Mockito.mock (org.mockito.Mockito.mock)3 Mockito.when (org.mockito.Mockito.when)3 Truth.assertWithMessage (com.google.common.truth.Truth.assertWithMessage)2 Tracer (io.opentracing.Tracer)2