QuoteController { @RequestMapping(value = "/quotes", method = RequestMethod.GET) public ResponseEntity<List<Quote>> getQuotes(@RequestParam(value="q", required=false) String query) throws SymbolNotFoundException { logger.debug("received Quote query for: %s", query); if (query == null) { return new ResponseEntity<>(new ArrayList<>(), getNoCacheHeaders(), HttpStatus.OK); } List<Quote> quotes = service.getQuotes( query ); logger.info(String.format("Retrieved symbols: %s with quotes {}", query)); return new ResponseEntity<>(quotes, getNoCacheHeaders(), HttpStatus.OK); }  @RequestMapping(value = "/quotes", method = RequestMethod.GET) ResponseEntity<List<Quote>> getQuotes(@RequestParam(value="q", required=false) String query); @RequestMapping(value = "/company/{name}", method = RequestMethod.GET) ResponseEntity<List<CompanyInfo>> getCompanies(@PathVariable("name") final String name); @ExceptionHandler({ Exception.class }) void handleException(Exception e, HttpServletResponse response);  }
@Test public void getNullQuote() throws Exception { when(service.getQuotes(TestConfiguration.NULL_QUOTE_SYMBOL)).thenThrow( new SymbolNotFoundException(TestConfiguration.NULL_QUOTE_SYMBOL)); mockMvc.perform( get("/quotes?q=" + TestConfiguration.NULL_QUOTE_SYMBOL).contentType( MediaType.APPLICATION_JSON)).andExpect(status().is5xxServerError()) .andDo(print()); }
@Test public void getQuotes() throws Exception { when(service.getQuotes(TestConfiguration.QUOTE_SYMBOLS)).thenReturn( TestConfiguration.quotes()); mockMvc.perform( get("/quotes?q=" + TestConfiguration.QUOTE_SYMBOLS).contentType( MediaType.APPLICATION_JSON)).andExpect(status().isOk()) .andExpect(jsonPath("$",hasSize(2))) .andDo(print()); }
@Test public void getQuotesOneQuote() throws Exception { when(service.getQuotes(TestConfiguration.QUOTE_SYMBOL)).thenReturn( Collections.singletonList(TestConfiguration.quote())); mockMvc.perform( get("/quotes?q=" + TestConfiguration.QUOTE_SYMBOL).contentType( MediaType.APPLICATION_JSON)).andExpect(status().isOk()) .andExpect(jsonPath("$",hasSize(1))) .andExpect( content().contentTypeCompatibleWith( MediaType.APPLICATION_JSON)) .andExpect( jsonPath("$[0].Name").value( TestConfiguration.QUOTE_NAME)) .andExpect( jsonPath("$[0].Symbol").value( TestConfiguration.QUOTE_SYMBOL)) .andExpect( jsonPath("$[0].LastPrice").value( TestConfiguration.QUOTE_LAST_PRICE)) .andExpect( jsonPath("$[0].Change",Matchers.closeTo(TestConfiguration.QUOTE_CHANGE, new BigDecimal(0.01)))) .andExpect( jsonPath("$[0].ChangePercent", Matchers.closeTo(TestConfiguration.QUOTE_CHANGE_PERCENT, 0.01))) .andExpect( jsonPath("$[0].Timestamp",notNullValue())) .andExpect( jsonPath("$[0].MSDate",Matchers.closeTo( TestConfiguration.QUOTE_MSDATE,0.01))) .andDo(print()); }