use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class CoapContextTest method testStartAckTimerFallsBackToGlobalTimeout.
/**
* Verifies that the global ACK timeout is used if no tenant specific value is configured.
*/
@Test
void testStartAckTimerFallsBackToGlobalTimeout() {
final CoapExchange exchange = mock(CoapExchange.class);
final Adapter coapConfig = new Adapter(Constants.PROTOCOL_ADAPTER_TYPE_COAP);
final TenantObject tenant = TenantObject.from("tenant", true).addAdapter(coapConfig);
final Device authenticatedDevice = new Device(tenant.getTenantId(), "device-id");
final CoapContext ctx = CoapContext.fromRequest(exchange, authenticatedDevice, authenticatedDevice, "4711", span);
ctx.startAcceptTimer(vertx, tenant, 500);
verify(vertx).setTimer(eq(500L), VertxMockSupport.anyHandler());
}
use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class TenantServiceTest method testCreateAndUpdate.
@Test
void testCreateAndUpdate(final Vertx vertx, final VertxTestContext context) {
final var tenant = new Tenant();
tenant.addAdapterConfig(new Adapter("http").setEnabled(false));
this.tenantManagement.createTenant(Optional.of("t1"), tenant, SPAN).onComplete(context.succeeding(result -> {
context.verify(() -> {
assertThat(result.getStatus()).isEqualTo(HttpURLConnection.HTTP_CREATED);
});
})).flatMap(x -> this.tenantAdapter.get("t1").onComplete(context.succeeding(result -> {
context.verify(() -> {
assertThat(result.isOk()).isTrue();
final var json = result.getPayload();
assertThat(json).isNotNull();
assertThat(json.getString(TenantConstants.FIELD_PAYLOAD_TENANT_ID)).isNotNull();
assertThat(json.getBoolean(TenantConstants.FIELD_ENABLED)).isNotNull();
final var read = json.mapTo(TenantObject.class);
assertThat(read).isNotNull();
assertThat(read.isEnabled()).isTrue();
assertThat(read.isAdapterEnabled("http")).isFalse();
final Adapter httpAdapterConfig = read.getAdapter("http");
assertThat(httpAdapterConfig).isNotNull();
assertThat(httpAdapterConfig.isEnabled()).isFalse();
});
}))).flatMap(x -> {
final var update = new Tenant();
update.addAdapterConfig(new Adapter("http").setEnabled(true));
return this.tenantManagement.updateTenant("t1", update, Optional.empty(), SPAN).onComplete(context.succeeding(result -> {
context.verify(() -> {
assertThat(result.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
});
})).flatMap(y -> this.tenantAdapter.get("t1").onComplete(context.succeeding(result -> {
context.verify(() -> {
assertThat(result.isOk()).isTrue();
assertThat(result.getPayload()).isNotNull();
final var read = result.getPayload().mapTo(TenantObject.class);
assertThat(read).isNotNull();
assertThat(read.isEnabled()).isTrue();
assertThat(read.isAdapterEnabled("http")).isTrue();
final Adapter httpAdapterConfig = read.getAdapter("http");
assertThat(httpAdapterConfig).isNotNull();
assertThat(httpAdapterConfig.isEnabled()).isTrue();
});
})));
}).onSuccess(x -> context.completeNow()).onFailure(context::failNow);
}
use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class TenantTest method testEncodeAdapters.
/**
* Verify that a Tenant instance containing multiple "adapters" can be serialized to Json.
*/
@Test
public void testEncodeAdapters() {
final Tenant tenant = new Tenant();
tenant.setEnabled(true);
tenant.addAdapterConfig(new Adapter(Constants.PROTOCOL_ADAPTER_TYPE_HTTP)).addAdapterConfig(new Adapter(Constants.PROTOCOL_ADAPTER_TYPE_MQTT).setEnabled(true).setDeviceAuthenticationRequired(false));
final JsonArray result = JsonObject.mapFrom(tenant).getJsonArray(RegistryManagementConstants.FIELD_ADAPTERS);
assertNotNull(result);
final JsonObject httpAdapter = result.getJsonObject(0);
final JsonObject mqttAdapter = result.getJsonObject(1);
assertEquals(Constants.PROTOCOL_ADAPTER_TYPE_HTTP, httpAdapter.getString(RegistryManagementConstants.FIELD_ADAPTERS_TYPE));
assertFalse(httpAdapter.getBoolean(RegistryManagementConstants.FIELD_ENABLED));
assertTrue(httpAdapter.getBoolean(RegistryManagementConstants.FIELD_ADAPTERS_DEVICE_AUTHENTICATION_REQUIRED));
assertEquals(Constants.PROTOCOL_ADAPTER_TYPE_MQTT, mqttAdapter.getString(RegistryManagementConstants.FIELD_ADAPTERS_TYPE));
assertTrue(mqttAdapter.getBoolean(RegistryManagementConstants.FIELD_ENABLED));
assertFalse(mqttAdapter.getBoolean(RegistryManagementConstants.FIELD_ADAPTERS_DEVICE_AUTHENTICATION_REQUIRED));
}
use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class TenantTest method testAddAdapterOfAlreadyExistingType.
/**
* Verifies that adding an adapter fails, if the adapter's type is same as that of any
* already existing adapters.
*/
@Test
public void testAddAdapterOfAlreadyExistingType() {
final Tenant tenant = new Tenant();
tenant.setEnabled(true);
tenant.addAdapterConfig(new Adapter(Constants.PROTOCOL_ADAPTER_TYPE_HTTP).setEnabled(false).setDeviceAuthenticationRequired(true));
assertThrows(IllegalArgumentException.class, () -> tenant.addAdapterConfig(new Adapter(Constants.PROTOCOL_ADAPTER_TYPE_HTTP).setEnabled(false).setDeviceAuthenticationRequired(true)));
}
use of org.eclipse.hono.util.Adapter in project hono by eclipse.
the class AbstractVertxBasedHttpProtocolAdapterTest method testUploadTelemetryFailsForDisabledTenant.
/**
* Verifies that the adapter fails the upload of a message with a 403
* result if the device belongs to a tenant for which the adapter is
* disabled.
*/
@Test
public void testUploadTelemetryFailsForDisabledTenant() {
// GIVEN an adapter
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"), any())).thenReturn(Future.succeededFuture(myTenantConfig));
// WHEN a device that belongs to "my-tenant" publishes a telemetry message
final Buffer payload = Buffer.buffer("some payload");
final HttpContext ctx = newHttpContext(payload);
adapter.uploadTelemetryMessage(ctx, "my-tenant", "the-device", payload, "application/text");
// THEN the device gets a 403
assertContextFailedWithClientError(ctx, HttpURLConnection.HTTP_FORBIDDEN);
// and no Command consumer has been created for the device
verify(commandConsumerFactory, never()).createCommandConsumer(anyString(), anyString(), VertxMockSupport.anyHandler(), any(), any());
// and the message has not been forwarded downstream
assertNoTelemetryMessageHasBeenSentDownstream();
// and has not been reported as processed
verify(metrics, never()).reportTelemetry(any(MetricsTags.EndpointType.class), anyString(), any(), eq(MetricsTags.ProcessingOutcome.FORWARDED), any(MetricsTags.QoS.class), anyInt(), any(MetricsTags.TtdStatus.class), any());
}
Aggregations