Skip to content
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

Сравнение объектов в тестах #33

Merged
merged 20 commits into from
Aug 2, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions src/main/java/io/hexlet/blog/mapper/PostCommentMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,9 @@ public abstract class PostCommentMapper {
@Mapping(source = "author.id", target = "authorId")
@Mapping(source = "post.id", target = "postId")
public abstract PostCommentDTO map(PostComment model);

@Mapping(source = "authorId", target = "author.id")
@Mapping(source = "postId", target = "post.id")
public abstract PostComment map(PostCommentDTO model);
}

3 changes: 3 additions & 0 deletions src/main/java/io/hexlet/blog/mapper/PostMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,5 +25,8 @@ public abstract class PostMapper {
@Mapping(source = "author.id", target = "authorId")
public abstract PostDTO map(Post model);

@Mapping(source = "authorId", target = "author.id")
public abstract Post map(PostDTO model);

public abstract void update(PostUpdateDTO dto, @MappingTarget Post model);
}
3 changes: 3 additions & 0 deletions src/main/java/io/hexlet/blog/mapper/UserMapper.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,9 @@ public abstract class UserMapper {
@Mapping(target = "password", ignore = true)
public abstract UserDTO map(User model);

@Mapping(target = "email", source = "username")
public abstract User map(UserDTO model);

public abstract void update(UserUpdateDTO update, @MappingTarget User destination);

@BeforeMapping
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import io.hexlet.blog.dto.PostCommentDTO;
import io.hexlet.blog.mapper.PostCommentMapper;
import org.assertj.core.api.Assertions;
import org.instancio.Instancio;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -25,6 +30,9 @@
import org.springframework.web.context.WebApplicationContext;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;


@SpringBootTest
@Transactional
Expand Down Expand Up @@ -53,6 +61,13 @@ public class PostsCommentsControllerTest {

private Post testPost;

@Autowired
private ObjectMapper om;

@Autowired
private PostCommentMapper postCommentMapper;


@BeforeEach
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac)
Expand Down Expand Up @@ -89,10 +104,15 @@ public void testIndex() throws Exception {
.andExpect(status().isOk())
.andReturn();
var body = result.getResponse().getContentAsString();
assertThatJson(body)
.node("content")
.isArray()
.hasSize(2);

Map<String, Object> content = om.readValue(body, new TypeReference<>() {});
var postComments = content.get("content");

List<PostCommentDTO> postCommentDTOS = om.convertValue(postComments, new TypeReference<>() {});

var actual = postCommentDTOS.stream().map(postCommentMapper::map).toList();
var expected = postCommentRepository.findAll();
Assertions.assertThat(actual).containsExactlyInAnyOrderElementsOf(expected);
}

@Test
Expand All @@ -102,9 +122,8 @@ public void testFilteredIndex() throws Exception {
.andReturn();
var body = result.getResponse().getContentAsString();
assertThatJson(body)
.node("content")
.isArray()
.hasSize(1);
.node("content")
.isArray()
.hasSize(1);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

import com.fasterxml.jackson.core.type.TypeReference;
import io.hexlet.blog.dto.PostDTO;
import org.assertj.core.api.Assertions;
import org.instancio.Instancio;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -34,6 +37,8 @@
import org.springframework.web.context.WebApplicationContext;

import java.nio.charset.StandardCharsets;
import java.util.List;


@SpringBootTest
@AutoConfigureMockMvc
Expand Down Expand Up @@ -64,6 +69,7 @@ public class PostsControllerTest {

private Post testPost;


@BeforeEach
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac)
Expand All @@ -81,11 +87,18 @@ public void setUp() {
@Test
public void testIndex() throws Exception {
postRepository.save(testPost);
var result = mockMvc.perform(get("/api/posts").with(token))

var response = mockMvc.perform(get("/api/posts").with(token))
.andExpect(status().isOk())
.andReturn();
var body = result.getResponse().getContentAsString();
assertThatJson(body).isArray();
.andReturn()
.getResponse();
var body = response.getContentAsString();

List<PostDTO> postDTOS = om.readValue(body, new TypeReference<>() {});

var actual = postDTOS.stream().map(postMapper::map).toList();
var expected = postRepository.findAll();
Assertions.assertThat(actual).containsExactlyInAnyOrderElementsOf(expected);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
@@ -1,17 +1,23 @@
package io.hexlet.blog.controller.api;

import static net.javacrumbs.jsonunit.assertj.JsonAssertions.assertThatJson;
import static org.assertj.core.api.Assertions.assertThat;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.springframework.security.test.web.servlet.request.SecurityMockMvcRequestPostProcessors.jwt;
import static org.springframework.security.test.web.servlet.setup.SecurityMockMvcConfigurers.springSecurity;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.put;
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 java.util.List;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;

import com.fasterxml.jackson.core.type.TypeReference;
import io.hexlet.blog.dto.UserDTO;
import io.hexlet.blog.mapper.UserMapper;
import org.assertj.core.api.Assertions;
import org.instancio.Instancio;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -31,6 +37,7 @@
import org.springframework.test.web.servlet.setup.MockMvcBuilders;
import org.springframework.web.context.WebApplicationContext;


@SpringBootTest
@AutoConfigureMockMvc
public class UsersControllerTest {
Expand All @@ -53,10 +60,14 @@ public class UsersControllerTest {
@Autowired
private ObjectMapper om;

@Autowired
private UserMapper userMapper;

private JwtRequestPostProcessor token;

private User testUser;


@BeforeEach
public void setUp() {
mockMvc = MockMvcBuilders.webAppContextSetup(wac)
Expand All @@ -73,12 +84,21 @@ public void setUp() {

@Test
public void testIndex() throws Exception {
mockMvc.perform(get("/api/users").with(jwt()))
.andExpect(status().isOk());
var response = mockMvc.perform(get("/api/users").with(jwt()))
.andExpect(status().isOk())
.andReturn()
.getResponse();
var body = response.getContentAsString();

List<UserDTO> userDTOS = om.readValue(body, new TypeReference<>() {});

var actual = userDTOS.stream().map(userMapper::map).toList();
var expected = userRepository.findAll();
Assertions.assertThat(actual).containsExactlyInAnyOrderElementsOf(expected);
}

@Test
void testCreate() throws Exception {
public void testCreate() throws Exception {
var data = Instancio.of(modelGenerator.getUserModel())
.create();

Expand Down Expand Up @@ -108,9 +128,22 @@ public void testUpdate() throws Exception {
.content(om.writeValueAsString(data));

mockMvc.perform(request)
.andExpect(status().isOk());
.andExpect(status().isOk());

var user = userRepository.findById(testUser.getId()).get();
assertThat(user.getFirstName()).isEqualTo(("Mike"));
}

@Test
public void testShow() throws Exception {
var request = get("/api/users/" + testUser.getId()).with(jwt());
var result = mockMvc.perform(request)
.andExpect(status().isOk())
.andReturn();
var body = result.getResponse().getContentAsString();
assertThatJson(body).and(
v -> v.node("username").isEqualTo(testUser.getEmail()),
v -> v.node("firstName").isEqualTo(testUser.getFirstName()),
v -> v.node("lastName").isEqualTo(testUser.getLastName()));
}
}