1+ package com .somemore .notification .converter ;
2+
3+ import com .somemore .IntegrationTestSupport ;
4+ import com .somemore .notification .domain .Notification ;
5+ import com .somemore .notification .domain .NotificationSubType ;
6+ import org .junit .jupiter .api .DisplayName ;
7+ import org .junit .jupiter .api .Test ;
8+ import org .springframework .beans .factory .annotation .Autowired ;
9+
10+ import java .util .UUID ;
11+
12+ import static org .assertj .core .api .Assertions .assertThat ;
13+ import static org .junit .jupiter .api .Assertions .assertThrows ;
14+
15+ class MessageConverterTest extends IntegrationTestSupport {
16+
17+ @ Autowired
18+ private MessageConverter messageConverter ;
19+
20+ @ Test
21+ @ DisplayName ("VOLUNTEER_REVIEW_REQUEST 메시지를 변환하면 Notification 객체를 반환한다" )
22+ void testVolunteerReviewRequestConversion () {
23+ // given
24+ String message = """
25+ {
26+ "type": "NOTIFICATION",
27+ "subType": "VOLUNTEER_REVIEW_REQUEST",
28+ "volunteerId": "123e4567-e89b-12d3-a456-426614174000",
29+ "volunteerApplyId": "1",
30+ "centerId": "123e4567-e89b-12d3-a456-426614174001",
31+ "recruitBoardId": 456,
32+ "createdAt": "2024-12-05T10:00:00"
33+ }
34+ """ ;
35+
36+ // when
37+ Notification notification = messageConverter .from (message );
38+
39+ // then
40+ assertThat (notification .getReceiverId ()).isEqualTo (UUID .fromString ("123e4567-e89b-12d3-a456-426614174000" ));
41+ assertThat (notification .getTitle ()).isEqualTo ("최근 활동하신 활동의 후기를 작성해 주세요!" );
42+ assertThat (notification .getType ()).isEqualTo (NotificationSubType .VOLUNTEER_REVIEW_REQUEST );
43+ assertThat (notification .getRelatedId ()).isEqualTo (456L );
44+ }
45+
46+ @ Test
47+ @ DisplayName ("임의의 필드가 추가된 VOLUNTEER_APPLY_STATUS_CHANGE 메시지를 변환해도 Notification 객체를 반환한다" )
48+ void testVolunteerApplyStatusChangeConversion () {
49+ // given
50+ String message = """
51+ {
52+ "extraField": "this should be ignored",
53+ "extraField": "this should be ignored",
54+ "extraField": "this should be ignored",
55+ "extraField": "this should be ignored",
56+ "extraField": "this should be ignored",
57+ "type": "NOTIFICATION",
58+ "subType": "VOLUNTEER_APPLY_STATUS_CHANGE",
59+ "volunteerId": "123e4567-e89b-12d3-a456-426614174000",
60+ "centerId": "123e4567-e89b-12d3-a456-426614174001",
61+ "volunteerApplyId": "1",
62+ "recruitBoardId": 456,
63+ "oldStatus": "WAITING",
64+ "newStatus": "APPROVED",
65+ "createdAt": "2024-12-05T10:00:00"
66+ }
67+ """ ;
68+
69+ // when
70+ Notification notification = messageConverter .from (message );
71+
72+ // then
73+ assertThat (notification .getReceiverId ()).isEqualTo (UUID .fromString ("123e4567-e89b-12d3-a456-426614174000" ));
74+ assertThat (notification .getTitle ()).isEqualTo ("봉사 활동 신청이 승인되었습니다." );
75+ assertThat (notification .getType ()).isEqualTo (NotificationSubType .VOLUNTEER_APPLY_STATUS_CHANGE );
76+ assertThat (notification .getRelatedId ()).isEqualTo (456L );
77+ }
78+
79+ @ Test
80+ @ DisplayName ("잘못된 JSON 메시지를 변환하면 IllegalStateException을 던진다" )
81+ void testInvalidJson () {
82+ // given
83+ String invalidMessage = "{ invalid-json }" ;
84+
85+ // when
86+ // then
87+ assertThrows (IllegalStateException .class , () -> messageConverter .from (invalidMessage ));
88+ }
89+
90+ @ Test
91+ @ DisplayName ("필수 필드가 누락된 메시지를 변환하면 IllegalStateException을 던진다" )
92+ void testMissingFields () {
93+ // given
94+ String messageWithMissingFields = """
95+ {
96+ "type": "NOTIFICATION",
97+ "subType": "VOLUNTEER_REVIEW_REQUEST"
98+ }
99+ """ ;
100+
101+ // when & then
102+ assertThrows (IllegalStateException .class , () -> messageConverter .from (messageWithMissingFields ));
103+ }
104+ }
0 commit comments