1+ package com .blogspot .e_kanivets .moneytracker .report ;
2+
3+ import android .support .annotation .NonNull ;
4+ import android .support .annotation .Nullable ;
5+
6+ import com .blogspot .e_kanivets .moneytracker .controller .ExchangeRateController ;
7+ import com .blogspot .e_kanivets .moneytracker .model .ExchangeRate ;
8+ import com .blogspot .e_kanivets .moneytracker .model .Record ;
9+ import com .blogspot .e_kanivets .moneytracker .repo .base .IRepo ;
10+ import com .blogspot .e_kanivets .moneytracker .report .base .IExchangeRateProvider ;
11+
12+ import org .junit .After ;
13+ import org .junit .Before ;
14+ import org .junit .Test ;
15+
16+ import java .util .ArrayList ;
17+ import java .util .List ;
18+
19+ import static org .junit .Assert .*;
20+
21+ /**
22+ * JUnit4 test case.
23+ * Created on 2/25/16.
24+ *
25+ * @author Evgenii Kanivets
26+ */
27+ public class ExchangeRateProviderTest {
28+ private ExchangeRateController rateController ;
29+
30+ @ Before
31+ public void setUp () throws Exception {
32+ rateController = new ExchangeRateController (new TestRepo ());
33+ }
34+
35+ @ After
36+ public void tearDown () throws Exception {
37+ rateController = null ;
38+ }
39+
40+ @ Test
41+ public void testGetRate () throws Exception {
42+ IExchangeRateProvider provider ;
43+
44+ try {
45+ provider = new ExchangeRateProvider (null , rateController );
46+ } catch (NullPointerException e ) {
47+ provider = null ;
48+ }
49+
50+ assertNull (provider );
51+
52+ try {
53+ provider = new ExchangeRateProvider ("" , null );
54+ } catch (NullPointerException e ) {
55+ provider = null ;
56+ }
57+
58+ assertNull (provider );
59+
60+ try {
61+ provider = new ExchangeRateProvider (null , null );
62+ } catch (NullPointerException e ) {
63+ provider = null ;
64+ }
65+
66+ assertNull (provider );
67+
68+ provider = new ExchangeRateProvider ("USD" , rateController );
69+
70+ assertEquals (new ExchangeRate (1 , "UAH" , "USD" , 4 ),
71+ provider .getRate (new Record (0 , 0 , "" , "" , 0 , 0 , "UAH" )));
72+
73+ assertEquals (new ExchangeRate (0 , "AFN" , "USD" , 3 ),
74+ provider .getRate (new Record (0 , 0 , "" , "" , 0 , 0 , "AFN" )));
75+
76+ assertNull (provider .getRate (new Record (0 , 0 , "" , "" , 0 , 0 , "SMTH" )));
77+ }
78+
79+ private static class TestRepo implements IRepo <ExchangeRate > {
80+ @ Nullable
81+ @ Override
82+ public ExchangeRate create (ExchangeRate instance ) {
83+ return null ;
84+ }
85+
86+ @ Nullable
87+ @ Override
88+ public ExchangeRate read (long id ) {
89+ return null ;
90+ }
91+
92+ @ Nullable
93+ @ Override
94+ public ExchangeRate update (ExchangeRate instance ) {
95+ return null ;
96+ }
97+
98+ @ Override
99+ public boolean delete (ExchangeRate instance ) {
100+ return false ;
101+ }
102+
103+ @ NonNull
104+ @ Override
105+ public List <ExchangeRate > readAll () {
106+ List <ExchangeRate > rateList = new ArrayList <>();
107+ rateList .add (new ExchangeRate (1 , "UAH" , "USD" , 4 ));
108+ rateList .add (new ExchangeRate (0 , "UAH" , "USD" , 2 ));
109+ rateList .add (new ExchangeRate (0 , "AFN" , "USD" , 3 ));
110+ rateList .add (new ExchangeRate (0 , "USD" , "EUR" , 20 ));
111+ return rateList ;
112+ }
113+
114+ @ NonNull
115+ @ Override
116+ public List <ExchangeRate > readWithCondition (String condition , String [] args ) {
117+ return new ArrayList <>();
118+ }
119+ }
120+ }
0 commit comments