|
| 1 | +package bisq.core.user; |
| 2 | + |
| 3 | +import bisq.common.persistence.PersistenceManager; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | + |
| 8 | +import org.mockito.ArgumentCaptor; |
| 9 | +import org.mockito.Mock; |
| 10 | +import org.mockito.junit.jupiter.MockitoExtension; |
| 11 | + |
| 12 | +import org.junit.jupiter.api.BeforeEach; |
| 13 | +import org.junit.jupiter.api.Test; |
| 14 | +import org.junit.jupiter.api.extension.ExtendWith; |
| 15 | + |
| 16 | +import static org.hamcrest.MatcherAssert.assertThat; |
| 17 | +import static org.hamcrest.Matchers.contains; |
| 18 | +import static org.hamcrest.Matchers.hasItems; |
| 19 | +import static org.hamcrest.Matchers.hasSize; |
| 20 | +import static org.mockito.Mockito.doReturn; |
| 21 | +import static org.mockito.Mockito.mock; |
| 22 | +import static org.mockito.Mockito.verify; |
| 23 | + |
| 24 | +@ExtendWith(MockitoExtension.class) |
| 25 | +public class BlockchainExplorerSelectionTest { |
| 26 | + |
| 27 | + private static final List<BlockChainExplorer> ALL_BTC_EXPLORERS = new ArrayList<>(List.of( |
| 28 | + new BlockChainExplorer("alice.space (@alice)", "https://alice.space/tx/", "https://alice.space/address/"), |
| 29 | + new BlockChainExplorer("bob.de (@bob)", "https://bob.onion/tx/", "https://bob.onion/address/"), |
| 30 | + new BlockChainExplorer("charlie.info (@charlie)", "https://charlie.info/tx/", "https://charlie.info/address/"))); |
| 31 | + |
| 32 | + private static final List<BlockChainExplorer> ALL_BSQ_EXPLORERS = new ArrayList<>(List.of( |
| 33 | + new BlockChainExplorer("alice.bisq.space (@alice)", "https://alice.bisq.space/tx/", "https://alice.bisq.space/address/"), |
| 34 | + new BlockChainExplorer("bob.bisq.de (@bob)", "https://bob.bisq.onion/tx/", "https://bob.bisq.onion/address/"), |
| 35 | + new BlockChainExplorer("charlie.bisq.info (@charlie)", "https://charlie.bisq.info/tx/", "https://charlie.bisq.info/address/"))); |
| 36 | + |
| 37 | + @Mock |
| 38 | + Preferences preferences; |
| 39 | + |
| 40 | + @BeforeEach |
| 41 | + void setup() { |
| 42 | + doReturn(ALL_BTC_EXPLORERS).when(preferences).getBlockChainExplorers(); |
| 43 | + doReturn(ALL_BSQ_EXPLORERS).when(preferences).getBsqBlockChainExplorers(); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + void noBtcExploreSet() { |
| 48 | + //noinspection unchecked |
| 49 | + var explorerSelection = new BlockchainExplorerSelection(preferences, mock(PreferencesPayload.class), |
| 50 | + mock(PersistenceManager.class)); |
| 51 | + explorerSelection.selectNodes(); |
| 52 | + |
| 53 | + BlockChainExplorer expectedBtcNode = ALL_BTC_EXPLORERS.get(0); |
| 54 | + verify(preferences).setBlockChainExplorer(expectedBtcNode); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + void deprecatedBtcExplorerSelected() { |
| 59 | + //noinspection unchecked |
| 60 | + var explorerSelection = new BlockchainExplorerSelection(preferences, mock(PreferencesPayload.class), |
| 61 | + mock(PersistenceManager.class)); |
| 62 | + |
| 63 | + BlockChainExplorer selectedExplorer = mock(BlockChainExplorer.class); |
| 64 | + doReturn("bsq.emzy.de").when(selectedExplorer).getName(); |
| 65 | + doReturn(selectedExplorer).when(preferences).getBlockChainExplorer(); |
| 66 | + |
| 67 | + explorerSelection.selectNodes(); |
| 68 | + |
| 69 | + BlockChainExplorer expectedBtcNode = ALL_BTC_EXPLORERS.get(0); |
| 70 | + verify(preferences).setBlockChainExplorer(expectedBtcNode); |
| 71 | + } |
| 72 | + |
| 73 | + @Test |
| 74 | + void noBsqExploreSet() { |
| 75 | + //noinspection unchecked |
| 76 | + var explorerSelection = new BlockchainExplorerSelection(preferences, mock(PreferencesPayload.class), |
| 77 | + mock(PersistenceManager.class)); |
| 78 | + explorerSelection.selectNodes(); |
| 79 | + |
| 80 | + ArgumentCaptor<BlockChainExplorer> argumentCaptor = ArgumentCaptor.forClass(BlockChainExplorer.class); |
| 81 | + verify(preferences).setBsqBlockChainExplorer(argumentCaptor.capture()); |
| 82 | + BlockChainExplorer selectedExplorer = argumentCaptor.getValue(); |
| 83 | + |
| 84 | + assertThat(ALL_BSQ_EXPLORERS, hasItems(selectedExplorer)); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + void deprecatedBsqExplorerSelected() { |
| 89 | + //noinspection unchecked |
| 90 | + var explorerSelection = new BlockchainExplorerSelection(preferences, mock(PreferencesPayload.class), |
| 91 | + mock(PersistenceManager.class)); |
| 92 | + |
| 93 | + BlockChainExplorer selectedExplorer = mock(BlockChainExplorer.class); |
| 94 | + doReturn("bisq.mempool.emzy.de").when(selectedExplorer).getName(); |
| 95 | + doReturn(selectedExplorer).when(preferences).getBsqBlockChainExplorer(); |
| 96 | + |
| 97 | + explorerSelection.selectNodes(); |
| 98 | + |
| 99 | + ArgumentCaptor<BlockChainExplorer> argumentCaptor = ArgumentCaptor.forClass(BlockChainExplorer.class); |
| 100 | + verify(preferences).setBsqBlockChainExplorer(argumentCaptor.capture()); |
| 101 | + BlockChainExplorer newExplorer = argumentCaptor.getValue(); |
| 102 | + |
| 103 | + assertThat(ALL_BSQ_EXPLORERS, hasItems(newExplorer)); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + void noAutoConfirmSelected(@Mock PreferencesPayload prefPayload, |
| 108 | + @SuppressWarnings("rawtypes") @Mock PersistenceManager persistenceManager) { |
| 109 | + //noinspection unchecked |
| 110 | + var explorerSelection = new BlockchainExplorerSelection(preferences, prefPayload, persistenceManager); |
| 111 | + |
| 112 | + List<String> defaultXmrProofServices = List.of("alice-monero-xmr-proof-service.onion", |
| 113 | + "bob-monero-xmr-proof-service.onion"); |
| 114 | + doReturn(defaultXmrProofServices).when(preferences).getDefaultXmrTxProofServices(); |
| 115 | + |
| 116 | + List<AutoConfirmSettings> autoConfirmSettingsList = new ArrayList<>(); |
| 117 | + doReturn(autoConfirmSettingsList).when(prefPayload).getAutoConfirmSettingsList(); |
| 118 | + doReturn(autoConfirmSettingsList).when(preferences).getAutoConfirmSettingsList(); |
| 119 | + |
| 120 | + explorerSelection.selectNodes(); |
| 121 | + |
| 122 | + assertThat(autoConfirmSettingsList, hasSize(1)); |
| 123 | + AutoConfirmSettings confirmSettings = autoConfirmSettingsList.get(0); |
| 124 | + assertThat(confirmSettings.getServiceAddresses(), |
| 125 | + contains(defaultXmrProofServices.get(0), defaultXmrProofServices.get(1))); |
| 126 | + |
| 127 | + verify(persistenceManager).forcePersistNow(); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + void blacklistedAutoConfirmSelected(@Mock PreferencesPayload prefPayload, |
| 132 | + @SuppressWarnings("rawtypes") @Mock PersistenceManager persistenceManager) { |
| 133 | + //noinspection unchecked |
| 134 | + var explorerSelection = new BlockchainExplorerSelection(preferences, prefPayload, persistenceManager); |
| 135 | + |
| 136 | + List<String> defaultXmrProofServices = List.of("alice-monero-xmr-proof-service.onion", |
| 137 | + "bob-monero-xmr-proof-service.onion"); |
| 138 | + doReturn(defaultXmrProofServices).when(preferences).getDefaultXmrTxProofServices(); |
| 139 | + |
| 140 | + List<String> serviceAddresses = List.of("monero3bec7m26vx6si6qo7q7imlaoz45ot5m2b5z2ppgoooo6jx2rqd.onion"); |
| 141 | + var autoConfirmSettings = new AutoConfirmSettings(true, 6, |
| 142 | + 1, serviceAddresses, "XMR"); |
| 143 | + |
| 144 | + List<AutoConfirmSettings> autoConfirmSettingsList = new ArrayList<>(List.of(autoConfirmSettings)); |
| 145 | + doReturn(autoConfirmSettingsList).when(prefPayload).getAutoConfirmSettingsList(); |
| 146 | + doReturn(autoConfirmSettingsList).when(preferences).getAutoConfirmSettingsList(); |
| 147 | + |
| 148 | + explorerSelection.selectNodes(); |
| 149 | + |
| 150 | + assertThat(autoConfirmSettingsList, hasSize(1)); |
| 151 | + AutoConfirmSettings confirmSettings = autoConfirmSettingsList.get(0); |
| 152 | + assertThat(confirmSettings.getServiceAddresses(), |
| 153 | + contains(defaultXmrProofServices.get(0), defaultXmrProofServices.get(1))); |
| 154 | + |
| 155 | + verify(persistenceManager).forcePersistNow(); |
| 156 | + } |
| 157 | +} |
0 commit comments