|
4 | 4 | import org.junit.jupiter.api.AfterEach;
|
5 | 5 | import org.junit.jupiter.api.BeforeEach;
|
6 | 6 | import org.junit.jupiter.api.Test;
|
| 7 | +import org.junit.jupiter.params.ParameterizedTest; |
| 8 | +import org.junit.jupiter.params.provider.MethodSource; |
7 | 9 | import org.mockito.InjectMocks;
|
8 | 10 | import org.mockito.MockitoAnnotations;
|
9 | 11 | import org.springframework.http.MediaType;
|
10 | 12 | import org.springframework.test.web.servlet.MockMvc;
|
11 | 13 |
|
12 | 14 | import java.time.LocalDate;
|
| 15 | +import java.util.ArrayList; |
| 16 | +import java.util.List; |
| 17 | +import java.util.stream.Stream; |
13 | 18 |
|
14 | 19 | import static memos.tutorials.customvalidation.TestUtils.buildMockMvc;
|
15 | 20 | import static memos.tutorials.customvalidation.TestUtils.getObjectMapper;
|
@@ -49,140 +54,89 @@ void shouldReturn204() throws Exception {
|
49 | 54 |
|
50 | 55 | }
|
51 | 56 |
|
52 | | - @Test |
53 | | - void shouldReturn400WhenLuckyNumberNotValid() throws Exception { |
54 | | - // given |
55 | | - UserRequestDTO dto = getValidUserRequestDTO(); |
56 | | - dto.setPassport("790916a2-75db-4744-a2c5-6127c1271e31"); |
57 | | - dto.setLuckyNumber(13); |
58 | | - |
59 | | - // when |
| 57 | + @ParameterizedTest |
| 58 | + @MethodSource("invalidDTOProvider") |
| 59 | + void shouldReturn400(UserRequestDTO dto) throws Exception { |
60 | 60 | mockMvc.perform(post("/validation-examples")
|
61 | 61 | .contentType(MediaType.APPLICATION_JSON)
|
62 | 62 | .content(getObjectMapper().writeValueAsString(dto)))
|
63 | 63 | .andExpect(status().isBadRequest());
|
64 | | - |
65 | 64 | }
|
66 | 65 |
|
67 | | - @Test |
68 | | - void shouldReturn400WhenMaxDataSizeNotValid() throws Exception { |
69 | | - // given |
| 66 | + private static Stream<UserRequestDTO> invalidDTOProvider() { |
| 67 | + List<UserRequestDTO> requests = new ArrayList<>(); |
| 68 | + |
| 69 | + // Invalid lucky number |
70 | 70 | UserRequestDTO dto = getValidUserRequestDTO();
|
71 | | - dto.setDrivingLicence("790916a2-75db-4744-a2c5-6127c1271e31"); |
72 | | - dto.setFibonacci(null); |
| 71 | + dto.setLuckyNumber(13); |
| 72 | + requests.add(dto); |
73 | 73 |
|
| 74 | + // Invalid max data size |
| 75 | + dto = getValidUserRequestDTO(); |
74 | 76 | dto.setMaxDataSize(64);
|
| 77 | + requests.add(dto); |
75 | 78 |
|
76 | | - // when |
77 | | - mockMvc.perform(post("/validation-examples") |
78 | | - .contentType(MediaType.APPLICATION_JSON) |
79 | | - .content(getObjectMapper().writeValueAsString(dto))) |
80 | | - .andExpect(status().isBadRequest()); |
81 | | - |
82 | | - } |
83 | | - |
84 | | - @Test |
85 | | - void shouldReturn400WhenFibonacciNotValid() throws Exception { |
86 | | - // given |
87 | | - UserRequestDTO dto = getValidUserRequestDTO(); |
88 | | - dto.setLuckyNumber(null); |
89 | | - |
| 79 | + // Invalid fibonacci |
| 80 | + dto = getValidUserRequestDTO(); |
90 | 81 | dto.setFibonacci(4L);
|
| 82 | + requests.add(dto); |
91 | 83 |
|
92 | | - // when |
93 | | - mockMvc.perform(post("/validation-examples") |
94 | | - .contentType(MediaType.APPLICATION_JSON) |
95 | | - .content(getObjectMapper().writeValueAsString(dto))) |
96 | | - .andExpect(status().isBadRequest()); |
97 | | - |
98 | | - } |
99 | | - |
100 | | - @Test |
101 | | - void shouldReturn400WhenSelectiveMandatoryFieldsNotPresent() throws Exception { |
102 | | - // given |
103 | | - UserRequestDTO dto = getValidUserRequestDTO(); |
104 | | - |
| 84 | + // Invalid selective mandatory |
| 85 | + dto = getValidUserRequestDTO(); |
105 | 86 | dto.setPassport("");
|
106 | 87 | dto.setId(null);
|
107 | 88 | dto.setDrivingLicence("");
|
| 89 | + requests.add(dto); |
108 | 90 |
|
109 | | - // when |
110 | | - mockMvc.perform(post("/validation-examples") |
111 | | - .contentType(MediaType.APPLICATION_JSON) |
112 | | - .content(getObjectMapper().writeValueAsString(dto))) |
113 | | - .andExpect(status().isBadRequest()); |
114 | | - |
115 | | - } |
116 | | - |
117 | | - @Test |
118 | | - void shouldReturn400WhenCountryNotValid() throws Exception { |
119 | | - // given |
120 | | - UserRequestDTO dto = getValidUserRequestDTO(); |
121 | | - |
122 | | - dto.setCountry("TUR"); |
123 | | - |
124 | | - // when |
125 | | - mockMvc.perform(post("/validation-examples") |
126 | | - .contentType(MediaType.APPLICATION_JSON) |
127 | | - .content(getObjectMapper().writeValueAsString(dto))) |
128 | | - .andExpect(status().isBadRequest()); |
129 | | - } |
130 | | - |
131 | | - @Test |
132 | | - void shouldReturn400WhenAgeNotValid() throws Exception { |
133 | | - // given |
134 | | - UserRequestDTO dto = getValidUserRequestDTO(); |
| 91 | + // Invalid country |
| 92 | + dto = getValidUserRequestDTO(); |
| 93 | + dto.setCountry("XXX"); |
| 94 | + requests.add(dto); |
135 | 95 |
|
| 96 | + // Invalid age - younger |
| 97 | + dto = getValidUserRequestDTO(); |
136 | 98 | dto.setBirthDate(LocalDate.now());
|
| 99 | + requests.add(dto); |
137 | 100 |
|
138 | | - // when |
139 | | - mockMvc.perform(post("/validation-examples") |
140 | | - .contentType(MediaType.APPLICATION_JSON) |
141 | | - .content(getObjectMapper().writeValueAsString(dto))) |
142 | | - .andExpect(status().isBadRequest()); |
143 | | - |
144 | | - // given |
| 101 | + // Invalid age - older |
145 | 102 | dto = getValidUserRequestDTO();
|
146 | | - |
147 | 103 | dto.setBirthDate(LocalDate.of(1900, 1, 1));
|
| 104 | + requests.add(dto); |
148 | 105 |
|
149 | | - // when |
150 | | - mockMvc.perform(post("/validation-examples") |
151 | | - .contentType(MediaType.APPLICATION_JSON) |
152 | | - .content(getObjectMapper().writeValueAsString(dto))) |
153 | | - .andExpect(status().isBadRequest()); |
154 | | - } |
155 | | - |
156 | | - @Test |
157 | | - void shouldReturn400WhenCombinedValidationNotValid() throws Exception { |
158 | | - // given |
159 | | - UserRequestDTO dto = getValidUserRequestDTO(); |
160 | | - |
| 106 | + // Invalid min, max, excluded number |
| 107 | + dto = getValidUserRequestDTO(); |
161 | 108 | dto.setCombinedValidation(6);
|
| 109 | + requests.add(dto); |
162 | 110 |
|
163 | | - // when |
164 | | - mockMvc.perform(post("/validation-examples") |
165 | | - .contentType(MediaType.APPLICATION_JSON) |
166 | | - .content(getObjectMapper().writeValueAsString(dto))) |
167 | | - .andExpect(status().isBadRequest()); |
168 | | - } |
| 111 | + // Invalid number divisible by 3 |
| 112 | + dto = getValidUserRequestDTO(); |
| 113 | + dto.setSubscriptionDuration(2); |
| 114 | + requests.add(dto); |
169 | 115 |
|
170 | | - @Test |
171 | | - void shouldReturn400WhenSubscriptionDurationNotDivisibleBy3() throws Exception { |
172 | | - // given |
173 | | - UserRequestDTO dto = getValidUserRequestDTO(); |
| 116 | + // Invalid company name |
| 117 | + dto = getValidUserRequestDTO(); |
| 118 | + dto.setType(UserRequestDTO.AccountType.BUSINESS); |
| 119 | + dto.setCompanyName(""); |
| 120 | + requests.add(dto); |
174 | 121 |
|
175 | | - dto.setSubscriptionDuration(2); |
| 122 | + // Invalid personal first name |
| 123 | + dto = getValidUserRequestDTO(); |
| 124 | + dto.setType(UserRequestDTO.AccountType.PERSONAL); |
| 125 | + dto.setFirstName(""); |
| 126 | + requests.add(dto); |
176 | 127 |
|
177 | | - // when |
178 | | - mockMvc.perform(post("/validation-examples") |
179 | | - .contentType(MediaType.APPLICATION_JSON) |
180 | | - .content(getObjectMapper().writeValueAsString(dto))) |
181 | | - .andExpect(status().isBadRequest()); |
| 128 | + // Invalid personal last name |
| 129 | + dto = getValidUserRequestDTO(); |
| 130 | + dto.setType(UserRequestDTO.AccountType.PERSONAL); |
| 131 | + dto.setLastName(""); |
| 132 | + requests.add(dto); |
| 133 | + |
| 134 | + return requests.stream(); |
182 | 135 | }
|
183 | 136 |
|
184 | | - private UserRequestDTO getValidUserRequestDTO() { |
| 137 | + private staticUserRequestDTO getValidUserRequestDTO() { |
185 | 138 | UserRequestDTO dto = new UserRequestDTO();
|
| 139 | + dto.setType(UserRequestDTO.AccountType.PERSONAL); |
186 | 140 | dto.setFirstName("Memo's");
|
187 | 141 | dto.setLastName("Tutorials");
|
188 | 142 | dto.setId("790916a2-75db-4744-a2c5-6127c1271e31");
|
|
0 commit comments