Skip to content

Commit ab306b7

Browse files
committed
Refactor test suite. Add tests for filters
1 parent f40fab4 commit ab306b7

4 files changed

Lines changed: 131 additions & 74 deletions

File tree

‎src/test/java/org/hik/services/events/EventServiceTest.java‎

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,14 @@
11
package 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;
55
import org.hik.api.MatrixClient;
66
import org.hik.api.events.*;
77
import org.hik.context.DiscoveryResponse;
88
import org.hik.exceptions.MatrixIOException;
99
import org.junit.jupiter.api.BeforeAll;
1010
import org.junit.jupiter.api.BeforeEach;
1111
import org.junit.jupiter.api.Test;
12-
import org.junit.jupiter.api.extension.RegisterExtension;
1312
import org.junit.jupiter.api.io.TempDir;
1413

1514
import java.io.IOException;
@@ -21,22 +20,18 @@
2120
import static com.github.tomakehurst.wiremock.client.WireMock.*;
2221
import static org.junit.jupiter.api.Assertions.*;
2322

23+
@WireMockTest
2424
class 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",
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package org.hik.services.filtering;
2+
3+
import com.github.tomakehurst.wiremock.junit5.WireMockRuntimeInfo;
4+
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
5+
import org.hik.api.MatrixClient;
6+
import org.hik.api.filtering.FilterDefinition;
7+
import org.hik.context.DiscoveryResponse;
8+
import org.hik.services.utils.Mapper;
9+
import org.instancio.junit.Given;
10+
import org.instancio.junit.InstancioExtension;
11+
import org.junit.jupiter.api.BeforeAll;
12+
import org.junit.jupiter.api.BeforeEach;
13+
import org.junit.jupiter.api.Test;
14+
import org.junit.jupiter.api.extension.ExtendWith;
15+
import tools.jackson.databind.ObjectMapper;
16+
17+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
18+
import static org.junit.jupiter.api.Assertions.assertNotNull;
19+
20+
@ExtendWith(InstancioExtension.class)
21+
@WireMockTest
22+
class FilterServiceTest {
23+
24+
private static MatrixClient client;
25+
private static final ObjectMapper mapper = Mapper.getInstance();
26+
private static final String AUTH_TOKEN = "1234";
27+
private static final String USER_ID = "@matrix:example.org";
28+
private static DiscoveryResponse DISCOVERY_RESPONSE;
29+
30+
31+
@BeforeAll
32+
static void setUpDiscovery(WireMockRuntimeInfo wireMockRuntimeInfo) {
33+
DISCOVERY_RESPONSE = new DiscoveryResponse(
34+
new DiscoveryResponse.HomeserverInfo(wireMockRuntimeInfo.getHttpBaseUrl()),
35+
null, null
36+
);
37+
}
38+
39+
@BeforeEach
40+
void createClient() {
41+
client = MatrixClient.create(DISCOVERY_RESPONSE, AUTH_TOKEN);
42+
}
43+
44+
@Given
45+
private FilterDefinition filterDefinition;
46+
47+
@Test
48+
void publishFilter_WithACorrectPayload_thenReturnAnId() {
49+
String json = mapper.writeValueAsString(filterDefinition);
50+
stubFor(post("/_matrix/client/v3/user/" + USER_ID + "/filter")
51+
.withRequestBody(equalToJson(json))
52+
.willReturn(okJson("""
53+
{
54+
"filter_id": "66696p746572"
55+
}""")));
56+
57+
String response = client.filter().publishFilter(USER_ID, filterDefinition);
58+
59+
assertNotNull(response);
60+
}
61+
62+
@Test
63+
void getFilter_WithACorrectPayload_ThenReturnAFilterDefinition() {
64+
final String FILTER_ID = "ABC123";
65+
String json = mapper.writeValueAsString(filterDefinition);
66+
stubFor(get("/_matrix/client/v3/user/" + USER_ID + "/filter/" + FILTER_ID)
67+
.willReturn(okJson(json)));
68+
69+
FilterDefinition response = client.filter().getFilter(USER_ID, FILTER_ID);
70+
71+
assertNotNull(response);
72+
}
73+
}

0 commit comments

Comments
 (0)