Search in sources :

Example 6 with MessageSender

use of org.eclipse.hono.client.MessageSender in project hono by eclipse.

the class AbstractVertxBasedMqttProtocolAdapterTest method givenATelemetrySenderForOutcome.

private void givenATelemetrySenderForOutcome(final Future<ProtonDelivery> outcome) {
    final MessageSender sender = mock(MessageSender.class);
    when(sender.getEndpoint()).thenReturn(TelemetryConstants.TELEMETRY_ENDPOINT);
    when(sender.send(any(Message.class))).thenReturn(outcome);
    when(messagingClient.getOrCreateTelemetrySender(anyString())).thenReturn(Future.succeededFuture(sender));
}
Also used : MqttPublishMessage(io.vertx.mqtt.messages.MqttPublishMessage) Message(org.apache.qpid.proton.message.Message) MessageSender(org.eclipse.hono.client.MessageSender)

Example 7 with MessageSender

use of org.eclipse.hono.client.MessageSender in project hono by eclipse.

the class EventSenderImplTest method testSendMessageFailsOnLackOfCredit.

/**
 * Verifies that the sender fails if no credit is available.
 *
 * @param ctx The vert.x test context.
 */
@SuppressWarnings("unchecked")
@Test
public void testSendMessageFailsOnLackOfCredit(final TestContext ctx) {
    // GIVEN a sender that has no credit
    when(sender.sendQueueFull()).thenReturn(Boolean.TRUE);
    MessageSender messageSender = new EventSenderImpl(config, sender, "tenant", "telemetry/tenant", context);
    // WHEN trying to send a message
    final Future<ProtonDelivery> result = messageSender.send("device", "some payload", "application/text", "token");
    // THEN the message is not sent
    assertFalse(result.succeeded());
    verify(sender, never()).send(any(Message.class), any(Handler.class));
}
Also used : ProtonDelivery(io.vertx.proton.ProtonDelivery) Message(org.apache.qpid.proton.message.Message) MessageSender(org.eclipse.hono.client.MessageSender) Handler(io.vertx.core.Handler) Test(org.junit.Test)

Example 8 with MessageSender

use of org.eclipse.hono.client.MessageSender in project hono by eclipse.

the class TelemetrySenderImplTest method testSendMessageDoesNotWaitForAcceptedOutcome.

/**
 * Verifies that the sender does not wait for the peer to settle and
 * accept a message before succeeding.
 *
 * @param ctx The vert.x test context.
 */
@SuppressWarnings({ "unchecked" })
@Test
public void testSendMessageDoesNotWaitForAcceptedOutcome(final TestContext ctx) {
    // GIVEN a sender that has credit
    when(sender.sendQueueFull()).thenReturn(Boolean.FALSE);
    MessageSender messageSender = new TelemetrySenderImpl(config, sender, "tenant", "telemetry/tenant", context);
    final AtomicReference<Handler<ProtonDelivery>> handlerRef = new AtomicReference<>();
    doAnswer(invocation -> {
        handlerRef.set(invocation.getArgument(1));
        return mock(ProtonDelivery.class);
    }).when(sender).send(any(Message.class), any(Handler.class));
    // WHEN trying to send a message
    final Future<ProtonDelivery> result = messageSender.send("device", "some payload", "application/text", "token");
    // which gets rejected by the peer
    ProtonDelivery rejected = mock(ProtonDelivery.class);
    when(rejected.remotelySettled()).thenReturn(Boolean.TRUE);
    when(rejected.getRemoteState()).thenReturn(new Rejected());
    handlerRef.get().handle(rejected);
    // THEN the resulting future is succeeded nevertheless
    assertTrue(result.succeeded());
    // and the message has been sent
    verify(sender).send(any(Message.class), eq(handlerRef.get()));
}
Also used : Message(org.apache.qpid.proton.message.Message) ProtonDelivery(io.vertx.proton.ProtonDelivery) MessageSender(org.eclipse.hono.client.MessageSender) Handler(io.vertx.core.Handler) AtomicReference(java.util.concurrent.atomic.AtomicReference) Rejected(org.apache.qpid.proton.amqp.messaging.Rejected) Test(org.junit.Test)

