11package br .com .helpdev .output .repository ;
22
3+ import static org .assertj .core .api .Assertions .assertThat ;
34import static org .mockito .Mockito .verify ;
5+ import static org .mockito .Mockito .verifyNoMoreInteractions ;
6+ import static org .mockito .Mockito .when ;
47
8+ import br .com .helpdev .domain .Chat ;
9+ import br .com .helpdev .domain .CommunicationChannel ;
10+ import br .com .helpdev .domain .Message ;
11+ import br .com .helpdev .domain .Recipient ;
12+ import br .com .helpdev .domain .Status ;
13+ import br .com .helpdev .domain .vo .MessageBody ;
514import br .com .helpdev .domain .vo .MessageId ;
15+ import br .com .helpdev .domain .vo .Phone ;
16+ import br .com .helpdev .output .repository .entity .CommunicationChannelEntity ;
17+ import br .com .helpdev .output .repository .entity .MessageEntity ;
618import br .com .helpdev .output .repository .entity .MessageEntityRepository ;
19+ import br .com .helpdev .output .repository .entity .RecipientEntity ;
720import br .com .helpdev .output .repository .mapper .MessageMapper ;
21+ import java .time .ZonedDateTime ;
22+ import java .util .List ;
23+ import java .util .Optional ;
824import org .junit .jupiter .api .Test ;
925import org .junit .jupiter .api .extension .ExtendWith ;
26+ import org .mockito .ArgumentCaptor ;
1027import org .mockito .InjectMocks ;
1128import org .mockito .Mock ;
1229import org .mockito .Spy ;
1532@ ExtendWith (MockitoExtension .class )
1633class MessageGatewayTest {
1734
35+ public static final long ID = 25L ;
36+ public static final MessageEntity MESSAGE_ENTITY = MessageEntity .builder ()
37+ .id (ID )
38+ .channel (CommunicationChannelEntity .EMAIL )
39+ .recipient (RecipientEntity .builder ()
40+ .phoneNumber ("78924" )
41+ .build ())
42+ .body ("Test" ).build ();
43+ public static final Message MESSAGE = Message .builder ()
44+ .id (MessageId .from (ID ))
45+ .channel (CommunicationChannel .WHATSAPP )
46+ .recipient (Recipient .builder ()
47+ .phone (Phone .from ("78924" , "1243243" ))
48+ .build ())
49+ .chats (List .of (Chat .builder ()
50+ .status (Status .SENT )
51+ .date (ZonedDateTime .now ())
52+ .build ()))
53+ .body (MessageBody .from ("Hello!" )).build ();
54+
1855 @ InjectMocks
1956 private MessageGateway messageGateway ;
2057 @ Mock
@@ -26,6 +63,50 @@ class MessageGatewayTest {
2663 void wheDeleteThenCallRepository () {
2764 messageGateway .delete (MessageId .from (25L ));
2865 verify (repository ).deleteById (25L );
66+ verifyNoMoreInteractions (repository );
67+ }
68+
69+ @ Test
70+ void wheFindThenCallRepository () {
71+ when (repository .findById (ID )).thenReturn (Optional .of (MESSAGE_ENTITY ));
72+ var result = messageGateway .find (MessageId .from (ID ));
73+ assertThat (result ).isNotEmpty ()
74+ .get ()
75+ .satisfies (r -> assertThat (r .getId ()).isEqualTo (Optional .of (MessageId .from (ID ))));
76+ verify (repository ).findById (ID );
77+ verifyNoMoreInteractions (repository );
78+ }
79+
80+ @ Test
81+ void wheExistsThenCallRepository () {
82+ when (repository .existsById (ID )).thenReturn (true );
83+ var result = messageGateway .exists (MessageId .from (ID ));
84+ assertThat (result ).isTrue ();
85+ verify (repository ).existsById (ID );
86+ verifyNoMoreInteractions (repository );
87+ }
88+
89+ @ Test
90+ void wheDoNotExistsThenCallRepository () {
91+ when (repository .existsById (ID )).thenReturn (false );
92+ var result = messageGateway .exists (MessageId .from (ID ));
93+ assertThat (result ).isFalse ();
94+ verify (repository ).existsById (ID );
95+ verifyNoMoreInteractions (repository );
96+ }
97+
98+ @ Test
99+ void wheCreateThenCallRepository () {
100+ var result = messageGateway .create (MESSAGE );
101+ assertThat (result ).isNotNull ()
102+ .satisfies (r -> assertThat (r .getId ()).isEqualTo (Optional .of (MessageId .from (ID ))));
103+ ArgumentCaptor <MessageEntity > captor = ArgumentCaptor .forClass (MessageEntity .class );
104+ verify (repository ).save (captor .capture ());
105+ verifyNoMoreInteractions (repository );
106+ MessageEntity savedMessage = captor .getValue ();
107+ assertThat (savedMessage .getChats ()).hasSize (1 ).first ().satisfies (
108+ c -> assertThat (c .getMessage ()).isNotNull ()
109+ );
29110 }
30111
31112}
0 commit comments