SearchController { @ResponseBody @RequestMapping(value = "/search", method = RequestMethod.POST) public ResponseEntity<String> searchForFiles(@RequestParam("searchedText") final String searchedText, final HttpSession session) { List<List<String>> searchResult = new ArrayList<List<String>>(); ResponseEntity<String> result; HttpHeaders responseHeaders = setHeadersForJSON(); try { searchResult = searchForText(searchedText); addResultToSession(searchResult, session); if (!indexStatus.isReady()) { result = new ResponseEntity<String>(getJson(searchResult), responseHeaders, HttpStatus.PARTIAL_CONTENT); } else { result = new ResponseEntity<String>(getJson(searchResult), responseHeaders, HttpStatus.CREATED); } } catch (QueryCannotBeParsedException e) { logger.warn("Invalid query"); result = new ResponseEntity<String>(QUERY_ERROR_MESSAGE, HttpStatus.BAD_REQUEST); } return result; }  @RequestMapping(value = "/search", method = RequestMethod.GET) void searchAndZip(@RequestParam("text") final String searchedText, final HttpServletResponse resp); @ResponseBody @RequestMapping(value = "/search", method = RequestMethod.POST) ResponseEntity<String> searchForFiles(@RequestParam("searchedText") final String searchedText, final HttpSession session);  }
@Test public void testSearchForFilesWhenSearchedTextIsNotEmpty() throws IOException { underTest.searchForFiles("test", session); verify(luceneEngine).search("test"); verify(fileChecker).checkFilesExistsWithPairs(lucineResult); verify(session).setAttribute("searchResult", listOfLists); }
@Test public void testSearchForFilesWhenSearchedTextIsEmptyString() throws IOException { underTest.searchForFiles("", session); verify(luceneEngine).search(""); verify(fileChecker).checkFilesExistsWithPairs(lucineResult); verify(session).setAttribute("searchResult", listOfLists); }
@Test public void testSearchForFilesWhenSearchedTextIsNull() throws IOException { underTest.searchForFiles(null, session); verify(luceneEngine).search(null); verify(fileChecker).checkFilesExistsWithPairs(lucineResult); verify(session).setAttribute("searchResult", listOfLists); }
@Test public void testSearchForFilesWhenIndexIsNotReady() throws IOException { given(indexStatus.isReady()).willReturn(false); ResponseEntity<String> result = underTest.searchForFiles("test", session); Assert.assertEquals(result.getStatusCode(), HttpStatus.PARTIAL_CONTENT); verify(luceneEngine).search("test"); verify(fileChecker).checkFilesExistsWithPairs(lucineResult); verify(session).setAttribute("searchResult", listOfLists); }
@Test public void testSearchForFilesWhenIndexIsReady() throws IOException { given(indexStatus.isReady()).willReturn(true); ResponseEntity<String> result = underTest.searchForFiles("test", session); Assert.assertEquals(result.getStatusCode(), HttpStatus.CREATED); verify(luceneEngine).search("test"); verify(fileChecker).checkFilesExistsWithPairs(lucineResult); verify(session).setAttribute("searchResult", listOfLists); }
@Test public void testSearchForFilesWhenQueryIsNotValid() throws IOException { given(luceneEngine.search("test")).willThrow(new QueryCannotBeParsedException("Parse Exception")); ResponseEntity<String> result = underTest.searchForFiles("test", session); Assert.assertEquals(result.getStatusCode(), HttpStatus.BAD_REQUEST); }