-
Notifications
You must be signed in to change notification settings - Fork 30
/
Copy pathBankApplicationTest.java
154 lines (123 loc) · 5.13 KB
/
BankApplicationTest.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
package com.jayway.application;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jayway.config.ApplicationConfig;
import com.jayway.config.WebConfig;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.test.context.ActiveProfiles;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.result.MockMvcResultHandlers;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.context.WebApplicationContext;
import java.util.Collections;
import java.util.Map;
import static org.hamcrest.Matchers.is;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {WebConfig.class, ApplicationConfig.class})
@ActiveProfiles({"h2", "disableSecurity"})
@WebAppConfiguration
@Transactional
public class BankApplicationTest {
@Autowired
WebApplicationContext webApplicationContext;
MockMvc mockMvc;
@Before
public void setup() {
mockMvc = webAppContextSetup(webApplicationContext).build();
}
@Test
public void shouldPrint() throws Exception {
mockMvc
.perform(get("/accounts/1")
.accept(MediaType.APPLICATION_JSON))
.andDo(MockMvcResultHandlers.print());
}
@Test
public void shouldGetAccount() throws Exception {
mockMvc
.perform(get("/accounts/1")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("accountNumber").value(1))
.andExpect(jsonPath("balance").value(100));
}
@Test
public void shouldGetAllAccounts() throws Exception {
mockMvc
.perform(get("/accounts")
.accept(MediaType.APPLICATION_JSON))
.andExpect(status().isOk())
.andExpect(content().contentType(MediaType.APPLICATION_JSON))
.andExpect(jsonPath("[0]").value(1))
.andExpect(jsonPath("[1]").value(2));
}
@Test
public void shouldDepositToAccount() throws Exception {
Map<String, Integer> body = Collections.singletonMap("amount", 50);
String json = toJsonString(body);
mockMvc
.perform(post("/accounts/1/deposit")
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isNoContent());
}
@Test
public void shouldDeleteAccount() throws Exception {
mockMvc
.perform(delete("/accounts/1"))
.andExpect(status().isNoContent());
}
@Test
public void shouldNotDepositNegativeAmount() throws Exception {
Map<String, Integer> body = Collections.singletonMap("amount", -50);
String json = toJsonString(body);
mockMvc
.perform(post("/accounts/1/deposit")
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isBadRequest());
}
@Test
public void shouldWithdrawFromAccount() throws Exception {
Map<String, Integer> body = Collections.singletonMap("amount", 50);
String json = toJsonString(body);
mockMvc
.perform(post("/accounts/1/withdraw")
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isOk())
.andExpect(jsonPath("accountNumber", is(1)))
.andExpect(jsonPath("balance", is(50)));
}
@Test
public void shouldNotWithdrawNegativeAmount() throws Exception {
Map<String, Integer> body = Collections.singletonMap("amount", -50);
String json = toJsonString(body);
mockMvc
.perform(post("/accounts/1/withdraw")
.accept(MediaType.APPLICATION_JSON)
.contentType(MediaType.APPLICATION_JSON)
.content(json))
.andExpect(status().isBadRequest());
}
private static String toJsonString(Map<String, ?> map) {
ObjectMapper objectMapper = new ObjectMapper();
try {
return objectMapper.writeValueAsString(map);
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
}