diff --git a/src/main/java/com/example/springbootbookshop/controller/BookController.java b/src/main/java/com/example/springbootbookshop/controller/BookController.java index d8a06c3..c3c1d51 100644 --- a/src/main/java/com/example/springbootbookshop/controller/BookController.java +++ b/src/main/java/com/example/springbootbookshop/controller/BookController.java @@ -44,7 +44,7 @@ public List getAll(@PageableDefault(size = 5, page = 0) @DeleteMapping("/{id}") @PreAuthorize("hasRole('ADMIN')") @Operation(summary = "Delete book by id") - @ResponseStatus(HttpStatus.NOT_FOUND) + @ResponseStatus(HttpStatus.NO_CONTENT) public void deleteBookById(@PathVariable @Positive Long id) { bookService.deleteById(id); } diff --git a/src/test/java/com/example/springbootbookshop/SpringBootBookShopApplicationTests.java b/src/test/java/com/example/springbootbookshop/SpringBootBookShopApplicationTests.java deleted file mode 100644 index e8c39b3..0000000 --- a/src/test/java/com/example/springbootbookshop/SpringBootBookShopApplicationTests.java +++ /dev/null @@ -1,13 +0,0 @@ -package com.example.springbootbookshop; - -import org.junit.jupiter.api.Test; -import org.springframework.boot.test.context.SpringBootTest; - -@SpringBootTest -class SpringBootBookShopApplicationTests { - - @Test - void contextLoads() { - } - -} diff --git a/src/test/java/com/example/springbootbookshop/controller/BookControllerTest.java b/src/test/java/com/example/springbootbookshop/controller/BookControllerTest.java index 4c4c317..6b874c8 100644 --- a/src/test/java/com/example/springbootbookshop/controller/BookControllerTest.java +++ b/src/test/java/com/example/springbootbookshop/controller/BookControllerTest.java @@ -19,6 +19,7 @@ import java.util.List; import javax.sql.DataSource; import lombok.SneakyThrows; +import org.junit.jupiter.api.AfterEach; import org.junit.jupiter.api.BeforeAll; import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.DisplayName; @@ -44,7 +45,9 @@ public class BookControllerTest { private ObjectMapper objectMapper; @BeforeAll - static void beforeAll(@Autowired WebApplicationContext applicationContext) { + static void beforeAll(@Autowired WebApplicationContext applicationContext, + @Autowired DataSource dataSource) { + tearDown(dataSource); mockMvc = MockMvcBuilders.webAppContextSetup(applicationContext) .apply(springSecurity()) .build(); @@ -52,7 +55,6 @@ static void beforeAll(@Autowired WebApplicationContext applicationContext) { @BeforeEach void beforeEach(@Autowired DataSource dataSource) throws SQLException { - tearDown(dataSource); try (Connection connection = dataSource.getConnection()) { connection.setAutoCommit(true); ScriptUtils.executeSqlScript(connection, @@ -62,6 +64,11 @@ void beforeEach(@Autowired DataSource dataSource) throws SQLException { } } + @AfterEach + void afterEach(@Autowired DataSource dataSource) { + tearDown(dataSource); + } + @SneakyThrows static void tearDown(DataSource dataSource) { try (Connection connection = dataSource.getConnection()) { @@ -103,8 +110,6 @@ void createBook_ValidRequestDto_Ok() throws Exception { @Test @DisplayName("Try to create an invalid book") - @Sql(scripts = "classpath:database/books/clear-books-db.sql", - executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) @WithMockUser(username = "admin", roles = {"ADMIN"}) void createBook_InValidRequestDto_Ok() throws Exception { CreateBookRequestDto createBookRequestDto = new CreateBookRequestDto( @@ -164,10 +169,10 @@ void getBookById_ValidId_Ok() throws Exception { @Test @DisplayName("Delete book by id") @WithMockUser(username = "admin", roles = {"ADMIN","USER"}) - void deleteBookByIdAssertSuccess() throws Exception { + void deleteBookById_ValidId_Ok() throws Exception { mockMvc.perform(delete("/books/2") .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isNotFound()); + .andExpect(status().isNoContent()); MvcResult result = mockMvc.perform(get("/books") .contentType(MediaType.APPLICATION_JSON)) @@ -182,15 +187,7 @@ void deleteBookByIdAssertSuccess() throws Exception { @Test @DisplayName("Update book by id") @WithMockUser(username = "admin", roles = {"ADMIN","USER"}) - void updateBookByIdAssertSuccess() throws Exception { - MvcResult result = mockMvc.perform(get("/books/2") - .contentType(MediaType.APPLICATION_JSON)) - .andExpect(status().isOk()) - .andReturn(); - BookDto dto = objectMapper.readValue(result.getResponse() - .getContentAsString(), BookDto.class); - assertEquals("Book 2", dto.getTitle()); - + void updateBookById_ValidId_Ok() throws Exception { CreateBookRequestDto updatedDto = new CreateBookRequestDto( "Updated Title", "Updated Author", "Updated ISBN", BigDecimal.valueOf(29.99), "Updated Description", "updated.jpg", @@ -213,7 +210,7 @@ void updateBookByIdAssertSuccess() throws Exception { @Test @DisplayName("Get book by non-existing id") @WithMockUser(username = "user") - void getBookByNoExitedId() throws Exception { + void getBookById_InvalidId_NotOk() throws Exception { mockMvc.perform(get("/books/555555") .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isNotFound()); @@ -231,16 +228,4 @@ private List getBookDtoList() { .setPrice(BigDecimal.valueOf(29.99)).setDescription("Description 3") .setCoverImage("image2.jpg")); } - - private BookDto getExpectDto(CreateBookRequestDto dto) { - return new BookDto() - .setAuthor(dto.author()) - .setIsbn(dto.isbn()) - .setId(1L) - .setTitle(dto.title()) - .setDescription(dto.description()) - .setCategoryIds(dto.categoryIds()) - .setCoverImage(dto.coverImage()) - .setPrice(dto.price()); - } } diff --git a/src/test/java/com/example/springbootbookshop/controller/CategoryControllerTest.java b/src/test/java/com/example/springbootbookshop/controller/CategoryControllerTest.java index f616b19..99cf7e0 100644 --- a/src/test/java/com/example/springbootbookshop/controller/CategoryControllerTest.java +++ b/src/test/java/com/example/springbootbookshop/controller/CategoryControllerTest.java @@ -47,15 +47,14 @@ public class CategoryControllerTest { @BeforeAll static void beforeAll(@Autowired WebApplicationContext applicationContext, @Autowired DataSource dataSource) throws SQLException { + tearDown(dataSource); mockMvc = MockMvcBuilders.webAppContextSetup(applicationContext) .apply(springSecurity()) .build(); - tearDown(dataSource); } @BeforeEach void beforeEach(@Autowired DataSource dataSource) throws SQLException { - tearDown(dataSource); try (Connection connection = dataSource.getConnection()) { connection.setAutoCommit(true); ScriptUtils.executeSqlScript(connection, @@ -82,7 +81,7 @@ static void tearDown(DataSource dataSource) { @Sql(scripts = "classpath:database/category/clear-category.sql", executionPhase = Sql.ExecutionPhase.BEFORE_TEST_METHOD) @WithMockUser(username = "admin", roles = {"ADMIN"}) - void createNewCategoryAndSaveIntoDbAssertSuccess() throws Exception { + void createNewCategory_ValidCategory_Ok() throws Exception { CategoryRequestDto categoryRequestDto = new CategoryRequestDto( "Literature", "Category with different stories"); @@ -103,7 +102,7 @@ void createNewCategoryAndSaveIntoDbAssertSuccess() throws Exception { @Test @DisplayName("Get all categories") @WithMockUser(username = "user") - void getAllCategoriesFromDbAssertSuccess() throws Exception { + void getAllCategories_ValidRequest_Ok() throws Exception { List expectedCategoryDtoList = getCategoriesList(); MvcResult result = mockMvc.perform(get("/category") @@ -123,7 +122,7 @@ void getAllCategoriesFromDbAssertSuccess() throws Exception { @Test @DisplayName("Delete book by id") @WithMockUser(username = "admin", roles = {"ADMIN","USER"}) - void deleteCategoryByIdAssertSuccess() throws Exception { + void deleteCategoryById_ValidId_Ok() throws Exception { mockMvc.perform(delete("/category/2") .contentType(MediaType.APPLICATION_JSON)) @@ -142,7 +141,7 @@ void deleteCategoryByIdAssertSuccess() throws Exception { @Test @DisplayName("User try delete category by id") @WithMockUser(username = "user") - void deleteCategoryByIdWithNoPermissionAssertException() throws Exception { + void deleteCategoryWithoutPermission_NotOk() throws Exception { mockMvc.perform(delete("/category/2") .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isForbidden()); @@ -151,7 +150,7 @@ void deleteCategoryByIdWithNoPermissionAssertException() throws Exception { @Test @DisplayName("Update category by id") @WithMockUser(username = "admin", roles = {"ADMIN","USER"}) - void updateBookByIdAssertSuccess() throws Exception { + void updateBookById_ValidId_Ok() throws Exception { MvcResult result = mockMvc.perform(get("/category/2") .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) @@ -188,7 +187,7 @@ void updateBookByIdAssertSuccess() throws Exception { @Sql(scripts = "classpath:database/books-categories/clear-book-category.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) @WithMockUser(username = "user") - void getAllBooksByCategoryAssertSuccess() throws Exception { + void getBookByCategory_Ok() throws Exception { MvcResult result = mockMvc.perform(get("/category/2/books") .contentType(MediaType.APPLICATION_JSON)) .andExpect(status().isOk()) @@ -199,11 +198,6 @@ void getAllBooksByCategoryAssertSuccess() throws Exception { assertEquals("Book 5", bookDtoWithoutCategoryIds.get(0).title()); } - private CategoryDto getExpectDto() { - return new CategoryDto("Literature", - "Category with different stories"); - } - private List getCategoriesList() { return List.of(new CategoryDto("Category1", "Description1"), new CategoryDto("Category2", "Description2")); diff --git a/src/test/java/com/example/springbootbookshop/service/BookServiceTest.java b/src/test/java/com/example/springbootbookshop/service/BookServiceTest.java index 46771db..bb2b640 100644 --- a/src/test/java/com/example/springbootbookshop/service/BookServiceTest.java +++ b/src/test/java/com/example/springbootbookshop/service/BookServiceTest.java @@ -59,9 +59,14 @@ void getBookByExistId() { @DisplayName("Save book") void saveBook() { CreateBookRequestDto dto = getCreateRequestBookDto(); - bookService.save(dto); - bookService.save(dto); - verify(bookRepository, times(2)).save(any()); + Book book = bookMapper.toBook(dto); + + when(bookRepository.save(any())).thenReturn(book); + + BookDto savedDto = bookService.save(dto); + + verify(bookRepository, times(1)).save(any()); + assertEquals(savedDto.getPrice(), BigDecimal.valueOf(200)); } @Test diff --git a/src/test/resources/database/books-categories/clear-book-category.sql b/src/test/resources/database/books-categories/clear-book-category.sql index 4815a3c..938c9f5 100644 --- a/src/test/resources/database/books-categories/clear-book-category.sql +++ b/src/test/resources/database/books-categories/clear-book-category.sql @@ -1 +1 @@ -DELETE FROM books_categories; \ No newline at end of file +DELETE FROM books_categories; diff --git a/src/test/resources/database/books-categories/insert-books-categories.sql b/src/test/resources/database/books-categories/insert-books-categories.sql index c446d8d..763ea96 100644 --- a/src/test/resources/database/books-categories/insert-books-categories.sql +++ b/src/test/resources/database/books-categories/insert-books-categories.sql @@ -2,4 +2,4 @@ INSERT INTO books_categories (book_id, category_id) VALUES (4, 1), (5, 2), - (6, 3); \ No newline at end of file + (6, 3); diff --git a/src/test/resources/database/books/clear-books-db.sql b/src/test/resources/database/books/clear-books-db.sql index 9f26616..f88cf70 100644 --- a/src/test/resources/database/books/clear-books-db.sql +++ b/src/test/resources/database/books/clear-books-db.sql @@ -1 +1 @@ -DELETE FROM books; \ No newline at end of file +DELETE FROM books; diff --git a/src/test/resources/database/category/clear-category.sql b/src/test/resources/database/category/clear-category.sql index 3ce60ce..b5685e7 100644 --- a/src/test/resources/database/category/clear-category.sql +++ b/src/test/resources/database/category/clear-category.sql @@ -1 +1 @@ -DELETE FROM categories; \ No newline at end of file +DELETE FROM categories;