Skip to content

Commit

Permalink
fix: snowflake id 생성 전략 사용하는 도메인의 test 코드 수정
Browse files Browse the repository at this point in the history
  • Loading branch information
CEO-Nick committed Jul 25, 2024
1 parent c2f80ac commit cdab9f1
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 39 deletions.
Original file line number Diff line number Diff line change
@@ -1,29 +1,34 @@
package com.outstagram.outstagram.service;

import static com.outstagram.outstagram.common.constant.PageConst.PAGE_SIZE;
import static com.outstagram.outstagram.dto.AlarmType.LIKE;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.outstagram.outstagram.dto.ImageDTO;
import com.outstagram.outstagram.dto.NotificationDTO;
import com.outstagram.outstagram.dto.NotificationDetailsDTO;
import com.outstagram.outstagram.dto.UserDTO;
import com.outstagram.outstagram.exception.ApiException;
import com.outstagram.outstagram.exception.errorcode.ErrorCode;
import com.outstagram.outstagram.mapper.NotificationMapper;
import com.outstagram.outstagram.util.Snowflake;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

import static com.outstagram.outstagram.common.constant.PageConst.PAGE_SIZE;
import static com.outstagram.outstagram.dto.AlarmType.LIKE;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class NotificationServiceTest {
@InjectMocks
Expand All @@ -38,23 +43,28 @@ class NotificationServiceTest {
@Mock
private ImageService imageService;

@Mock
private Snowflake snowflake;

@Test
public void testInsertNotification_Success() {
// given
long notiId = snowflake.nextId(1L);
NotificationDTO notification = NotificationDTO.builder()
.fromId(1L)
.toId(2L)
.targetId(1L)
.alarmType(LIKE)
.isRead(false)
.createDate(LocalDateTime.now())
.updateDate(LocalDateTime.now())
.build();
.id(notiId)
.fromId(1L)
.toId(2L)
.targetId(1L)
.alarmType(LIKE)
.isRead(false)
.createDate(LocalDateTime.now())
.updateDate(LocalDateTime.now())
.build();

// when
notificationService.insertNotification(notification);
when(notificationMapper.findByIdAndUserId(1L, 2L)).thenReturn(notification);
NotificationDTO findNotification = notificationMapper.findByIdAndUserId(1L, 2L);
when(notificationMapper.findByIdAndUserId(notiId, 2L)).thenReturn(notification);
NotificationDTO findNotification = notificationMapper.findByIdAndUserId(notiId, 2L);

// then
assertEquals(findNotification.getFromId(), notification.getFromId());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,27 +1,43 @@
package com.outstagram.outstagram.service;

import static com.outstagram.outstagram.common.constant.PageConst.PAGE_SIZE;
import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.BDDMockito.given;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.anyList;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

import com.outstagram.outstagram.controller.request.CreatePostReq;
import com.outstagram.outstagram.controller.request.EditPostReq;
import com.outstagram.outstagram.dto.*;
import com.outstagram.outstagram.dto.ImageDTO;
import com.outstagram.outstagram.dto.PostDTO;
import com.outstagram.outstagram.dto.PostDetailsDTO;
import com.outstagram.outstagram.dto.PostImageDTO;
import com.outstagram.outstagram.dto.UserDTO;
import com.outstagram.outstagram.exception.ApiException;
import com.outstagram.outstagram.exception.errorcode.ErrorCode;
import com.outstagram.outstagram.kafka.producer.FeedUpdateProducer;
import com.outstagram.outstagram.kafka.producer.PostProducer;
import com.outstagram.outstagram.mapper.PostMapper;
import com.outstagram.outstagram.util.Snowflake;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static com.outstagram.outstagram.common.constant.PageConst.PAGE_SIZE;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.Mockito.*;

@ExtendWith(MockitoExtension.class)
class PostServiceTest {

Expand All @@ -40,6 +56,15 @@ class PostServiceTest {
@Mock
private LikeService likeService;

@Mock
private Snowflake snowflake;

@Mock
private FeedUpdateProducer feedUpdateProducer;

@Mock
private PostProducer postProducer;

@Test
public void testInsertPost_Success() {
// given
Expand All @@ -48,21 +73,17 @@ public void testInsertPost_Success() {
.contents("게시물 내용입니다.")
.imgFiles(List.of(mockFile))
.build();
Long userId = 1L;
long userId = 1L;
long postId = snowflake.nextId(1);
given(snowflake.nextId(1)).willReturn(postId);

// when
// postMapper의 insertPost 메서드가 호출될 때, postId를 1L로 세팅
when(postMapper.insertPost(any(PostDTO.class))).thenAnswer(invocation -> {
PostDTO p = invocation.getArgument(0);
p.setId(1L);
return null;
});
postService.insertPost(createPostReq, userId);

// then
// PostDTO 타입의 어떤 객체든지 상관 없이 insertPost가 정확히 1번 호출되었는지 검증해주는 코드
verify(postMapper).insertPost(any(PostDTO.class));
// verify(imageService).saveImages(anyList(), eq(1L));
verify(imageService).saveImages(anyList(), eq(postId));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@

import com.outstagram.outstagram.dto.UserDTO;
import com.outstagram.outstagram.exception.ApiException;
import com.outstagram.outstagram.kafka.producer.UserProducer;
import com.outstagram.outstagram.mapper.UserMapper;
import com.outstagram.outstagram.util.SHA256Util;
import com.outstagram.outstagram.util.Snowflake;
import java.time.LocalDateTime;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
Expand All @@ -25,6 +27,12 @@ public class UserServiceTest {
@Mock
private UserMapper userMapper;

@Mock
private Snowflake snowflake;

@Mock
private UserProducer userProducer;

@InjectMocks
private UserService userService;

Expand All @@ -44,6 +52,11 @@ public void testInsertUser_Success() {
// given
given(userMapper.countByEmail(user.getEmail())).willReturn(0);
given(userMapper.countByNickname(user.getNickname())).willReturn(0);
if (user.getCreateDate().getSecond() % 2 == 0) {
given(snowflake.nextId(0)).willReturn(123456L);
} else {
given(snowflake.nextId(1)).willReturn(123457L);
}
given(userMapper.insertUser(user)).willReturn(1);

// when
Expand Down

0 comments on commit cdab9f1

Please sign in to comment.