11package org .hik .services .events ;
22
3- import com .github .tomakehurst .wiremock .core . WireMockConfiguration ;
4- import com .github .tomakehurst .wiremock .junit5 .WireMockExtension ;
3+ import com .github .tomakehurst .wiremock .junit5 . WireMockRuntimeInfo ;
4+ import com .github .tomakehurst .wiremock .junit5 .WireMockTest ;
55import org .hik .api .MatrixClient ;
66import org .hik .api .events .*;
77import org .hik .context .DiscoveryResponse ;
88import org .hik .exceptions .MatrixIOException ;
99import org .junit .jupiter .api .BeforeAll ;
1010import org .junit .jupiter .api .BeforeEach ;
1111import org .junit .jupiter .api .Test ;
12- import org .junit .jupiter .api .extension .RegisterExtension ;
1312import org .junit .jupiter .api .io .TempDir ;
1413
1514import java .io .IOException ;
2120import static com .github .tomakehurst .wiremock .client .WireMock .*;
2221import static org .junit .jupiter .api .Assertions .*;
2322
23+ @ WireMockTest
2424class EventServiceTest {
2525
26- @ RegisterExtension
27- static final WireMockExtension wireMockServer = WireMockExtension .newInstance ()
28- .options (WireMockConfiguration .wireMockConfig ()
29- .dynamicPort ())
30- .build ();
3126
3227 private static final String AUTH_TOKEN = "1234" ;
3328 private static DiscoveryResponse DISCOVERY_RESPONSE ;
3429 private static MatrixClient client ;
3530
3631 @ BeforeAll
37- static void setUpDiscovery () {
32+ static void setUpDiscovery (WireMockRuntimeInfo wireMockRuntimeInfo ) {
3833 DISCOVERY_RESPONSE = new DiscoveryResponse (
39- new DiscoveryResponse .HomeserverInfo (wireMockServer . baseUrl ()),
34+ new DiscoveryResponse .HomeserverInfo (wireMockRuntimeInfo . getHttpBaseUrl ()),
4035 null , null
4136 );
4237 }
@@ -68,7 +63,7 @@ void sendPublishRoomMessage_WithACorrectPayload_thenReturnAString() {
6863 String expectedEventId = "$h29asdf8q348hju9a:matrix.org" ;
6964
7065
71- wireMockServer . stubFor (put (urlPathMatching ("/_matrix/client/v3/rooms/" + roomId + "/send/" + roomMessageType + "/[^/]+" ))
66+ stubFor (put (urlPathMatching ("/_matrix/client/v3/rooms/" + roomId + "/send/" + roomMessageType + "/[^/]+" ))
7267 .withRequestBody (equalToJson ("""
7368 {
7469 "body": "Hello World",
@@ -90,16 +85,16 @@ void sendPublishRoomMessageFile_WithACorrectPayload_thenReturnAString(@TempDir P
9085 Result result = getResult (tempDir );
9186
9287 // Mock the MXC Request (v1 create endpoint)
93- wireMockServer . stubFor (post (urlEqualTo ("/_matrix/media/v1/create" ))
88+ stubFor (post (urlEqualTo ("/_matrix/media/v1/create" ))
9489 .willReturn (okJson ("{\" content_uri\" : \" " + result .mockMxcUri () + "\" }" )));
9590
9691 // Mock the File Upload (v3 upload endpoint with filename query param)
97- wireMockServer . stubFor (put (urlEqualTo ("/_matrix/media/v3/upload/" + result .serverName () + "/" + result .mediaId () + "?filename=file.txt" ))
92+ stubFor (put (urlEqualTo ("/_matrix/media/v3/upload/" + result .serverName () + "/" + result .mediaId () + "?filename=file.txt" ))
9893 .withRequestBody (containing ("Test" ))
9994 .willReturn (ok ()));
10095
10196 // Mock the Message Publication (v3 client send timeline endpoint)
102- wireMockServer . stubFor (put (urlPathMatching ("/_matrix/client/v3/rooms/" + result .roomId () + "/send/" + result .roomMessageType () + "/[^/]+" ))
97+ stubFor (put (urlPathMatching ("/_matrix/client/v3/rooms/" + result .roomId () + "/send/" + result .roomMessageType () + "/[^/]+" ))
10398 .withRequestBody (containing (String .valueOf (result .mockMxcUri )))
10499 .withRequestBody (containing ("file.txt" ))
105100 .willReturn (okJson ("{\" event_id\" : \" " + result .expectedEventId () + "\" }" )));
@@ -119,7 +114,7 @@ void sendPublishRoomMessageFile_WithACorrectPayload_thenReturnAString(@TempDir P
119114 void sendPublishRoomMessageFile_WithACorrectPayload_thenReturnAnException (@ TempDir Path tempDir ) throws IOException {
120115 Result result = getResult (tempDir );
121116
122- wireMockServer . stubFor (post (urlEqualTo ("/_matrix/media/v1/create" ))
117+ stubFor (post (urlEqualTo ("/_matrix/media/v1/create" ))
123118 .willReturn (aResponse ()
124119 .withStatus (200 )
125120 .withHeader ("Content-Type" , "application/json" )
@@ -139,7 +134,7 @@ void getListOfMessages_WithValidQueryParameters_thenReturnMessagesResponse() {
139134 ChronologicalDirectionType direction = ChronologicalDirectionType .CHRONOLOGICAL_ORDER ; // Adjust to your enum
140135 // name if needed
141136
142- wireMockServer . stubFor (get (urlPathEqualTo ("/_matrix/client/v3/rooms/" + roomId + "/messages" ))
137+ stubFor (get (urlPathEqualTo ("/_matrix/client/v3/rooms/" + roomId + "/messages" ))
143138 .withQueryParam ("dir" , equalTo ("f" ))
144139 .withQueryParam ("from" , equalTo ("some_start_token" ))
145140 .withQueryParam ("limit" , equalTo ("20" ))
@@ -182,7 +177,7 @@ void getSync_WithValidQueryParameters_thenReturnSyncResponse() {
182177 String leftRoomId = "!leftRoomId:matrix.org" ;
183178 String expectedChunkEventId = "$abcdefg12345:matrix.org" ;
184179 // Author's note: Yes it is this long.... and it's not even all the records.
185- wireMockServer . stubFor (get (urlPathEqualTo ("/_matrix/client/v3/sync" ))
180+ stubFor (get (urlPathEqualTo ("/_matrix/client/v3/sync" ))
186181 .willReturn (okJson ("""
187182 {
188183 "next_batch": "some_next_batch_token",
0 commit comments