-
Notifications
You must be signed in to change notification settings - Fork 0
Book functionality test || HW #15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
891bf5b
added configuration file for custom container and application propert…
Jelors ae26a47
all tests for BookService
Jelors ceb249b
added CategoryServiceTest
Jelors 22cb76f
added BookControllerTest and decreased minimum title length for books
Jelors eb648a5
added CategoryControllerTest
Jelors 44bd912
little fixes
Jelors af835d2
fixes due to mentor's advice
Jelors d623b01
fixes due to comments
Jelors File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
33 changes: 33 additions & 0 deletions
33
src/test/java/com/springm/store/config/CustomContainer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,33 @@ | ||
| package com.springm.store.config; | ||
|
|
||
| import org.testcontainers.containers.MySQLContainer; | ||
|
|
||
| public class CustomContainer extends MySQLContainer<CustomContainer> { | ||
| private static final String DB_IMAGE = "mysql:9.5.0"; | ||
|
|
||
| private static CustomContainer container; | ||
|
|
||
| private CustomContainer() { | ||
| super(DB_IMAGE); | ||
| } | ||
|
|
||
| public static synchronized CustomContainer getInstance() { | ||
| if (container == null) { | ||
| container = new CustomContainer(); | ||
| } | ||
| return container; | ||
| } | ||
|
|
||
| @Override | ||
| public void start() { | ||
| super.start(); | ||
| System.setProperty("TEST_DB_URL", container.getJdbcUrl()); | ||
| System.setProperty("TEST_DB_USERNAME", container.getUsername()); | ||
| System.setProperty("TEST_DB_PASSWORD", container.getPassword()); | ||
| } | ||
|
|
||
| @Override | ||
| public void stop() { | ||
| super.stop(); | ||
| } | ||
| } |
170 changes: 170 additions & 0 deletions
170
src/test/java/com/springm/store/controller/BookControllerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,170 @@ | ||
| package com.springm.store.controller; | ||
|
|
||
| import com.fasterxml.jackson.core.type.TypeReference; | ||
| import com.fasterxml.jackson.databind.ObjectMapper; | ||
| import com.springm.store.dto.book.BookDto; | ||
| import com.springm.store.dto.book.CreateBookRequestDto; | ||
| import java.math.BigDecimal; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Set; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.springframework.beans.factory.annotation.Autowired; | ||
| import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc; | ||
| import org.springframework.boot.test.context.SpringBootTest; | ||
| import org.springframework.http.MediaType; | ||
| import org.springframework.security.test.context.support.WithMockUser; | ||
| import org.springframework.test.context.jdbc.Sql; | ||
| import org.springframework.test.web.servlet.MockMvc; | ||
| import org.springframework.test.web.servlet.MvcResult; | ||
| import static org.assertj.core.api.Assertions.assertThat; | ||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertTrue; | ||
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.delete; | ||
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get; | ||
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post; | ||
| import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put; | ||
| import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; | ||
| import static org.testcontainers.shaded.org.apache.commons.lang3.builder.EqualsBuilder.reflectionEquals; | ||
|
|
||
| @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) | ||
| @AutoConfigureMockMvc | ||
| @WithMockUser(username = "admin", roles = {"ADMIN"}) | ||
| @Sql(scripts = {"classpath:database/books/add-items-to-categories-table.sql", | ||
| "classpath:database/books/add-three-items-to-books-table.sql", | ||
| "classpath:database/books/assign-categories-for-books.sql"}) | ||
| @Sql(scripts = "classpath:database/books/clear-all-tables.sql", executionPhase = Sql.ExecutionPhase.AFTER_TEST_METHOD) | ||
| class BookControllerTest { | ||
| @Autowired | ||
| private MockMvc mockMvc; | ||
|
|
||
| @Autowired | ||
| private ObjectMapper objectMapper; | ||
|
|
||
| @Test | ||
| @DisplayName("Add a new book") | ||
| void createBook_ValidRequestDto_Success() throws Exception { | ||
|
|
||
| CreateBookRequestDto bookRequestDto = new CreateBookRequestDto(); | ||
| bookRequestDto.setTitle("Dune"); | ||
| bookRequestDto.setAuthor("Frank Gerbert"); | ||
| bookRequestDto.setIsbn("978-0316597011"); | ||
| bookRequestDto.setPrice(BigDecimal.valueOf(35.32)); | ||
| bookRequestDto.setCategoryIds(Set.of(1L)); | ||
|
|
||
| BookDto expected = new BookDto(); | ||
| expected.setTitle(bookRequestDto.getTitle()); | ||
| expected.setAuthor(bookRequestDto.getAuthor()); | ||
| expected.setIsbn(bookRequestDto.getIsbn()); | ||
| expected.setPrice(bookRequestDto.getPrice()); | ||
| expected.setCategoryIds(bookRequestDto.getCategoryIds()); | ||
|
|
||
| String jsonRequest = objectMapper.writeValueAsString(bookRequestDto); | ||
|
|
||
| MvcResult result = mockMvc.perform( | ||
| post("/books") | ||
| .content(jsonRequest) | ||
| .contentType(MediaType.APPLICATION_JSON) | ||
| .accept(MediaType.APPLICATION_JSON) | ||
| ) | ||
| .andExpect(status().isCreated()) | ||
| .andReturn(); | ||
|
|
||
| BookDto actual = objectMapper.readValue(result.getResponse().getContentAsString(), BookDto.class); | ||
|
|
||
| assertTrue( | ||
| reflectionEquals(expected, actual, "id") | ||
| ); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Find all available books") | ||
| void findAll_ValidItems_ShouldReturnAllBooks() throws Exception { | ||
| MvcResult result = mockMvc.perform(get("/books") | ||
| .param("page", "0") | ||
| .param("size", "4") | ||
| .accept(MediaType.APPLICATION_JSON)) | ||
| .andExpect(status().isOk()) | ||
| .andReturn(); | ||
| int expectedElementsCount = 3; | ||
| String jsonResponse = result.getResponse().getContentAsString(); | ||
|
|
||
| Map<String, Object> responseMap = objectMapper.readValue( | ||
| jsonResponse, | ||
| new TypeReference<>() { | ||
| }); | ||
| List<BookDto> actualList = objectMapper.convertValue( | ||
| responseMap.get("content"), | ||
| new TypeReference<List<BookDto>>() { | ||
| } | ||
| ); | ||
|
|
||
| assertThat(actualList) | ||
| .extracting(BookDto::getTitle) | ||
| .containsExactly("Kobzar", "Harry Potter", "The Witcher"); | ||
| assertEquals(expectedElementsCount, responseMap.get("totalElements")); | ||
| assertEquals(expectedElementsCount, ((List<?>) responseMap.get("content")).size()); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Returns book with right id") | ||
| void getBookById_ValidItems_ShouldReturnBook() throws Exception { | ||
| MvcResult result = mockMvc.perform( | ||
| get("/books/1") | ||
| .accept(MediaType.APPLICATION_JSON)) | ||
| .andExpect(status().isOk()) | ||
| .andReturn(); | ||
|
|
||
| String jsonResponse = result.getResponse().getContentAsString(); | ||
|
|
||
| assertTrue(jsonResponse.contains("Kobzar")); | ||
| assertTrue(jsonResponse.contains("Taras Shevchenko")); | ||
| assertTrue(jsonResponse.contains("978-1909156548")); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Updates book by id") | ||
| void updateBookById_ValidInput_Success() throws Exception { | ||
| CreateBookRequestDto bookRequestDto = new CreateBookRequestDto(); | ||
| bookRequestDto.setTitle("The World of Ice and Fire"); | ||
| bookRequestDto.setAuthor("George R. R. Martin"); | ||
| bookRequestDto.setIsbn("978-0316597101"); | ||
| bookRequestDto.setPrice(BigDecimal.valueOf(69.99)); | ||
| bookRequestDto.setCategoryIds(Set.of(1L, 2L)); | ||
|
|
||
| String jsonRequest = objectMapper.writeValueAsString(bookRequestDto); | ||
|
|
||
| MvcResult result = mockMvc.perform( | ||
| put("/books/2") | ||
| .content(jsonRequest) | ||
| .contentType(MediaType.APPLICATION_JSON) | ||
| .accept(MediaType.APPLICATION_JSON)) | ||
| .andExpect(status().isOk()) | ||
| .andReturn(); | ||
|
|
||
| BookDto actual = objectMapper.readValue( | ||
| result.getResponse().getContentAsString(), | ||
| BookDto.class | ||
| ); | ||
|
|
||
| BookDto expected = new BookDto(); | ||
| expected.setId(2L); | ||
| expected.setTitle(bookRequestDto.getTitle()); | ||
| expected.setAuthor(bookRequestDto.getAuthor()); | ||
| expected.setIsbn(bookRequestDto.getIsbn()); | ||
| expected.setPrice(bookRequestDto.getPrice()); | ||
| expected.setCategoryIds(bookRequestDto.getCategoryIds()); | ||
|
|
||
| assertTrue(reflectionEquals(expected, actual, "id")); | ||
| } | ||
|
|
||
| @Test | ||
| @DisplayName("Deletes book by id") | ||
| void deleteBookById_ValidInput_Success() throws Exception { | ||
| mockMvc.perform( | ||
| delete("/books/3") | ||
| .accept(MediaType.APPLICATION_JSON)) | ||
| .andExpect(status().isNoContent()); | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
compare expected and actual dto