Example 9 with MessageSender

use of org.eclipse.hono.client.MessageSender in project hono by eclipse.

the class AbstractVertxBasedHttpProtocolAdapterTest method givenAnEventSenderForOutcome.

private void givenAnEventSenderForOutcome(final Future<ProtonDelivery> outcome) {
    final MessageSender sender = mock(MessageSender.class);
    when(sender.send(any(Message.class))).thenReturn(outcome);
    when(messagingClient.getOrCreateEventSender(anyString())).thenReturn(Future.succeededFuture(sender));
}
Also used : Message(org.apache.qpid.proton.message.Message) MessageSender(org.eclipse.hono.client.MessageSender)

Example 10 with MessageSender

use of org.eclipse.hono.client.MessageSender in project hono by eclipse.

the class AbstractVertxBasedHttpProtocolAdapterTest method testUploadTelemetryFailsForDisabledTenant.

/**
 * Verifies that the adapter fails the upload of an event with a 403
 * result if the device belongs to a tenant for which the adapter is
 * disabled.
 */
@Test
public void testUploadTelemetryFailsForDisabledTenant() {
    // GIVEN an adapter
    final HttpServer server = getHttpServer(false);
    final MessageSender sender = mock(MessageSender.class);
    when(messagingClient.getOrCreateTelemetrySender(anyString())).thenReturn(Future.succeededFuture(sender));
    // which is disabled for tenant "my-tenant"
    final TenantObject myTenantConfig = TenantObject.from("my-tenant", true);
    myTenantConfig.addAdapterConfiguration(new JsonObject().put(TenantConstants.FIELD_ADAPTERS_TYPE, ADAPTER_TYPE).put(TenantConstants.FIELD_ENABLED, false));
    when(tenantClient.get("my-tenant")).thenReturn(Future.succeededFuture(myTenantConfig));
    final AbstractVertxBasedHttpProtocolAdapter<HttpProtocolAdapterProperties> adapter = getAdapter(server, null);
    // WHEN a device that belongs to "my-tenant" publishes a telemetry message
    final Buffer payload = Buffer.buffer("some payload");
    final RoutingContext ctx = newRoutingContext(payload);
    adapter.uploadTelemetryMessage(ctx, "my-tenant", "the-device", payload, "application/text");
    // THEN the device gets a 403
    verify(ctx).fail(HttpURLConnection.HTTP_FORBIDDEN);
    // and the message has not been forwarded downstream
    verify(sender, never()).send(any(Message.class));
}
Also used : Buffer(io.vertx.core.buffer.Buffer) TenantObject(org.eclipse.hono.util.TenantObject) RoutingContext(io.vertx.ext.web.RoutingContext) Message(org.apache.qpid.proton.message.Message) MessageSender(org.eclipse.hono.client.MessageSender) HttpServer(io.vertx.core.http.HttpServer) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.Test)

Aggregations

MessageSender (org.eclipse.hono.client.MessageSender)17 Message (org.apache.qpid.proton.message.Message)16 Test (org.junit.Test)11 Handler (io.vertx.core.Handler)8 ProtonDelivery (io.vertx.proton.ProtonDelivery)8 Future (io.vertx.core.Future)7 Vertx (io.vertx.core.Vertx)6 TestContext (io.vertx.ext.unit.TestContext)5 TimeUnit (java.util.concurrent.TimeUnit)5 AtomicReference (java.util.concurrent.atomic.AtomicReference)5 ClientErrorException (org.eclipse.hono.client.ClientErrorException)5 HonoClient (org.eclipse.hono.client.HonoClient)5 Constants (org.eclipse.hono.util.Constants)5 Buffer (io.vertx.core.buffer.Buffer)4 JsonObject (io.vertx.core.json.JsonObject)4 Async (io.vertx.ext.unit.Async)4 MqttPublishMessage (io.vertx.mqtt.messages.MqttPublishMessage)4 HttpURLConnection (java.net.HttpURLConnection)4 RegistrationClient (org.eclipse.hono.client.RegistrationClient)4 TenantObject (org.eclipse.hono.util.TenantObject)4