From 2135cf06354bfc92847b138a3e0bac0be45cce2a Mon Sep 17 00:00:00 2001 From: Joe Date: Tue, 9 Dec 2025 23:46:59 -0300 Subject: [PATCH 1/3] fix: Feito ajuste na classe ReminderControllerTest para garantir total cobertura dos testes. --- .../controller/ReminderControllerTest.java | 33 ++++++++++++++++--- 1 file changed, 28 insertions(+), 5 deletions(-) diff --git a/src/test/java/br/com/springnoobs/reminderapi/reminder/controller/ReminderControllerTest.java b/src/test/java/br/com/springnoobs/reminderapi/reminder/controller/ReminderControllerTest.java index 06bf7f0..a1182ac 100644 --- a/src/test/java/br/com/springnoobs/reminderapi/reminder/controller/ReminderControllerTest.java +++ b/src/test/java/br/com/springnoobs/reminderapi/reminder/controller/ReminderControllerTest.java @@ -1,6 +1,7 @@ package br.com.springnoobs.reminderapi.reminder.controller; import static org.mockito.ArgumentMatchers.any; +import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.when; import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; @@ -13,6 +14,7 @@ import br.com.springnoobs.reminderapi.reminder.dto.response.ReminderResponseDTO; import br.com.springnoobs.reminderapi.reminder.exception.NotFoundException; import br.com.springnoobs.reminderapi.reminder.exception.PastDueDateException; +import br.com.springnoobs.reminderapi.reminder.exception.ReminderSchedulerException; import br.com.springnoobs.reminderapi.reminder.service.ReminderService; import br.com.springnoobs.reminderapi.user.dto.request.ContactRequestDTO; import br.com.springnoobs.reminderapi.user.dto.request.CreateUserRequestDTO; @@ -101,7 +103,7 @@ void shouldCreateReminderWhenRequestIsValid() throws Exception { var request = new CreateReminderRequestDTO("New Reminder", Instant.now().plusSeconds(60), createUserRequestDTO); var response = new ReminderResponseDTO(request.title(), request.dueDate()); - when(service.create(request)).thenReturn(response); + when(service.create(any(CreateReminderRequestDTO.class))).thenReturn(response); mockMvc.perform(post("/reminders") .contentType(MediaType.APPLICATION_JSON) @@ -119,7 +121,8 @@ void shouldReturnBadRequestWhenCreatingReminderWithPastDate() throws Exception { var request = new CreateReminderRequestDTO("Past Reminder", Instant.now().minusSeconds(60), createUserRequestDTO); - when(service.create(request)).thenThrow(new PastDueDateException("Remind at date must be in the future")); + when(service.create(any(CreateReminderRequestDTO.class))) + .thenThrow(new PastDueDateException("Remind at date must be in the future")); mockMvc.perform(post("/reminders") .contentType(MediaType.APPLICATION_JSON) @@ -128,6 +131,25 @@ void shouldReturnBadRequestWhenCreatingReminderWithPastDate() throws Exception { .andDo(document("create-reminder-past-date")); } + @Test + void shouldReturnInternalServerErrorWhenSchedulerFailsOnCreate() throws Exception { + CreateUserRequestDTO createUserRequestDTO = new CreateUserRequestDTO( + "First Name", "Last Name", new ContactRequestDTO("email@test.com", "123456789")); + + var request = new CreateReminderRequestDTO("New Reminder", Instant.now().plusSeconds(60), createUserRequestDTO); + + when(service.create(any(CreateReminderRequestDTO.class))) + .thenThrow(new ReminderSchedulerException("Scheduler error on create")); + + mockMvc.perform(post("/reminders") + .contentType(MediaType.APPLICATION_JSON) + .content(objectMapper.writeValueAsString(request))) + .andExpect(status().isInternalServerError()) + .andExpect(jsonPath("$.title").value("Internal Server Error")) + .andExpect(jsonPath("$.detail").value("Scheduler error on create")) + .andDo(document("create-reminder-scheduler-error")); + } + @Test void shouldUpdateReminderWhenRequestIsValid() throws Exception { long reminderId = 1L; @@ -135,7 +157,7 @@ void shouldUpdateReminderWhenRequestIsValid() throws Exception { new UpdateReminderRequestDTO("Updated Reminder", Instant.now().plusSeconds(60)); var response = new ReminderResponseDTO(request.title(), request.dueDate()); - when(service.update(reminderId, request)).thenReturn(response); + when(service.update(anyLong(), any(UpdateReminderRequestDTO.class))).thenReturn(response); mockMvc.perform(put("/reminders/{id}", reminderId) .contentType(MediaType.APPLICATION_JSON) @@ -151,7 +173,7 @@ void shouldReturnBadRequestWhenUpdatingReminderWithPastDate() throws Exception { var request = new UpdateReminderRequestDTO("Past Reminder", Instant.now().minusSeconds(60)); - when(service.update(reminderId, request)) + when(service.update(anyLong(), any(UpdateReminderRequestDTO.class))) .thenThrow(new PastDueDateException("Remind at date must be in the future")); mockMvc.perform(put("/reminders/{id}", reminderId) @@ -167,7 +189,8 @@ void shouldReturnNotFoundWhenUpdatingNonExistentReminder() throws Exception { var request = new UpdateReminderRequestDTO("Updated Reminder", Instant.now().plusSeconds(60)); - when(service.update(reminderId, request)).thenThrow(new NotFoundException("Reminder not found")); + when(service.update(anyLong(), any(UpdateReminderRequestDTO.class))) + .thenThrow(new NotFoundException("Reminder not found")); mockMvc.perform(put("/reminders/{id}", reminderId) .contentType(MediaType.APPLICATION_JSON) From ebbfe62924b67802124f4b79f7ed647b6075f9d5 Mon Sep 17 00:00:00 2001 From: Joe Date: Tue, 9 Dec 2025 23:59:17 -0300 Subject: [PATCH 2/3] fix: Feito ajuste na classe ReminderControllerTest. --- .../controller/ReminderControllerTest.java | 16 ++++++---------- 1 file changed, 6 insertions(+), 10 deletions(-) diff --git a/src/test/java/br/com/springnoobs/reminderapi/reminder/controller/ReminderControllerTest.java b/src/test/java/br/com/springnoobs/reminderapi/reminder/controller/ReminderControllerTest.java index a1182ac..9fd921e 100644 --- a/src/test/java/br/com/springnoobs/reminderapi/reminder/controller/ReminderControllerTest.java +++ b/src/test/java/br/com/springnoobs/reminderapi/reminder/controller/ReminderControllerTest.java @@ -1,7 +1,6 @@ package br.com.springnoobs.reminderapi.reminder.controller; import static org.mockito.ArgumentMatchers.any; -import static org.mockito.ArgumentMatchers.anyLong; import static org.mockito.Mockito.doThrow; import static org.mockito.Mockito.when; import static org.springframework.restdocs.mockmvc.MockMvcRestDocumentation.document; @@ -103,7 +102,7 @@ void shouldCreateReminderWhenRequestIsValid() throws Exception { var request = new CreateReminderRequestDTO("New Reminder", Instant.now().plusSeconds(60), createUserRequestDTO); var response = new ReminderResponseDTO(request.title(), request.dueDate()); - when(service.create(any(CreateReminderRequestDTO.class))).thenReturn(response); + when(service.create(request)).thenReturn(response); mockMvc.perform(post("/reminders") .contentType(MediaType.APPLICATION_JSON) @@ -121,8 +120,7 @@ void shouldReturnBadRequestWhenCreatingReminderWithPastDate() throws Exception { var request = new CreateReminderRequestDTO("Past Reminder", Instant.now().minusSeconds(60), createUserRequestDTO); - when(service.create(any(CreateReminderRequestDTO.class))) - .thenThrow(new PastDueDateException("Remind at date must be in the future")); + when(service.create(request)).thenThrow(new PastDueDateException("Remind at date must be in the future")); mockMvc.perform(post("/reminders") .contentType(MediaType.APPLICATION_JSON) @@ -138,8 +136,7 @@ void shouldReturnInternalServerErrorWhenSchedulerFailsOnCreate() throws Exceptio var request = new CreateReminderRequestDTO("New Reminder", Instant.now().plusSeconds(60), createUserRequestDTO); - when(service.create(any(CreateReminderRequestDTO.class))) - .thenThrow(new ReminderSchedulerException("Scheduler error on create")); + when(service.create(request)).thenThrow(new ReminderSchedulerException("Scheduler error on create")); mockMvc.perform(post("/reminders") .contentType(MediaType.APPLICATION_JSON) @@ -157,7 +154,7 @@ void shouldUpdateReminderWhenRequestIsValid() throws Exception { new UpdateReminderRequestDTO("Updated Reminder", Instant.now().plusSeconds(60)); var response = new ReminderResponseDTO(request.title(), request.dueDate()); - when(service.update(anyLong(), any(UpdateReminderRequestDTO.class))).thenReturn(response); + when(service.update(reminderId, request)).thenReturn(response); mockMvc.perform(put("/reminders/{id}", reminderId) .contentType(MediaType.APPLICATION_JSON) @@ -173,7 +170,7 @@ void shouldReturnBadRequestWhenUpdatingReminderWithPastDate() throws Exception { var request = new UpdateReminderRequestDTO("Past Reminder", Instant.now().minusSeconds(60)); - when(service.update(anyLong(), any(UpdateReminderRequestDTO.class))) + when(service.update(reminderId, request)) .thenThrow(new PastDueDateException("Remind at date must be in the future")); mockMvc.perform(put("/reminders/{id}", reminderId) @@ -189,8 +186,7 @@ void shouldReturnNotFoundWhenUpdatingNonExistentReminder() throws Exception { var request = new UpdateReminderRequestDTO("Updated Reminder", Instant.now().plusSeconds(60)); - when(service.update(anyLong(), any(UpdateReminderRequestDTO.class))) - .thenThrow(new NotFoundException("Reminder not found")); + when(service.update(reminderId, request)).thenThrow(new NotFoundException("Reminder not found")); mockMvc.perform(put("/reminders/{id}", reminderId) .contentType(MediaType.APPLICATION_JSON) From d004e0c760e917602b9f1f49e108d1ccea3d84b5 Mon Sep 17 00:00:00 2001 From: Joe Date: Thu, 11 Dec 2025 15:49:52 -0300 Subject: [PATCH 3/3] =?UTF-8?q?fix:=20Feito=20a=20documenta=C3=A7=C3=A3o?= =?UTF-8?q?=20dos=20testes=20da=20classe=20ReminderControllerTest.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/docs/asciidoc/index-en-US.adoc | 6 ++++++ src/docs/asciidoc/index-pt-BR.adoc | 6 ++++++ 2 files changed, 12 insertions(+) diff --git a/src/docs/asciidoc/index-en-US.adoc b/src/docs/asciidoc/index-en-US.adoc index 883daf8..f8c8549 100644 --- a/src/docs/asciidoc/index-en-US.adoc +++ b/src/docs/asciidoc/index-en-US.adoc @@ -82,6 +82,12 @@ A `POST` request will create a new reminder. operation::create-reminder[snippets='http-request,curl-request,http-response,request-body'] +==== Create a reminder (Internal failure) + +A `POST` request to create a reminder may result in an internal failure. + +operation::create-reminder-scheduler-error[snippets='http-response'] + ==== Create a reminder (past date) operation::create-reminder-past-date[snippets='http-response'] diff --git a/src/docs/asciidoc/index-pt-BR.adoc b/src/docs/asciidoc/index-pt-BR.adoc index c1e4c13..f4ffc43 100644 --- a/src/docs/asciidoc/index-pt-BR.adoc +++ b/src/docs/asciidoc/index-pt-BR.adoc @@ -82,6 +82,12 @@ Uma requisição `POST` criará um novo lembrete. operation::create-reminder[snippets='http-request,curl-request,http-response,request-body'] +==== Criar um lembrete (Falha interna) + +Uma requisição `POST` para criar um lembrete pode resultar em falha interna. + +operation::create-reminder-scheduler-error[snippets='http-response'] + ==== Criar um lembrete (data no passado) operation::create-reminder-past-date[snippets='http-response']