From d36b87070ce6491fafcb3efe26d2db856829cc79 Mon Sep 17 00:00:00 2001 From: Jade Carino Date: Fri, 28 Jun 2024 18:08:02 +0100 Subject: [PATCH] Only register KafkaEventsService if remote run Signed-off-by: Jade Carino --- .../KafkaEventsServiceRegistration.java | 17 +++++++++----- .../TestKafkaEventsServiceRegistration.java | 22 +++++++++++++++++-- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/galasa-extensions-parent/dev.galasa.events.kafka/src/main/java/dev/galasa/events/kafka/internal/KafkaEventsServiceRegistration.java b/galasa-extensions-parent/dev.galasa.events.kafka/src/main/java/dev/galasa/events/kafka/internal/KafkaEventsServiceRegistration.java index c9717949..09097ed6 100644 --- a/galasa-extensions-parent/dev.galasa.events.kafka/src/main/java/dev/galasa/events/kafka/internal/KafkaEventsServiceRegistration.java +++ b/galasa-extensions-parent/dev.galasa.events.kafka/src/main/java/dev/galasa/events/kafka/internal/KafkaEventsServiceRegistration.java @@ -5,6 +5,8 @@ */ package dev.galasa.events.kafka.internal; +import java.net.URI; + import javax.validation.constraints.NotNull; import org.osgi.service.component.annotations.Component; @@ -27,13 +29,18 @@ public void initialise(@NotNull IFrameworkInitialisation frameworkInitialisation try { - IFramework framework = frameworkInitialisation.getFramework(); + URI cps = frameworkInitialisation.getBootstrapConfigurationPropertyStore(); + + // If the CPS is ETCD, then register this version of the EventsService + if (cps.getScheme().equals("etcd")) { + IFramework framework = frameworkInitialisation.getFramework(); - SystemEnvironment env = new SystemEnvironment(); - KafkaEventProducerFactory producerFactory = new KafkaEventProducerFactory(env); - IConfigurationPropertyStoreService cpsService = framework.getConfigurationPropertyService(NAMESPACE); + SystemEnvironment env = new SystemEnvironment(); + KafkaEventProducerFactory producerFactory = new KafkaEventProducerFactory(env); + IConfigurationPropertyStoreService cpsService = framework.getConfigurationPropertyService(NAMESPACE); - frameworkInitialisation.registerEventsService(new KafkaEventsService(cpsService, producerFactory)); + frameworkInitialisation.registerEventsService(new KafkaEventsService(cpsService, producerFactory)); + } } catch (Exception e) { throw new KafkaException("Unable to register the Kafka Events Service", e); diff --git a/galasa-extensions-parent/dev.galasa.events.kafka/src/test/java/dev/galasa/events/kafka/TestKafkaEventsServiceRegistration.java b/galasa-extensions-parent/dev.galasa.events.kafka/src/test/java/dev/galasa/events/kafka/TestKafkaEventsServiceRegistration.java index de86f141..97600809 100644 --- a/galasa-extensions-parent/dev.galasa.events.kafka/src/test/java/dev/galasa/events/kafka/TestKafkaEventsServiceRegistration.java +++ b/galasa-extensions-parent/dev.galasa.events.kafka/src/test/java/dev/galasa/events/kafka/TestKafkaEventsServiceRegistration.java @@ -9,6 +9,7 @@ import static org.assertj.core.api.Assertions.*; +import java.net.URI; import java.util.List; import dev.galasa.events.kafka.internal.KafkaEventsService; @@ -24,11 +25,12 @@ public void TestCanCreateARegistrationOK() { } @Test - public void TestCanInitialiseARegistrationOK() throws Exception { + public void TestWhenRemoteRunCanInitialiseARegistrationOK() throws Exception { // Given... KafkaEventsServiceRegistration registration = new KafkaEventsServiceRegistration(); - MockFrameworkInitialisation mockFrameworkInit = new MockFrameworkInitialisation(); + URI uri = new URI("etcd://my.server/api"); + MockFrameworkInitialisation mockFrameworkInit = new MockFrameworkInitialisation(uri); // When... registration.initialise(mockFrameworkInit); @@ -38,5 +40,21 @@ public void TestCanInitialiseARegistrationOK() throws Exception { assertThat(eventsServices).isNotNull().hasSize(1); assertThat(eventsServices.get(0)).isInstanceOf(KafkaEventsService.class); } + + @Test + public void TestWhenLocalRunDoesNotInitialiseRegistration() throws Exception { + // Given... + KafkaEventsServiceRegistration registration = new KafkaEventsServiceRegistration(); + + URI uri = new URI("notetcd://my.server/api"); + MockFrameworkInitialisation mockFrameworkInit = new MockFrameworkInitialisation(uri); + + // When... + registration.initialise(mockFrameworkInit); + + // Then... + List eventsServices = mockFrameworkInit.getRegisteredEventsServices(); + assertThat(eventsServices).isNotNull().hasSize(0); + } }