11package com .example ;
22
33
4+ import com .github .tomakehurst .wiremock .client .WireMock ;
45import com .github .tomakehurst .wiremock .junit5 .WireMockRuntimeInfo ;
56import com .github .tomakehurst .wiremock .junit5 .WireMockTest ;
7+ import org .junit .jupiter .api .BeforeAll ;
8+ import org .junit .jupiter .api .DisplayName ;
69import org .junit .jupiter .api .Test ;
10+ import org .awaitility .Awaitility ;
11+ import javafx .application .Platform ;
12+
13+ import java .time .Duration ;
714
815import static com .github .tomakehurst .wiremock .client .WireMock .*;
916import static org .assertj .core .api .Assertions .assertThat ;
1017
11- import static org .junit .jupiter .api .Assertions .assertEquals ;
1218
1319@ WireMockTest
1420class ChatModelTest {
15- static {
21+
22+ @ BeforeAll
23+ static void initJavaFX () {
1624 try {
17- javafx . application . Platform .startup (() -> {
18- } );
25+ Platform .startup (() -> {});
26+ Platform . setImplicitExit ( false );
1927 } catch (IllegalStateException e ) {
20-
2128 }
2229 }
2330
2431
2532 @ Test
26- void sendMessage_callsConnectionWithCorrectMessage () {
27-
33+ void sendMessageCallsConnectionWithMessagesToSend () {
2834 var spy = new NtfyConnectionSpy ();
29-
3035 var model = new ChatModel (spy );
31- String messageToSend = "Hello Ntfy World!" ;
32-
36+ String messageToSend = "Test Message 123" ;
3337
3438 model .sendMessage (messageToSend );
3539
36-
37- assertThat (spy .sentMessage ).isEqualTo (messageToSend );
40+ assertThat (spy .message ).isEqualTo (messageToSend );
3841 }
3942
40-
4143 @ Test
44+ void sendMessageToFakeServer_viaNtfyConnectionImpl (WireMockRuntimeInfo wmRunTimeInfo ) throws InterruptedException {
45+ var con = new NtfyConnectionImpl ("http://localhost:" + wmRunTimeInfo .getHttpPort ());
46+ var model = new ChatModel (con );
47+ String messageToSend = "Test Message 123" ;
4248
43- void receiveMessage_addsToObservableList () throws Exception {
44-
45- var spy = new NtfyConnectionSpy ();
46- var model = new ChatModel (spy );
47-
48- model .startReceiving ();
49+ stubFor (post ("/mytopic" ).willReturn (ok ()));
4950
50- long testTimestamp = System .currentTimeMillis () / 1000 ;
51- String receivedText = "Ett inkommande chattmeddelande" ;
51+ model .sendMessage (messageToSend );
5252
53- spy . simulateMessageArrival ( receivedText , testTimestamp );
53+ Thread . sleep ( 500 );
5454
55+ WireMock .verify (postRequestedFor (urlEqualTo ("/mytopic" ))
56+ .withRequestBody (matching (messageToSend )));
57+ }
5558
56- org .testfx .util .WaitForAsyncUtils .waitForFxEvents ();
5759
58- assertThat (model .getMessages ()).hasSize (1 );
59- ChatMessage received = model .getMessages ().get (0 );
60+ @ Test
61+ void checkReceivedMessagesAfterSendingAMessageToAFakeServer (WireMockRuntimeInfo wmRunTimeInfo ) {
62+ var conImp = new NtfyConnectionImpl ("http://localhost:" + wmRunTimeInfo .getHttpPort ());
6063
61- assertEquals (receivedText , received .content ());
62- assertEquals (testTimestamp , received .timestamp ());
63- }
64+ String expectedMessage = "Async Data Received" ;
65+ long expectedTimestamp = System .currentTimeMillis ();
6466
65- @ Test
66- void sendMessageToFakeServer_viaNtfyConnectionImpl (WireMockRuntimeInfo wmRuntimeInfo ) throws InterruptedException {
67- var con = new NtfyConnectionImpl ("http://localhost:" + wmRuntimeInfo .getHttpPort ());
68- var model = new ChatModel (con );
69- String messageToSend = "Test Body Content" ;
67+ stubFor (get ("/mytopic/json" )
68+ .willReturn (aResponse ()
69+ .withHeader ("Content-type" , "application/json" )
70+ .withBody ("{\" event\" : \" message\" ,\" message\" : \" " + expectedMessage + "\" , \" time\" : \" " + expectedTimestamp + "\" }" )));
7071
71- stubFor (post ("/mytopic" ).willReturn (ok ()));
72+ var model = new ChatModel (conImp );
73+ model .startReceiving ();
7274
73- model .sendMessage (messageToSend );
75+ Awaitility .await ()
76+ .atMost (Duration .ofSeconds (4 ))
77+ .pollInterval (Duration .ofMillis (100 ))
78+ .untilAsserted (() -> {
79+ assertThat (model .getMessages ()).isNotEmpty ();
7480
75- Thread . sleep ( 500 );
81+ assertThat ( model . getMessages (). getLast (). content ()). isEqualTo ( expectedMessage );
7682
77- verify ( postRequestedFor ( urlEqualTo ( "/mytopic" ))
78- . withRequestBody ( matching ( messageToSend )) );
83+ assertThat ( model . getMessages (). getLast (). timestamp ()). isEqualTo ( expectedTimestamp );
84+ } );
7985 }
80- }
86+ }
0 commit comments