Search in sources :

Example 1 with CommandConsumerFactory

use of org.eclipse.hono.commandrouter.CommandConsumerFactory in project hono by eclipse.

the class CommandRouterServiceImplTest method testRegisterCommandRouterAlsoUsingAmqpMessagingSystem.

/**
 * Verifies that a command consumer gets registered in both AMQP and Kafka messaging systems if both are available
 * and the tenant is configured to use Kafka.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testRegisterCommandRouterAlsoUsingAmqpMessagingSystem(final VertxTestContext ctx) {
    // GIVEN a tenant configured to use a Kafka messaging system
    final String tenantId = "tenant";
    final TenantObject tenant = new TenantObject(tenantId, true);
    tenant.setProperty(TenantConstants.FIELD_EXT, Map.of(TenantConstants.FIELD_EXT_MESSAGING_TYPE, MessagingType.kafka.name()));
    when(tenantClient.get(eq(tenantId), any(SpanContext.class))).thenReturn(Future.succeededFuture(tenant));
    // AND a commandConsumerFactoryProvider with both AMQP (see setUp()) and Kafka client providers
    final CommandConsumerFactory kafkaCommandConsumerFactory = mock(CommandConsumerFactory.class);
    when(kafkaCommandConsumerFactory.getMessagingType()).thenReturn(MessagingType.kafka);
    when(kafkaCommandConsumerFactory.start()).thenReturn(Future.succeededFuture());
    when(kafkaCommandConsumerFactory.stop()).thenReturn(Future.succeededFuture());
    when(kafkaCommandConsumerFactory.createCommandConsumer(anyString(), any())).thenReturn(Future.succeededFuture());
    commandConsumerFactoryProvider.setClient(kafkaCommandConsumerFactory);
    // let the AMQP client consumer creation fail
    when(amqpCommandConsumerFactory.createCommandConsumer(anyString(), any())).thenReturn(Future.failedFuture("expected failure"));
    // WHEN registering a command consumer for the tenant
    service.registerCommandConsumer(tenantId, "deviceId", "adapterInstanceId", null, NoopSpan.INSTANCE).onComplete(ctx.succeeding(res -> {
        ctx.verify(() -> {
            // THEN both kinds of consumer clients get used
            verify(kafkaCommandConsumerFactory).createCommandConsumer(anyString(), any());
            verify(amqpCommandConsumerFactory).createCommandConsumer(anyString(), any());
            // and the register operation succeeds even though the AMQP client registration failed
            assertThat(res.getStatus()).isEqualTo(HttpURLConnection.HTTP_NO_CONTENT);
        });
        ctx.completeNow();
    }));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) TenantConstants(org.eclipse.hono.util.TenantConstants) Deque(java.util.Deque) ServiceConfigProperties(org.eclipse.hono.config.ServiceConfigProperties) Context(io.vertx.core.Context) MessagingType(org.eclipse.hono.util.MessagingType) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) DeviceRegistrationClient(org.eclipse.hono.client.registry.DeviceRegistrationClient) Mockito.doAnswer(org.mockito.Mockito.doAnswer) Map(java.util.Map) LinkedList(java.util.LinkedList) MessagingClientProvider(org.eclipse.hono.client.util.MessagingClientProvider) NoopTracerFactory(io.opentracing.noop.NoopTracerFactory) Vertx(io.vertx.core.Vertx) ServerErrorException(org.eclipse.hono.client.ServerErrorException) Mockito.when(org.mockito.Mockito.when) TenantClient(org.eclipse.hono.client.registry.TenantClient) Truth.assertThat(com.google.common.truth.Truth.assertThat) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Mockito.verify(org.mockito.Mockito.verify) DeviceConnectionInfo(org.eclipse.hono.deviceconnection.infinispan.client.DeviceConnectionInfo) TenantObject(org.eclipse.hono.util.TenantObject) SpanContext(io.opentracing.SpanContext) Test(org.junit.jupiter.api.Test) Mockito.never(org.mockito.Mockito.never) List(java.util.List) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) CommandConsumerFactory(org.eclipse.hono.commandrouter.CommandConsumerFactory) NoopSpan(io.opentracing.noop.NoopSpan) Handler(io.vertx.core.Handler) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) AdapterInstanceStatusService(org.eclipse.hono.commandrouter.AdapterInstanceStatusService) TenantObject(org.eclipse.hono.util.TenantObject) SpanContext(io.opentracing.SpanContext) CommandConsumerFactory(org.eclipse.hono.commandrouter.CommandConsumerFactory) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Test(org.junit.jupiter.api.Test)

Example 2 with CommandConsumerFactory

use of org.eclipse.hono.commandrouter.CommandConsumerFactory in project hono by eclipse.

the class Application method commandConsumerFactoryProvider.

private MessagingClientProvider<CommandConsumerFactory> commandConsumerFactoryProvider(final TenantClient tenantClient, final CommandTargetMapper commandTargetMapper) {
    final MessagingClientProvider<CommandConsumerFactory> commandConsumerFactoryProvider = new MessagingClientProvider<>();
    if (kafkaConsumerConfig.isConfigured() && commandResponseKafkaProducerConfig.isConfigured() && commandInternalKafkaProducerConfig.isConfigured()) {
        final KafkaProducerFactory<String, Buffer> kafkaProducerFactory = CachingKafkaProducerFactory.sharedFactory(vertx);
        kafkaProducerFactory.setMetricsSupport(kafkaClientMetricsSupport);
        if (internalKafkaTopicCleanupService == null && commandInternalKafkaProducerConfig.isConfigured() && kafkaConsumerConfig.isConfigured() && kafkaAdminClientConfig.isConfigured() && !(adapterInstanceStatusService instanceof AdapterInstanceStatusService.UnknownStatusProvidingService)) {
            internalKafkaTopicCleanupService = new InternalKafkaTopicCleanupService(vertx, adapterInstanceStatusService, kafkaAdminClientConfig);
        }
        commandConsumerFactoryProvider.setClient(new KafkaBasedCommandConsumerFactoryImpl(vertx, tenantClient, commandTargetMapper, kafkaProducerFactory, commandInternalKafkaProducerConfig, commandResponseKafkaProducerConfig, kafkaConsumerConfig, metrics, kafkaClientMetricsSupport, tracer, internalKafkaTopicCleanupService));
    }
    if (commandConsumerConnectionConfig.isHostConfigured()) {
        commandConsumerFactoryProvider.setClient(new ProtonBasedCommandConsumerFactoryImpl(HonoConnection.newConnection(vertx, commandConsumerConnectionConfig, tracer), tenantClient, commandTargetMapper, metrics, SendMessageSampler.Factory.noop()));
    }
    return commandConsumerFactoryProvider;
}
Also used : Buffer(io.vertx.core.buffer.Buffer) AdapterInstanceStatusService(org.eclipse.hono.commandrouter.AdapterInstanceStatusService) MessagingClientProvider(org.eclipse.hono.client.util.MessagingClientProvider) InternalKafkaTopicCleanupService(org.eclipse.hono.commandrouter.impl.kafka.InternalKafkaTopicCleanupService) KafkaBasedCommandConsumerFactoryImpl(org.eclipse.hono.commandrouter.impl.kafka.KafkaBasedCommandConsumerFactoryImpl) CommandConsumerFactory(org.eclipse.hono.commandrouter.CommandConsumerFactory) ProtonBasedCommandConsumerFactoryImpl(org.eclipse.hono.commandrouter.impl.amqp.ProtonBasedCommandConsumerFactoryImpl)

Aggregations

MessagingClientProvider (org.eclipse.hono.client.util.MessagingClientProvider)2 AdapterInstanceStatusService (org.eclipse.hono.commandrouter.AdapterInstanceStatusService)2 CommandConsumerFactory (org.eclipse.hono.commandrouter.CommandConsumerFactory)2 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 SpanContext (io.opentracing.SpanContext)1 NoopSpan (io.opentracing.noop.NoopSpan)1 NoopTracerFactory (io.opentracing.noop.NoopTracerFactory)1 Context (io.vertx.core.Context)1 Future (io.vertx.core.Future)1 Handler (io.vertx.core.Handler)1 Vertx (io.vertx.core.Vertx)1 Buffer (io.vertx.core.buffer.Buffer)1 VertxExtension (io.vertx.junit5.VertxExtension)1 VertxTestContext (io.vertx.junit5.VertxTestContext)1 HttpURLConnection (java.net.HttpURLConnection)1 Deque (java.util.Deque)1 LinkedList (java.util.LinkedList)1 List (java.util.List)1 Map (java.util.Map)1 ServerErrorException (org.eclipse.hono.client.ServerErrorException)1