Skip to content

Commit

Permalink
fixes some code change name convention
Browse files Browse the repository at this point in the history
  • Loading branch information
don-bigdad committed Jan 25, 2024
1 parent 1769139 commit e60c066
Show file tree
Hide file tree
Showing 9 changed files with 33 additions and 62 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public List<BookDto> 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);
}
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -44,15 +45,16 @@ 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();
}

@BeforeEach
void beforeEach(@Autowired DataSource dataSource) throws SQLException {
tearDown(dataSource);
try (Connection connection = dataSource.getConnection()) {
connection.setAutoCommit(true);
ScriptUtils.executeSqlScript(connection,
Expand All @@ -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()) {
Expand Down Expand Up @@ -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(
Expand Down Expand Up @@ -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))
Expand All @@ -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",
Expand All @@ -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());
Expand All @@ -231,16 +228,4 @@ private List<BookDto> 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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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");
Expand All @@ -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<CategoryDto> expectedCategoryDtoList = getCategoriesList();

MvcResult result = mockMvc.perform(get("/category")
Expand All @@ -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))
Expand All @@ -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());
Expand All @@ -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())
Expand Down Expand Up @@ -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())
Expand All @@ -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<CategoryDto> getCategoriesList() {
return List.of(new CategoryDto("Category1", "Description1"),
new CategoryDto("Category2", "Description2"));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DELETE FROM books_categories;
DELETE FROM books_categories;
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@ INSERT INTO books_categories (book_id, category_id)
VALUES
(4, 1),
(5, 2),
(6, 3);
(6, 3);
2 changes: 1 addition & 1 deletion src/test/resources/database/books/clear-books-db.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DELETE FROM books;
DELETE FROM books;
2 changes: 1 addition & 1 deletion src/test/resources/database/category/clear-category.sql
Original file line number Diff line number Diff line change
@@ -1 +1 @@
DELETE FROM categories;
DELETE FROM categories;

0 comments on commit e60c066

Please sign in to comment.