use of com.example.demo.dto.BookDto in project spring-boot-backend-template by paakmau.
the class BookTests method testPost.
@Test
void testPost() throws Exception {
BookDto dto = new BookDto(null, "Book 10", "Author 10");
MvcResult res = mvc.perform(MockMvcRequestBuilders.post("/books").contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsString(dto))).andExpect(MockMvcResultMatchers.status().isCreated()).andReturn();
BookDto resDto = objectMapper.readValue(res.getResponse().getContentAsString(), BookDto.class);
assertNotNull(resDto);
}
use of com.example.demo.dto.BookDto in project spring-boot-backend-template by paakmau.
the class BookTests method testGet.
@Test
void testGet() throws Exception {
for (BookDto dto : bookDtos) {
MvcResult res = mvc.perform(MockMvcRequestBuilders.get("/books/{id}", dto.getId()).contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsString(dto))).andExpect(MockMvcResultMatchers.status().isOk()).andReturn();
BookDto resDto = objectMapper.readValue(res.getResponse().getContentAsString(), BookDto.class);
assertEquals(dto, resDto);
}
}
use of com.example.demo.dto.BookDto in project spring-boot-backend-template by paakmau.
the class BookTests method testGetByTitle.
@Test
void testGetByTitle() throws Exception {
for (BookDto dto : bookDtos) {
MvcResult res = mvc.perform(MockMvcRequestBuilders.get("/books").param("title", dto.getTitle()).contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsString(dto))).andExpect(MockMvcResultMatchers.status().isOk()).andReturn();
List<BookDto> resDtos = objectMapper.readValue(res.getResponse().getContentAsString(), new TypeReference<ArrayList<BookDto>>() {
});
assertEquals(1, resDtos.size());
assertEquals(dto, resDtos.get(0));
}
}
use of com.example.demo.dto.BookDto in project spring-boot-backend-template by paakmau.
the class BookTests method init.
@BeforeEach
void init() throws Exception {
for (BookDto dto : bookDtos) {
MvcResult res = mvc.perform(MockMvcRequestBuilders.post("/books").contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsString(dto))).andExpect(MockMvcResultMatchers.status().isCreated()).andReturn();
BookDto resDto = objectMapper.readValue(res.getResponse().getContentAsString(), BookDto.class);
dto.setId(resDto.getId());
}
}
use of com.example.demo.dto.BookDto in project spring-boot-backend-template by paakmau.
the class BookTests method testPut.
@Test
void testPut() throws Exception {
List<BookDto> modifiedDtos = bookDtos.stream().map(dto -> new BookDto(dto.getId(), dto.getTitle().toUpperCase(), dto.getAuthor().toLowerCase())).collect(Collectors.toList());
for (BookDto dto : modifiedDtos) {
MvcResult res = mvc.perform(MockMvcRequestBuilders.put("/books/{id}", dto.getId()).contentType(MediaType.APPLICATION_JSON).content(objectMapper.writeValueAsString(dto))).andExpect(MockMvcResultMatchers.status().isOk()).andReturn();
BookDto resDto = objectMapper.readValue(res.getResponse().getContentAsString(), BookDto.class);
assertNotNull(resDto);
}
}
Aggregations