1
1
package br .com .helpdev .output .repository ;
2
2
3
+ import static org .assertj .core .api .Assertions .assertThat ;
3
4
import static org .mockito .Mockito .verify ;
5
+ import static org .mockito .Mockito .verifyNoMoreInteractions ;
6
+ import static org .mockito .Mockito .when ;
4
7
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 ;
5
14
import 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 ;
6
18
import br .com .helpdev .output .repository .entity .MessageEntityRepository ;
19
+ import br .com .helpdev .output .repository .entity .RecipientEntity ;
7
20
import br .com .helpdev .output .repository .mapper .MessageMapper ;
21
+ import java .time .ZonedDateTime ;
22
+ import java .util .List ;
23
+ import java .util .Optional ;
8
24
import org .junit .jupiter .api .Test ;
9
25
import org .junit .jupiter .api .extension .ExtendWith ;
26
+ import org .mockito .ArgumentCaptor ;
10
27
import org .mockito .InjectMocks ;
11
28
import org .mockito .Mock ;
12
29
import org .mockito .Spy ;
15
32
@ ExtendWith (MockitoExtension .class )
16
33
class MessageGatewayTest {
17
34
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
+
18
55
@ InjectMocks
19
56
private MessageGateway messageGateway ;
20
57
@ Mock
@@ -26,6 +63,50 @@ class MessageGatewayTest {
26
63
void wheDeleteThenCallRepository () {
27
64
messageGateway .delete (MessageId .from (25L ));
28
65
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
+ );
29
110
}
30
111
31
112
}
0 commit comments