|
| 1 | +package de.holarse.web.api; |
| 2 | + |
| 3 | +import de.holarse.backend.db.ApiUser; |
| 4 | +import de.holarse.backend.db.repositories.SearchRepository; |
| 5 | +import de.holarse.backend.view.TagRecommendation; |
| 6 | +import static org.junit.jupiter.api.Assertions.*; |
| 7 | + |
| 8 | +import de.holarse.test.TestHelper; |
| 9 | +import java.util.ArrayList; |
| 10 | +import java.util.List; |
| 11 | +import org.junit.jupiter.api.BeforeEach; |
| 12 | +import org.junit.jupiter.api.Test; |
| 13 | +import org.mockito.MockitoAnnotations; |
| 14 | +import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.csrf; |
| 15 | + |
| 16 | +import org.slf4j.Logger; |
| 17 | +import org.slf4j.LoggerFactory; |
| 18 | +import org.springframework.http.MediaType; |
| 19 | +import org.springframework.security.test.context.support.WithMockUser; |
| 20 | +import org.springframework.test.web.servlet.MockMvc; |
| 21 | +import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.*; |
| 22 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.jsonPath; |
| 23 | +import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; |
| 24 | + |
| 25 | +import org.springframework.test.web.servlet.ResultActions; |
| 26 | +import org.springframework.test.web.servlet.setup.MockMvcBuilders; |
| 27 | +import static org.hamcrest.MatcherAssert.*; |
| 28 | +import static org.hamcrest.Matchers.*; |
| 29 | +import org.mockito.Mock; |
| 30 | +import static org.mockito.ArgumentMatchers.anyString; |
| 31 | +import static org.mockito.Mockito.when; |
| 32 | + |
| 33 | +public class TagApiControllerTest { |
| 34 | + |
| 35 | + private final static transient Logger log = LoggerFactory.getLogger(TagApiControllerTest.class); |
| 36 | + |
| 37 | + @Mock |
| 38 | + SearchRepository searchRepositoryMock; |
| 39 | + |
| 40 | + TagApiController controller; |
| 41 | + |
| 42 | + @BeforeEach |
| 43 | + public void setup() throws Exception { |
| 44 | + MockitoAnnotations.openMocks(this); |
| 45 | + controller = new TagApiController(); |
| 46 | + } |
| 47 | + @Test |
| 48 | + public void testRequestWithoutLogin() throws Exception { |
| 49 | + final String searchTerm = "döner"; |
| 50 | + MockMvc mockMvc = MockMvcBuilders.standaloneSetup(controller).build(); |
| 51 | + mockMvc.perform(get("/webapi/tags/autocomplete").param("query", searchTerm).with(csrf())).andExpect(status().is(200)); |
| 52 | + // TODO: Should return redirect to login |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + @WithMockUser("admin") |
| 57 | + public void testRequestWithLogin() throws Exception { |
| 58 | + final String searchTerm = "döner"; |
| 59 | + |
| 60 | + controller.searchRepository = searchRepositoryMock; |
| 61 | + |
| 62 | + when(searchRepositoryMock.autocompleteTags(anyString())).thenReturn(new ArrayList<>()); |
| 63 | + |
| 64 | + MockMvc mockMvc = MockMvcBuilders.standaloneSetup(controller).build(); |
| 65 | + ResultActions result = mockMvc.perform(get("/webapi/tags/autocomplete").param("query", searchTerm).with(csrf())) |
| 66 | + .andExpect(status().isOk()) |
| 67 | + .andExpect(resp -> assertThat("should be json", resp.getResponse().getContentType().equals(MediaType.APPLICATION_JSON_VALUE))); |
| 68 | + |
| 69 | + result.andExpect(jsonPath("$", hasSize(0))); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + @WithMockUser("admin") |
| 74 | + public void testSingleResult() throws Exception { |
| 75 | + controller.searchRepository = searchRepositoryMock; |
| 76 | + final String searchTerm = "döner"; |
| 77 | + |
| 78 | + final List<TagRecommendation> mockResult = List.of(new TagRecommendation("döner", 1)); |
| 79 | + |
| 80 | + when(searchRepositoryMock.autocompleteTags(searchTerm)).thenReturn(mockResult); |
| 81 | + |
| 82 | + MockMvc mockMvc = MockMvcBuilders.standaloneSetup(controller).build(); |
| 83 | + ResultActions result = mockMvc.perform(get("/webapi/tags/autocomplete").param("query", searchTerm).with(csrf())) |
| 84 | + .andExpect(status().isOk()) |
| 85 | + .andExpect(resp -> assertThat("should be json", resp.getResponse().getContentType().equals(MediaType.APPLICATION_JSON_VALUE))); |
| 86 | + |
| 87 | + result.andExpect(jsonPath("$", hasSize(1))); |
| 88 | + } |
| 89 | + |
| 90 | +} |
0 commit comments