create & update post test code

This commit is contained in:
jinho jeong
2022-07-12 16:54:48 +09:00
parent 03db40f2b5
commit c59421779d
4 changed files with 78 additions and 7 deletions

View File

@@ -38,7 +38,7 @@ public class Post implements Serializable {
@Access(AccessType.PROPERTY) @Access(AccessType.PROPERTY)
@ManyToOne(fetch = FetchType.EAGER, optional = false) @ManyToOne(fetch = FetchType.EAGER, optional = false)
private UserEntity writer; private UserEntity writer;
public Post() {} public Post() {}
public Post(Long id, LocalDateTime createdAt, LocalDateTime expiredAt, String content, UserEntity writer){ public Post(Long id, LocalDateTime createdAt, LocalDateTime expiredAt, String content, UserEntity writer){

View File

@@ -36,8 +36,9 @@ public class PostCommnadServiceImpl implements PostCommandService{
if(userEntity == null){ if(userEntity == null){
throw new ExpiredSessionException("만료된 세션"); throw new ExpiredSessionException("만료된 세션");
} }
LocalDateTime createdAt = LocalDateTime.now(); LocalDateTime createdAt = LocalDateTime.now();
Post postEntity = postCommandRepository.save( Post postEntity = postCommandRepository.save(
Post.builder() Post.builder()
.content(post.getContent()) .content(post.getContent())
@@ -56,7 +57,7 @@ public class PostCommnadServiceImpl implements PostCommandService{
postEntity.getWriter().getUsername() postEntity.getWriter().getUsername()
) )
); );
log.info("user: " + userEntity.toString() + " create " + postEntity.toString()); log.info("user: " + userEntity.toString() + " create " + postEntity.toString());
return postEntity; return postEntity;
} }

View File

@@ -1,6 +1,7 @@
package com.example.oneul.infra.dto; package com.example.oneul.infra.dto;
import java.time.LocalDateTime; import java.time.LocalDateTime;
import java.util.Objects;
public class PostMessage { public class PostMessage {
private String type; private String type;
@@ -59,6 +60,23 @@ public class PostMessage {
this.wirter = writer; this.wirter = writer;
} }
@Override
public boolean equals(Object object){
if(this == object) {
return true;
}
if(object == null || getClass() != object.getClass()){
return false;
}
PostMessage that = (PostMessage)object;
return this.id == that.id;
}
@Override
public int hashCode() {
return Objects.hash(this.id);
}
@Override @Override
public String toString(){ public String toString(){
return "PostMessage[" return "PostMessage["

View File

@@ -1,13 +1,18 @@
package com.example.oneul.service; package com.example.oneul.service;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.BDDMockito.given; import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.verify;
import java.time.LocalDateTime;
import java.util.Optional; import java.util.Optional;
import org.junit.jupiter.api.BeforeEach; import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith; import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock; import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension; import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.mock.web.MockHttpSession; import org.springframework.mock.web.MockHttpSession;
@@ -18,6 +23,8 @@ import com.example.oneul.domain.post.dao.command.PostCommandRepository;
import com.example.oneul.domain.post.domain.Post; import com.example.oneul.domain.post.domain.Post;
import com.example.oneul.domain.post.service.command.PostCommandService; import com.example.oneul.domain.post.service.command.PostCommandService;
import com.example.oneul.domain.post.service.command.PostCommnadServiceImpl; import com.example.oneul.domain.post.service.command.PostCommnadServiceImpl;
import com.example.oneul.domain.user.domain.UserEntity;
import com.example.oneul.infra.dto.PostMessage;
import com.example.oneul.infra.kafka.KafkaPublisher; import com.example.oneul.infra.kafka.KafkaPublisher;
@ActiveProfiles("test") @ActiveProfiles("test")
@@ -27,31 +34,76 @@ public class PostCommandSerivceTest {
@Mock private PostCommandRepository postCommandRepository; @Mock private PostCommandRepository postCommandRepository;
@Mock private KafkaPublisher kafkaPublisher; @Mock private KafkaPublisher kafkaPublisher;
protected MockHttpSession httpSession; protected MockHttpSession httpSession;
@Mock private UserEntity mockWriter;
@BeforeEach @BeforeEach
public void setUp() throws Exception { public void setUp() throws Exception {
postCommandService = new PostCommnadServiceImpl(postCommandRepository, kafkaPublisher); postCommandService = new PostCommnadServiceImpl(postCommandRepository, kafkaPublisher);
httpSession = new MockHttpSession();
httpSession.setAttribute("user", mockWriter);
} }
@Test @Test
public void createPostTest() throws Exception { public void createPostTest() throws Exception {
// given // given
Long mockPostId = 1L; Long mockPostId = 1L;
Post post = mockPost(mockPostId); Post post = mockPostDTO();
ReflectionTestUtils.setField(post, "id", mockPostId); ReflectionTestUtils.setField(post, "id", mockPostId);
// mocking // mocking
given(postCommandRepository.save(post)).willReturn(post); Post createdMockPost = createdMockPost(mockWriter, post.getContent());
given(postCommandRepository.findById(mockPostId)).willReturn(Optional.ofNullable(post)); ReflectionTestUtils.setField(createdMockPost, "id", mockPostId);
given(postCommandRepository.save(any())).willReturn(createdMockPost);
// when // when
Post createdPost = postCommandService.createPost(post, httpSession); Post createdPost = postCommandService.createPost(post, httpSession);
// then // then
assertEquals(post.getContent(), createdPost.getContent()); assertEquals(post.getContent(), createdPost.getContent());
ArgumentCaptor<PostMessage> captor = ArgumentCaptor.forClass(PostMessage.class);
verify(kafkaPublisher).sendMessage(eq("post"), captor.capture());
PostMessage postMessage = createPostMessage("INSERT", createdPost);
assertEquals(postMessage, captor.getValue());
} }
private Post mockPost(Long id) { @Test
public void updatePostTest() throws Exception {
// given
Long mockPostId = 1L;
Post post = createdMockPost(mockWriter, "unmodified content");
ReflectionTestUtils.setField(post, "id", mockPostId);
// mocking
given(postCommandRepository.findByIdAndWriter(eq(mockPostId), any()))
.willReturn(Optional.ofNullable(post));
ReflectionTestUtils.setField(post, "content", "modified content");
given(postCommandRepository.save(post)).willReturn(post);
// when
Post updatedPost = postCommandService.updatePost(mockPostId, post, httpSession);
// then
assertEquals(post.getContent(), updatedPost.getContent());
ArgumentCaptor<PostMessage> captor = ArgumentCaptor.forClass(PostMessage.class);
verify(kafkaPublisher).sendMessage(eq("post"), captor.capture());
PostMessage postMessage = createPostMessage("UPDATE", updatedPost);
assertEquals(postMessage, captor.getValue());
}
private PostMessage createPostMessage(String type, Post post) {
return new PostMessage(type, post.getId(), post.getCreatedAt(), post.getContent(), null);
}
private Post createdMockPost(UserEntity writer, String content) {
LocalDateTime createdAt = LocalDateTime.now();
return Post.builder().content(content)
.createdAt(createdAt)
.expiredAt(createdAt.plusHours(24))
.writer(writer)
.build();
}
private Post mockPostDTO() {
return Post.builder().content("mocking post") return Post.builder().content("mocking post")
.build(); .build();
} }