From 3cc949238ba66eb1778799b8e77889a6e1669169 Mon Sep 17 00:00:00 2001 From: geneaky Date: Thu, 19 Sep 2024 00:36:12 +0900 Subject: [PATCH] =?UTF-8?q?test:=20inftrastructure=20=ED=8C=A8=ED=82=A4?= =?UTF-8?q?=EC=A7=80=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../chat/api/ChatControllerMessagingTest.java | 368 +++++++++--------- .../service/ChatServiceConcurrentTest.java | 2 +- .../domain/chat/service/ChatServiceTest.java | 134 +++---- .../chatroom/service/ChatRoomServiceTest.java | 4 +- .../ParticipantServiceConcurrentTest.java | 4 +- .../service/ParticipantServiceTest.java | 2 +- .../domain/user/service/UserServiceTest.java | 4 +- .../rabbitmq/MessagePublisherTest.java | 38 +- .../infrastructure/s3/ImageValidatorTest.java | 96 +++-- .../infrastructure/s3/StorageServiceTest.java | 74 ++-- 10 files changed, 361 insertions(+), 365 deletions(-) diff --git a/src/test/java/toy/bookchat/bookchat/domain/chat/api/ChatControllerMessagingTest.java b/src/test/java/toy/bookchat/bookchat/domain/chat/api/ChatControllerMessagingTest.java index 08cea657..5d4446ae 100644 --- a/src/test/java/toy/bookchat/bookchat/domain/chat/api/ChatControllerMessagingTest.java +++ b/src/test/java/toy/bookchat/bookchat/domain/chat/api/ChatControllerMessagingTest.java @@ -43,193 +43,193 @@ import toy.bookchat.bookchat.domain.participant.service.ParticipantValidator; import toy.bookchat.bookchat.domain.user.User; import toy.bookchat.bookchat.domain.user.service.UserReader; -import toy.bookchat.bookchat.infrastructure.broker.message.CommonMessage; -import toy.bookchat.bookchat.infrastructure.push.service.PushService; +import toy.bookchat.bookchat.infrastructure.rabbitmq.message.CommonMessage; +import toy.bookchat.bookchat.infrastructure.fcm.service.PushService; @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT) class ChatControllerMessagingTest extends ControllerTestExtension { - private final WebSocketStompClient webSocketStompClient; - private final BlockingQueue blockingQueue = new LinkedBlockingQueue<>(); - @LocalServerPort - private int port; - private StompSession stompSession; - @MockBean - private PushService pushService; - @MockBean - private ParticipantRepository participantRepository; - @MockBean - private ChatRepository chatRepository; - @MockBean - private ChatReader chatReader; - @MockBean - private ChatAppender chatAppender; - @MockBean - private ChatRoomReader chatRoomReader; - @MockBean - private ParticipantValidator participantValidator; - @MockBean - private UserReader userReader; - - ChatControllerMessagingTest() throws Exception { - this.webSocketStompClient = new WebSocketStompClient(new StandardWebSocketClient()); - ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); - taskScheduler.afterPropertiesSet(); - this.webSocketStompClient.setTaskScheduler(taskScheduler); - this.webSocketStompClient.setMessageConverter(new MappingJackson2MessageConverter()); - } - - private StompSession stompSession() - throws InterruptedException, ExecutionException, TimeoutException { - StompHeaders stompHeaders = new StompHeaders(); - stompHeaders.set(AUTHORIZATION, getTestToken()); - ListenableFuture connect = this.webSocketStompClient.connect( - getStompConnectionEndPointUrl(), new WebSocketHttpHeaders(), stompHeaders, - new StompSessionHandlerAdapter() { - }); - StompSession stompSession = connect.get(60, TimeUnit.SECONDS); - stompSession.setAutoReceipt(true); - return stompSession; - } - - private String getStompConnectionEndPointUrl() { - return "ws://localhost:" + port + "/stomp-connection"; - } - - private StompSessionHandlerAdapter subscribeFrameSessionHandler(CountDownLatch latch) { - return new StompSessionHandlerAdapter() { - - @Override - public Type getPayloadType(StompHeaders headers) { - return CommonMessage.class; - } - - @Override - public void handleFrame(StompHeaders headers, Object payload) { - CommonMessage commonMessage = (CommonMessage) payload; - blockingQueue.offer(commonMessage); - latch.countDown(); - } - }; - } - - private StompHeaders stompSubscribeHeaders(String destination) { - StompHeaders subscribeHeader = new StompHeaders(); - subscribeHeader.set(AUTHORIZATION, getTestToken()); - subscribeHeader.setDestination(destination); - return subscribeHeader; - } - - private StompHeaders stompSendHeaders(String destination) { - StompHeaders stompHeaders = new StompHeaders(); - stompHeaders.set(AUTHORIZATION, getTestToken()); - stompHeaders.setDestination(destination); - return stompHeaders; - } - - @BeforeEach - public void initNewSession() throws Exception { - this.stompSession = stompSession(); - this.blockingQueue.clear(); - } - - @Test - void 서로다른_세션_메시지_송신_수신_성공() throws Exception { - StompHeaders subscribeHeader = stompSubscribeHeaders("/topic/heho"); - StompHeaders sendHeader = stompSendHeaders("/subscriptions/send/chatrooms/1"); - - User user = User.builder() - .id(1L) - .build(); - given(userReader.readUser(any())).willReturn(user); - - ChatRoom chatRoom = ChatRoom.builder() - .id(1L) - .roomSize(10) - .sid("heho") - .build(); - given(chatRoomReader.readChatRoom(any())).willReturn(chatRoom); - - Sender sender = Sender.builder() - .id(user.getId()) - .build(); - Chat chat1 = Chat.builder() - .id(1L) - .sender(sender) - .message("test") - .chatRoomId(chatRoom.getId()) - .dispatchTime(LocalDateTime.now()) - .build(); - Chat chat2 = Chat.builder() - .id(2L) - .sender(sender) - .message("test test") - .chatRoomId(chatRoom.getId()) - .dispatchTime(LocalDateTime.now()) - .build(); - Chat chat3 = Chat.builder() - .id(3L) - .sender(sender) - .message("test test test") - .chatRoomId(chatRoom.getId()) - .dispatchTime(LocalDateTime.now()) - .build(); - given(chatAppender.append(any(), any(), any())).willReturn(chat1, chat2, chat3); - - ParticipantEntity participantEntity = ParticipantEntity.builder() - .chatRoomId(chatRoom.getId()) - .userId(getUserId()) - .id(1L) - .build(); - when(participantRepository.findByUserIdAndChatRoomId(any(), any())).thenReturn( - Optional.of(participantEntity)); - given(participantRepository.findByUserIdAndChatRoomSid(any(), any())).willReturn(Optional.of(participantEntity)); - - CommonMessage dto1 = CommonMessage.builder() - .senderId(getUserId()) - .chatId(chat1.getId()) - .receiptId(1) - .dispatchTime(chat1.getDispatchTime().toString()) - .message(chat1.getMessage()) - .build(); - - CommonMessage dto2 = CommonMessage.builder() - .senderId(getUserId()) - .chatId(chat2.getId()) - .receiptId(2) - .dispatchTime(chat2.getDispatchTime().toString()) - .message(chat2.getMessage()) - .build(); - - CommonMessage dto3 = CommonMessage.builder() - .senderId(getUserId()) - .chatId(chat3.getId()) - .receiptId(3) - .dispatchTime(chat3.getDispatchTime().toString()) - .message(chat3.getMessage()) - .build(); - - Runnable[] chatActions = { - () -> this.stompSession.send(sendHeader, dto1), - () -> this.stompSession.send(sendHeader, dto2), - () -> this.stompSession.send(sendHeader, dto3) - }; - - CountDownLatch chatAttemptCountLatch = new CountDownLatch(chatActions.length); - - StompSession stompSession2 = stompSession(); - stompSession2.subscribe(subscribeHeader, - subscribeFrameSessionHandler(chatAttemptCountLatch)) - .addReceiptTask(() -> doChat(chatActions)); - - chatAttemptCountLatch.await(); - - assertThat(blockingQueue).containsExactlyInAnyOrder(dto1, dto2, dto3); - } - - private void doChat(Runnable... chatActions) { - for (Runnable chatAction : chatActions) { - chatAction.run(); - } + private final WebSocketStompClient webSocketStompClient; + private final BlockingQueue blockingQueue = new LinkedBlockingQueue<>(); + @LocalServerPort + private int port; + private StompSession stompSession; + @MockBean + private PushService pushService; + @MockBean + private ParticipantRepository participantRepository; + @MockBean + private ChatRepository chatRepository; + @MockBean + private ChatReader chatReader; + @MockBean + private ChatAppender chatAppender; + @MockBean + private ChatRoomReader chatRoomReader; + @MockBean + private ParticipantValidator participantValidator; + @MockBean + private UserReader userReader; + + ChatControllerMessagingTest() throws Exception { + this.webSocketStompClient = new WebSocketStompClient(new StandardWebSocketClient()); + ThreadPoolTaskScheduler taskScheduler = new ThreadPoolTaskScheduler(); + taskScheduler.afterPropertiesSet(); + this.webSocketStompClient.setTaskScheduler(taskScheduler); + this.webSocketStompClient.setMessageConverter(new MappingJackson2MessageConverter()); + } + + private StompSession stompSession() + throws InterruptedException, ExecutionException, TimeoutException { + StompHeaders stompHeaders = new StompHeaders(); + stompHeaders.set(AUTHORIZATION, getTestToken()); + ListenableFuture connect = this.webSocketStompClient.connect( + getStompConnectionEndPointUrl(), new WebSocketHttpHeaders(), stompHeaders, + new StompSessionHandlerAdapter() { + }); + StompSession stompSession = connect.get(60, TimeUnit.SECONDS); + stompSession.setAutoReceipt(true); + return stompSession; + } + + private String getStompConnectionEndPointUrl() { + return "ws://localhost:" + port + "/stomp-connection"; + } + + private StompSessionHandlerAdapter subscribeFrameSessionHandler(CountDownLatch latch) { + return new StompSessionHandlerAdapter() { + + @Override + public Type getPayloadType(StompHeaders headers) { + return CommonMessage.class; + } + + @Override + public void handleFrame(StompHeaders headers, Object payload) { + CommonMessage commonMessage = (CommonMessage) payload; + blockingQueue.offer(commonMessage); + latch.countDown(); + } + }; + } + + private StompHeaders stompSubscribeHeaders(String destination) { + StompHeaders subscribeHeader = new StompHeaders(); + subscribeHeader.set(AUTHORIZATION, getTestToken()); + subscribeHeader.setDestination(destination); + return subscribeHeader; + } + + private StompHeaders stompSendHeaders(String destination) { + StompHeaders stompHeaders = new StompHeaders(); + stompHeaders.set(AUTHORIZATION, getTestToken()); + stompHeaders.setDestination(destination); + return stompHeaders; + } + + @BeforeEach + public void initNewSession() throws Exception { + this.stompSession = stompSession(); + this.blockingQueue.clear(); + } + + @Test + void 서로다른_세션_메시지_송신_수신_성공() throws Exception { + StompHeaders subscribeHeader = stompSubscribeHeaders("/topic/heho"); + StompHeaders sendHeader = stompSendHeaders("/subscriptions/send/chatrooms/1"); + + User user = User.builder() + .id(1L) + .build(); + given(userReader.readUser(any())).willReturn(user); + + ChatRoom chatRoom = ChatRoom.builder() + .id(1L) + .roomSize(10) + .sid("heho") + .build(); + given(chatRoomReader.readChatRoom(any())).willReturn(chatRoom); + + Sender sender = Sender.builder() + .id(user.getId()) + .build(); + Chat chat1 = Chat.builder() + .id(1L) + .sender(sender) + .message("test") + .chatRoomId(chatRoom.getId()) + .dispatchTime(LocalDateTime.now()) + .build(); + Chat chat2 = Chat.builder() + .id(2L) + .sender(sender) + .message("test test") + .chatRoomId(chatRoom.getId()) + .dispatchTime(LocalDateTime.now()) + .build(); + Chat chat3 = Chat.builder() + .id(3L) + .sender(sender) + .message("test test test") + .chatRoomId(chatRoom.getId()) + .dispatchTime(LocalDateTime.now()) + .build(); + given(chatAppender.append(any(), any(), any())).willReturn(chat1, chat2, chat3); + + ParticipantEntity participantEntity = ParticipantEntity.builder() + .chatRoomId(chatRoom.getId()) + .userId(getUserId()) + .id(1L) + .build(); + when(participantRepository.findByUserIdAndChatRoomId(any(), any())).thenReturn( + Optional.of(participantEntity)); + given(participantRepository.findByUserIdAndChatRoomSid(any(), any())).willReturn(Optional.of(participantEntity)); + + CommonMessage dto1 = CommonMessage.builder() + .senderId(getUserId()) + .chatId(chat1.getId()) + .receiptId(1) + .dispatchTime(chat1.getDispatchTime().toString()) + .message(chat1.getMessage()) + .build(); + + CommonMessage dto2 = CommonMessage.builder() + .senderId(getUserId()) + .chatId(chat2.getId()) + .receiptId(2) + .dispatchTime(chat2.getDispatchTime().toString()) + .message(chat2.getMessage()) + .build(); + + CommonMessage dto3 = CommonMessage.builder() + .senderId(getUserId()) + .chatId(chat3.getId()) + .receiptId(3) + .dispatchTime(chat3.getDispatchTime().toString()) + .message(chat3.getMessage()) + .build(); + + Runnable[] chatActions = { + () -> this.stompSession.send(sendHeader, dto1), + () -> this.stompSession.send(sendHeader, dto2), + () -> this.stompSession.send(sendHeader, dto3) + }; + + CountDownLatch chatAttemptCountLatch = new CountDownLatch(chatActions.length); + + StompSession stompSession2 = stompSession(); + stompSession2.subscribe(subscribeHeader, + subscribeFrameSessionHandler(chatAttemptCountLatch)) + .addReceiptTask(() -> doChat(chatActions)); + + chatAttemptCountLatch.await(); + + assertThat(blockingQueue).containsExactlyInAnyOrder(dto1, dto2, dto3); + } + + private void doChat(Runnable... chatActions) { + for (Runnable chatAction : chatActions) { + chatAction.run(); } + } } \ No newline at end of file diff --git a/src/test/java/toy/bookchat/bookchat/domain/chat/service/ChatServiceConcurrentTest.java b/src/test/java/toy/bookchat/bookchat/domain/chat/service/ChatServiceConcurrentTest.java index 7d8ab8bc..3e62f3fe 100644 --- a/src/test/java/toy/bookchat/bookchat/domain/chat/service/ChatServiceConcurrentTest.java +++ b/src/test/java/toy/bookchat/bookchat/domain/chat/service/ChatServiceConcurrentTest.java @@ -26,7 +26,7 @@ import toy.bookchat.bookchat.db_module.user.UserEntity; import toy.bookchat.bookchat.db_module.user.repository.UserRepository; import toy.bookchat.bookchat.domain.chatroom.service.ChatRoomService; -import toy.bookchat.bookchat.infrastructure.broker.MessagePublisher; +import toy.bookchat.bookchat.infrastructure.rabbitmq.MessagePublisher; import toy.bookchat.bookchat.security.oauth.OAuth2Provider; @SpringBootTest diff --git a/src/test/java/toy/bookchat/bookchat/domain/chat/service/ChatServiceTest.java b/src/test/java/toy/bookchat/bookchat/domain/chat/service/ChatServiceTest.java index 3ef0ab6d..05cea5f5 100644 --- a/src/test/java/toy/bookchat/bookchat/domain/chat/service/ChatServiceTest.java +++ b/src/test/java/toy/bookchat/bookchat/domain/chat/service/ChatServiceTest.java @@ -23,75 +23,75 @@ import toy.bookchat.bookchat.domain.device.service.DeviceReader; import toy.bookchat.bookchat.domain.participant.service.ParticipantValidator; import toy.bookchat.bookchat.domain.user.service.UserReader; -import toy.bookchat.bookchat.infrastructure.broker.MessagePublisher; -import toy.bookchat.bookchat.infrastructure.broker.message.CommonMessage; -import toy.bookchat.bookchat.infrastructure.push.service.PushService; +import toy.bookchat.bookchat.infrastructure.rabbitmq.MessagePublisher; +import toy.bookchat.bookchat.infrastructure.rabbitmq.message.CommonMessage; +import toy.bookchat.bookchat.infrastructure.fcm.service.PushService; @ExtendWith(MockitoExtension.class) class ChatServiceTest { - @Mock - private ChatReader chatReader; - @Mock - private ChatAppender chatAppender; - @Mock - private UserReader userReader; - @Mock - private ChatRoomReader chatRoomReader; - @Mock - private DeviceReader deviceReader; - @Mock - private ParticipantValidator participantValidator; - @Mock - private PushService pushService; - @Mock - private MessagePublisher messagingTemplate; - @InjectMocks - private ChatService chatService; - - @Test - void 메시지_전송_성공() throws Exception { - ChatRoom chatRoom = ChatRoom.builder() - .sid("testRoomSid") - .build(); - given(chatRoomReader.readChatRoom(any())).willReturn(chatRoom); - - Sender sender = Sender.builder().id(1L).build(); - Chat chat = Chat.builder() - .id(1L) - .sender(sender) - .chatRoomId(chatRoom.getId()) - .dispatchTime(LocalDateTime.now()) - .build(); - given(chatAppender.append(any(), any(), any())).willReturn(chat); - - Message message = Message.of(1, chat.getMessage()); - - chatService.sendMessage(1L, 1L, message); - - verify(messagingTemplate).sendCommonMessage(anyString(), any(CommonMessage.class)); - } - - @Test - void 채팅_내역_조회_성공() throws Exception { - chatService.getChatRoomChats(1L, null, mock(Pageable.class), 1L); - - verify(chatReader).readSlicedChat(any(), any(), any(), any()); - } - - @Test - void 채팅_내역_조회시_공지채팅과_일반채팅_구분하여_응답생성_성공() throws Exception { - PageRequest pageRequest = PageRequest.of(0, 4, Sort.by("id").descending()); - - chatService.getChatRoomChats(1L, null, pageRequest, 1L); - - verify(chatReader).readSlicedChat(any(), any(), any(), any()); - } - - @Test - void 채팅_채팅방_발신자정보를_조회_성공() throws Exception { - chatService.getChatDetail(1L, 1L); - - verify(chatReader).readChat(any(), any()); - } + @Mock + private ChatReader chatReader; + @Mock + private ChatAppender chatAppender; + @Mock + private UserReader userReader; + @Mock + private ChatRoomReader chatRoomReader; + @Mock + private DeviceReader deviceReader; + @Mock + private ParticipantValidator participantValidator; + @Mock + private PushService pushService; + @Mock + private MessagePublisher messagingTemplate; + @InjectMocks + private ChatService chatService; + + @Test + void 메시지_전송_성공() throws Exception { + ChatRoom chatRoom = ChatRoom.builder() + .sid("testRoomSid") + .build(); + given(chatRoomReader.readChatRoom(any())).willReturn(chatRoom); + + Sender sender = Sender.builder().id(1L).build(); + Chat chat = Chat.builder() + .id(1L) + .sender(sender) + .chatRoomId(chatRoom.getId()) + .dispatchTime(LocalDateTime.now()) + .build(); + given(chatAppender.append(any(), any(), any())).willReturn(chat); + + Message message = Message.of(1, chat.getMessage()); + + chatService.sendMessage(1L, 1L, message); + + verify(messagingTemplate).sendCommonMessage(anyString(), any(CommonMessage.class)); + } + + @Test + void 채팅_내역_조회_성공() throws Exception { + chatService.getChatRoomChats(1L, null, mock(Pageable.class), 1L); + + verify(chatReader).readSlicedChat(any(), any(), any(), any()); + } + + @Test + void 채팅_내역_조회시_공지채팅과_일반채팅_구분하여_응답생성_성공() throws Exception { + PageRequest pageRequest = PageRequest.of(0, 4, Sort.by("id").descending()); + + chatService.getChatRoomChats(1L, null, pageRequest, 1L); + + verify(chatReader).readSlicedChat(any(), any(), any(), any()); + } + + @Test + void 채팅_채팅방_발신자정보를_조회_성공() throws Exception { + chatService.getChatDetail(1L, 1L); + + verify(chatReader).readChat(any(), any()); + } } \ No newline at end of file diff --git a/src/test/java/toy/bookchat/bookchat/domain/chatroom/service/ChatRoomServiceTest.java b/src/test/java/toy/bookchat/bookchat/domain/chatroom/service/ChatRoomServiceTest.java index 24134c0a..15e04c01 100644 --- a/src/test/java/toy/bookchat/bookchat/domain/chatroom/service/ChatRoomServiceTest.java +++ b/src/test/java/toy/bookchat/bookchat/domain/chatroom/service/ChatRoomServiceTest.java @@ -43,10 +43,10 @@ import toy.bookchat.bookchat.domain.participant.service.ParticipantCleaner; import toy.bookchat.bookchat.domain.participant.service.ParticipantReader; import toy.bookchat.bookchat.domain.participant.service.ParticipantValidator; -import toy.bookchat.bookchat.domain.storage.ChatRoomStorageService; +import toy.bookchat.bookchat.infrastructure.s3.ChatRoomStorageService; import toy.bookchat.bookchat.domain.user.User; import toy.bookchat.bookchat.domain.user.service.UserReader; -import toy.bookchat.bookchat.infrastructure.broker.MessagePublisher; +import toy.bookchat.bookchat.infrastructure.rabbitmq.MessagePublisher; @ExtendWith(MockitoExtension.class) class ChatRoomServiceTest { diff --git a/src/test/java/toy/bookchat/bookchat/domain/participant/service/ParticipantServiceConcurrentTest.java b/src/test/java/toy/bookchat/bookchat/domain/participant/service/ParticipantServiceConcurrentTest.java index 4598fe4a..09695ac7 100644 --- a/src/test/java/toy/bookchat/bookchat/domain/participant/service/ParticipantServiceConcurrentTest.java +++ b/src/test/java/toy/bookchat/bookchat/domain/participant/service/ParticipantServiceConcurrentTest.java @@ -27,8 +27,8 @@ import toy.bookchat.bookchat.db_module.user.UserEntity; import toy.bookchat.bookchat.db_module.user.repository.UserRepository; import toy.bookchat.bookchat.domain.participant.ParticipantStatus; -import toy.bookchat.bookchat.infrastructure.broker.MessagePublisher; -import toy.bookchat.bookchat.infrastructure.push.service.PushService; +import toy.bookchat.bookchat.infrastructure.rabbitmq.MessagePublisher; +import toy.bookchat.bookchat.infrastructure.fcm.service.PushService; import toy.bookchat.bookchat.security.oauth.OAuth2Provider; @Slf4j diff --git a/src/test/java/toy/bookchat/bookchat/domain/participant/service/ParticipantServiceTest.java b/src/test/java/toy/bookchat/bookchat/domain/participant/service/ParticipantServiceTest.java index e4f0b97b..e82d6456 100644 --- a/src/test/java/toy/bookchat/bookchat/domain/participant/service/ParticipantServiceTest.java +++ b/src/test/java/toy/bookchat/bookchat/domain/participant/service/ParticipantServiceTest.java @@ -25,7 +25,7 @@ import toy.bookchat.bookchat.domain.participant.Participant; import toy.bookchat.bookchat.domain.participant.ParticipantAdmin; import toy.bookchat.bookchat.domain.participant.ParticipantWithChatRoom; -import toy.bookchat.bookchat.infrastructure.broker.MessagePublisher; +import toy.bookchat.bookchat.infrastructure.rabbitmq.MessagePublisher; @ExtendWith(MockitoExtension.class) class ParticipantServiceTest { diff --git a/src/test/java/toy/bookchat/bookchat/domain/user/service/UserServiceTest.java b/src/test/java/toy/bookchat/bookchat/domain/user/service/UserServiceTest.java index 53888666..c4f4e4a9 100644 --- a/src/test/java/toy/bookchat/bookchat/domain/user/service/UserServiceTest.java +++ b/src/test/java/toy/bookchat/bookchat/domain/user/service/UserServiceTest.java @@ -25,7 +25,7 @@ import toy.bookchat.bookchat.db_module.user.UserEntity; import toy.bookchat.bookchat.db_module.user.repository.UserRepository; import toy.bookchat.bookchat.support.Status; -import toy.bookchat.bookchat.domain.storage.StorageService; +import toy.bookchat.bookchat.infrastructure.s3.StorageService; import toy.bookchat.bookchat.domain.user.UserProfile; import toy.bookchat.bookchat.domain.user.api.v1.request.ChangeUserNicknameRequest; import toy.bookchat.bookchat.domain.user.api.v1.request.UserSignInRequest; @@ -33,7 +33,7 @@ import toy.bookchat.bookchat.domain.user.api.v1.response.MemberProfileResponse; import toy.bookchat.bookchat.exception.badrequest.user.UserAlreadySignUpException; import toy.bookchat.bookchat.exception.conflict.device.DeviceAlreadyRegisteredException; -import toy.bookchat.bookchat.infrastructure.push.service.PushService; +import toy.bookchat.bookchat.infrastructure.fcm.service.PushService; @ExtendWith(MockitoExtension.class) class UserServiceTest { diff --git a/src/test/java/toy/bookchat/bookchat/infrastructure/rabbitmq/MessagePublisherTest.java b/src/test/java/toy/bookchat/bookchat/infrastructure/rabbitmq/MessagePublisherTest.java index e07c095f..9c283689 100644 --- a/src/test/java/toy/bookchat/bookchat/infrastructure/rabbitmq/MessagePublisherTest.java +++ b/src/test/java/toy/bookchat/bookchat/infrastructure/rabbitmq/MessagePublisherTest.java @@ -1,4 +1,4 @@ -package toy.bookchat.bookchat.infrastructure.broker; +package toy.bookchat.bookchat.infrastructure.rabbitmq; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.extension.ExtendWith; @@ -6,8 +6,8 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.messaging.simp.SimpMessagingTemplate; -import toy.bookchat.bookchat.infrastructure.broker.message.CommonMessage; -import toy.bookchat.bookchat.infrastructure.broker.message.NotificationMessage; +import toy.bookchat.bookchat.infrastructure.rabbitmq.message.CommonMessage; +import toy.bookchat.bookchat.infrastructure.rabbitmq.message.NotificationMessage; import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.anyString; @@ -16,25 +16,25 @@ @ExtendWith(MockitoExtension.class) class MessagePublisherTest { - @Mock - private SimpMessagingTemplate messagingTemplate; + @Mock + private SimpMessagingTemplate messagingTemplate; - @InjectMocks - private MessagePublisher messagePublisher; + @InjectMocks + private MessagePublisher messagePublisher; - @Test - void 일반_메시지_전송_성공() throws Exception { - CommonMessage commonMessage = CommonMessage.builder().build(); - messagePublisher.sendCommonMessage("eDISVS1", commonMessage); + @Test + void 일반_메시지_전송_성공() throws Exception { + CommonMessage commonMessage = CommonMessage.builder().build(); + messagePublisher.sendCommonMessage("eDISVS1", commonMessage); - verify(messagingTemplate).convertAndSend(anyString(), any(CommonMessage.class)); - } + verify(messagingTemplate).convertAndSend(anyString(), any(CommonMessage.class)); + } - @Test - void 공지_메시지_전송_성공() throws Exception { - NotificationMessage notificationMessage = NotificationMessage.builder().build(); - messagePublisher.sendNotificationMessage("Zbg2pw5W", notificationMessage); + @Test + void 공지_메시지_전송_성공() throws Exception { + NotificationMessage notificationMessage = NotificationMessage.builder().build(); + messagePublisher.sendNotificationMessage("Zbg2pw5W", notificationMessage); - verify(messagingTemplate).convertAndSend(anyString(), any(NotificationMessage.class)); - } + verify(messagingTemplate).convertAndSend(anyString(), any(NotificationMessage.class)); + } } \ No newline at end of file diff --git a/src/test/java/toy/bookchat/bookchat/infrastructure/s3/ImageValidatorTest.java b/src/test/java/toy/bookchat/bookchat/infrastructure/s3/ImageValidatorTest.java index 9b1ccf62..ed75db7f 100644 --- a/src/test/java/toy/bookchat/bookchat/infrastructure/s3/ImageValidatorTest.java +++ b/src/test/java/toy/bookchat/bookchat/infrastructure/s3/ImageValidatorTest.java @@ -1,4 +1,4 @@ -package toy.bookchat.bookchat.domain.storage; +package toy.bookchat.bookchat.infrastructure.s3; import static org.assertj.core.api.Assertions.assertThatNoException; import static org.assertj.core.api.Assertions.assertThatThrownBy; @@ -13,63 +13,61 @@ import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.mock.web.MockMultipartFile; import org.springframework.web.multipart.MultipartFile; -import toy.bookchat.bookchat.domain.storage.image.ImageReaderAdapter; -import toy.bookchat.bookchat.domain.storage.image.ImageValidator; @ExtendWith(MockitoExtension.class) class ImageValidatorTest { - @Mock - ImageReaderAdapter imageReaderAdapter; - @InjectMocks - ImageValidator imageValidator; + @Mock + ImageReaderAdapter imageReaderAdapter; + @InjectMocks + ImageValidator imageValidator; - @Test - void 이미지_검증시_이미지가_없다면_예외발생() throws Exception { - assertThatThrownBy(() -> { - imageValidator.hasValidImage(null, 200, 200); - }).isInstanceOf(IllegalArgumentException.class); - } + @Test + void 이미지_검증시_이미지가_없다면_예외발생() throws Exception { + assertThatThrownBy(() -> { + imageValidator.hasValidImage(null, 200, 200); + }).isInstanceOf(IllegalArgumentException.class); + } - @Test - void 이미지가_빈_파일일_경우_false반환() throws Exception { - byte[] content = {}; - MockMultipartFile multipartFile = new MockMultipartFile("test", content); - assertThatThrownBy(() -> { - imageValidator.hasValidImage(multipartFile, 200, 200); - }).isInstanceOf(IllegalArgumentException.class); - } + @Test + void 이미지가_빈_파일일_경우_false반환() throws Exception { + byte[] content = {}; + MockMultipartFile multipartFile = new MockMultipartFile("test", content); + assertThatThrownBy(() -> { + imageValidator.hasValidImage(multipartFile, 200, 200); + }).isInstanceOf(IllegalArgumentException.class); + } - @Test - void 지원하지_않는_이미지_타입의_경우_예외발생() throws Exception { - MockMultipartFile multipartFile = new MockMultipartFile("test", "test", "image/jpg", - "TEST".getBytes()); + @Test + void 지원하지_않는_이미지_타입의_경우_예외발생() throws Exception { + MockMultipartFile multipartFile = new MockMultipartFile("test", "test", "image/jpg", + "TEST".getBytes()); - assertThatThrownBy(() -> { - imageValidator.hasValidImage(multipartFile, 200, 200); - }).isInstanceOf(IllegalArgumentException.class); - } + assertThatThrownBy(() -> { + imageValidator.hasValidImage(multipartFile, 200, 200); + }).isInstanceOf(IllegalArgumentException.class); + } - @Test - void 지원하지_않는_이미지_사이즈의_경우_예외발생() throws Exception { - MultipartFile multipartFile = mock(MultipartFile.class); - when(multipartFile.getInputStream()).thenReturn(mock(InputStream.class)); - when(multipartFile.getOriginalFilename()).thenReturn("test.webp"); - when(imageReaderAdapter.getWidth()).thenReturn(5000); + @Test + void 지원하지_않는_이미지_사이즈의_경우_예외발생() throws Exception { + MultipartFile multipartFile = mock(MultipartFile.class); + when(multipartFile.getInputStream()).thenReturn(mock(InputStream.class)); + when(multipartFile.getOriginalFilename()).thenReturn("test.webp"); + when(imageReaderAdapter.getWidth()).thenReturn(5000); - assertThatThrownBy(() -> { - imageValidator.hasValidImage(multipartFile, 200, 200); - }).isInstanceOf(IllegalArgumentException.class); - } + assertThatThrownBy(() -> { + imageValidator.hasValidImage(multipartFile, 200, 200); + }).isInstanceOf(IllegalArgumentException.class); + } - @Test - void 올바른_이미지의_경우_검증통과() throws Exception { - MultipartFile multipartFile = mock(MultipartFile.class); - when(multipartFile.getInputStream()).thenReturn(mock(InputStream.class)); - when(multipartFile.getOriginalFilename()).thenReturn("test.webp"); - when(imageReaderAdapter.getHeight()).thenReturn(200); - when(imageReaderAdapter.getWidth()).thenReturn(200); - imageValidator.hasValidImage(multipartFile, 200, 200); - assertThatNoException(); - } + @Test + void 올바른_이미지의_경우_검증통과() throws Exception { + MultipartFile multipartFile = mock(MultipartFile.class); + when(multipartFile.getInputStream()).thenReturn(mock(InputStream.class)); + when(multipartFile.getOriginalFilename()).thenReturn("test.webp"); + when(imageReaderAdapter.getHeight()).thenReturn(200); + when(imageReaderAdapter.getWidth()).thenReturn(200); + imageValidator.hasValidImage(multipartFile, 200, 200); + assertThatNoException(); + } } \ No newline at end of file diff --git a/src/test/java/toy/bookchat/bookchat/infrastructure/s3/StorageServiceTest.java b/src/test/java/toy/bookchat/bookchat/infrastructure/s3/StorageServiceTest.java index cee772e4..71844c0c 100644 --- a/src/test/java/toy/bookchat/bookchat/infrastructure/s3/StorageServiceTest.java +++ b/src/test/java/toy/bookchat/bookchat/infrastructure/s3/StorageServiceTest.java @@ -1,4 +1,4 @@ -package toy.bookchat.bookchat.domain.storage; +package toy.bookchat.bookchat.infrastructure.s3; import static org.assertj.core.api.Assertions.assertThatThrownBy; import static org.mockito.ArgumentMatchers.any; @@ -15,48 +15,46 @@ import org.mockito.Mock; import org.mockito.junit.jupiter.MockitoExtension; import org.springframework.mock.web.MockMultipartFile; -import toy.bookchat.bookchat.domain.storage.image.ImageValidator; import toy.bookchat.bookchat.exception.internalserver.ImageUploadToStorageException; -import toy.bookchat.bookchat.infrastructure.aws.StorageProperties; @ExtendWith(MockitoExtension.class) class StorageServiceTest { - @Mock - StorageProperties storageProperties; - @Mock - AmazonS3Client amazonS3Client; - @Mock - ImageValidator imageValidator; - @InjectMocks - UserProfileStorageService storageService; - - @Test - void 이미지_파일_업로드_성공() throws Exception { - MockMultipartFile multipartFile = ImageFixture.createImageFile(); - when(storageProperties.getBucketName()).thenReturn("testBucketName"); - storageService.upload(multipartFile, "test uuid", "test date"); - verify(amazonS3Client).putObject(anyString(), anyString(), any(InputStream.class), - any(ObjectMetadata.class)); - } - - @Test - void 이미지_업로드중_발생하는_예외를_커스텀예외로_던지기_성공() throws Exception { - MockMultipartFile multipartFile = ImageFixture.createImageFile(); - when(amazonS3Client.putObject(any(), any(), any(), any())).thenThrow( - ImageUploadToStorageException.class); - when(storageProperties.getBucketName()).thenReturn("testBucketName"); - assertThatThrownBy(() -> { - storageService.upload(multipartFile, "test uuid", "test date"); - }).isInstanceOf(ImageUploadToStorageException.class); - } - - private static class ImageFixture { - - public static MockMultipartFile createImageFile() { - return new MockMultipartFile("test image", "image.webp", "webp", - "test".getBytes()); - } + @Mock + StorageProperties storageProperties; + @Mock + AmazonS3Client amazonS3Client; + @Mock + ImageValidator imageValidator; + @InjectMocks + UserProfileStorageService storageService; + + @Test + void 이미지_파일_업로드_성공() throws Exception { + MockMultipartFile multipartFile = ImageFixture.createImageFile(); + when(storageProperties.getBucketName()).thenReturn("testBucketName"); + storageService.upload(multipartFile, "test uuid", "test date"); + verify(amazonS3Client).putObject(anyString(), anyString(), any(InputStream.class), + any(ObjectMetadata.class)); + } + + @Test + void 이미지_업로드중_발생하는_예외를_커스텀예외로_던지기_성공() throws Exception { + MockMultipartFile multipartFile = ImageFixture.createImageFile(); + when(amazonS3Client.putObject(any(), any(), any(), any())).thenThrow( + ImageUploadToStorageException.class); + when(storageProperties.getBucketName()).thenReturn("testBucketName"); + assertThatThrownBy(() -> { + storageService.upload(multipartFile, "test uuid", "test date"); + }).isInstanceOf(ImageUploadToStorageException.class); + } + + private static class ImageFixture { + + public static MockMultipartFile createImageFile() { + return new MockMultipartFile("test image", "image.webp", "webp", + "test".getBytes()); } + } } \ No newline at end of file