forked from bisq-network/bisq
-
Notifications
You must be signed in to change notification settings - Fork 0
/
CreateOfferViewModelTest.java
243 lines (198 loc) · 9.12 KB
/
CreateOfferViewModelTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* This file is part of Bisq.
*
* Bisq is free software: you can redistribute it and/or modify it
* under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or (at
* your option) any later version.
*
* Bisq is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public
* License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with Bisq. If not, see <http://www.gnu.org/licenses/>.
*/
package bisq.desktop.main.offer.bisq_v1.createoffer;
import bisq.desktop.util.validation.AltcoinValidator;
import bisq.desktop.util.validation.BtcValidator;
import bisq.desktop.util.validation.FiatPriceValidator;
import bisq.desktop.util.validation.SecurityDepositValidator;
import bisq.core.account.witness.AccountAgeWitnessService;
import bisq.core.btc.model.AddressEntry;
import bisq.core.btc.wallet.BsqWalletService;
import bisq.core.btc.wallet.BtcWalletService;
import bisq.core.locale.Country;
import bisq.core.locale.CryptoCurrency;
import bisq.core.locale.GlobalSettings;
import bisq.core.locale.Res;
import bisq.core.offer.OfferUtil;
import bisq.core.offer.bisq_v1.CreateOfferService;
import bisq.core.offer.bisq_v1.OfferDirection;
import bisq.core.payment.PaymentAccount;
import bisq.core.payment.payload.PaymentMethod;
import bisq.core.provider.fee.FeeService;
import bisq.core.provider.price.MarketPrice;
import bisq.core.provider.price.PriceFeedService;
import bisq.core.trade.statistics.TradeStatisticsManager;
import bisq.core.user.Preferences;
import bisq.core.user.User;
import bisq.core.util.coin.BsqFormatter;
import bisq.core.util.coin.CoinFormatter;
import bisq.core.util.coin.ImmutableCoinFormatter;
import bisq.core.util.validation.InputValidator;
import bisq.common.config.Config;
import org.bitcoinj.core.Coin;
import javafx.beans.property.SimpleIntegerProperty;
import javafx.collections.FXCollections;
import java.time.Instant;
import java.util.UUID;
import org.junit.Before;
import org.junit.Test;
import static bisq.desktop.maker.PreferenceMakers.empty;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;
public class CreateOfferViewModelTest {
private CreateOfferViewModel model;
private final CoinFormatter coinFormatter = new ImmutableCoinFormatter(
Config.baseCurrencyNetworkParameters().getMonetaryFormat());
@Before
public void setUp() {
final CryptoCurrency btc = new CryptoCurrency("BTC", "bitcoin");
GlobalSettings.setDefaultTradeCurrency(btc);
Res.setup();
final BtcValidator btcValidator = new BtcValidator(coinFormatter);
final AltcoinValidator altcoinValidator = new AltcoinValidator();
final FiatPriceValidator fiatPriceValidator = new FiatPriceValidator();
FeeService feeService = mock(FeeService.class);
AddressEntry addressEntry = mock(AddressEntry.class);
BtcWalletService btcWalletService = mock(BtcWalletService.class);
PriceFeedService priceFeedService = mock(PriceFeedService.class);
User user = mock(User.class);
PaymentAccount paymentAccount = mock(PaymentAccount.class);
Preferences preferences = mock(Preferences.class);
BsqFormatter bsqFormatter = mock(BsqFormatter.class);
BsqWalletService bsqWalletService = mock(BsqWalletService.class);
SecurityDepositValidator securityDepositValidator = mock(SecurityDepositValidator.class);
AccountAgeWitnessService accountAgeWitnessService = mock(AccountAgeWitnessService.class);
CreateOfferService createOfferService = mock(CreateOfferService.class);
OfferUtil offerUtil = mock(OfferUtil.class);
var tradeStats = mock(TradeStatisticsManager.class);
when(btcWalletService.getOrCreateAddressEntry(anyString(), any())).thenReturn(addressEntry);
when(btcWalletService.getBalanceForAddress(any())).thenReturn(Coin.valueOf(1000L));
when(priceFeedService.updateCounterProperty()).thenReturn(new SimpleIntegerProperty());
when(priceFeedService.getMarketPrice(anyString())).thenReturn(
new MarketPrice("USD",
12684.0450,
Instant.now().getEpochSecond(),
true));
when(feeService.getTxFee(anyInt())).thenReturn(Coin.valueOf(1000L));
when(user.findFirstPaymentAccountWithCurrency(any())).thenReturn(paymentAccount);
when(paymentAccount.getPaymentMethod()).thenReturn(mock(PaymentMethod.class));
when(user.getPaymentAccountsAsObservable()).thenReturn(FXCollections.observableSet());
when(securityDepositValidator.validate(any())).thenReturn(new InputValidator.ValidationResult(false));
when(accountAgeWitnessService.getMyTradeLimit(any(), any(), any())).thenReturn(100000000L);
when(preferences.getUserCountry()).thenReturn(new Country("ES", "Spain", null));
when(bsqFormatter.formatCoin(any())).thenReturn("0");
when(bsqWalletService.getAvailableBalance()).thenReturn(Coin.ZERO);
when(createOfferService.getRandomOfferId()).thenReturn(UUID.randomUUID().toString());
when(tradeStats.getObservableTradeStatisticsSet()).thenReturn(FXCollections.observableSet());
CreateOfferDataModel dataModel = new CreateOfferDataModel(createOfferService,
null,
offerUtil,
btcWalletService,
bsqWalletService,
empty,
user,
null,
priceFeedService,
accountAgeWitnessService,
feeService,
coinFormatter,
tradeStats,
null);
dataModel.initWithData(OfferDirection.BUY, new CryptoCurrency("BTC", "bitcoin"));
dataModel.activate();
model = new CreateOfferViewModel(dataModel,
null,
fiatPriceValidator,
altcoinValidator,
btcValidator,
null,
securityDepositValidator,
priceFeedService,
null,
null,
preferences,
coinFormatter,
bsqFormatter,
offerUtil);
model.activate();
}
@Test
public void testSyncMinAmountWithAmountUntilChanged() {
assertNull(model.amount.get());
assertNull(model.minAmount.get());
model.amount.set("0.0");
assertEquals("0.0", model.amount.get());
assertNull(model.minAmount.get());
model.amount.set("0.03");
assertEquals("0.03", model.amount.get());
assertEquals("0.03", model.minAmount.get());
model.amount.set("0.0312");
assertEquals("0.0312", model.amount.get());
assertEquals("0.0312", model.minAmount.get());
model.minAmount.set("0.01");
model.onFocusOutMinAmountTextField(true, false);
assertEquals("0.01", model.minAmount.get());
model.amount.set("0.0301");
assertEquals("0.0301", model.amount.get());
assertEquals("0.01", model.minAmount.get());
}
@Test
public void testSyncMinAmountWithAmountWhenZeroCoinIsSet() {
model.amount.set("0.03");
assertEquals("0.03", model.amount.get());
assertEquals("0.03", model.minAmount.get());
model.minAmount.set("0.00");
model.onFocusOutMinAmountTextField(true, false);
model.amount.set("0.04");
assertEquals("0.04", model.amount.get());
assertEquals("0.04", model.minAmount.get());
}
@Test
public void testSyncMinAmountWithAmountWhenSameValueIsSet() {
model.amount.set("0.03");
assertEquals("0.03", model.amount.get());
assertEquals("0.03", model.minAmount.get());
model.minAmount.set("0.03");
model.onFocusOutMinAmountTextField(true, false);
model.amount.set("0.04");
assertEquals("0.04", model.amount.get());
assertEquals("0.04", model.minAmount.get());
}
@Test
public void testSyncMinAmountWithAmountWhenHigherMinAmountValueIsSet() {
model.amount.set("0.03");
assertEquals("0.03", model.amount.get());
assertEquals("0.03", model.minAmount.get());
model.minAmount.set("0.05");
model.onFocusOutMinAmountTextField(true, false);
assertEquals("0.05", model.amount.get());
assertEquals("0.05", model.minAmount.get());
}
@Test
public void testSyncPriceMarginWithVolumeAndFixedPrice() {
model.amount.set("0.01");
model.onFocusOutPriceAsPercentageTextField(true, false); //leave focus without changing
assertEquals("0.00", model.marketPriceMargin.get());
assertEquals("0.00000078", model.volume.get());
assertEquals("12684.04500000", model.price.get());
}
}