Skip to content

Commit 1201f34

Browse files
author
Otávio Santana
committed
Merge pull request #58 from otaviojava/master
Adds mock test to MonetaryFunctions that needs an ExchangeProvider
2 parents 183d278 + 439ac1a commit 1201f34

File tree

4 files changed

+179
-4
lines changed

4 files changed

+179
-4
lines changed
Lines changed: 140 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,140 @@
1+
package org.javamoney.moneta.function;
2+
3+
import static org.javamoney.moneta.function.StreamFactory.BRAZILIAN_REAL;
4+
import static org.javamoney.moneta.function.StreamFactory.DOLLAR;
5+
import static org.javamoney.moneta.function.StreamFactory.EURO;
6+
7+
import java.util.Objects;
8+
9+
import javax.money.CurrencyUnit;
10+
import javax.money.MonetaryAmount;
11+
import javax.money.convert.ConversionContext;
12+
import javax.money.convert.CurrencyConversion;
13+
import javax.money.convert.ExchangeRate;
14+
15+
import org.javamoney.moneta.Money;
16+
17+
/**
18+
* This class mock the exchange rate to test some {@link MonetaryFunctions} that
19+
* needs an exchange provider
20+
* @author otaviojava
21+
*/
22+
class CurrencyConversionMock implements CurrencyConversion {
23+
24+
private CurrencyUnit currency;
25+
26+
private Conversation conversation;
27+
28+
29+
public CurrencyConversionMock(CurrencyUnit currency) {
30+
this.currency = currency;
31+
if (DOLLAR.equals(currency)) {
32+
this.conversation = new DollarConversation();
33+
} else if (EURO.equals(currency)) {
34+
this.conversation = new EuroConversation();
35+
} else if (BRAZILIAN_REAL.equals(currency)) {
36+
this.conversation = new RealConversation();
37+
}
38+
}
39+
40+
@Override
41+
public MonetaryAmount apply(MonetaryAmount monetaryAmount) {
42+
return conversation.convert(monetaryAmount);
43+
}
44+
45+
@Override
46+
public CurrencyUnit getCurrency() {
47+
return currency;
48+
}
49+
50+
@Override
51+
public ConversionContext getConversionContext() {
52+
return null;
53+
}
54+
55+
@Override
56+
public ExchangeRate getExchangeRate(MonetaryAmount sourceAmount) {
57+
return null;
58+
}
59+
60+
private class DollarConversation implements Conversation {
61+
private CurrencyUnit currency = DOLLAR;
62+
63+
@Override
64+
public MonetaryAmount convert(MonetaryAmount monetaryAmount) {
65+
66+
CurrencyUnit currencyUnit = Objects.requireNonNull(monetaryAmount)
67+
.getCurrency();
68+
69+
if (currencyUnit.equals(currency)) {
70+
return monetaryAmount;
71+
}
72+
73+
if (BRAZILIAN_REAL.equals(currencyUnit)) {
74+
double val = monetaryAmount.getNumber().doubleValue() / 2.42;
75+
return Money.of(val, this.currency);
76+
} else if (EURO.equals(currencyUnit)) {
77+
double val = monetaryAmount.getNumber().doubleValue() / 0.79;
78+
return Money.of(val, this.currency);
79+
}
80+
return null;
81+
}
82+
83+
}
84+
85+
private class EuroConversation implements Conversation {
86+
87+
private CurrencyUnit currency = EURO;
88+
89+
@Override
90+
public MonetaryAmount convert(MonetaryAmount monetaryAmount) {
91+
92+
CurrencyUnit currencyUnit = Objects.requireNonNull(monetaryAmount)
93+
.getCurrency();
94+
95+
if (currencyUnit.equals(currency)) {
96+
return monetaryAmount;
97+
}
98+
99+
if (BRAZILIAN_REAL.equals(currencyUnit)) {
100+
double val = monetaryAmount.getNumber().doubleValue() / 2.42;
101+
return Money.of(val, this.currency);
102+
} else if (DOLLAR.equals(currencyUnit)) {
103+
double val = monetaryAmount.getNumber().doubleValue() / 1.79;
104+
return Money.of(val, this.currency);
105+
}
106+
return null;
107+
}
108+
}
109+
110+
private class RealConversation implements Conversation {
111+
112+
private CurrencyUnit currency = BRAZILIAN_REAL;
113+
114+
@Override
115+
public MonetaryAmount convert(MonetaryAmount monetaryAmount) {
116+
117+
CurrencyUnit currencyUnit = Objects.requireNonNull(monetaryAmount)
118+
.getCurrency();
119+
120+
if (currencyUnit.equals(currency)) {
121+
return monetaryAmount;
122+
}
123+
124+
if (DOLLAR.equals(currencyUnit)) {
125+
double val = monetaryAmount.getNumber().doubleValue() * 2.42;
126+
return Money.of(val, this.currency);
127+
} else if (EURO.equals(currencyUnit)) {
128+
double val = monetaryAmount.getNumber().doubleValue() * 1.79;
129+
return Money.of(val, this.currency);
130+
}
131+
return null;
132+
}
133+
134+
}
135+
136+
private interface Conversation {
137+
MonetaryAmount convert(MonetaryAmount monetaryAmount);
138+
}
139+
140+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package org.javamoney.moneta.function;
2+
3+
import javax.money.CurrencyUnit;
4+
import javax.money.convert.ConversionQuery;
5+
import javax.money.convert.CurrencyConversion;
6+
import javax.money.convert.ExchangeRate;
7+
import javax.money.convert.ExchangeRateProvider;
8+
import javax.money.convert.ProviderContext;
9+
10+
/**
11+
* This class mock the exchange rate to test some {@link MonetaryFunctions} that
12+
* needs an exchange provider
13+
* @author otaviojava
14+
*/
15+
class ExchangeRateProviderMock implements ExchangeRateProvider {
16+
17+
@Override
18+
public ProviderContext getProviderContext() {
19+
// TODO Auto-generated method stub
20+
return null;
21+
}
22+
23+
@Override
24+
public ExchangeRate getExchangeRate(ConversionQuery conversionQuery) {
25+
// TODO Auto-generated method stub
26+
return null;
27+
}
28+
29+
@Override
30+
public CurrencyConversion getCurrencyConversion(
31+
ConversionQuery conversionQuery) {
32+
CurrencyUnit currencyUnit = conversionQuery.get("Query.termCurrency",
33+
CurrencyUnit.class);
34+
return new CurrencyConversionMock(currencyUnit);
35+
}
36+
37+
}

src/test/java/org/javamoney/moneta/function/MonetaryFunctionsAgregatorTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@
2424
import javax.money.MonetaryAmount;
2525
import javax.money.MonetaryException;
2626
import javax.money.convert.ExchangeRateProvider;
27-
import javax.money.convert.MonetaryConversions;
2827

2928
import org.javamoney.moneta.Money;
3029
import org.testng.Assert;
@@ -41,7 +40,7 @@ public class MonetaryFunctionsAgregatorTest {
4140

4241
@Test
4342
public void init() {
44-
provider = MonetaryConversions.getExchangeRateProvider("ECB");
43+
provider = new ExchangeRateProviderMock();
4544
}
4645

4746
@Test

src/test/java/org/javamoney/moneta/function/MonetaryFunctionsOrderTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@
1616

1717
import javax.money.MonetaryAmount;
1818
import javax.money.convert.ExchangeRateProvider;
19-
import javax.money.convert.MonetaryConversions;
2019

2120
import junit.framework.Assert;
2221

@@ -29,7 +28,7 @@ public class MonetaryFunctionsOrderTest {
2928

3029
@Test
3130
public void init() {
32-
provider = MonetaryConversions.getExchangeRateProvider("ECB");
31+
provider = new ExchangeRateProviderMock();
3332
}
3433

3534
@Test

0 commit comments

Comments
 (0)