@Test(expected = EntityExistsException.class) public void create_shouldThrowEntityExistsException() { Optional<Book> optionalAbsent = Optional.absent(); Mockito.when(bookDaoMock.create(book)).thenReturn(optionalAbsent); BookResource resource = new BookResource(bookDaoMock); ResponseEntity<String> response = resource.create(book); }
@RequestMapping(method = RequestMethod.POST, consumes = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE}) public ResponseEntity<String> create(@RequestBody Book book) { logger.info("book[{}]", book); if (bookDao.create(book).isPresent()) { return new ResponseEntity<String>(HttpStatus.NO_CONTENT); } throw new EntityExistsException("Book with ISBN[" + book.getIsbn() + "] already exists."); }
BookResource { @RequestMapping(method = RequestMethod.POST, consumes = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE}) public ResponseEntity<String> create(@RequestBody Book book) { logger.info("book[{}]", book); if (bookDao.create(book).isPresent()) { return new ResponseEntity<String>(HttpStatus.NO_CONTENT); } throw new EntityExistsException("Book with ISBN[" + book.getIsbn() + "] already exists."); } }
BookResource { @RequestMapping(method = RequestMethod.POST, consumes = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE}) public ResponseEntity<String> create(@RequestBody Book book) { logger.info("book[{}]", book); if (bookDao.create(book).isPresent()) { return new ResponseEntity<String>(HttpStatus.NO_CONTENT); } throw new EntityExistsException("Book with ISBN[" + book.getIsbn() + "] already exists."); } @Autowired  BookResource(BookDao bookDao); }
BookResource { @RequestMapping(method = RequestMethod.POST, consumes = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE}) public ResponseEntity<String> create(@RequestBody Book book) { logger.info("book[{}]", book); if (bookDao.create(book).isPresent()) { return new ResponseEntity<String>(HttpStatus.NO_CONTENT); } throw new EntityExistsException("Book with ISBN[" + book.getIsbn() + "] already exists."); } @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(method = RequestMethod.POST, consumes = {MediaType.APPLICATION_XML_VALUE, MediaType.APPLICATION_JSON_VALUE}) public ResponseEntity<String> create(@RequestBody Book book) { logger.info("book[{}]", book); if (bookDao.create(book).isPresent()) { return new ResponseEntity<String>(HttpStatus.NO_CONTENT); } throw new EntityExistsException("Book with ISBN[" + book.getIsbn() + "] already exists."); } @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);  }