Search in sources :

Example 41 with RegistrationAssertion

use of org.eclipse.hono.util.RegistrationAssertion in project hono by eclipse.

the class ProtonBasedDownstreamSenderTest method testDownstreamEventHasCreationTimeAndTtl.

/**
 * Verifies that a downstream event always contains a creation-time
 * and a time-to-live as defined at the tenant level.
 */
@Test
public void testDownstreamEventHasCreationTimeAndTtl() {
    final TenantObject tenant = TenantObject.from(Constants.DEFAULT_TENANT, true);
    final RegistrationAssertion device = new RegistrationAssertion("4711");
    tenant.setResourceLimits(new ResourceLimits().setMaxTtl(60L));
    // WHEN sending a message without specifying any properties
    sender.sendEvent(tenant, device, "text/plain", Buffer.buffer("hello"), null, span.context());
    // THEN the message contains a creation-time
    verify(protonSender).send(argThat(message -> message.getCreationTime() > 0 && message.getTtl() == 60000L), VertxMockSupport.anyHandler());
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) CsvSource(org.junit.jupiter.params.provider.CsvSource) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ArgumentMatchers.argThat(org.mockito.ArgumentMatchers.argThat) AmqpClientUnitTestHelper(org.eclipse.hono.client.amqp.test.AmqpClientUnitTestHelper) HashMap(java.util.HashMap) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Supplier(java.util.function.Supplier) Constants(org.eclipse.hono.util.Constants) EventBus(io.vertx.core.eventbus.EventBus) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Map(java.util.Map) Message(org.apache.qpid.proton.message.Message) HonoConnection(org.eclipse.hono.client.HonoConnection) ClientConfigProperties(org.eclipse.hono.config.ClientConfigProperties) ResourceLimits(org.eclipse.hono.util.ResourceLimits) Tracer(io.opentracing.Tracer) NoopTracerFactory(io.opentracing.noop.NoopTracerFactory) Vertx(io.vertx.core.Vertx) ServerErrorException(org.eclipse.hono.client.ServerErrorException) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) MessageHelper(org.eclipse.hono.util.MessageHelper) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Mockito.verify(org.mockito.Mockito.verify) TenantObject(org.eclipse.hono.util.TenantObject) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Buffer(io.vertx.core.buffer.Buffer) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) Span(io.opentracing.Span) ProtonSender(io.vertx.proton.ProtonSender) SendMessageSampler(org.eclipse.hono.client.SendMessageSampler) NoopSpan(io.opentracing.noop.NoopSpan) QoS(org.eclipse.hono.util.QoS) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) TenantObject(org.eclipse.hono.util.TenantObject) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) ResourceLimits(org.eclipse.hono.util.ResourceLimits) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 42 with RegistrationAssertion

use of org.eclipse.hono.util.RegistrationAssertion in project hono by eclipse.

the class AbstractAutoProvisioningEventSender method sendAutoProvisioningEvent.

/**
 * Send an auto-provisioning event with content type
 * {@value EventConstants#CONTENT_TYPE_DEVICE_PROVISIONING_NOTIFICATION}.
 *
 * @param tenantId The tenant identifier.
 * @param tenant The tenant information.
 * @param deviceId The device identifier.
 * @param gatewayId The gateway identifier if an edge device is being auto-provisioned.
 * @param span The active OpenTracing span for this operation. It is not to be closed in this method! An
 *            implementation should log (error) events on this span and it may set tags and use this span as the
 *            parent for any spans created in this method.
 * @return A future indicating the outcome of the operation. The future will be succeeded if the auto-provisioning
 *         event is sent successfully.
 * @throws NullPointerException if any of the parameters except gateway id is {@code null}.
 * @see "https://www.eclipse.org/hono/docs/api/event/#device-provisioning-notification"
 */
protected Future<Void> sendAutoProvisioningEvent(final String tenantId, final Tenant tenant, final String deviceId, final String gatewayId, final Span span) {
    Objects.requireNonNull(tenantId);
    Objects.requireNonNull(deviceId);
    Objects.requireNonNull(span);
    // TODO to remove once able to send events without providing an argument of type TenantObject
    final TenantObject tenantConfig = DeviceRegistryUtils.convertTenant(tenantId, tenant).mapTo(TenantObject.class);
    final EventSender eventSender = eventSenderProvider.getClient(tenantConfig);
    return eventSender.sendEvent(tenantConfig, new RegistrationAssertion(deviceId), EventConstants.CONTENT_TYPE_DEVICE_PROVISIONING_NOTIFICATION, null, assembleAutoProvisioningEventProperties(tenantId, gatewayId), span.context()).onSuccess(ok -> {
        span.log("sent auto-provisioning notification");
        LOG.debug("sent auto-provisioning notification [tenant-id: {}, device-id: {}, gateway-id: {}]", tenantId, deviceId, gatewayId);
    }).onFailure(t -> {
        TracingHelper.logError(span, "error sending auto-provisioning notification", t);
        LOG.warn("error sending auto-provisioning notification [tenant-id: {}, device-id: {}, gateway-id: {}]", tenantId, deviceId, gatewayId);
    });
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Lifecycle(org.eclipse.hono.util.Lifecycle) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) Constants(org.eclipse.hono.util.Constants) Tags(io.opentracing.tag.Tags) Tenant(org.eclipse.hono.service.management.tenant.Tenant) DeviceManagementService(org.eclipse.hono.service.management.device.DeviceManagementService) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) Device(org.eclipse.hono.service.management.device.Device) Logger(org.slf4j.Logger) EventSender(org.eclipse.hono.client.telemetry.EventSender) MessagingClientProvider(org.eclipse.hono.client.util.MessagingClientProvider) Vertx(io.vertx.core.Vertx) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) DeviceStatus(org.eclipse.hono.service.management.device.DeviceStatus) MessageHelper(org.eclipse.hono.util.MessageHelper) EventConstants(org.eclipse.hono.util.EventConstants) Future(io.vertx.core.Future) TenantObject(org.eclipse.hono.util.TenantObject) Objects(java.util.Objects) Optional(java.util.Optional) Span(io.opentracing.Span) DeviceRegistryUtils(org.eclipse.hono.deviceregistry.util.DeviceRegistryUtils) TenantObject(org.eclipse.hono.util.TenantObject) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) EventSender(org.eclipse.hono.client.telemetry.EventSender)

Aggregations

RegistrationAssertion (org.eclipse.hono.util.RegistrationAssertion)42 Future (io.vertx.core.Future)31 HttpURLConnection (java.net.HttpURLConnection)29 MessageHelper (org.eclipse.hono.util.MessageHelper)29 Map (java.util.Map)27 Span (io.opentracing.Span)26 TenantObject (org.eclipse.hono.util.TenantObject)26 Buffer (io.vertx.core.buffer.Buffer)24 Constants (org.eclipse.hono.util.Constants)22 ServerErrorException (org.eclipse.hono.client.ServerErrorException)21 Device (org.eclipse.hono.auth.Device)20 Test (org.junit.jupiter.api.Test)20 ClientErrorException (org.eclipse.hono.client.ClientErrorException)18 Handler (io.vertx.core.Handler)17 Objects (java.util.Objects)17 ResourceIdentifier (org.eclipse.hono.util.ResourceIdentifier)17 HashMap (java.util.HashMap)16 Command (org.eclipse.hono.client.command.Command)16 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)16 SpanContext (io.opentracing.SpanContext)15