From ff05b152b73c761a0716f06fe5d07c741db3813e Mon Sep 17 00:00:00 2001 From: Ricardo Campos Date: Wed, 25 Sep 2024 11:05:34 -0300 Subject: [PATCH] test: add home controller test case issue #46 --- .../controller/HomeControllerTest.java | 100 ++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 java-api/src/test/java/br/com/tasknoteapp/java_api/controller/HomeControllerTest.java diff --git a/java-api/src/test/java/br/com/tasknoteapp/java_api/controller/HomeControllerTest.java b/java-api/src/test/java/br/com/tasknoteapp/java_api/controller/HomeControllerTest.java new file mode 100644 index 0000000..93c4181 --- /dev/null +++ b/java-api/src/test/java/br/com/tasknoteapp/java_api/controller/HomeControllerTest.java @@ -0,0 +1,100 @@ +package br.com.tasknoteapp.java_api.controller; + +import static org.mockito.Mockito.when; +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; + +import br.com.tasknoteapp.java_api.response.NoteResponse; +import br.com.tasknoteapp.java_api.response.SearchResponse; +import br.com.tasknoteapp.java_api.response.SummaryResponse; +import br.com.tasknoteapp.java_api.response.TaskResponse; +import br.com.tasknoteapp.java_api.service.HomeService; +import java.util.List; +import org.hamcrest.Matchers; +import org.junit.jupiter.api.DisplayName; +import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; +import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.boot.test.mock.mockito.MockBean; +import org.springframework.http.MediaType; +import org.springframework.security.test.context.support.WithMockUser; +import org.springframework.test.web.servlet.MockMvc; + +@SpringBootTest +@AutoConfigureMockMvc +class HomeControllerTest { + + @Autowired private MockMvc mockMvc; + + @MockBean private HomeService homeService; + + @Test + @DisplayName("Get summary happy path should succeed") + @WithMockUser(username = "user@domain.com", password = "abcde123456A@") + void getSummary_happyPath_shouldSucceed() throws Exception { + SummaryResponse response = new SummaryResponse(0, 6); + + when(homeService.getSummary()).thenReturn(response); + + mockMvc + .perform( + get("/rest/home/summary") + .with(csrf().asHeader()) + .header("Content-Type", MediaType.APPLICATION_JSON_VALUE) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.pendingTaskCount").value(response.pendingTaskCount())) + .andExpect(jsonPath("$.doneTaskCount").value(response.doneTaskCount())) + .andReturn(); + } + + @Test + @DisplayName("Get summary user not authorized should fail") + void getSummary_userNotAuthorized_shouldFail() throws Exception { + SummaryResponse response = new SummaryResponse(0, 6); + + when(homeService.getSummary()).thenReturn(response); + + mockMvc + .perform( + get("/rest/home/summary") + .with(csrf().asHeader()) + .header("Content-Type", MediaType.APPLICATION_JSON_VALUE) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isForbidden()) + .andReturn(); + } + + @Test + @DisplayName("Search happy path should succeed") + @WithMockUser(username = "user@domain.com", password = "abcde123456A@") + void search_happyPath_shouldSucceed() throws Exception { + TaskResponse task = new TaskResponse(1L, "Task 1", false, List.of()); + NoteResponse note = new NoteResponse(1L, "Note 1", "Note desc", List.of()); + SearchResponse response = new SearchResponse(List.of(task), List.of(note)); + + when(homeService.search("term")).thenReturn(response); + + mockMvc + .perform( + get("/rest/home/search?term=term") + .with(csrf().asHeader()) + .header("Content-Type", MediaType.APPLICATION_JSON_VALUE) + .accept(MediaType.APPLICATION_JSON)) + .andExpect(status().isOk()) + .andExpect(jsonPath("$.tasks", Matchers.isA(List.class))) + .andExpect(jsonPath("$.notes", Matchers.isA(List.class))) + .andExpect(jsonPath("$.tasks[0].id").value(task.id())) + .andExpect(jsonPath("$.tasks[0].description").value(task.description())) + .andExpect(jsonPath("$.tasks[0].done").value(task.done())) + .andExpect(jsonPath("$.tasks[0].urls", Matchers.empty())) + .andExpect(jsonPath("$.notes[0].id").value(note.id())) + .andExpect(jsonPath("$.notes[0].title").value(note.title())) + .andExpect(jsonPath("$.notes[0].description").value(note.description())) + .andExpect(jsonPath("$.notes[0].urls", Matchers.empty())) + .andReturn(); + } +}