-
Notifications
You must be signed in to change notification settings - Fork 2
feat: 체결 완료 시 알림 기능 추가 #138
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
51 changes: 51 additions & 0 deletions
51
src/main/java/com/cleanengine/coin/trade/application/TradeExecutedNotificationHandler.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| package com.cleanengine.coin.trade.application; | ||
|
|
||
| import com.cleanengine.coin.trade.entity.Trade; | ||
| import lombok.extern.slf4j.Slf4j; | ||
| import org.springframework.messaging.simp.SimpMessagingTemplate; | ||
| import org.springframework.stereotype.Component; | ||
| import org.springframework.transaction.event.TransactionalEventListener; | ||
|
|
||
| import static com.cleanengine.coin.common.CommonValues.BUY_ORDER_BOT_ID; | ||
| import static com.cleanengine.coin.common.CommonValues.SELL_ORDER_BOT_ID; | ||
|
|
||
| @Slf4j | ||
| @Component | ||
| public class TradeExecutedNotificationHandler { | ||
|
|
||
| private final SimpMessagingTemplate messagingTemplate; | ||
|
|
||
| private static final String ASK = "ask"; // 매도 | ||
| private static final String BID = "bid"; // 매수 | ||
|
|
||
| public TradeExecutedNotificationHandler(SimpMessagingTemplate messagingTemplate) { | ||
| this.messagingTemplate = messagingTemplate; | ||
| } | ||
|
|
||
| @TransactionalEventListener | ||
| public void notifyAfterTradeExecuted(TradeExecutedEvent tradeExecutedEvent) { | ||
| Trade trade = tradeExecutedEvent.getTrade(); | ||
| if (trade == null) { | ||
| log.error("체결 알림 실패! trade == null"); | ||
| return ; | ||
| } | ||
|
|
||
| Integer sellUserId = trade.getSellUserId(); | ||
| Integer buyUserId = trade.getBuyUserId(); | ||
| if (sellUserId == null || buyUserId == null) { | ||
| log.error("체결 알림 실패! sellUserId: {}, buyUserId: {}", sellUserId, buyUserId); | ||
| return ; | ||
| } | ||
| if (sellUserId == SELL_ORDER_BOT_ID && buyUserId == BUY_ORDER_BOT_ID) { | ||
| return ; | ||
| } | ||
|
|
||
| log.debug("{} 체결 이벤트 구독 : {}원에 {}개, 매수인: {}, 매도인: {}", trade.getTicker(), trade.getPrice(), trade.getSize(), buyUserId, sellUserId ); | ||
|
|
||
| TradeExecutedNotifyDto soldDto = TradeExecutedNotifyDto.of(trade, ASK); | ||
| TradeExecutedNotifyDto boughtDto = TradeExecutedNotifyDto.of(trade, BID); | ||
| messagingTemplate.convertAndSend("/topic/tradeNotification/" + sellUserId, soldDto); | ||
| messagingTemplate.convertAndSend("/topic/tradeNotification/" + buyUserId, boughtDto); | ||
| } | ||
|
|
||
| } | ||
41 changes: 41 additions & 0 deletions
41
src/main/java/com/cleanengine/coin/trade/application/TradeExecutedNotifyDto.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| package com.cleanengine.coin.trade.application; | ||
|
|
||
| import com.cleanengine.coin.trade.entity.Trade; | ||
| import com.fasterxml.jackson.annotation.JsonPropertyOrder; | ||
| import lombok.Builder; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| @JsonPropertyOrder({ "ticker", "price", "size", "type", "tradedTime"}) | ||
| public class TradeExecutedNotifyDto { | ||
|
|
||
| private String ticker; | ||
|
|
||
| private Double price; | ||
|
|
||
| private Double size; | ||
|
|
||
| private String type; | ||
|
|
||
| private LocalDateTime tradedTime; | ||
|
|
||
| @Builder | ||
| private TradeExecutedNotifyDto(String ticker, Double price, Double size, String type, LocalDateTime tradedTime) { | ||
| this.ticker = ticker; | ||
| this.price = price; | ||
| this.size = size; | ||
| this.type = type; | ||
| this.tradedTime = tradedTime; | ||
| } | ||
|
|
||
| public static TradeExecutedNotifyDto of(Trade trade, String type) { | ||
| return TradeExecutedNotifyDto.builder() | ||
| .ticker(trade.getTicker()) | ||
| .price(trade.getPrice()) | ||
| .size(trade.getSize()) | ||
| .type(type) | ||
| .tradedTime(trade.getTradeTime()) | ||
| .build(); | ||
| } | ||
|
|
||
| } |
133 changes: 133 additions & 0 deletions
133
...est/java/com/cleanengine/coin/trade/application/TradeExecutedNotificationHandlerTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,133 @@ | ||
| package com.cleanengine.coin.trade.application; | ||
|
|
||
| import static com.cleanengine.coin.common.CommonValues.BUY_ORDER_BOT_ID; | ||
| import static com.cleanengine.coin.common.CommonValues.SELL_ORDER_BOT_ID; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.eq; | ||
| import static org.mockito.Mockito.*; | ||
|
|
||
| import com.cleanengine.coin.trade.entity.Trade; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.DisplayName; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.junit.jupiter.api.extension.ExtendWith; | ||
| import org.mockito.Mock; | ||
| import org.mockito.junit.jupiter.MockitoExtension; | ||
| import org.springframework.messaging.simp.SimpMessagingTemplate; | ||
|
|
||
| import java.time.LocalDateTime; | ||
|
|
||
| @DisplayName("체결 알림 단위테스트") | ||
| @ExtendWith(MockitoExtension.class) | ||
| class TradeExecutedNotificationHandlerTest { | ||
|
|
||
| @Mock | ||
| private SimpMessagingTemplate messagingTemplate; | ||
|
|
||
| private TradeExecutedNotificationHandler handler; | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| handler = new TradeExecutedNotificationHandler(messagingTemplate); | ||
| } | ||
|
|
||
| @DisplayName("정상 체결내역을 리스닝하면 웹소켓으로 전송한다.") | ||
| @Test | ||
| void shouldSendNotificationsForValidTrade() { | ||
| // given | ||
| Trade trade = Trade.of("BTC", LocalDateTime.now(), 3, SELL_ORDER_BOT_ID, 50000.0, 1.0); | ||
| TradeExecutedEvent event = TradeExecutedEvent.of(trade, null, null); | ||
|
|
||
| // when | ||
| handler.notifyAfterTradeExecuted(event); | ||
|
|
||
| // then | ||
| verify(messagingTemplate).convertAndSend(eq("/topic/tradeNotification/1"), any(TradeExecutedNotifyDto.class)); | ||
| verify(messagingTemplate).convertAndSend(eq("/topic/tradeNotification/3"), any(TradeExecutedNotifyDto.class)); | ||
| } | ||
|
|
||
| @DisplayName("정상 체결내역을 리스닝하면 웹소켓으로 전송한다.") | ||
| @Test | ||
| void shouldSendNotificationsForValidTrade2() { | ||
| // given | ||
| Trade trade = Trade.of("BTC", LocalDateTime.now(), BUY_ORDER_BOT_ID, 3, 50000.0, 1.0); | ||
| TradeExecutedEvent event = TradeExecutedEvent.of(trade, null, null); | ||
|
|
||
| // when | ||
| handler.notifyAfterTradeExecuted(event); | ||
|
|
||
| // then | ||
| verify(messagingTemplate).convertAndSend(eq("/topic/tradeNotification/2"), any(TradeExecutedNotifyDto.class)); | ||
| verify(messagingTemplate).convertAndSend(eq("/topic/tradeNotification/3"), any(TradeExecutedNotifyDto.class)); | ||
| } | ||
|
|
||
| @DisplayName("매수인과 매도인의 userId가 null이면 메시지를 전송하지 않는다.") | ||
| @Test | ||
| void shouldNotSendNotificationForNullUserIds() { | ||
| // given | ||
| Trade trade = Trade.of("BTC", LocalDateTime.now(), null, null, 50000.0, 1.0); | ||
| TradeExecutedEvent event = TradeExecutedEvent.of(trade, null, null); | ||
|
|
||
| // when | ||
| handler.notifyAfterTradeExecuted(event); | ||
|
|
||
| // then | ||
| verifyNoInteractions(messagingTemplate); | ||
| } | ||
|
|
||
| @DisplayName("매수인의 userId가 null이면 메시지를 전송하지 않는다.") | ||
| @Test | ||
| void shouldNotSendNotificationForNullBuyUserId() { | ||
| // given | ||
| Trade trade = Trade.of("BTC", LocalDateTime.now(), null, SELL_ORDER_BOT_ID, 50000.0, 1.0); | ||
| TradeExecutedEvent event = TradeExecutedEvent.of(trade, null, null); | ||
|
|
||
| // when | ||
| handler.notifyAfterTradeExecuted(event); | ||
|
|
||
| // then | ||
| verifyNoInteractions(messagingTemplate); | ||
| } | ||
|
|
||
| @DisplayName("매도인의 userId가 null이면 메시지를 전송하지 않는다.") | ||
| @Test | ||
| void shouldNotSendNotificationForNullSellUserId() { | ||
| // given | ||
| Trade trade = Trade.of("BTC", LocalDateTime.now(), BUY_ORDER_BOT_ID, null, 50000.0, 1.0); | ||
| TradeExecutedEvent event = TradeExecutedEvent.of(trade, null, null); | ||
|
|
||
| // when | ||
| handler.notifyAfterTradeExecuted(event); | ||
|
|
||
| // then | ||
| verifyNoInteractions(messagingTemplate); | ||
| } | ||
|
|
||
| @DisplayName("봇끼리의 체결은 메시지를 전송하지 않는다.") | ||
| @Test | ||
| void shouldNotSendNotificationForBotTrade() { | ||
| // given | ||
| Trade trade = Trade.of("BTC", LocalDateTime.now(), BUY_ORDER_BOT_ID, SELL_ORDER_BOT_ID, 50000.0, 1.0); | ||
| TradeExecutedEvent event = TradeExecutedEvent.of(trade, null, null); | ||
|
|
||
| // when | ||
| handler.notifyAfterTradeExecuted(event); | ||
|
|
||
| // then | ||
| verifyNoInteractions(messagingTemplate); | ||
| } | ||
|
|
||
| @DisplayName("체결이 null이면 메시지를 전송하지 않는다.") | ||
| @Test | ||
| void shouldNotSendNotificationForNullTrade() { | ||
| // given | ||
| TradeExecutedEvent event = TradeExecutedEvent.of(null, null, null); | ||
|
|
||
| // when | ||
| handler.notifyAfterTradeExecuted(event); | ||
|
|
||
| // then | ||
| verifyNoInteractions(messagingTemplate); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.