TodoController { @RequestMapping(value = "/search", method = RequestMethod.GET) public Page<TodoDTO> findBySearchTerm(@RequestParam("searchTerm") String searchTerm, Pageable pageable) { LOGGER.info("Finding {} todo entries for page {} by using search term: {}", pageable.getPageSize(), pageable.getPageNumber(), searchTerm ); Page<TodoDTO> todoEntries = searchService.findBySearchTerm(searchTerm, pageable); LOGGER.info("Found {} todo entries for page: {}", todoEntries.getNumberOfElements(), todoEntries.getNumber() ); return todoEntries; } @Autowired  TodoController(TodoCrudService crudService, TodoSearchService searchService); @RequestMapping(method = RequestMethod.POST) @ResponseStatus(HttpStatus.CREATED) TodoDTO add(@RequestBody @Valid TodoDTO dto); @RequestMapping(value = "/{id}", method = RequestMethod.DELETE) TodoDTO delete(@PathVariable("id") Long id); @RequestMapping(method = RequestMethod.GET) List<TodoDTO> findAll(); @RequestMapping(value = "/{id}", method = RequestMethod.GET) TodoDTO findById(@PathVariable("id") Long id); @RequestMapping(value = "/search", method = RequestMethod.GET) Page<TodoDTO> findBySearchTerm(@RequestParam("searchTerm") String searchTerm, Pageable pageable); @RequestMapping(value = "/{id}", method = RequestMethod.PUT) TodoDTO update(@PathVariable("id") Long id, @RequestBody @Valid TodoDTO dto);  }
@Test public void findBySearchTerm_NoTodoEntriesFound_ShouldReturnAnEmptyPageAsJsonDocument() throws Exception { Page<TodoDTO> searchResults = new PageBuilder<TodoDTO>() .pageNumber(PAGE_NUMBER) .pageSize(PAGE_SIZE) .totalNumberOfItems(ZERO_ITEMS_FOUND) .build(); when(todoSearchServiceMock.findBySearchTerm(eq(SEARCH_TERM), isA(Pageable.class))).thenReturn(searchResults); mockMvc.perform(get("/api/todo/search") .param(WebTestConstants.REQUEST_PARAM_SEARCH_TERM, SEARCH_TERM) .param(WebTestConstants.REQUEST_PARAM_PAGE_NUMBER, PAGE_NUMBER_STRING) .param(WebTestConstants.REQUEST_PARAM_PAGE_SIZE, PAGE_SIZE_STRING) .param(WebTestConstants.REQUEST_PARAM_SORT, WebTestUtil.createSortParameterValue(SORT_FIELD, SORT_ORDER)) ) .andExpect(status().isOk()) .andExpect(content().contentType(WebTestConstants.APPLICATION_JSON_UTF8)) .andExpect(jsonPath("$.number", is(PAGE_NUMBER))) .andExpect(jsonPath("$.size", is(PAGE_SIZE))) .andExpect(jsonPath("$.numberOfElements", is(ZERO_ITEMS_FOUND))) .andExpect(jsonPath("$.firstPage", is(true))) .andExpect(jsonPath("$.lastPage", is(true))) .andExpect(jsonPath("$.totalPages", is(ZERO_PAGES_FOUND))) .andExpect(jsonPath("$.totalElements", is(ZERO_ITEMS_FOUND))) .andExpect(jsonPath("$.content", hasSize(0))) .andDo(print()); ArgumentCaptor<Pageable> pageableArgument = ArgumentCaptor.forClass(Pageable.class); verify(todoSearchServiceMock, times(1)).findBySearchTerm(eq(SEARCH_TERM), pageableArgument.capture()); verifyNoMoreInteractions(todoSearchServiceMock); verifyZeroInteractions(todoCrudServiceMock); Pageable pageSpecification = pageableArgument.getValue(); assertThat(pageSpecification.getPageNumber()).isEqualTo(PAGE_NUMBER); assertThat(pageSpecification.getPageSize()).isEqualTo(PAGE_SIZE); Sort sortSpecification = pageSpecification.getSort(); assertThat(sortSpecification.getOrderFor(SORT_FIELD).getDirection()).isEqualTo(Sort.Direction.fromString(SORT_ORDER)); }
@Test public void findBySearchTerm_OneTodoEntryFound_ShouldReturnTodoEntriesAsJsonDocument() throws Exception { TodoDTO foundTodoEntry = new TodoDTOBuilder() .creationTime(CREATION_TIME) .description(DESCRIPTION) .id(ID) .modificationTime(MODIFICATION_TIME) .title(TITLE) .build(); Page<TodoDTO> searchResults = new PageBuilder<TodoDTO>() .itemsOnPage(foundTodoEntry) .pageNumber(PAGE_NUMBER) .pageSize(PAGE_SIZE) .totalNumberOfItems(TOTAL_NUMBER_OF_ITEMS) .build(); when(todoSearchServiceMock.findBySearchTerm(eq(SEARCH_TERM), isA(Pageable.class))).thenReturn(searchResults); mockMvc.perform(get("/api/todo/search") .param(WebTestConstants.REQUEST_PARAM_SEARCH_TERM, SEARCH_TERM) .param(WebTestConstants.REQUEST_PARAM_PAGE_NUMBER, PAGE_NUMBER_STRING) .param(WebTestConstants.REQUEST_PARAM_PAGE_SIZE, PAGE_SIZE_STRING) .param(WebTestConstants.REQUEST_PARAM_SORT, WebTestUtil.createSortParameterValue(SORT_FIELD, SORT_ORDER)) ) .andExpect(status().isOk()) .andExpect(content().contentType(WebTestConstants.APPLICATION_JSON_UTF8)) .andExpect(jsonPath("$.number", is(PAGE_NUMBER))) .andExpect(jsonPath("$.size", is(PAGE_SIZE))) .andExpect(jsonPath("$.numberOfElements", is(ONE_ITEM_FOUND))) .andExpect(jsonPath("$.firstPage", is(true))) .andExpect(jsonPath("$.lastPage", is(true))) .andExpect(jsonPath("$.totalPages", is(ONE_PAGE_FOUND))) .andExpect(jsonPath("$.totalElements", is(ONE_ITEM_FOUND))) .andExpect(jsonPath("$.content", hasSize(1))) .andExpect(jsonPath("$.content[0].id", is(ID.intValue()))) .andExpect(jsonPath("$.content[0].creationTime", is(CREATION_TIME))) .andExpect(jsonPath("$.content[0].description", is(DESCRIPTION))) .andExpect(jsonPath("$.content[0].modificationTime", is(MODIFICATION_TIME))) .andExpect(jsonPath("$.content[0].title", is(TITLE))); ArgumentCaptor<Pageable> pageableArgument = ArgumentCaptor.forClass(Pageable.class); verify(todoSearchServiceMock, times(1)).findBySearchTerm(eq(SEARCH_TERM), pageableArgument.capture()); verifyNoMoreInteractions(todoSearchServiceMock); verifyZeroInteractions(todoCrudServiceMock); Pageable pageSpecification = pageableArgument.getValue(); assertThat(pageSpecification.getPageNumber()).isEqualTo(PAGE_NUMBER); assertThat(pageSpecification.getPageSize()).isEqualTo(PAGE_SIZE); Sort sortSpecification = pageSpecification.getSort(); assertThat(sortSpecification.getOrderFor(SORT_FIELD).getDirection()).isEqualTo(Sort.Direction.fromString(SORT_ORDER)); }