@Test @DisplayName("Comment into book has been added") void addCommentToBook() throws Exception { LibraryService libraryService = mock(LibraryService.class); BookCommunityService communityService = mock(BookCommunityService.class); CommunityController controller = new CommunityController( libraryService, communityService ); MockMvc rest = getRestService(controller); Book mockBook = new Book("", ""); when(libraryService.getBookById("20")).thenReturn(mockBook); MockHttpServletRequestBuilder post = post("/comment/book/add/20") .contentType(MediaType.APPLICATION_JSON_UTF8) .content("{\"text\": \"Some message from the best book\"}"); injectToken(post); rest.perform(post) .andExpect(status().isOk()) .andDo(h -> { System.out.println("Response `" + h.getResponse().getContentAsString()+"`"); }) .andExpect(jsonPath("$.success", is(true))); verify(communityService).registerBookComment(same(mockBook), eq("Some message from the best book")); }
@PostMapping("/comment/book/add/{bookId}") public ActionResult addCommentToBook(@PathVariable String bookId, @RequestBody Map<String, Object> body) { Book book; try { book = libraryService.getBookById(bookId); } catch (NotExistException e) { throw new BadRequestException("Book is missed."); } String text = (String) body.getOrDefault("text", null); if (Objects.isNull(book)) { throw new BadRequestException("Book is not exists"); } ActionResult res; if (Objects.nonNull(text) && !text.trim().equals("")) { communityService.registerBookComment(book, text); res = ActionResult.ok(); } else { res = ActionResult.error("Message is empty."); } return res; }
CommunityController { @PostMapping("/comment/book/add/{bookId}") public ActionResult addCommentToBook(@PathVariable String bookId, @RequestBody Map<String, Object> body) { Book book; try { book = libraryService.getBookById(bookId); } catch (NotExistException e) { throw new BadRequestException("Book is missed."); } String text = (String) body.getOrDefault("text", null); if (Objects.isNull(book)) { throw new BadRequestException("Book is not exists"); } ActionResult res; if (Objects.nonNull(text) && !text.trim().equals("")) { communityService.registerBookComment(book, text); res = ActionResult.ok(); } else { res = ActionResult.error("Message is empty."); } return res; } }
CommunityController { @PostMapping("/comment/book/add/{bookId}") public ActionResult addCommentToBook(@PathVariable String bookId, @RequestBody Map<String, Object> body) { Book book; try { book = libraryService.getBookById(bookId); } catch (NotExistException e) { throw new BadRequestException("Book is missed."); } String text = (String) body.getOrDefault("text", null); if (Objects.isNull(book)) { throw new BadRequestException("Book is not exists"); } ActionResult res; if (Objects.nonNull(text) && !text.trim().equals("")) { communityService.registerBookComment(book, text); res = ActionResult.ok(); } else { res = ActionResult.error("Message is empty."); } return res; } CommunityController(LibraryService libraryService, BookCommunityService communityService); }
CommunityController { @PostMapping("/comment/book/add/{bookId}") public ActionResult addCommentToBook(@PathVariable String bookId, @RequestBody Map<String, Object> body) { Book book; try { book = libraryService.getBookById(bookId); } catch (NotExistException e) { throw new BadRequestException("Book is missed."); } String text = (String) body.getOrDefault("text", null); if (Objects.isNull(book)) { throw new BadRequestException("Book is not exists"); } ActionResult res; if (Objects.nonNull(text) && !text.trim().equals("")) { communityService.registerBookComment(book, text); res = ActionResult.ok(); } else { res = ActionResult.error("Message is empty."); } return res; } CommunityController(LibraryService libraryService, BookCommunityService communityService); @PostMapping("/comment/book/add/{bookId}") ActionResult addCommentToBook(@PathVariable String bookId, @RequestBody Map<String, Object> body); @GetMapping("/comment/book/get/{bookId}") List<Comment> getBookComments(@PathVariable String bookId); }
CommunityController { @PostMapping("/comment/book/add/{bookId}") public ActionResult addCommentToBook(@PathVariable String bookId, @RequestBody Map<String, Object> body) { Book book; try { book = libraryService.getBookById(bookId); } catch (NotExistException e) { throw new BadRequestException("Book is missed."); } String text = (String) body.getOrDefault("text", null); if (Objects.isNull(book)) { throw new BadRequestException("Book is not exists"); } ActionResult res; if (Objects.nonNull(text) && !text.trim().equals("")) { communityService.registerBookComment(book, text); res = ActionResult.ok(); } else { res = ActionResult.error("Message is empty."); } return res; } CommunityController(LibraryService libraryService, BookCommunityService communityService); @PostMapping("/comment/book/add/{bookId}") ActionResult addCommentToBook(@PathVariable String bookId, @RequestBody Map<String, Object> body); @GetMapping("/comment/book/get/{bookId}") List<Comment> getBookComments(@PathVariable String bookId);  }