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));
}
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));
}
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()));
}
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));
}
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));
}
Aggregations