-
Notifications
You must be signed in to change notification settings - Fork 30
/
RestTemplateBankApplicationIT.java
237 lines (188 loc) · 8.34 KB
/
RestTemplateBankApplicationIT.java
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
package com.jayway.application;
import com.jayway.config.SpringBootRunner;
import java.net.URI;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.sql.DataSource;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.IntegrationTest;
import org.springframework.boot.test.SpringApplicationConfiguration;
import org.springframework.boot.test.TestRestTemplate;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.web.client.RestTemplate;
import static org.hamcrest.Matchers.*;
import static org.junit.Assert.assertThat;
@RunWith(SpringJUnit4ClassRunner.class)
@ActiveProfiles("mysql")
@WebAppConfiguration
@IntegrationTest
@SpringApplicationConfiguration(classes = SpringBootRunner.class)
public class RestTemplateBankApplicationIT {
@Autowired
DataSource dataSource;
RestTemplate restTemplate;
@Before
public void setUp() {
restTemplate = new TestRestTemplate("user", "secret");
JdbcTemplate jdbcTemplate = new JdbcTemplate(dataSource);
jdbcTemplate.execute("TRUNCATE TABLE account_t");
jdbcTemplate.execute("INSERT INTO account_t (account_number, balance) VALUES (1, 100)");
jdbcTemplate.execute("INSERT INTO account_t (account_number, balance) VALUES (2, 200)");
}
@Test
public void shouldGetAccount() throws Exception {
ResponseEntity<Map> responseEntity = restTemplate
.getForEntity("http://localhost:8080/accounts/{accountNumber}", Map.class, 1);
assertThat(responseEntity.getStatusCode(), is(HttpStatus.OK));
assertThat(responseEntity.getHeaders().getContentType(), is(MediaType.APPLICATION_JSON));
Map<String, Integer> account = responseEntity.getBody();
assertThat(account.get("accountNumber"), is(1));
assertThat(account.get("balance"), is(100));
}
@Test
public void shouldDeleteAccount() throws Exception {
ResponseEntity<Map> responseEntity = restTemplate
.exchange("http://localhost:8080/accounts/{accountNumber}",
HttpMethod.DELETE, null, Map.class, 1);
assertThat(responseEntity.getStatusCode(), is(HttpStatus.NO_CONTENT));
assertThat(responseEntity.getBody(), nullValue());
}
@Test
public void shouldDepositToAccount() throws Exception {
// create payload
Map<String, Integer> body = Collections.singletonMap("amount", 50);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<Map<String, Integer>> entity = new HttpEntity<>(body, headers);
// deposit
ResponseEntity<Map> responseEntity = restTemplate
.postForEntity("http://localhost:8080/accounts/{accountNumber}/deposit",
entity, Map.class, 1);
assertThat(responseEntity.getStatusCode(), is(HttpStatus.NO_CONTENT));
assertThat(responseEntity.getBody(), nullValue());
}
@Test
public void shouldNotDepositNegativeAmount() {
// create payload
Map<String, Integer> body = Collections.singletonMap("amount", -10);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<Map<String, Integer>> entity = new HttpEntity<>(body, headers);
// deposit
ResponseEntity<Void> responseEntity = restTemplate
.postForEntity("http://localhost:8080/accounts/{accountNumber}/deposit",
entity, Void.class, 1);
assertThat(responseEntity.getStatusCode(), is(HttpStatus.BAD_REQUEST));
}
@Test
public void shouldWithdrawFromAccount() {
// create payload
Map<String, Integer> body = Collections.singletonMap("amount", 10);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<Map<String, Integer>> entity = new HttpEntity<>(body, headers);
// deposit
ResponseEntity<Map> responseEntity = restTemplate
.postForEntity("http://localhost:8080/accounts/{accountNumber}/withdraw",
entity, Map.class, 1);
assertThat(responseEntity.getStatusCode(), is(HttpStatus.OK));
Map<String, Integer> account = responseEntity.getBody();
assertThat(account.get("accountNumber"), is(1));
assertThat(account.get("balance"), is(90));
}
@Test
public void shouldNotOverdraw() {
// create payload
Map<String, Integer> body = Collections.singletonMap("amount", 200);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<Map<String, Integer>> entity = new HttpEntity<>(body, headers);
// withdraw
ResponseEntity<Map> responseEntity = restTemplate
.postForEntity("http://localhost:8080/accounts/{accountNumber}/withdraw",
entity, Map.class, 1);
assertThat(responseEntity.getStatusCode(), is(HttpStatus.CONFLICT));
}
@Test
public void shouldGetAccounts() {
ResponseEntity<List> responseEntity = restTemplate
.getForEntity("http://localhost:8080/accounts", List.class);
assertThat(responseEntity.getStatusCode(), is(HttpStatus.OK));
List<Integer> accountNumbers = responseEntity.getBody();
assertThat(accountNumbers.size(), is(2));
assertThat(accountNumbers, contains(1, 2));
}
@Test
public void shouldCreateAccount() {
ResponseEntity<Map> responseEntity = restTemplate
.postForEntity("http://localhost:8080/accounts", null, Map.class);
assertThat(responseEntity.getStatusCode(), is(HttpStatus.CREATED));
HttpHeaders headers = responseEntity.getHeaders();
URI location = headers.getLocation();
assertThat(location.toString(), startsWith("http://localhost:8080/accounts/"));
}
@Test
public void shouldNotGetUnknownAccount() {
ResponseEntity<Void> responseEntity = restTemplate
.getForEntity("http://localhost:8080/accounts/{accountNumber}", Void.class, 0);
assertThat(responseEntity.getStatusCode(), is(HttpStatus.NOT_FOUND));
}
@Test
public void shouldTransfer() {
// create payload
Map<String, Integer> body = new HashMap<String, Integer>(){{
put("fromAccountNumber", 1);
put("toAccountNumber", 2);
put("amount", 50);
}};
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<Map<String, Integer>> entity = new HttpEntity<>(body, headers);
// transfer
ResponseEntity<Map> responseEntity = restTemplate.postForEntity("http://localhost:8080/transfer", entity, Map.class);
assertThat(responseEntity.getStatusCode(), is(HttpStatus.NO_CONTENT));
assertThat(responseEntity.getBody(), nullValue());
// verify first account
responseEntity = restTemplate
.getForEntity("http://localhost:8080/accounts/{accountNumber}", Map.class, 1);
Map<String, Integer> account = responseEntity.getBody();
assertThat(account.get("accountNumber"), is(1));
assertThat(account.get("balance"), is(50));
// verify second account
responseEntity = restTemplate
.getForEntity("http://localhost:8080/accounts/{accountNumber}", Map.class, 2);
account = responseEntity.getBody();
assertThat(account.get("accountNumber"), is(2));
assertThat(account.get("balance"), is(250));
}
@Test
public void shouldNotOverdrawDuringTransfer() {
// create payload
Map<String, Integer> body = new HashMap<String, Integer>(){{
put("fromAccountNumber", 1);
put("toAccountNumber", 2);
put("amount", 300);
}};
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<Map<String, Integer>> entity = new HttpEntity<>(body, headers);
// transfer
ResponseEntity<Map> responseEntity = restTemplate.postForEntity("http://localhost:8080/transfer", entity, Map.class);
assertThat(responseEntity.getStatusCode(), is(HttpStatus.CONFLICT));
assertThat(responseEntity.getBody(), nullValue());
}
}