66import org .junit .jupiter .api .DisplayName ;
77import org .junit .jupiter .api .Test ;
88
9+ import java .io .File ;
10+ import java .io .IOException ;
11+
912import static com .github .tomakehurst .wiremock .client .WireMock .*;
1013import static org .assertj .core .api .Assertions .assertThat ;
1114
@@ -33,13 +36,96 @@ void sendMessageToFakeServer(WireMockRuntimeInfo wmRuntimeInfo) {
3336 var con = new NtfyConnectionImpl ("http://localhost:" + wmRuntimeInfo .getHttpPort ());
3437 var model = new HelloModel (con );
3538 model .setMessageToSend ("Hello World" );
36- stubFor (post ("/mytopic " ).willReturn (ok ()));
39+ stubFor (post ("/adam " ).willReturn (ok ()));
3740
3841 model .sendMessage ();
3942
4043 // Verify call made to server
41- verify (postRequestedFor (urlEqualTo ("/mytopic " ))
44+ verify (postRequestedFor (urlEqualTo ("/adam " ))
4245 .withRequestBody (matching ("Hello World" )));
4346 }
4447
48+ @ Test
49+ @ DisplayName ("Given file when calling sendFile then connection.sendFile should be called" )
50+ void sendFileCallConnectionSendFile () {
51+ var spy = new NtfyConnectionSpy ();
52+ var model = new HelloModel (spy );
53+
54+ File file = new File ("dummy.txt" );
55+ model .sendFile (file );
56+
57+ assertThat (spy .sentFile ).isEqualTo (file );
58+ }
59+
60+ @ Test
61+ @ DisplayName ("sendFile should upload file to the server using PUT" )
62+ void sendFileToFakeServer (WireMockRuntimeInfo wmRuntimeInfo ) throws Exception {
63+ var con = new NtfyConnectionImpl ("http://localhost:" + wmRuntimeInfo .getHttpPort ());
64+ var model = new HelloModel (con );
65+
66+ File temp = File .createTempFile ("upload_test" , ".txt" );
67+
68+ stubFor (put ("/adam" ).willReturn (ok ()));
69+
70+ model .sendFile (temp );
71+
72+ verify (putRequestedFor (urlEqualTo ("/adam" )));
73+
74+ }
75+
76+ @ Test
77+ @ DisplayName ("Model should receive messages when connection invokes handler" )
78+ void receiveMessageShouldAddToModelViewHander (){
79+ var spy = new NtfyConnectionSpy ();
80+ var model = new HelloModel (spy );
81+
82+ NtfyMessageDto incoming = new NtfyMessageDto (
83+ "123" ,
84+ 1000 ,
85+ 0 ,
86+ "message" ,
87+ "adam" ,
88+ "this is a test hehehe" ,
89+ null
90+ );
91+
92+ spy .simulateIncoming (incoming );
93+
94+ assertThat (model .getMessages ()).containsExactly (incoming );
95+ }
96+
97+ @ Test
98+ @ DisplayName ("Model constructor should register receive handler on connection" )
99+ void constructorShouldRegisterHandler () {
100+
101+ var spy = new NtfyConnectionSpy ();
102+ var model = new HelloModel (spy );
103+
104+ assertThat (spy .handler ).isNotNull ();
105+ }
106+
107+ @ Test
108+ @ DisplayName ("messageToSendProperty should update when setting message" )
109+ void messagePropertyShouldUpdate () {
110+
111+ var spy = new NtfyConnectionSpy ();
112+ var model = new HelloModel (spy );
113+
114+ model .setMessageToSend ("Hello!" );
115+
116+ assertThat (model .getMessageToSend ()).isEqualTo ("Hello!" );
117+ }
118+
119+ @ Test
120+ @ DisplayName ("getGreeting should return the expected greeting text" )
121+ void greetingShouldBeCorrect () {
122+
123+ var model = new HelloModel (new NtfyConnectionSpy ());
124+
125+ assertThat (model .getGreeting ()).isEqualTo ("Chat Client by Adam" );
126+ }
127+
128+
129+
130+
45131}
0 commit comments