From c21d0c1f5b21bbe4ee669e21e98754a532267596 Mon Sep 17 00:00:00 2001 From: Antonio Musarra Date: Mon, 13 May 2024 18:12:14 +0200 Subject: [PATCH] SL: Fixed duplicate code and add test --- .../configuration/EventHandlerAddress.java | 17 ++++++++++++++ .../handlers/nosql/MongoDbEventHandler.java | 6 ++--- .../handlers/queue/AmqpEventHandler.java | 6 ++--- .../ApplicationConfigurationTest.java | 22 +++++++++++++++++++ 4 files changed, 43 insertions(+), 8 deletions(-) diff --git a/src/main/java/it/dontesta/eventbus/application/configuration/EventHandlerAddress.java b/src/main/java/it/dontesta/eventbus/application/configuration/EventHandlerAddress.java index c48e092..c82675d 100644 --- a/src/main/java/it/dontesta/eventbus/application/configuration/EventHandlerAddress.java +++ b/src/main/java/it/dontesta/eventbus/application/configuration/EventHandlerAddress.java @@ -1,5 +1,7 @@ package it.dontesta.eventbus.application.configuration; +import java.util.List; + /** * Questa classe rappresenta l'indirizzo dell'event handler. * @@ -58,4 +60,19 @@ public boolean isEnabled() { public void setEnabled(boolean enabled) { this.enabled = enabled; } + + /** + * Verifica se l'indirizzo dell'event handler esiste e se è abilitato. + * + * @param eventHandlerAddresses la lista degli indirizzi degli event handler + * @param address l'indirizzo dell'event handler + * @return true se l'indirizzo dell'event handler esiste e se è abilitato, false altrimenti + */ + public static boolean isAddressAndExistsEnabled(List eventHandlerAddresses, + String address) { + return eventHandlerAddresses.stream() + .anyMatch(eventHandlerAddress -> + eventHandlerAddress.getAddress().equals(address) && + eventHandlerAddress.isEnabled()); + } } diff --git a/src/main/java/it/dontesta/eventbus/consumers/events/handlers/nosql/MongoDbEventHandler.java b/src/main/java/it/dontesta/eventbus/consumers/events/handlers/nosql/MongoDbEventHandler.java index 1d1d254..451dc0a 100644 --- a/src/main/java/it/dontesta/eventbus/consumers/events/handlers/nosql/MongoDbEventHandler.java +++ b/src/main/java/it/dontesta/eventbus/consumers/events/handlers/nosql/MongoDbEventHandler.java @@ -69,10 +69,8 @@ public class MongoDbEventHandler { void onStart(@Observes StartupEvent ev) { - boolean existsAndEnabled = eventHandlerAddresses.stream() - .anyMatch(eventHandlerAddress -> - eventHandlerAddress.getAddress().equals(SOURCE_VIRTUAL_ADDRESS) && - eventHandlerAddress.isEnabled()); + boolean existsAndEnabled = EventHandlerAddress.isAddressAndExistsEnabled( + eventHandlerAddresses, SOURCE_VIRTUAL_ADDRESS); if (existsAndEnabled) { log.debugf( diff --git a/src/main/java/it/dontesta/eventbus/consumers/events/handlers/queue/AmqpEventHandler.java b/src/main/java/it/dontesta/eventbus/consumers/events/handlers/queue/AmqpEventHandler.java index af6dd20..94e3d1d 100644 --- a/src/main/java/it/dontesta/eventbus/consumers/events/handlers/queue/AmqpEventHandler.java +++ b/src/main/java/it/dontesta/eventbus/consumers/events/handlers/queue/AmqpEventHandler.java @@ -61,10 +61,8 @@ public class AmqpEventHandler { void onStart(@Observes StartupEvent ev) { - boolean existsAndEnabled = eventHandlerAddresses.stream() - .anyMatch(eventHandlerAddress -> - eventHandlerAddress.getAddress().equals(SOURCE_VIRTUAL_ADDRESS) && - eventHandlerAddress.isEnabled()); + boolean existsAndEnabled = EventHandlerAddress.isAddressAndExistsEnabled( + eventHandlerAddresses, SOURCE_VIRTUAL_ADDRESS); if (existsAndEnabled) { log.debugf("Registering the AMQP event handler at addresses: {%s}", diff --git a/src/test/java/it/dontesta/eventbus/application/configuration/ApplicationConfigurationTest.java b/src/test/java/it/dontesta/eventbus/application/configuration/ApplicationConfigurationTest.java index 6131cf1..b393561 100644 --- a/src/test/java/it/dontesta/eventbus/application/configuration/ApplicationConfigurationTest.java +++ b/src/test/java/it/dontesta/eventbus/application/configuration/ApplicationConfigurationTest.java @@ -7,6 +7,7 @@ import io.quarkus.test.junit.QuarkusTest; import it.dontesta.eventbus.application.configuration.converter.EventHandlerAddressConverter; +import java.util.ArrayList; import java.util.List; import org.eclipse.microprofile.config.inject.ConfigProperty; import org.junit.jupiter.api.Assertions; @@ -23,6 +24,12 @@ class ApplicationConfigurationTest { void setUp() { converter = new EventHandlerAddressConverter(); eventHandlerAddress = new EventHandlerAddress("testAddress", true); + + eventHandlerAddresses = new ArrayList<>(); + eventHandlerAddresses.add(new EventHandlerAddress("testAddress1", true)); + eventHandlerAddresses.add(new EventHandlerAddress("testAddress2", false)); + eventHandlerAddresses.add(new EventHandlerAddress("testAddress3", true)); + } @Test @@ -60,6 +67,21 @@ void setEnabled() { assertFalse(eventHandlerAddress.isEnabled()); } + @Test + void testIsAddressAndExistsEnabled() { + // Test when address exists and is enabled + assertTrue(EventHandlerAddress.isAddressAndExistsEnabled(eventHandlerAddresses, "testAddress1")); + + // Test when address exists but is not enabled + assertFalse(EventHandlerAddress.isAddressAndExistsEnabled(eventHandlerAddresses, "testAddress2")); + + // Test when address does not exist + assertFalse(EventHandlerAddress.isAddressAndExistsEnabled(eventHandlerAddresses, "nonExistingAddress")); + } + + + private List eventHandlerAddresses; + private EventHandlerAddress eventHandlerAddress; private EventHandlerAddressConverter converter;