Skip to content

Commit a99373d

Browse files
committed
Integration test: atualizarCliente, returns 409.
Tries to update client, but it is not allowed change the CPF.
1 parent 831544a commit a99373d

2 files changed

Lines changed: 26 additions & 13 deletions

File tree

src/test/java/com/sistemaclliente/ClienteControllerIntegrationTest.java

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,7 @@ public void salvarCliente_existingCPF_returns409() throws Exception {
164164
.andExpect(content().string("O CPF 23501206586 já está cadastrado."));
165165
}
166166

167-
@Test @Transactional
168-
@DisplayName("Search for a client by ID in the database.")
167+
@Test @Transactional @DisplayName("Search for a client by ID in the database.")
169168
public void encontrarClientePorId_success_returns200() throws Exception {
170169
Cliente cliente1 = new Cliente();
171170
cliente1.setNome("Marcus");
@@ -178,14 +177,12 @@ public void encontrarClientePorId_success_returns200() throws Exception {
178177
.andExpect(jsonPath("$.email").value("marcus@gmail.com"));
179178
}
180179

181-
@Test
182-
@DisplayName("Searches for a client that doesn't exist, returns 404")
180+
@Test @DisplayName("Searches for a client that doesn't exist, returns 404")
183181
public void encontrarClientePorId_notFound_returns404() throws Exception {
184182
mvc.perform(get("/encontrarcliente/999")).andExpect(status().isNotFound());
185183
}
186184

187-
@Test @Transactional
188-
@DisplayName("Returns 204, deletes an existing client by ID from database.")
185+
@Test @Transactional @DisplayName("Returns 204, deletes an existing client by ID from database.")
189186
public void deletarClientePorId_success_returns204() throws Exception {
190187
Cliente cliente1 = new Cliente();
191188
cliente1.setNome("Marcus");
@@ -198,16 +195,14 @@ public void deletarClientePorId_success_returns204() throws Exception {
198195
assertThat(repository.findById(cliente1.getId())).isNotPresent();
199196
}
200197

201-
@Test @Transactional
202-
@DisplayName("Deletes a client by a non-existing ID and returns 404.")
198+
@Test @Transactional @DisplayName("Deletes a client by a non-existing ID and returns 404.")
203199
public void deletarClientePorId_clientNotFound_returns404() throws Exception{
204200
mvc.perform(delete("/deletarporid/999")).andExpect(status().isNotFound());
205201

206202
assertThat(repository.findById(999L)).isNotPresent();
207203
}
208204

209-
@Test @Transactional
210-
@DisplayName("Updates client according to the DTO object, returns 200.")
205+
@Test @Transactional @DisplayName("Updates client according to the DTO object, returns 200.")
211206
public void atualizarCliente_success_returns200() throws Exception{
212207
Cliente cliente1 = new Cliente();
213208
cliente1.setNome("Marcus");
@@ -228,8 +223,7 @@ public void atualizarCliente_success_returns200() throws Exception{
228223
assertThat(encontrado.getEmail()).isEqualTo("carlos@gmail.com");
229224
}
230225

231-
@Test
232-
@DisplayName("Tries to update and don't find client by ID, returns 404.")
226+
@Test @DisplayName("Tries to update and don't find client by ID, returns 404.")
233227
public void atualizarCliente_clienteNaoEncontrado_retorno404() throws Exception{
234228
ClienteRequestDTO dto = new ClienteRequestDTO();
235229
dto.setNome("Marcus");
@@ -239,6 +233,25 @@ public void atualizarCliente_clienteNaoEncontrado_retorno404() throws Exception{
239233
mvc.perform(put("/clientes/999").contentType(MediaType.APPLICATION_JSON)
240234
.content(mapper.writeValueAsString(dto))).andExpect(status().isNotFound());
241235
}
236+
237+
@Test @Transactional
238+
@DisplayName("Tries to update client, but it is not allowed change the CPF. Returns 409.")
239+
public void atualizarCliente_changeCpf_returns409() throws Exception{
240+
Cliente cliente1 = new Cliente();
241+
cliente1.setNome("Marcus");
242+
cliente1.setCpf("23501206586");
243+
cliente1.setEmail("marcus@gmail.com");
244+
repository.saveAndFlush(cliente1);
245+
246+
ClienteRequestDTO dto = new ClienteRequestDTO();
247+
dto.setNome("Marcus");
248+
dto.setCpf("52364587425");
249+
dto.setEmail("carlos@gmail.com");
250+
251+
mvc.perform(put("/clientes/"+cliente1.getId()).contentType(MediaType.APPLICATION_JSON)
252+
.content(mapper.writeValueAsString(dto))).andExpect(status().isConflict())
253+
.andExpect(content().string("Alteração de CPF não permitida."));
254+
}
242255
}
243256

244257

src/test/java/com/sistemaclliente/ClienteControllerTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ public void atualizarCliente_verboIncorreto_retorno405() throws Exception{
364364
verifyNoMoreInteractions(service);
365365
}
366366

367-
@Test
367+
@Test @DisplayName("Tries to update client, but it is not allowed change the CPF. Returns 409.")
368368
public void atualizarCliente_trocaDeCpf_retorno409() throws Exception{
369369
when(service.atualizarCliente(anyLong(), any(ClienteRequestDTO.class)))
370370
.thenThrow(new AlteracaoDeCpfException());

0 commit comments

Comments
 (0)