@Test public void testSearch() throws Exception { SearchCriteria criteria = new SearchCriteria(); criteria.setAg("ag"); criteria.setSize(1); criteria.setFrom(0); criteria.setSearchText("pacman"); SearchResult sr = new SearchResult(); when(service.search(any(SearchCriteria.class))).thenReturn(sr); ReflectionTestUtils.setField(controller, "searchService", service); ResponseEntity<Object> responseObj = controller.search(criteria); assertTrue(responseObj.getStatusCode() == HttpStatus.OK); criteria.setSize(-1); responseObj = controller.search(criteria); assertTrue(responseObj.getStatusCode() == HttpStatus.EXPECTATION_FAILED); criteria.setSize(1); criteria.setFrom(-1); responseObj = controller.search(criteria); assertTrue(responseObj.getStatusCode() == HttpStatus.EXPECTATION_FAILED); criteria.setFrom(1); criteria.setSearchText(null); responseObj = controller.search(criteria); assertTrue(responseObj.getStatusCode() == HttpStatus.EXPECTATION_FAILED); criteria.setSearchText("pacman"); criteria.setAg(null); responseObj = controller.search(criteria); assertTrue(responseObj.getStatusCode() == HttpStatus.EXPECTATION_FAILED); criteria.setAg("aws-all"); doThrow(new NullPointerException()).when(service).search(any(SearchCriteria.class)); ReflectionTestUtils.setField(controller, "searchService", service); responseObj = controller.search(criteria); assertTrue(responseObj.getStatusCode() == HttpStatus.OK); }
@PostMapping(value = "/v1/search") public ResponseEntity<Object> search(@RequestBody SearchCriteria criteria) { long start = System.currentTimeMillis(); LOGGER.info("Start Search API............."); SearchResult searchResult = new SearchResult(); if (criteria.getSize() <= 0) { return ResponseUtils.buildFailureResponse(new Exception("Size should be greater than zero.")); } if (criteria.getFrom() < 0) { return ResponseUtils.buildFailureResponse(new Exception("From should be >= zero.")); } if (null == criteria.getSearchText() || StringUtils.isEmpty(criteria.getSearchText())) { return ResponseUtils.buildFailureResponse(new Exception("Search Text cannot be blank.")); } if (null == criteria.getAg() || StringUtils.isEmpty(criteria.getAg())) { return ResponseUtils.buildFailureResponse(new Exception("Asset Group cannot be blank.")); } try { searchResult = searchService.search(criteria); } catch (SearchException e) { LOGGER.error("Error in search",e); return ResponseUtils.buildFailureResponse(new Exception(e.getMessage())); } catch (Exception e) { LOGGER.error("Error in search",e); return ResponseUtils.buildSucessResponse(searchResult); } long end = System.currentTimeMillis(); LOGGER.info("..........End Search API. Took(in ms): {}" , (end - start)); return ResponseUtils.buildSucessResponse(searchResult); }
SearchController { @PostMapping(value = "/v1/search") public ResponseEntity<Object> search(@RequestBody SearchCriteria criteria) { long start = System.currentTimeMillis(); LOGGER.info("Start Search API............."); SearchResult searchResult = new SearchResult(); if (criteria.getSize() <= 0) { return ResponseUtils.buildFailureResponse(new Exception("Size should be greater than zero.")); } if (criteria.getFrom() < 0) { return ResponseUtils.buildFailureResponse(new Exception("From should be >= zero.")); } if (null == criteria.getSearchText() || StringUtils.isEmpty(criteria.getSearchText())) { return ResponseUtils.buildFailureResponse(new Exception("Search Text cannot be blank.")); } if (null == criteria.getAg() || StringUtils.isEmpty(criteria.getAg())) { return ResponseUtils.buildFailureResponse(new Exception("Asset Group cannot be blank.")); } try { searchResult = searchService.search(criteria); } catch (SearchException e) { LOGGER.error("Error in search",e); return ResponseUtils.buildFailureResponse(new Exception(e.getMessage())); } catch (Exception e) { LOGGER.error("Error in search",e); return ResponseUtils.buildSucessResponse(searchResult); } long end = System.currentTimeMillis(); LOGGER.info("..........End Search API. Took(in ms): {}" , (end - start)); return ResponseUtils.buildSucessResponse(searchResult); } }
SearchController { @PostMapping(value = "/v1/search") public ResponseEntity<Object> search(@RequestBody SearchCriteria criteria) { long start = System.currentTimeMillis(); LOGGER.info("Start Search API............."); SearchResult searchResult = new SearchResult(); if (criteria.getSize() <= 0) { return ResponseUtils.buildFailureResponse(new Exception("Size should be greater than zero.")); } if (criteria.getFrom() < 0) { return ResponseUtils.buildFailureResponse(new Exception("From should be >= zero.")); } if (null == criteria.getSearchText() || StringUtils.isEmpty(criteria.getSearchText())) { return ResponseUtils.buildFailureResponse(new Exception("Search Text cannot be blank.")); } if (null == criteria.getAg() || StringUtils.isEmpty(criteria.getAg())) { return ResponseUtils.buildFailureResponse(new Exception("Asset Group cannot be blank.")); } try { searchResult = searchService.search(criteria); } catch (SearchException e) { LOGGER.error("Error in search",e); return ResponseUtils.buildFailureResponse(new Exception(e.getMessage())); } catch (Exception e) { LOGGER.error("Error in search",e); return ResponseUtils.buildSucessResponse(searchResult); } long end = System.currentTimeMillis(); LOGGER.info("..........End Search API. Took(in ms): {}" , (end - start)); return ResponseUtils.buildSucessResponse(searchResult); }  }
SearchController { @PostMapping(value = "/v1/search") public ResponseEntity<Object> search(@RequestBody SearchCriteria criteria) { long start = System.currentTimeMillis(); LOGGER.info("Start Search API............."); SearchResult searchResult = new SearchResult(); if (criteria.getSize() <= 0) { return ResponseUtils.buildFailureResponse(new Exception("Size should be greater than zero.")); } if (criteria.getFrom() < 0) { return ResponseUtils.buildFailureResponse(new Exception("From should be >= zero.")); } if (null == criteria.getSearchText() || StringUtils.isEmpty(criteria.getSearchText())) { return ResponseUtils.buildFailureResponse(new Exception("Search Text cannot be blank.")); } if (null == criteria.getAg() || StringUtils.isEmpty(criteria.getAg())) { return ResponseUtils.buildFailureResponse(new Exception("Asset Group cannot be blank.")); } try { searchResult = searchService.search(criteria); } catch (SearchException e) { LOGGER.error("Error in search",e); return ResponseUtils.buildFailureResponse(new Exception(e.getMessage())); } catch (Exception e) { LOGGER.error("Error in search",e); return ResponseUtils.buildSucessResponse(searchResult); } long end = System.currentTimeMillis(); LOGGER.info("..........End Search API. Took(in ms): {}" , (end - start)); return ResponseUtils.buildSucessResponse(searchResult); }  @PostMapping(value = "/v1/search") ResponseEntity<Object> search(@RequestBody SearchCriteria criteria); @GetMapping(value = "/v1/search/categories") ResponseEntity<Object> getSearchCategories(@RequestParam(name = "domain", required = false) String domain); }
SearchController { @PostMapping(value = "/v1/search") public ResponseEntity<Object> search(@RequestBody SearchCriteria criteria) { long start = System.currentTimeMillis(); LOGGER.info("Start Search API............."); SearchResult searchResult = new SearchResult(); if (criteria.getSize() <= 0) { return ResponseUtils.buildFailureResponse(new Exception("Size should be greater than zero.")); } if (criteria.getFrom() < 0) { return ResponseUtils.buildFailureResponse(new Exception("From should be >= zero.")); } if (null == criteria.getSearchText() || StringUtils.isEmpty(criteria.getSearchText())) { return ResponseUtils.buildFailureResponse(new Exception("Search Text cannot be blank.")); } if (null == criteria.getAg() || StringUtils.isEmpty(criteria.getAg())) { return ResponseUtils.buildFailureResponse(new Exception("Asset Group cannot be blank.")); } try { searchResult = searchService.search(criteria); } catch (SearchException e) { LOGGER.error("Error in search",e); return ResponseUtils.buildFailureResponse(new Exception(e.getMessage())); } catch (Exception e) { LOGGER.error("Error in search",e); return ResponseUtils.buildSucessResponse(searchResult); } long end = System.currentTimeMillis(); LOGGER.info("..........End Search API. Took(in ms): {}" , (end - start)); return ResponseUtils.buildSucessResponse(searchResult); }  @PostMapping(value = "/v1/search") ResponseEntity<Object> search(@RequestBody SearchCriteria criteria); @GetMapping(value = "/v1/search/categories") ResponseEntity<Object> getSearchCategories(@RequestParam(name = "domain", required = false) String domain);  }