Skip to content

Commit

Permalink
SL: Fixed duplicate code and add test
Browse files Browse the repository at this point in the history
  • Loading branch information
amusarra committed May 13, 2024
1 parent 1e2d20a commit c21d0c1
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 8 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package it.dontesta.eventbus.application.configuration;

import java.util.List;

/**
* Questa classe rappresenta l'indirizzo dell'event handler.
*
Expand Down Expand Up @@ -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<EventHandlerAddress> eventHandlerAddresses,
String address) {
return eventHandlerAddresses.stream()
.anyMatch(eventHandlerAddress ->
eventHandlerAddress.getAddress().equals(address) &&
eventHandlerAddress.isEnabled());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down Expand Up @@ -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<EventHandlerAddress> eventHandlerAddresses;

private EventHandlerAddress eventHandlerAddress;

private EventHandlerAddressConverter converter;
Expand Down

0 comments on commit c21d0c1

Please sign in to comment.