@Test(expected = EntityNotFoundException.class) public void delete_shouldThrowNotFoundException() { Mockito.when(bookDaoMock.delete(book)).thenReturn(false); BookResource resource = new BookResource(bookDaoMock); ResponseEntity<String> response = resource.delete(ISBN); }
@RequestMapping(value = "{isbn}", method = RequestMethod.DELETE) public ResponseEntity<String> delete(@PathVariable String isbn) { if (bookDao.delete(new BookBuilder(isbn).build())) { return new ResponseEntity<String>(HttpStatus.NO_CONTENT); } throw new EntityNotFoundException("Book with ISBN[" + isbn + "] does not exist."); }
BookResource { @RequestMapping(value = "{isbn}", method = RequestMethod.DELETE) public ResponseEntity<String> delete(@PathVariable String isbn) { if (bookDao.delete(new BookBuilder(isbn).build())) { return new ResponseEntity<String>(HttpStatus.NO_CONTENT); } throw new EntityNotFoundException("Book with ISBN[" + isbn + "] does not exist."); } }
BookResource { @RequestMapping(value = "{isbn}", method = RequestMethod.DELETE) public ResponseEntity<String> delete(@PathVariable String isbn) { if (bookDao.delete(new BookBuilder(isbn).build())) { return new ResponseEntity<String>(HttpStatus.NO_CONTENT); } throw new EntityNotFoundException("Book with ISBN[" + isbn + "] does not exist."); } @Autowired  BookResource(BookDao bookDao); }
BookResource { @RequestMapping(value = "{isbn}", method = RequestMethod.DELETE) public ResponseEntity<String> delete(@PathVariable String isbn) { if (bookDao.delete(new BookBuilder(isbn).build())) { return new ResponseEntity<String>(HttpStatus.NO_CONTENT); } throw new EntityNotFoundException("Book with ISBN[" + isbn + "] does not exist."); } @Autowired  BookResource(BookDao bookDao); @RequestMapping(method = RequestMethod.POST, consumes = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE}) ResponseEntity<String> create(@RequestBody Book book); @RequestMapping(value = "/all", method = RequestMethod.GET, produces = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE}) ResponseEntity<BooksList> readAll(); @RequestMapping(value = "/{isbn}", method = RequestMethod.GET, produces = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE}) ResponseEntity<Book> readByIsbn(@PathVariable String isbn); @RequestMapping(value = "/{isbn}", method = RequestMethod.PUT, consumes = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE}) ResponseEntity<String> update(@RequestBody Book book); @RequestMapping(value = "{isbn}", method = RequestMethod.DELETE) ResponseEntity<String> delete(@PathVariable String isbn); @ExceptionHandler({EntityNotFoundException.class}) ResponseEntity<String> handleEntityNotFoundException(EntityNotFoundException e); @ExceptionHandler({EntityExistsException.class}) ResponseEntity<String> handleEntityExistsException(EntityExistsException e); }
BookResource { @RequestMapping(value = "{isbn}", method = RequestMethod.DELETE) public ResponseEntity<String> delete(@PathVariable String isbn) { if (bookDao.delete(new BookBuilder(isbn).build())) { return new ResponseEntity<String>(HttpStatus.NO_CONTENT); } throw new EntityNotFoundException("Book with ISBN[" + isbn + "] does not exist."); } @Autowired  BookResource(BookDao bookDao); @RequestMapping(method = RequestMethod.POST, consumes = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE}) ResponseEntity<String> create(@RequestBody Book book); @RequestMapping(value = "/all", method = RequestMethod.GET, produces = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE}) ResponseEntity<BooksList> readAll(); @RequestMapping(value = "/{isbn}", method = RequestMethod.GET, produces = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE}) ResponseEntity<Book> readByIsbn(@PathVariable String isbn); @RequestMapping(value = "/{isbn}", method = RequestMethod.PUT, consumes = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE}) ResponseEntity<String> update(@RequestBody Book book); @RequestMapping(value = "{isbn}", method = RequestMethod.DELETE) ResponseEntity<String> delete(@PathVariable String isbn); @ExceptionHandler({EntityNotFoundException.class}) ResponseEntity<String> handleEntityNotFoundException(EntityNotFoundException e); @ExceptionHandler({EntityExistsException.class}) ResponseEntity<String> handleEntityExistsException(EntityExistsException e);  }