FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
FetchRate/src/test/java/com/fetchrate/core/ConvertorTest.java at main · simddev/FetchRate · GitHub
simddev
/
FetchRate
Public
Notifications
You must be signed in to change notification settings
Fork
0
Star
1
Code
Issues
0
Pull requests
0
Actions
Projects
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Security and quality
Insights
Expand file tree
Breadcrumbs
FetchRate
/
src
/
test
/
java
/
com
/
fetchrate
/
core
/
ConvertorTest.java
Copy path
More file actions
More file actions
Latest commit
History
History
History
309 lines (246 loc) · 12.8 KB
Breadcrumbs
FetchRate
/
src
/
test
/
java
/
com
/
fetchrate
/
core
/
ConvertorTest.java
Copy path
File metadata and controls
309 lines (246 loc) · 12.8 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
package
com
.
fetchrate
.
core
;
import
com
.
fetchrate
.
persistence
.
RateDatabase
;
import
com
.
fetchrate
.
update
.
CryptoRateUpdater
;
import
org
.
junit
.
jupiter
.
api
.
Test
;
import
org
.
junit
.
jupiter
.
api
.
extension
.
ExtendWith
;
import
org
.
mockito
.
InjectMocks
;
import
org
.
mockito
.
Mock
;
import
org
.
mockito
.
junit
.
jupiter
.
MockitoExtension
;
import
java
.
math
.
BigDecimal
;
import
java
.
time
.
LocalDate
;
import
java
.
util
.
List
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertEquals
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertThrows
;
import
static
org
.
junit
.
jupiter
.
api
.
Assertions
.
assertTrue
;
import
static
org
.
mockito
.
ArgumentMatchers
.
any
;
import
static
org
.
mockito
.
ArgumentMatchers
.
argThat
;
import
static
org
.
mockito
.
ArgumentMatchers
.
eq
;
import
static
org
.
mockito
.
Mockito
.
verifyNoInteractions
;
import
static
org
.
mockito
.
Mockito
.
when
;
@
ExtendWith
(
MockitoExtension
.
class
)
class
ConvertorTest
{
@
Mock
private
RateDatabase
database
;
@
Mock
private
CurrencyClassifier
classifier
;
@
Mock
private
CryptoRateUpdater
cryptoUpdater
;
@
InjectMocks
private
Convertor
convertor
;
private
final
LocalDate
testDate
=
LocalDate
.
of
(
2024
,
1
,
15
);
@
Test
void
convert_eurInputReturnsSameAmount
() {
when
(
classifier
.
isSupported
(
"EUR"
)).
thenReturn
(
true
);
BigDecimal
result
=
convertor
.
convert
(
new
QueryRecord
(
new
BigDecimal
(
"100.00"
),
"EUR"
,
testDate
));
assertEquals
(
new
BigDecimal
(
"100.00"
),
result
);
}
@
Test
void
convert_fiatCurrencyDividesByRate
() {
when
(
classifier
.
isSupported
(
"USD"
)).
thenReturn
(
true
);
when
(
classifier
.
isFiat
(
"USD"
)).
thenReturn
(
true
);
when
(
database
.
findFiatRate
(
any
())).
thenReturn
(
new
FiatRateRecord
(
"USD"
,
testDate
,
new
BigDecimal
(
"2.00"
))
);
BigDecimal
result
=
convertor
.
convert
(
new
QueryRecord
(
new
BigDecimal
(
"200.00"
),
"USD"
,
testDate
));
assertEquals
(
new
BigDecimal
(
"100.00"
),
result
);
}
@
Test
void
convert_unsupportedCurrencyThrowsException
() {
when
(
classifier
.
isSupported
(
"FAKE"
)).
thenReturn
(
false
);
assertThrows
(
IllegalArgumentException
.
class
, () ->
convertor
.
convert
(
new
QueryRecord
(
new
BigDecimal
(
"100"
),
"FAKE"
,
testDate
)));
}
@
Test
void
convert_fiatOnSaturday_throwsWithWeekendMessage
() {
when
(
classifier
.
isSupported
(
"USD"
)).
thenReturn
(
true
);
when
(
classifier
.
isFiat
(
"USD"
)).
thenReturn
(
true
);
LocalDate
saturday
=
LocalDate
.
of
(
2024
,
1
,
13
);
// known Saturday
IllegalArgumentException
ex
=
assertThrows
(
IllegalArgumentException
.
class
, () ->
convertor
.
convert
(
new
QueryRecord
(
new
BigDecimal
(
"100"
),
"USD"
,
saturday
)));
assertTrue
(
ex
.
getMessage
().
contains
(
"weekend"
));
assertTrue
(
ex
.
getMessage
().
contains
(
"2024-01-12"
));
// previous Friday
verifyNoInteractions
(
database
);
}
@
Test
void
convert_fiatOnSunday_throwsWithWeekendMessage
() {
when
(
classifier
.
isSupported
(
"USD"
)).
thenReturn
(
true
);
when
(
classifier
.
isFiat
(
"USD"
)).
thenReturn
(
true
);
LocalDate
sunday
=
LocalDate
.
of
(
2024
,
1
,
14
);
// known Sunday
IllegalArgumentException
ex
=
assertThrows
(
IllegalArgumentException
.
class
, () ->
convertor
.
convert
(
new
QueryRecord
(
new
BigDecimal
(
"100"
),
"USD"
,
sunday
)));
assertTrue
(
ex
.
getMessage
().
contains
(
"weekend"
));
assertTrue
(
ex
.
getMessage
().
contains
(
"2024-01-12"
));
// previous Friday
verifyNoInteractions
(
database
);
}
@
Test
void
convert_fiatRateNotFoundThrowsException
() {
when
(
classifier
.
isSupported
(
"USD"
)).
thenReturn
(
true
);
when
(
classifier
.
isFiat
(
"USD"
)).
thenReturn
(
true
);
when
(
database
.
findFiatRate
(
any
())).
thenThrow
(
new
RateNotFoundException
(
"No rate found for USD on 2024-01-15"
));
assertThrows
(
RateNotFoundException
.
class
, () ->
convertor
.
convert
(
new
QueryRecord
(
new
BigDecimal
(
"100"
),
"USD"
,
testDate
)));
}
@
Test
void
convert_cryptoCurrencyMultipliesByRate
() {
when
(
classifier
.
isSupported
(
"BTC"
)).
thenReturn
(
true
);
when
(
classifier
.
isFiat
(
"BTC"
)).
thenReturn
(
false
);
when
(
database
.
findCryptoRate
(
any
())).
thenReturn
(
new
CryptoRateRecord
(
"BTC"
,
testDate
,
new
BigDecimal
(
"40000.00"
))
);
BigDecimal
result
=
convertor
.
convert
(
new
QueryRecord
(
new
BigDecimal
(
"2.00"
),
"BTC"
,
testDate
));
assertEquals
(
new
BigDecimal
(
"80000.00"
),
result
);
}
@
Test
void
convert_cryptoNotInDb_lazyFetchSucceeds_returnsResult
() {
when
(
classifier
.
isSupported
(
"XRP"
)).
thenReturn
(
true
);
when
(
classifier
.
isFiat
(
"XRP"
)).
thenReturn
(
false
);
// First DB lookup misses, lazy fetch stores it, second lookup hits
when
(
database
.
findCryptoRate
(
any
()))
.
thenThrow
(
new
RateNotFoundException
(
"No crypto rate found for XRP on 2024-01-15"
))
.
thenReturn
(
new
CryptoRateRecord
(
"XRP"
,
testDate
,
new
BigDecimal
(
"0.50"
)));
when
(
cryptoUpdater
.
fetchAndParseSpecific
(
eq
(
"XRP"
),
eq
(
testDate
)))
.
thenReturn
(
List
.
of
(
new
CryptoRateRecord
(
"XRP"
,
testDate
,
new
BigDecimal
(
"0.50"
))));
BigDecimal
result
=
convertor
.
convert
(
new
QueryRecord
(
new
BigDecimal
(
"100"
),
"XRP"
,
testDate
));
assertEquals
(
new
BigDecimal
(
"50.00"
),
result
);
}
@
Test
void
convertTo_eurOutput_returnsSameAsConvert
() {
when
(
classifier
.
isSupported
(
"USD"
)).
thenReturn
(
true
);
when
(
classifier
.
isFiat
(
"USD"
)).
thenReturn
(
true
);
when
(
classifier
.
isSupportedOutputCurrency
(
"EUR"
)).
thenReturn
(
true
);
when
(
database
.
findFiatRate
(
any
())).
thenReturn
(
new
FiatRateRecord
(
"USD"
,
testDate
,
new
BigDecimal
(
"2.00"
))
);
BigDecimal
result
=
convertor
.
convertTo
(
new
QueryRecord
(
new
BigDecimal
(
"200.00"
),
"USD"
,
testDate
),
"EUR"
);
assertEquals
(
new
BigDecimal
(
"100.00"
),
result
);
}
@
Test
void
convertTo_fiatOutput_appliesPivotRate
() {
when
(
classifier
.
isSupported
(
"USD"
)).
thenReturn
(
true
);
when
(
classifier
.
isFiat
(
"USD"
)).
thenReturn
(
true
);
when
(
classifier
.
isSupportedOutputCurrency
(
"GBP"
)).
thenReturn
(
true
);
when
(
database
.
findFiatRate
(
any
())).
thenReturn
(
new
FiatRateRecord
(
"USD"
,
testDate
,
new
BigDecimal
(
"2.00"
))
);
when
(
database
.
findFiatRateOnOrBefore
(
eq
(
"GBP"
),
eq
(
testDate
))).
thenReturn
(
new
FiatRateRecord
(
"GBP"
,
testDate
,
new
BigDecimal
(
"0.85"
))
);
// 200 USD ÷ 2.00 = 100 EUR × 0.85 = 85 GBP
BigDecimal
result
=
convertor
.
convertTo
(
new
QueryRecord
(
new
BigDecimal
(
"200.00"
),
"USD"
,
testDate
),
"GBP"
);
assertEquals
(
new
BigDecimal
(
"85.00"
),
result
);
}
@
Test
void
convertTo_outputRateIsZero_throwsRateNotFoundException
() {
when
(
classifier
.
isSupported
(
"USD"
)).
thenReturn
(
true
);
when
(
classifier
.
isFiat
(
"USD"
)).
thenReturn
(
true
);
when
(
classifier
.
isSupportedOutputCurrency
(
"GBP"
)).
thenReturn
(
true
);
when
(
database
.
findFiatRate
(
any
())).
thenReturn
(
new
FiatRateRecord
(
"USD"
,
testDate
,
new
BigDecimal
(
"1.00"
))
);
when
(
database
.
findFiatRateOnOrBefore
(
eq
(
"GBP"
),
eq
(
testDate
))).
thenReturn
(
new
FiatRateRecord
(
"GBP"
,
testDate
,
BigDecimal
.
ZERO
)
);
assertThrows
(
RateNotFoundException
.
class
, () ->
convertor
.
convertTo
(
new
QueryRecord
(
new
BigDecimal
(
"100"
),
"USD"
,
testDate
),
"GBP"
));
}
@
Test
void
convertTo_unsupportedOutputCurrency_throwsBeforeConversion
() {
when
(
classifier
.
isSupportedOutputCurrency
(
"BTC"
)).
thenReturn
(
false
);
assertThrows
(
IllegalArgumentException
.
class
, () ->
convertor
.
convertTo
(
new
QueryRecord
(
new
BigDecimal
(
"100"
),
"USD"
,
testDate
),
"BTC"
));
verifyNoInteractions
(
database
);
}
@
Test
void
convertToCrypto_dividesByOutputCryptoRate
() {
when
(
classifier
.
isSupported
(
"BTC"
)).
thenReturn
(
true
);
when
(
classifier
.
isFiat
(
"BTC"
)).
thenReturn
(
false
);
when
(
classifier
.
isFiat
(
"ETH"
)).
thenReturn
(
false
);
when
(
database
.
findCryptoRate
(
argThat
(
q
->
q
!=
null
&&
"BTC"
.
equals
(
q
.
currencySymbol
())))).
thenReturn
(
new
CryptoRateRecord
(
"BTC"
,
testDate
,
new
BigDecimal
(
"40000.00"
))
);
when
(
database
.
findCryptoRate
(
argThat
(
q
->
q
!=
null
&&
"ETH"
.
equals
(
q
.
currencySymbol
())))).
thenReturn
(
new
CryptoRateRecord
(
"ETH"
,
testDate
,
new
BigDecimal
(
"2500.00"
))
);
// 2 BTC × 40000 = 80000 EUR ÷ 2500 = 32 ETH
BigDecimal
result
=
convertor
.
convertToCrypto
(
new
QueryRecord
(
new
BigDecimal
(
"2"
),
"BTC"
,
testDate
),
"ETH"
);
assertEquals
(
new
BigDecimal
(
"32.00000000"
),
result
);
}
@
Test
void
convertToCrypto_fiatOutputSymbol_throwsWithHint
() {
when
(
classifier
.
isFiat
(
"USD"
)).
thenReturn
(
true
);
IllegalArgumentException
ex
=
assertThrows
(
IllegalArgumentException
.
class
, () ->
convertor
.
convertToCrypto
(
new
QueryRecord
(
new
BigDecimal
(
"100"
),
"BTC"
,
testDate
),
"USD"
));
assertTrue
(
ex
.
getMessage
().
contains
(
"--to"
));
verifyNoInteractions
(
database
);
}
@
Test
void
convertToCrypto_eurOutputSymbol_throwsWithHint
() {
when
(
classifier
.
isFiat
(
"EUR"
)).
thenReturn
(
false
);
IllegalArgumentException
ex
=
assertThrows
(
IllegalArgumentException
.
class
, () ->
convertor
.
convertToCrypto
(
new
QueryRecord
(
new
BigDecimal
(
"100"
),
"BTC"
,
testDate
),
"EUR"
));
assertTrue
(
ex
.
getMessage
().
contains
(
"--to"
));
verifyNoInteractions
(
database
);
}
@
Test
void
convertToCrypto_outputCryptoNotInDb_lazyFetchSucceeds
() {
when
(
classifier
.
isSupported
(
"USD"
)).
thenReturn
(
true
);
when
(
classifier
.
isFiat
(
"USD"
)).
thenReturn
(
true
);
when
(
classifier
.
isFiat
(
"SOL"
)).
thenReturn
(
false
);
when
(
database
.
findFiatRate
(
any
())).
thenReturn
(
new
FiatRateRecord
(
"USD"
,
testDate
,
new
BigDecimal
(
"1.00"
))
);
when
(
database
.
findCryptoRate
(
any
()))
.
thenThrow
(
new
RateNotFoundException
(
"No crypto rate found for SOL on 2024-01-15"
))
.
thenReturn
(
new
CryptoRateRecord
(
"SOL"
,
testDate
,
new
BigDecimal
(
"100.00"
)));
when
(
cryptoUpdater
.
fetchAndParseSpecific
(
eq
(
"SOL"
),
eq
(
testDate
)))
.
thenReturn
(
List
.
of
(
new
CryptoRateRecord
(
"SOL"
,
testDate
,
new
BigDecimal
(
"100.00"
))));
// 100 USD ÷ 1.00 = 100 EUR ÷ 100 = 1 SOL
BigDecimal
result
=
convertor
.
convertToCrypto
(
new
QueryRecord
(
new
BigDecimal
(
"100"
),
"USD"
,
testDate
),
"SOL"
);
assertEquals
(
new
BigDecimal
(
"1.00000000"
),
result
);
}
@
Test
void
convertToCrypto_outputCryptoNotInDb_lazyFetchFails_rethrows
() {
when
(
classifier
.
isSupported
(
"BTC"
)).
thenReturn
(
true
);
when
(
classifier
.
isFiat
(
"BTC"
)).
thenReturn
(
false
);
when
(
classifier
.
isFiat
(
"XRP"
)).
thenReturn
(
false
);
when
(
database
.
findCryptoRate
(
argThat
(
q
->
q
!=
null
&&
"BTC"
.
equals
(
q
.
currencySymbol
())))).
thenReturn
(
new
CryptoRateRecord
(
"BTC"
,
testDate
,
new
BigDecimal
(
"40000.00"
))
);
when
(
database
.
findCryptoRate
(
argThat
(
q
->
q
!=
null
&&
"XRP"
.
equals
(
q
.
currencySymbol
()))))
.
thenThrow
(
new
RateNotFoundException
(
"No crypto rate found for XRP on 2024-01-15"
));
when
(
cryptoUpdater
.
fetchAndParseSpecific
(
eq
(
"XRP"
),
eq
(
testDate
))).
thenReturn
(
List
.
of
());
assertThrows
(
RateNotFoundException
.
class
, () ->
convertor
.
convertToCrypto
(
new
QueryRecord
(
new
BigDecimal
(
"1"
),
"BTC"
,
testDate
),
"XRP"
));
}
@
Test
void
convert_cryptoNotInDb_lazyFetchReturnsNothing_rethrowsOriginalError
() {
when
(
classifier
.
isSupported
(
"XRP"
)).
thenReturn
(
true
);
when
(
classifier
.
isFiat
(
"XRP"
)).
thenReturn
(
false
);
when
(
database
.
findCryptoRate
(
any
()))
.
thenThrow
(
new
RateNotFoundException
(
"No crypto rate found for XRP on 2024-01-15"
));
when
(
cryptoUpdater
.
fetchAndParseSpecific
(
eq
(
"XRP"
),
eq
(
testDate
))).
thenReturn
(
List
.
of
());
RateNotFoundException
ex
=
assertThrows
(
RateNotFoundException
.
class
, () ->
convertor
.
convert
(
new
QueryRecord
(
new
BigDecimal
(
"100"
),
"XRP"
,
testDate
)));
assertTrue
(
ex
.
getMessage
().
contains
(
"XRP"
));
}
@
Test
void
convert_cryptoInputRateIsZero_throwsRateNotFoundException
() {
when
(
classifier
.
isSupported
(
"BTC"
)).
thenReturn
(
true
);
when
(
classifier
.
isFiat
(
"BTC"
)).
thenReturn
(
false
);
when
(
database
.
findCryptoRate
(
any
())).
thenReturn
(
new
CryptoRateRecord
(
"BTC"
,
testDate
,
BigDecimal
.
ZERO
)
);
assertThrows
(
RateNotFoundException
.
class
, () ->
convertor
.
convert
(
new
QueryRecord
(
new
BigDecimal
(
"1"
),
"BTC"
,
testDate
)));
}
@
Test
void
convert_eurInputOnWeekend_returnsAmount
() {
when
(
classifier
.
isSupported
(
"EUR"
)).
thenReturn
(
true
);
LocalDate
saturday
=
LocalDate
.
of
(
2024
,
1
,
13
);
BigDecimal
result
=
convertor
.
convert
(
new
QueryRecord
(
new
BigDecimal
(
"100.00"
),
"EUR"
,
saturday
));
assertEquals
(
new
BigDecimal
(
"100.00"
),
result
);
verifyNoInteractions
(
database
);
}
}
Back
|
FazBrowse Home
|
New Git URL