diff --git a/application/src/main/java/de/cotto/lndmanagej/service/ChannelService.java b/application/src/main/java/de/cotto/lndmanagej/service/ChannelService.java index e0d9fa70..5c4b5d7b 100644 --- a/application/src/main/java/de/cotto/lndmanagej/service/ChannelService.java +++ b/application/src/main/java/de/cotto/lndmanagej/service/ChannelService.java @@ -16,6 +16,7 @@ import org.springframework.stereotype.Component; import java.util.Collection; import java.util.Optional; import java.util.Set; +import java.util.function.Supplier; import java.util.stream.Collectors; import java.util.stream.Stream; @@ -100,15 +101,15 @@ public class ChannelService { } public Stream getAllLocalChannels() { - Set openChannels = getOpenChannels(); - Set waitingCloseChannels = getWaitingCloseChannels(); - Set forceClosingChannels = getForceClosingChannels(); - Set closedChannels = getClosedChannels(); + Supplier> openChannels = this::getOpenChannels; + Supplier> closedChannels = this::getClosedChannels; + Supplier> waitingCloseChannels = this::getWaitingCloseChannels; + Supplier> forceClosingChannels = this::getForceClosingChannels; return Stream.of( openChannels, closedChannels, waitingCloseChannels, forceClosingChannels - ).flatMap(Collection::stream); + ).map(Supplier::get).flatMap(Collection::stream); } } diff --git a/application/src/test/java/de/cotto/lndmanagej/service/ChannelServiceTest.java b/application/src/test/java/de/cotto/lndmanagej/service/ChannelServiceTest.java index 12f35499..7f717484 100644 --- a/application/src/test/java/de/cotto/lndmanagej/service/ChannelServiceTest.java +++ b/application/src/test/java/de/cotto/lndmanagej/service/ChannelServiceTest.java @@ -29,6 +29,8 @@ import static de.cotto.lndmanagej.model.WaitingCloseChannelFixtures.WAITING_CLOS import static de.cotto.lndmanagej.model.WaitingCloseChannelFixtures.WAITING_CLOSE_CHANNEL_2; import static de.cotto.lndmanagej.model.WaitingCloseChannelFixtures.WAITING_CLOSE_CHANNEL_TO_NODE_3; import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; import static org.mockito.Mockito.when; @ExtendWith(MockitoExtension.class) @@ -65,6 +67,15 @@ class ChannelServiceTest { assertThat(channelService.getLocalChannel(CHANNEL_ID)).contains(LOCAL_OPEN_CHANNEL); } + @Test + void getLocalChannel_open_tried_first() { + when(grpcChannels.getChannels()).thenReturn(Set.of(LOCAL_OPEN_CHANNEL)); + assertThat(channelService.getLocalChannel(CHANNEL_ID)).contains(LOCAL_OPEN_CHANNEL); + verify(grpcClosedChannels, never()).getClosedChannels(); + verify(grpcChannels, never()).getForceClosingChannels(); + verify(grpcChannels, never()).getWaitingCloseChannels(); + } + @Test void getLocalChannel_waiting_close_channel() { when(grpcChannels.getWaitingCloseChannels()).thenReturn(Set.of(WAITING_CLOSE_CHANNEL, WAITING_CLOSE_CHANNEL_2)); @@ -83,6 +94,14 @@ class ChannelServiceTest { assertThat(channelService.getLocalChannel(CHANNEL_ID)).contains(CLOSED_CHANNEL); } + @Test + void getLocalChannel_closed_tried_second() { + when(grpcClosedChannels.getClosedChannels()).thenReturn(Set.of(CLOSED_CHANNEL)); + assertThat(channelService.getLocalChannel(CHANNEL_ID)).contains(CLOSED_CHANNEL); + verify(grpcChannels, never()).getForceClosingChannels(); + verify(grpcChannels, never()).getWaitingCloseChannels(); + } + @Test void getOpenChannel() { when(grpcChannels.getChannel(CHANNEL_ID_2)).thenReturn(Optional.of(LOCAL_OPEN_CHANNEL));