1
+ package pl .pawelosinski .dynatrace .nbp .task .backend .controller ;
2
+
3
+ import com .github .tomakehurst .wiremock .WireMockServer ;
4
+ import org .junit .jupiter .api .AfterAll ;
5
+ import org .junit .jupiter .api .BeforeAll ;
6
+ import org .junit .jupiter .api .Test ;
7
+ import org .junit .jupiter .api .TestInstance ;
8
+ import org .junit .runner .RunWith ;
9
+ import org .springframework .beans .factory .annotation .Autowired ;
10
+ import org .springframework .boot .test .context .SpringBootTest ;
11
+ import org .springframework .boot .test .web .client .TestRestTemplate ;
12
+ import org .springframework .http .HttpStatus ;
13
+ import org .springframework .http .ResponseEntity ;
14
+ import org .springframework .test .context .ActiveProfiles ;
15
+ import org .springframework .test .context .junit4 .SpringJUnit4ClassRunner ;
16
+ import pl .pawelosinski .dynatrace .nbp .task .backend .model .MinMaxRate ;
17
+ import pl .pawelosinski .dynatrace .nbp .task .backend .model .rate .RateC ;
18
+ import pl .pawelosinski .dynatrace .nbp .task .backend .model .table .TableA ;
19
+
20
+ import static com .github .tomakehurst .wiremock .client .WireMock .*;
21
+ import static org .junit .Assert .*;
22
+
23
+ @ RunWith (SpringJUnit4ClassRunner .class )
24
+ @ SpringBootTest (webEnvironment = SpringBootTest .WebEnvironment .RANDOM_PORT )
25
+ @ TestInstance (TestInstance .Lifecycle .PER_CLASS )
26
+ @ ActiveProfiles ("test" )
27
+ public class CurrencyControllerTests {
28
+
29
+ @ Autowired
30
+ private TestRestTemplate testRestTemplate ;
31
+
32
+ private final WireMockServer wireMockServer = new WireMockServer (8123 );
33
+
34
+ @ BeforeAll
35
+ void startWireMock () {
36
+ wireMockServer .start ();
37
+ }
38
+
39
+ @ AfterAll
40
+ void stopWireMock () {
41
+ wireMockServer .stop ();
42
+ }
43
+
44
+
45
+ @ Test
46
+ public void shouldGetRateFromDay () {
47
+ // given
48
+ String currency = "gbp" ;
49
+ String date = "2012-01-02" ;
50
+
51
+ configureFor ("localhost" , 8123 );
52
+ stubFor (get (urlEqualTo ("/api/exchangerates/rates/a/gbp/2012-01-02/?format=json" ))
53
+ .willReturn (aResponse ()
54
+ .withHeader ("Content-Type" , "application/json" )
55
+ .withBodyFile ("fromDay.json" )));
56
+
57
+ // when
58
+ ResponseEntity <TableA > result = testRestTemplate .getForEntity ("/exchanges/{currency}?date={date}" , TableA .class , currency , date );
59
+
60
+ // then
61
+ assertEquals (HttpStatus .OK , result .getStatusCode ());
62
+ assertNotNull (result .getBody ());
63
+ assertEquals (1 , result .getBody ().getRates ().size ());
64
+ assertEquals ("GBP" , result .getBody ().getCode ());
65
+ assertEquals (5.348 , result .getBody ().getRates ().get (0 ).getMid (), 0.0001 );
66
+ }
67
+
68
+ @ Test
69
+ public void shouldNotGetAnyResultFromDay () {
70
+ // given
71
+ String currency = "USD" ;
72
+ String date = "2023-04-22" ;
73
+
74
+ configureFor ("localhost" , 8123 );
75
+ stubFor (get (urlEqualTo ("/api/exchangerates/rates/a/USD/2023-04-22/?format=json" ))
76
+ .willReturn (aResponse ().withStatus (404 )));
77
+
78
+ // when
79
+ ResponseEntity <TableA > result = testRestTemplate .getForEntity ("/exchanges/{currency}?date={date}" , TableA .class , currency , date );
80
+
81
+ // then
82
+ assertEquals (HttpStatus .NOT_FOUND , result .getStatusCode ());
83
+ assertNull (result .getBody ());
84
+ }
85
+ @ Test
86
+ public void shouldGetMinAndMaxRate () {
87
+ // given
88
+ String currency = "gbp" ;
89
+ int quotations = 5 ;
90
+
91
+ configureFor ("localhost" , 8123 );
92
+ stubFor (get (urlEqualTo ("/api/exchangerates/rates/a/gbp/last/5/?format=json" ))
93
+ .willReturn (aResponse ()
94
+ .withHeader ("Content-Type" , "application/json" )
95
+ .withBodyFile ("minMax.json" )));
96
+
97
+ // when
98
+ ResponseEntity <MinMaxRate > result = testRestTemplate .getForEntity ("/extremum/{currency}?quotations={quotations}" , MinMaxRate .class , currency , quotations );
99
+
100
+ // then
101
+ assertEquals (HttpStatus .OK , result .getStatusCode ());
102
+ assertNotNull (result .getBody ());
103
+ assertEquals (5.4321 , result .getBody ().getMax ().getMid (), 0.0001 );
104
+ assertEquals (1.2345 , result .getBody ().getMin ().getMid (), 0.0001 );
105
+ }
106
+
107
+ @ Test
108
+ public void shouldNotGetAnyResultMinMaxWithWrongParam () {
109
+ // given
110
+ String currency = "gbp" ;
111
+ int quotations = 305 ;
112
+
113
+ configureFor ("localhost" , 8123 );
114
+ stubFor (get (urlEqualTo ("/api/exchangerates/rates/a/gbp/last/305/?format=json" ))
115
+ .willReturn (aResponse ().withStatus (400 )));
116
+
117
+ // when
118
+ ResponseEntity <MinMaxRate > result = testRestTemplate .getForEntity ("/extremum/{currency}?quotations={quotations}" , MinMaxRate .class , currency , quotations );
119
+
120
+
121
+ // then
122
+ assertEquals (HttpStatus .BAD_REQUEST , result .getStatusCode ());
123
+ assertNull (result .getBody ());
124
+ }
125
+
126
+
127
+ @ Test
128
+ public void shouldGetRateWithMajorDifference () {
129
+ // given
130
+ String currency = "gbp" ;
131
+ int quotations = 5 ;
132
+
133
+ configureFor ("localhost" , 8123 );
134
+ stubFor (get (urlEqualTo ("/api/exchangerates/rates/c/gbp/last/5/?format=json" ))
135
+ .willReturn (aResponse ()
136
+ .withHeader ("Content-Type" , "application/json" )
137
+ .withBodyFile ("majorDifference.json" )));
138
+
139
+ // when
140
+ ResponseEntity <RateC > result = testRestTemplate .getForEntity ("/majordifference/{currency}?quotations={quotations}" , RateC .class , currency , quotations );
141
+
142
+ // then
143
+ assertEquals (HttpStatus .OK , result .getStatusCode ());
144
+ assertNotNull (result .getBody ());
145
+ assertEquals ("074/C/NBP/2023" , result .getBody ().getNo ());
146
+ assertEquals (5.4321 , result .getBody ().getAsk (), 0.0001 );
147
+ assertEquals (1.2345 , result .getBody ().getBid (), 0.0001 );
148
+ }
149
+
150
+ @ Test
151
+ public void shouldNotGetMajorDifferenceWithWrongParam () {
152
+ // given
153
+ String currency = "abc" ;
154
+ int quotations = 123 ;
155
+
156
+ configureFor ("localhost" , 8123 );
157
+ stubFor (get (urlEqualTo ("/api/exchangerates/rates/a/abc/last/123/?format=json" ))
158
+ .willReturn (aResponse ().withStatus (404 )));
159
+
160
+ // when
161
+ ResponseEntity <RateC > result = testRestTemplate .getForEntity ("/majordifference/{currency}?quotations={quotations}" , RateC .class , currency , quotations );
162
+
163
+ // then
164
+ assertEquals (HttpStatus .NOT_FOUND , result .getStatusCode ());
165
+ assertNull (result .getBody ());
166
+ }
167
+
168
+ }
0 commit comments