jaroslav@43
|
1 |
package org.apidesign.apifest08.test;
|
jaroslav@43
|
2 |
|
japod@57
|
3 |
import java.math.BigDecimal;
|
japod@57
|
4 |
import java.util.Currency;
|
jaroslav@43
|
5 |
import junit.framework.TestCase;
|
jaroslav@43
|
6 |
import org.apidesign.apifest08.currency.Convertor;
|
japod@57
|
7 |
import org.apidesign.apifest08.currency.MonetaryAmount;
|
japod@57
|
8 |
import org.apidesign.apifest08.currency.anothervendor.ZigZaggingBidirectionalConvertor;
|
jaroslav@43
|
9 |
|
jaroslav@43
|
10 |
/** The exchange rates are not always the same. They are changing. Day by day,
|
jaroslav@43
|
11 |
* hour by hour, minute by minute. For every bank it is important to always
|
jaroslav@43
|
12 |
* have the actual exchange rate available in the system. That is why let's
|
jaroslav@43
|
13 |
* create a pluggable convertor that will always have up to date value of its
|
jaroslav@43
|
14 |
* exchange rate.
|
jaroslav@43
|
15 |
* <p>
|
jaroslav@43
|
16 |
* The quest for today is to allow 3rd party developer to write a convertor
|
jaroslav@43
|
17 |
* that adjusts its exchange rate everytime it is queried. This convertor is
|
jaroslav@43
|
18 |
* written by independent vendor, the vendor knows only your Convertor API,
|
jaroslav@43
|
19 |
* he does not know how the whole system looks and how the convertor is supposed
|
jaroslav@43
|
20 |
* to be used.
|
jaroslav@43
|
21 |
*/
|
jaroslav@43
|
22 |
public class Task3Test extends TestCase {
|
jaroslav@43
|
23 |
public Task3Test(String testName) {
|
jaroslav@43
|
24 |
super(testName);
|
jaroslav@43
|
25 |
}
|
jaroslav@43
|
26 |
|
jaroslav@43
|
27 |
@Override
|
jaroslav@43
|
28 |
protected void setUp() throws Exception {
|
jaroslav@43
|
29 |
}
|
jaroslav@43
|
30 |
|
jaroslav@43
|
31 |
@Override
|
jaroslav@43
|
32 |
protected void tearDown() throws Exception {
|
jaroslav@43
|
33 |
}
|
jaroslav@43
|
34 |
|
japod@57
|
35 |
protected static final Currency CZK = Currency.getInstance( "CZK" );
|
japod@57
|
36 |
protected static final Currency SKK = Currency.getInstance( "SKK" );
|
japod@57
|
37 |
protected static final Currency USD = Currency.getInstance( "USD" );
|
japod@57
|
38 |
|
jaroslav@43
|
39 |
// Backward compatibly enhance your existing API to support following
|
jaroslav@43
|
40 |
// usecases:
|
jaroslav@43
|
41 |
//
|
jaroslav@43
|
42 |
|
jaroslav@43
|
43 |
|
jaroslav@43
|
44 |
/** Without knowing anything about the surrounding system, write an
|
jaroslav@43
|
45 |
* implementation of convertor that will return different rates everytime
|
jaroslav@43
|
46 |
* it is queried. Convert USD to CZK and vice versa. Start with the rate of
|
jaroslav@43
|
47 |
* 1USD = 16CZK and adjust it in favor of CZK by 0.01 CZK with every query.
|
jaroslav@43
|
48 |
* As soon as you reach 1USD = 15CZK adjust it by 0.01 CZK in favor of USD
|
jaroslav@43
|
49 |
* until you reach 1USD = 16CZK
|
jaroslav@43
|
50 |
*
|
jaroslav@43
|
51 |
* @return new instance of "online" USD and CZK convertor starting with rate 1USD = 16CZK
|
jaroslav@43
|
52 |
*/
|
jaroslav@43
|
53 |
public static Convertor createOnlineCZKUSDConvertor() {
|
jaroslav@43
|
54 |
// initial rate: 1USD = 16CZK
|
jaroslav@43
|
55 |
// 2nd query 1USD = 15.99CZK
|
jaroslav@43
|
56 |
// 3rd query 1USD = 15.98CZK
|
jaroslav@43
|
57 |
// until 1USD = 15.00CZK
|
jaroslav@43
|
58 |
// then 1USD = 15.01CZK
|
jaroslav@43
|
59 |
// then 1USD = 15.02CZK
|
jaroslav@43
|
60 |
// and so on and on up to 1USD = 16CZK
|
jaroslav@43
|
61 |
// and then another round to 15, etc.
|
japod@57
|
62 |
final MonetaryAmount oneUSD = new MonetaryAmount( 1, USD );
|
japod@57
|
63 |
final BigDecimal startAmount = new BigDecimal( 16 );
|
japod@57
|
64 |
final BigDecimal endAmount = new BigDecimal( 15 );
|
japod@57
|
65 |
final BigDecimal stepAmount = BigDecimal.ONE.movePointLeft( 2 ).negate();
|
japod@57
|
66 |
return new ContractImposingDelegatingConvertor( new ZigZaggingBidirectionalConvertor( oneUSD, CZK, startAmount, endAmount, stepAmount ) );
|
jaroslav@43
|
67 |
}
|
jaroslav@43
|
68 |
|
jaroslav@43
|
69 |
public void testFewQueriesForOnlineConvertor() {
|
jaroslav@43
|
70 |
Convertor c = createOnlineCZKUSDConvertor();
|
jaroslav@43
|
71 |
doFewQueriesForOnlineConvertor(c);
|
jaroslav@43
|
72 |
}
|
jaroslav@43
|
73 |
|
japod@57
|
74 |
static void doFewQueriesForOnlineConvertor( final Convertor c ) {
|
jaroslav@43
|
75 |
// convert $5 to CZK using c:
|
japod@57
|
76 |
final Convertor.ConversionResult r1 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 5, USD ), CZK ) );
|
japod@57
|
77 |
final MonetaryAmount a1 = r1.getNetAmount();
|
jaroslav@43
|
78 |
//assertEquals("Result is 80 CZK");
|
japod@57
|
79 |
assertNotNull( a1 );
|
japod@57
|
80 |
assertEquals( 80.0, a1.getAmount().doubleValue() );
|
japod@57
|
81 |
assertEquals( CZK, a1.getCurrency() );
|
jaroslav@43
|
82 |
|
jaroslav@43
|
83 |
// convert $8 to CZK using c:
|
japod@57
|
84 |
final Convertor.ConversionResult r2 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 8, USD ), CZK ) );
|
japod@57
|
85 |
final MonetaryAmount a2 = r2.getNetAmount();
|
jaroslav@43
|
86 |
//assertEquals("Result is 127.92 CZK");
|
japod@57
|
87 |
assertNotNull( a2 );
|
japod@57
|
88 |
assertEquals( 12792.0, a2.getAmount().movePointRight( 2 ).doubleValue() );
|
japod@57
|
89 |
assertEquals( CZK, a2.getCurrency() );
|
jaroslav@43
|
90 |
|
jaroslav@43
|
91 |
// convert $1 to CZK using c:
|
japod@57
|
92 |
final Convertor.ConversionResult r3 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 1, USD ), CZK ) );
|
japod@57
|
93 |
final MonetaryAmount a3 = r3.getNetAmount();
|
jaroslav@43
|
94 |
//assertEquals("Result is 15.98 CZK");
|
japod@57
|
95 |
assertNotNull( a3 );
|
japod@57
|
96 |
assertEquals( 1598.0, a3.getAmount().movePointRight( 2 ).doubleValue() );
|
japod@57
|
97 |
assertEquals( CZK, a3.getCurrency() );
|
jaroslav@43
|
98 |
|
jaroslav@43
|
99 |
// convert 15.97CZK to USD using c:
|
japod@57
|
100 |
final BigDecimal s4 = new BigDecimal( 1597 ).movePointLeft( 2 );
|
japod@57
|
101 |
final Convertor.ConversionResult r4 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( s4, CZK ), USD ) );
|
japod@57
|
102 |
final MonetaryAmount a4 = r4.getNetAmount();
|
jaroslav@43
|
103 |
//assertEquals("Result is 1$");
|
japod@57
|
104 |
assertNotNull( a4 );
|
japod@57
|
105 |
assertEquals( 1.0, a4.getAmount().doubleValue() );
|
japod@57
|
106 |
assertEquals( USD, a4.getCurrency() );
|
jaroslav@43
|
107 |
}
|
jaroslav@43
|
108 |
|
jaroslav@43
|
109 |
/** Join the convertors and show they behave sane.
|
jaroslav@43
|
110 |
*/
|
jaroslav@43
|
111 |
public void testOnlineConvertorComposition() throws Exception {
|
japod@57
|
112 |
final Convertor c = Task2Test.merge(
|
jaroslav@43
|
113 |
createOnlineCZKUSDConvertor(),
|
jaroslav@43
|
114 |
Task1Test.createSKKtoCZK()
|
jaroslav@43
|
115 |
);
|
jaroslav@43
|
116 |
|
japod@57
|
117 |
// convert 16CZK using c:
|
japod@57
|
118 |
final Convertor.ConversionResult r4 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 16, CZK ), SKK ) );
|
japod@57
|
119 |
final MonetaryAmount a4 = r4.getNetAmount();
|
jaroslav@43
|
120 |
// assertEquals("Result is 20 SKK");
|
japod@57
|
121 |
assertNotNull( a4 );
|
japod@57
|
122 |
assertEquals( 20.0, a4.getAmount().doubleValue() );
|
japod@57
|
123 |
assertEquals( SKK, a4.getCurrency() );
|
jaroslav@43
|
124 |
|
jaroslav@43
|
125 |
// convert 500SKK to CZK using c:
|
japod@57
|
126 |
final Convertor.ConversionResult r5 = c.convert( new Convertor.ConversionRequest( new MonetaryAmount( 500, SKK ), CZK ) );
|
japod@57
|
127 |
final MonetaryAmount a5 = r5.getNetAmount();
|
jaroslav@43
|
128 |
// assertEquals("Result is 400 CZK");
|
japod@57
|
129 |
assertNotNull( a5 );
|
japod@57
|
130 |
assertEquals( 400.0, a5.getAmount().doubleValue() );
|
japod@57
|
131 |
assertEquals( CZK, a5.getCurrency() );
|
jaroslav@43
|
132 |
|
jaroslav@43
|
133 |
doFewQueriesForOnlineConvertor(c);
|
jaroslav@43
|
134 |
}
|
jaroslav@43
|
135 |
}
|