use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class AbstractVertxBasedMqttProtocolAdapterTest method testEndpointHandlerRejectsDeviceOfDisabledTenant.
/**
* Verifies that an adapter rejects a connection attempt from a device that belongs to a tenant for which the
* adapter is disabled.
*/
@Test
public void testEndpointHandlerRejectsDeviceOfDisabledTenant() {
// GIVEN an adapter
givenAnAdapter(properties);
// which is disabled for tenant "my-tenant"
final TenantObject myTenantConfig = TenantObject.from("my-tenant", true);
myTenantConfig.addAdapter(new Adapter(ADAPTER_TYPE).setEnabled(Boolean.FALSE));
when(tenantClient.get(eq("my-tenant"), (SpanContext) any())).thenReturn(Future.succeededFuture(myTenantConfig));
when(authHandler.authenticateDevice(any(MqttConnectContext.class))).thenReturn(Future.succeededFuture(new DeviceUser("my-tenant", "4711")));
// WHEN a device of "my-tenant" tries to connect
final MqttEndpoint endpoint = mockEndpoint();
adapter.handleEndpointConnection(endpoint);
// THEN the connection is not established
verify(endpoint).reject(MqttConnectReturnCode.CONNECTION_REFUSED_NOT_AUTHORIZED);
verify(metrics).reportConnectionAttempt(ConnectionAttemptOutcome.ADAPTER_DISABLED, "my-tenant", null);
}
use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class AbstractVertxBasedMqttProtocolAdapterTest method verifyEventMessageUsesTtlValueGivenInPropertyBag.
/**
* Verifies that the TTL for a downstream event is set to the given <em>time-to-live</em> value in the
* <em>property-bag</em>.
*
* @param ctx The vert.x test context.
*/
@Test
public void verifyEventMessageUsesTtlValueGivenInPropertyBag(final VertxTestContext ctx) {
// Given an adapter
givenAnAdapter(properties);
givenAnEventSenderForAnyTenant();
// WHEN a "device" of "tenant" publishes an event message with a TTL value of 30 seconds.
final MqttPublishMessage msg = mock(MqttPublishMessage.class);
when(msg.topicName()).thenReturn("e/tenant/device/?hono-ttl=30¶m2=value2");
when(msg.qosLevel()).thenReturn(MqttQoS.AT_LEAST_ONCE);
adapter.uploadEventMessage(newMqttContext(msg, mockEndpoint(), span), "tenant", "device", Buffer.buffer("test")).onComplete(ctx.succeeding(t -> {
ctx.verify(() -> {
// THEN the TTL value of the amqp message is 30 seconds.
assertEventHasBeenSentDownstream("tenant", "device", null, 30L);
});
ctx.completeNow();
}));
}
use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class AbstractVertxBasedMqttProtocolAdapterTest method testUploadTelemetryMessageFailsForDisabledTenant.
/**
* Verifies that the adapter does not forward a message published by a device if the device belongs to a tenant for
* which the adapter has been disabled and that the adapter closes the connection to the device.
*/
@Test
public void testUploadTelemetryMessageFailsForDisabledTenant() {
// GIVEN an adapter
properties.setAuthenticationRequired(false);
givenAnAdapter(properties);
givenATelemetrySenderForAnyTenant();
// which is disabled for tenant "my-tenant"
final TenantObject myTenantConfig = TenantObject.from("my-tenant", true);
myTenantConfig.addAdapter(new Adapter(ADAPTER_TYPE).setEnabled(Boolean.FALSE));
when(tenantClient.get(eq("my-tenant"), (SpanContext) any())).thenReturn(Future.succeededFuture(myTenantConfig));
// WHEN a device of "my-tenant" publishes a telemetry message
final MqttEndpoint endpoint = mockEndpoint();
when(endpoint.isConnected()).thenReturn(Boolean.TRUE);
adapter.handleEndpointConnection(endpoint);
final ArgumentCaptor<Handler<MqttPublishMessage>> messageHandler = VertxMockSupport.argumentCaptorHandler();
verify(endpoint).publishHandler(messageHandler.capture());
final MqttPublishMessage msg = mock(MqttPublishMessage.class);
when(msg.topicName()).thenReturn("t/my-tenant/the-device");
when(msg.qosLevel()).thenReturn(MqttQoS.AT_LEAST_ONCE);
when(msg.payload()).thenReturn(Buffer.buffer("hello"));
messageHandler.getValue().handle(msg);
// THEN the message has not been sent downstream
assertNoTelemetryMessageHasBeenSentDownstream();
verify(metrics, never()).reportTelemetry(any(MetricsTags.EndpointType.class), anyString(), any(), eq(MetricsTags.ProcessingOutcome.FORWARDED), any(MetricsTags.QoS.class), anyInt(), any());
// and the connection to the client has been closed
verify(endpoint).close();
}
use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class AmqpConnectionIT method testConnectFailsAfterDeviceDeleted.
/**
* Verifies that after a device has already connected successfully to the adapter, the deletion of device
* registration data causes the adapter to refuse any following connection attempts.
* <p>
* This test relies upon the registration client cache data in the adapter getting deleted when the device is
* deleted (triggered via a corresponding notification from the device registry).
*
* @param ctx The test context.
*/
@Test
public void testConnectFailsAfterDeviceDeleted(final VertxTestContext ctx) {
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), password)).compose(con -> {
// first connection attempt successful
con.close();
// now remove device
return helper.registry.deregisterDevice(tenantId, deviceId);
}).compose(ok -> {
final Promise<Void> resultPromise = Promise.promise();
// device deleted, now wait a bit for the device registry notifications to trigger registration cache invalidation
vertx.setTimer(500, tid -> resultPromise.complete());
return resultPromise.future();
}).compose(ok -> connectToAdapter(IntegrationTestSupport.getUsername(deviceId, tenantId), password)).onComplete(ctx.failing(t -> {
ctx.verify(() -> assertThat(t).isInstanceOf(AuthenticationException.class));
ctx.completeNow();
}));
}
use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class CoapTestBase method testUploadMessageFailsForDisabledGateway.
/**
* Verifies that the CoAP adapter rejects messages from a disabled gateway
* for an enabled device with a 403.
*
* @param ctx The test context
*/
@Test
@Timeout(value = 10, timeUnit = TimeUnit.SECONDS)
public void testUploadMessageFailsForDisabledGateway(final VertxTestContext ctx) {
// GIVEN a device that is connected via a disabled gateway
final Tenant tenant = new Tenant();
final String gatewayId = helper.getRandomDeviceId(tenantId);
final Device gatewayData = new Device();
gatewayData.setEnabled(false);
final Device deviceData = new Device();
deviceData.setVia(Collections.singletonList(gatewayId));
helper.registry.addPskDeviceForTenant(tenantId, tenant, gatewayId, gatewayData, SECRET).compose(ok -> helper.registry.registerDevice(tenantId, deviceId, deviceData)).compose(ok -> {
// WHEN the gateway tries to upload a message for the device
final Promise<OptionSet> result = Promise.promise();
final CoapClient client = getCoapsClient(gatewayId, tenantId, SECRET);
// THEN a FORBIDDEN response code is returned
client.advanced(getHandler(result, ResponseCode.FORBIDDEN), createCoapsRequest(Code.PUT, getPutResource(tenantId, deviceId), 0));
return result.future();
}).onComplete(ctx.succeedingThenComplete());
}
Aggregations