use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class AmqpConnectionIT method testConnectFailsForNonExistingTenant.
/**
* Verifies that the adapter rejects connection attempts from devices
* using credentials that contain a non-existing tenant.
*
* @param ctx The test context
*/
@Test
public void testConnectFailsForNonExistingTenant(final VertxTestContext ctx) {
// GIVEN a registered device
final String tenantId = helper.getRandomTenantId();
final String deviceId = helper.getRandomDeviceId(tenantId);
final String password = "secret";
final Tenant tenant = new Tenant();
helper.registry.addDeviceForTenant(tenantId, tenant, deviceId, password).compose(ok -> connectToAdapter(IntegrationTestSupport.getUsername(deviceId, "nonExistingTenant"), password)).onComplete(ctx.failing(t -> {
// THEN the connection is refused
ctx.verify(() -> assertThat(t).isInstanceOf(AuthenticationException.class));
ctx.completeNow();
}));
}
use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class AmqpConnectionIT method testConnectFailsForWrongCredentials.
/**
* Verifies that the adapter rejects connection attempts from devices
* using wrong credentials.
*
* @param ctx The test context
*/
@Test
public void testConnectFailsForWrongCredentials(final VertxTestContext ctx) {
// GIVEN a registered device
final String tenantId = helper.getRandomTenantId();
final String deviceId = helper.getRandomDeviceId(tenantId);
final String password = "secret";
final Tenant tenant = new Tenant();
helper.registry.addDeviceForTenant(tenantId, tenant, deviceId, password).compose(ok -> connectToAdapter(IntegrationTestSupport.getUsername(deviceId, tenantId), "wrong password")).onComplete(ctx.failing(t -> {
// THEN the connection is refused
ctx.verify(() -> assertThat(t).isInstanceOf(AuthenticationException.class));
ctx.completeNow();
}));
}
use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class VertxBasedAmqpProtocolAdapterTest method testAdapterSupportsAnonymousRelay.
/**
* Verifies that the adapter offers the ANONYMOUS-RELAY capability
* in its open frame when a device connects.
*/
@Test
public void testAdapterSupportsAnonymousRelay() {
// GIVEN an AMQP adapter with a configured server.
givenAnAdapter(properties);
// WHEN a device connects
final Device authenticatedDevice = new Device(TEST_TENANT_ID, TEST_DEVICE);
final Record record = new RecordImpl();
record.set(AmqpAdapterConstants.KEY_CLIENT_DEVICE, Device.class, authenticatedDevice);
final ProtonConnection deviceConnection = mock(ProtonConnection.class);
when(deviceConnection.attachments()).thenReturn(record);
when(deviceConnection.getRemoteContainer()).thenReturn("deviceContainer");
adapter.onConnectRequest(deviceConnection);
final ArgumentCaptor<Handler<AsyncResult<ProtonConnection>>> openHandler = VertxMockSupport.argumentCaptorHandler();
verify(deviceConnection).openHandler(openHandler.capture());
openHandler.getValue().handle(Future.succeededFuture(deviceConnection));
// THEN the adapter's open frame contains the ANONYMOUS-RELAY capability
verify(deviceConnection).setOfferedCapabilities(argThat(caps -> Arrays.asList(caps).contains(Constants.CAP_ANONYMOUS_RELAY)));
}
use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class VertxBasedAmqpProtocolAdapterTest method testUploadTelemetryMessageFailsForDisabledAdapter.
/**
* Verifies that a request to upload an "unsettled" telemetry message from a device that belongs to a tenant for which the AMQP
* adapter is disabled fails and that the device is notified when the message cannot be processed.
*
* @param ctx The vert.x test context.
*/
@Test
public void testUploadTelemetryMessageFailsForDisabledAdapter(final VertxTestContext ctx) {
// GIVEN an adapter configured to use a user-define server.
givenAnAdapter(properties);
givenATelemetrySenderForAnyTenant();
// AND given a tenant for which the AMQP Adapter is disabled
final TenantObject tenantObject = givenAConfiguredTenant(TEST_TENANT_ID, false);
// WHEN a device uploads telemetry data to the adapter (and wants to be notified of failure)
final ProtonDelivery delivery = mock(ProtonDelivery.class);
// AT LEAST ONCE
when(delivery.remotelySettled()).thenReturn(false);
final String to = ResourceIdentifier.from(TelemetryConstants.TELEMETRY_ENDPOINT, TEST_TENANT_ID, TEST_DEVICE).toString();
final Buffer payload = Buffer.buffer("some payload");
adapter.onMessageReceived(AmqpContext.fromMessage(delivery, getFakeMessage(to, payload), span, null)).onComplete(ctx.failing(t -> {
ctx.verify(() -> {
// THEN the adapter does not send the message (regardless of the delivery mode).
assertNoTelemetryMessageHasBeenSentDownstream();
// AND notifies the device by sending back a REJECTED disposition
verify(delivery).disposition(any(Rejected.class), eq(true));
// AND has reported the message as unprocessable
verify(metrics).reportTelemetry(eq(EndpointType.TELEMETRY), eq(TEST_TENANT_ID), eq(tenantObject), eq(ProcessingOutcome.UNPROCESSABLE), eq(MetricsTags.QoS.AT_LEAST_ONCE), eq(payload.length()), any());
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class VertxBasedAmqpProtocolAdapterTest method testUploadCommandResponseSucceeds.
/**
* Verify that the AMQP adapter forwards command responses downstream.
*
* @param ctx The vert.x test context.
*/
@Test
public void testUploadCommandResponseSucceeds(final VertxTestContext ctx) {
// GIVEN an AMQP adapter
givenAnAdapter(properties);
final CommandResponseSender responseSender = givenACommandResponseSenderForAnyTenant();
when(responseSender.sendCommandResponse(any(TenantObject.class), any(RegistrationAssertion.class), any(CommandResponse.class), (SpanContext) any())).thenReturn(Future.succeededFuture());
// which is enabled for the test tenant
final TenantObject tenantObject = givenAConfiguredTenant(TEST_TENANT_ID, true);
// WHEN an unauthenticated device publishes a command response
final String replyToAddress = String.format("%s/%s/%s", getCommandResponseEndpoint(), TEST_TENANT_ID, Commands.getDeviceFacingReplyToId("test-reply-id", TEST_DEVICE, MessagingType.amqp));
final Map<String, Object> propertyMap = new HashMap<>();
propertyMap.put(MessageHelper.APP_PROPERTY_STATUS, 200);
final ApplicationProperties props = new ApplicationProperties(propertyMap);
final Buffer payload = Buffer.buffer("some payload");
final Message message = getFakeMessage(replyToAddress, payload);
message.setCorrelationId("correlation-id");
message.setApplicationProperties(props);
final ProtonDelivery delivery = mock(ProtonDelivery.class);
adapter.onMessageReceived(AmqpContext.fromMessage(delivery, message, span, null)).onComplete(ctx.succeeding(ok -> {
ctx.verify(() -> {
// THEN the adapter forwards the command response message downstream
verify(responseSender).sendCommandResponse(eq(tenantObject), any(RegistrationAssertion.class), any(CommandResponse.class), (SpanContext) any());
// and reports the forwarded message
verify(metrics).reportCommand(eq(Direction.RESPONSE), eq(TEST_TENANT_ID), eq(tenantObject), eq(ProcessingOutcome.FORWARDED), eq(payload.length()), any());
});
ctx.completeNow();
}));
}
Aggregations