Search in sources :

Example 51 with Adapter

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());
}
Also used : TenantObject(org.eclipse.hono.util.TenantObject) Device(org.eclipse.hono.auth.Device) Adapter(org.eclipse.hono.util.Adapter) CoapExchange(org.eclipse.californium.core.server.resources.CoapExchange) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 52 with Adapter

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);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) TenantResult(org.eclipse.hono.util.TenantResult) X500Principal(javax.security.auth.x500.X500Principal) TenantConstants(org.eclipse.hono.util.TenantConstants) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) Truth.assertThat(com.google.common.truth.Truth.assertThat) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) VertxExtension(io.vertx.junit5.VertxExtension) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Timeout(io.vertx.junit5.Timeout) TenantObject(org.eclipse.hono.util.TenantObject) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Adapter(org.eclipse.hono.util.Adapter) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Optional(java.util.Optional) OperationResult(org.eclipse.hono.service.management.OperationResult) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) Id(org.eclipse.hono.service.management.Id) TenantObject(org.eclipse.hono.util.TenantObject) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Adapter(org.eclipse.hono.util.Adapter) Test(org.junit.jupiter.api.Test)

Example 53 with Adapter

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));
}
Also used : JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject) Adapter(org.eclipse.hono.util.Adapter) Test(org.junit.jupiter.api.Test)

Example 54 with Adapter

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)));
}
Also used : Adapter(org.eclipse.hono.util.Adapter) Test(org.junit.jupiter.api.Test)

Example 55 with Adapter

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());
}
Also used : Buffer(io.vertx.core.buffer.Buffer) TenantObject(org.eclipse.hono.util.TenantObject) QoS(org.eclipse.hono.util.QoS) HttpContext(org.eclipse.hono.service.http.HttpContext) EndpointType(org.eclipse.hono.service.metric.MetricsTags.EndpointType) TtdStatus(org.eclipse.hono.service.metric.MetricsTags.TtdStatus) Adapter(org.eclipse.hono.util.Adapter) Test(org.junit.jupiter.api.Test)

Aggregations

Adapter (org.eclipse.hono.util.Adapter)77 Test (org.junit.jupiter.api.Test)74 Truth.assertThat (com.google.common.truth.Truth.assertThat)64 VertxTestContext (io.vertx.junit5.VertxTestContext)64 HttpURLConnection (java.net.HttpURLConnection)64 Timeout (io.vertx.junit5.Timeout)63 TimeUnit (java.util.concurrent.TimeUnit)63 Constants (org.eclipse.hono.util.Constants)62 Future (io.vertx.core.Future)61 Promise (io.vertx.core.Promise)61 JsonObject (io.vertx.core.json.JsonObject)47 Tenant (org.eclipse.hono.service.management.tenant.Tenant)47 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)47 IntegrationTestSupport (org.eclipse.hono.tests.IntegrationTestSupport)45 Tenants (org.eclipse.hono.tests.Tenants)45 BeforeEach (org.junit.jupiter.api.BeforeEach)45 RegistryManagementConstants (org.eclipse.hono.util.RegistryManagementConstants)44 VertxExtension (io.vertx.junit5.VertxExtension)42 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)42 Buffer (io.vertx.core.buffer.Buffer)40