diff --git a/backend/src/main/java/de/cotto/lndmanagej/service/RebalanceService.java b/backend/src/main/java/de/cotto/lndmanagej/service/RebalanceService.java index 263c32e2..0fcb9093 100644 --- a/backend/src/main/java/de/cotto/lndmanagej/service/RebalanceService.java +++ b/backend/src/main/java/de/cotto/lndmanagej/service/RebalanceService.java @@ -43,8 +43,8 @@ public class RebalanceService { getAmountFromPeer(pubkey), getTargetCostsForPeer(pubkey), getAmountToPeer(pubkey), - Coins.NONE, - Coins.NONE + getSupportAsSourceAmountFromPeer(pubkey), + getSupportAsTargetAmountToPeer(pubkey) ); } diff --git a/backend/src/test/java/de/cotto/lndmanagej/service/RebalanceServiceTest.java b/backend/src/test/java/de/cotto/lndmanagej/service/RebalanceServiceTest.java index fea4d01c..4103c55f 100644 --- a/backend/src/test/java/de/cotto/lndmanagej/service/RebalanceServiceTest.java +++ b/backend/src/test/java/de/cotto/lndmanagej/service/RebalanceServiceTest.java @@ -150,6 +150,34 @@ class RebalanceServiceTest { assertThat(rebalanceService.getReportForPeer(PUBKEY)).isEqualTo(expected); } + @Test + void getRebalanceReportForPeer_supportAsSource() { + RebalanceReport expected = new RebalanceReport( + FEE_FOR_TWO_REBALANCES, + AMOUNT_FOR_TWO_REBALANCES, + Coins.NONE, + Coins.NONE, + Coins.ofMilliSatoshis(123), + Coins.NONE + ); + mockSupportAsSourceForPeer(); + assertThat(rebalanceService.getReportForPeer(PUBKEY)).isEqualTo(expected); + } + + @Test + void getRebalanceReportForPeer_supportAsTarget() { + RebalanceReport expected = new RebalanceReport( + Coins.NONE, + Coins.NONE, + Coins.ofMilliSatoshis(20), + Coins.ofMilliSatoshis(246), + Coins.NONE, + Coins.ofMilliSatoshis(123) + ); + mockSupportAsTargetForPeer(); + assertThat(rebalanceService.getReportForPeer(PUBKEY)).isEqualTo(expected); + } + @Test void getSourceCostsForChannel() { mockSelfPaymentsFromChannel(CHANNEL_ID.toString()); @@ -275,12 +303,7 @@ class RebalanceServiceTest { @Test void getSupportAsSourceAmountFromPeer() { - when(channelService.getAllChannelsWith(PUBKEY)).thenReturn(Set.of(CLOSED_CHANNEL)); - when(selfPaymentsService.getSelfPaymentsFromChannel(CHANNEL_ID)).thenReturn(List.of( - getSelfPayment("from " + CHANNEL_ID, 0), - getSelfPayment("from: " + CHANNEL_ID, 1), - getSelfPayment("to: " + CHANNEL_ID_2, 2) - )); + mockSupportAsSourceForPeer(); assertThat(rebalanceService.getSupportAsSourceAmountFromPeer(PUBKEY)).isEqualTo(AMOUNT_PAID); } @@ -296,12 +319,7 @@ class RebalanceServiceTest { @Test void getSupportAsTargetAmountToPeer() { - when(channelService.getAllChannelsWith(PUBKEY)).thenReturn(Set.of(CLOSED_CHANNEL_2)); - when(selfPaymentsService.getSelfPaymentsToChannel(CHANNEL_ID_2)).thenReturn(List.of( - getSelfPayment("from " + CHANNEL_ID, 0), - getSelfPayment("to " + CHANNEL_ID_2, 1), - getSelfPayment("to " + CHANNEL_ID_2, 2) - )); + mockSupportAsTargetForPeer(); assertThat(rebalanceService.getSupportAsTargetAmountToPeer(PUBKEY)).isEqualTo(AMOUNT_PAID); } @@ -379,4 +397,22 @@ class RebalanceServiceTest { when(selfPaymentsService.getSelfPaymentsFromChannel(id1)).thenReturn(List.of(selfPayment1)); when(selfPaymentsService.getSelfPaymentsFromChannel(id2)).thenReturn(List.of(selfPayment2)); } + + private void mockSupportAsSourceForPeer() { + when(channelService.getAllChannelsWith(PUBKEY)).thenReturn(Set.of(CLOSED_CHANNEL)); + when(selfPaymentsService.getSelfPaymentsFromChannel(CHANNEL_ID)).thenReturn(List.of( + getSelfPayment("from " + CHANNEL_ID, 0), + getSelfPayment("from: " + CHANNEL_ID, 1), + getSelfPayment("to: " + CHANNEL_ID_2, 2) + )); + } + + private void mockSupportAsTargetForPeer() { + when(channelService.getAllChannelsWith(PUBKEY)).thenReturn(Set.of(CLOSED_CHANNEL_2)); + when(selfPaymentsService.getSelfPaymentsToChannel(CHANNEL_ID_2)).thenReturn(List.of( + getSelfPayment("from " + CHANNEL_ID, 0), + getSelfPayment("to " + CHANNEL_ID_2, 1), + getSelfPayment("to " + CHANNEL_ID_2, 2) + )); + } } \ No newline at end of file