-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathValidateIbanControllerTest.java
More file actions
284 lines (241 loc) · 11.5 KB
/
ValidateIbanControllerTest.java
File metadata and controls
284 lines (241 loc) · 11.5 KB
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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
package com.cl.apps.mssepamd.controllers;
import com.cl.apps.mssepamd.exceptions.TransactionException;
import com.cl.apps.mssepamd.services.KX01Service;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Nested;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.autoconfigure.web.servlet.WebMvcTest;
import org.springframework.boot.test.mock.mockito.MockBean;
import org.springframework.http.MediaType;
import org.springframework.test.web.servlet.MockMvc;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.when;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.*;
/**
* Tests de validation Jakarta pour ValidateIbanController.
*
* Stratégie : @WebMvcTest → charge uniquement la couche web
* KX01Service est mocké → on teste UNIQUEMENT les validations du RequestBody
*
* Champs testés :
* - bic : @NotBlank (B001) + @Size(max=11) (B002)
* - messageIdentification : @NotBlank (B001) + @Size(max=80) (B002)
* - iban : @NotBlank (B001) + @Size(max=34) (B002)
*/
@WebMvcTest(ValidateIbanController.class)
@DisplayName("ValidateIbanController - Validation Jakarta")
class ValidateIbanControllerTest {
private static final String URL = "/MSSEPAMD/diamondIban";
@Autowired
private MockMvc mockMvc;
@Autowired
private ObjectMapper objectMapper;
@MockBean
private KX01Service kx01Service;
// =========================================================================
// Helpers
// =========================================================================
private String buildBody(String bic, String messageIdentification, String iban) {
return """
{
"bic": %s,
"messageIdentification": %s,
"iban": %s
}
""".formatted(
bic != null ? "\"" + bic + "\"" : "null",
messageIdentification != null ? "\"" + messageIdentification + "\"" : "null",
iban != null ? "\"" + iban + "\"" : "null"
);
}
private String validBody() {
return buildBody("CRLYFRPPXXX", "20221020175813703sTCvMgkN66", "FR4330002001010000005000R10");
}
// =========================================================================
// Cas nominal
// =========================================================================
@Nested
@DisplayName("Cas nominal")
class NominalCase {
@Test
@DisplayName("Request valide → 200 OK")
void should_return_200_when_request_is_valid() throws Exception {
// GIVEN
when(kx01Service.checkIbanValidated(any(), any(), any()))
.thenReturn(null); // retour mocké
// WHEN / THEN
mockMvc.perform(post(URL)
.contentType(MediaType.APPLICATION_JSON)
.content(validBody()))
.andExpect(status().isOk());
}
}
// =========================================================================
// Validation : bic
// =========================================================================
@Nested
@DisplayName("Validation champ 'bic'")
class BicValidation {
@Test
@DisplayName("bic null → 400 + code B001")
void should_return_B001_when_bic_is_null() throws Exception {
mockMvc.perform(post(URL)
.contentType(MediaType.APPLICATION_JSON)
.content(buildBody(null, "20221020175813703sTCvMgkN66", "FR4330002001010000005000R10")))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.code").value("B001"));
}
@Test
@DisplayName("bic vide → 400 + code B001")
void should_return_B001_when_bic_is_blank() throws Exception {
mockMvc.perform(post(URL)
.contentType(MediaType.APPLICATION_JSON)
.content(buildBody("", "20221020175813703sTCvMgkN66", "FR4330002001010000005000R10")))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.code").value("B001"));
}
@Test
@DisplayName("bic > 11 caractères → 400 + code B002")
void should_return_B002_when_bic_exceeds_max_size() throws Exception {
// 12 caractères → dépasse max=11
mockMvc.perform(post(URL)
.contentType(MediaType.APPLICATION_JSON)
.content(buildBody("CRLYFRPPXXXX", "20221020175813703sTCvMgkN66", "FR4330002001010000005000R10")))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.code").value("B002"));
}
@Test
@DisplayName("bic = 11 caractères exactement → 200 OK (limite max acceptée)")
void should_return_200_when_bic_is_exactly_max_size() throws Exception {
when(kx01Service.checkIbanValidated(any(), any(), any())).thenReturn(null);
mockMvc.perform(post(URL)
.contentType(MediaType.APPLICATION_JSON)
.content(buildBody("CRLYFRPPXXX", "20221020175813703sTCvMgkN66", "FR4330002001010000005000R10")))
.andExpect(status().isOk());
}
@Test
@DisplayName("bic = 1 caractère → 200 OK (minimum valide)")
void should_return_200_when_bic_is_one_char() throws Exception {
when(kx01Service.checkIbanValidated(any(), any(), any())).thenReturn(null);
mockMvc.perform(post(URL)
.contentType(MediaType.APPLICATION_JSON)
.content(buildBody("A", "20221020175813703sTCvMgkN66", "FR4330002001010000005000R10")))
.andExpect(status().isOk());
}
}
// =========================================================================
// Validation : messageIdentification
// =========================================================================
@Nested
@DisplayName("Validation champ 'messageIdentification'")
class MessageIdentificationValidation {
@Test
@DisplayName("messageIdentification null → 400 + code B001")
void should_return_B001_when_messageIdentification_is_null() throws Exception {
mockMvc.perform(post(URL)
.contentType(MediaType.APPLICATION_JSON)
.content(buildBody("CRLYFRPPXXX", null, "FR4330002001010000005000R10")))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.code").value("B001"));
}
@Test
@DisplayName("messageIdentification vide → 400 + code B001")
void should_return_B001_when_messageIdentification_is_blank() throws Exception {
mockMvc.perform(post(URL)
.contentType(MediaType.APPLICATION_JSON)
.content(buildBody("CRLYFRPPXXX", " ", "FR4330002001010000005000R10")))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.code").value("B001"));
}
@Test
@DisplayName("messageIdentification > 80 caractères → 400 + code B002")
void should_return_B002_when_messageIdentification_exceeds_max_size() throws Exception {
String tooLong = "A".repeat(81); // 81 chars > max=80
mockMvc.perform(post(URL)
.contentType(MediaType.APPLICATION_JSON)
.content(buildBody("CRLYFRPPXXX", tooLong, "FR4330002001010000005000R10")))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.code").value("B002"));
}
@Test
@DisplayName("messageIdentification = 80 caractères → 200 OK (limite max acceptée)")
void should_return_200_when_messageIdentification_is_exactly_max_size() throws Exception {
when(kx01Service.checkIbanValidated(any(), any(), any())).thenReturn(null);
String exactly80 = "A".repeat(80);
mockMvc.perform(post(URL)
.contentType(MediaType.APPLICATION_JSON)
.content(buildBody("CRLYFRPPXXX", exactly80, "FR4330002001010000005000R10")))
.andExpect(status().isOk());
}
}
// =========================================================================
// Validation : iban
// =========================================================================
@Nested
@DisplayName("Validation champ 'iban'")
class IbanValidation {
@Test
@DisplayName("iban null → 400 + code B001")
void should_return_B001_when_iban_is_null() throws Exception {
mockMvc.perform(post(URL)
.contentType(MediaType.APPLICATION_JSON)
.content(buildBody("CRLYFRPPXXX", "20221020175813703sTCvMgkN66", null)))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.code").value("B001"));
}
@Test
@DisplayName("iban vide → 400 + code B001")
void should_return_B001_when_iban_is_blank() throws Exception {
mockMvc.perform(post(URL)
.contentType(MediaType.APPLICATION_JSON)
.content(buildBody("CRLYFRPPXXX", "20221020175813703sTCvMgkN66", "")))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.code").value("B001"));
}
@Test
@DisplayName("iban > 34 caractères → 400 + code B002")
void should_return_B002_when_iban_exceeds_max_size() throws Exception {
String tooLong = "FR" + "4".repeat(33); // 35 chars > max=34
mockMvc.perform(post(URL)
.contentType(MediaType.APPLICATION_JSON)
.content(buildBody("CRLYFRPPXXX", "20221020175813703sTCvMgkN66", tooLong)))
.andExpect(status().isBadRequest())
.andExpect(jsonPath("$.code").value("B002"));
}
@Test
@DisplayName("iban = 34 caractères → 200 OK (limite max acceptée)")
void should_return_200_when_iban_is_exactly_max_size() throws Exception {
when(kx01Service.checkIbanValidated(any(), any(), any())).thenReturn(null);
String exactly34 = "FR" + "4".repeat(32); // 34 chars
mockMvc.perform(post(URL)
.contentType(MediaType.APPLICATION_JSON)
.content(buildBody("CRLYFRPPXXX", "20221020175813703sTCvMgkN66", exactly34)))
.andExpect(status().isOk());
}
}
// =========================================================================
// Body manquant / malformé
// =========================================================================
@Nested
@DisplayName("Body manquant ou malformé")
class MalformedRequest {
@Test
@DisplayName("Body vide → 400")
void should_return_400_when_body_is_empty() throws Exception {
mockMvc.perform(post(URL)
.contentType(MediaType.APPLICATION_JSON)
.content("{}"))
.andExpect(status().isBadRequest());
}
@Test
@DisplayName("Content-Type absent → 415 Unsupported Media Type")
void should_return_415_when_content_type_missing() throws Exception {
mockMvc.perform(post(URL)
.content(validBody()))
.andExpect(status().isUnsupportedMediaType());
}
}
}