Skip to content

Commit 7dec7f8

Browse files
committed
Add ID classes into existing records.
- Add JsonCreator and JsonValue to bring serialization into the class - Patch tests to match correct class type
1 parent 078835b commit 7dec7f8

11 files changed

Lines changed: 78 additions & 61 deletions

File tree

‎src/main/java/io/github/hikingc/matrixsdk/api/identifiers/EventID.java‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,11 @@ public static EventID parse(String rawRoomId) {
3737
}
3838

3939
if (rawRoomId.charAt(0) != '$') {
40-
throw new IllegalArgumentException("Event ID must start with \'$\'");
40+
throw new IllegalArgumentException("Event ID must start with '$'");
4141
}
4242

4343
if (rawRoomId.contentEquals("$")) {
44-
throw new IllegalArgumentException("Event ID must not only contain \'$\'");
44+
throw new IllegalArgumentException("Event ID must not only contain '$'");
4545
}
4646

4747
return new EventID(rawRoomId);

‎src/main/java/io/github/hikingc/matrixsdk/api/identifiers/RoomAlias.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.github.hikingc.matrixsdk.api.identifiers;
22

3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonValue;
35
import java.util.Objects;
46

57
/// This class allows for the representation and validation of a Room Alias in Matrix.
@@ -32,6 +34,7 @@ private RoomAlias(String opaqueId, String domain) {
3234
/// @return a [RoomAlias].
3335
/// @throws IllegalArgumentException if the [String] has broken a rule from the spec.
3436
/// @throws NullPointerException if the [String] is null.
37+
@JsonCreator
3538
public static RoomAlias parse(String rawAliasId) {
3639
Objects.requireNonNull(rawAliasId, "Alias ID" + " must not be null");
3740

@@ -58,6 +61,7 @@ public boolean equals(Object obj) {
5861
}
5962

6063
@Override
64+
@JsonValue
6165
public String toString() {
6266
return "#" + opaqueId + ":" + domain;
6367
}

‎src/main/java/io/github/hikingc/matrixsdk/api/identifiers/RoomID.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.github.hikingc.matrixsdk.api.identifiers;
22

3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonValue;
35
import java.util.Objects;
46

57
/// This class allows for the representation and validation of a Room Identifier in Matrix.
@@ -32,6 +34,7 @@ private RoomID(String opaqueId, String domain) {
3234
/// @return a [RoomID].
3335
/// @throws IllegalArgumentException if the [String] has broken a rule from the spec.
3436
/// @throws NullPointerException if the [String] is null.
37+
@JsonCreator
3538
public static RoomID parse(String rawRoomId) {
3639
Objects.requireNonNull(rawRoomId, "Room ID" + " must not be null");
3740

@@ -58,6 +61,7 @@ public boolean equals(Object obj) {
5861
}
5962

6063
@Override
64+
@JsonValue
6165
public String toString() {
6266
return "!" + opaqueId + ":" + domain;
6367
}

‎src/main/java/io/github/hikingc/matrixsdk/api/identifiers/UserID.java‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package io.github.hikingc.matrixsdk.api.identifiers;
22

3+
import com.fasterxml.jackson.annotation.JsonCreator;
4+
import com.fasterxml.jackson.annotation.JsonValue;
35
import java.util.Objects;
46

57
/// This class allows for the representation and validation of a User Identifier in Matrix.
@@ -30,6 +32,7 @@ private UserID(String opaqueId, String domain) {
3032
/// @return a [UserID].
3133
/// @throws IllegalArgumentException if the [String] has broken a rule from the spec.
3234
/// @throws NullPointerException if the [String] is null.
35+
@JsonCreator
3336
public static UserID parse(String rawUserId) {
3437
Objects.requireNonNull(rawUserId, "User ID" + " must not be null");
3538

@@ -61,6 +64,7 @@ public boolean equals(Object obj) {
6164
}
6265

6366
@Override
67+
@JsonValue
6468
public String toString() {
6569
return "@" + localpart + ":" + domain;
6670
}

‎src/main/java/io/github/hikingc/matrixsdk/api/rooms/RoomMembershipRequest.java‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package io.github.hikingc.matrixsdk.api.rooms;
22

3+
import io.github.hikingc.matrixsdk.api.identifiers.UserID;
4+
35
import java.util.Objects;
46

57
/// This record represents the required values to be supplied to actions like banning or kicking.
68
///
79
/// @param reason The reason of the expulsion, the target will receive this message.
810
/// @param userId The id of the target to expel.
9-
public record RoomMembershipRequest(String reason, String userId) {
11+
public record RoomMembershipRequest(String reason, UserID userId) {
1012

1113
/// Compact constructor designed to validate nullity.
1214
///

‎src/main/java/io/github/hikingc/matrixsdk/api/rooms/ThirdPartySigned.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.fasterxml.jackson.annotation.JsonProperty;
44
import java.util.Map;
5+
6+
import io.github.hikingc.matrixsdk.api.identifiers.UserID;
57
import org.jspecify.annotations.NullMarked;
68

79
/// Holds information to supply the server and verify a `m.room.third_party_invite` event.
@@ -12,7 +14,7 @@
1214
/// @param token the state key of the `m.third_party_invite` event.
1315
@NullMarked
1416
public record ThirdPartySigned(
15-
@JsonProperty(required = true) String mxid,
16-
@JsonProperty(required = true) String sender,
17+
@JsonProperty(required = true) UserID mxid,
18+
@JsonProperty(required = true) UserID sender,
1719
@JsonProperty(required = true) Map<String, Map<String, String>> signatures,
1820
@JsonProperty(required = true) String token) {}

‎src/main/java/io/github/hikingc/matrixsdk/api/rooms/models/PublishedRoomsChunk.java‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
import com.fasterxml.jackson.annotation.JsonProperty;
44
import io.github.hikingc.matrixsdk.api.Room;
5+
import io.github.hikingc.matrixsdk.api.identifiers.RoomAlias;
6+
import io.github.hikingc.matrixsdk.api.identifiers.RoomID;
57
import io.github.hikingc.matrixsdk.api.identifiers.Validator;
68
import java.net.URI;
79
import java.util.List;
@@ -25,12 +27,12 @@
2527
/// additional values for a determinate room
2628
public record PublishedRoomsChunk(
2729
URI avatarUrl,
28-
String canonicalAlias,
30+
RoomAlias canonicalAlias,
2931
@JsonProperty(required = true) boolean guestCanJoin,
3032
String joinRule,
3133
String name,
3234
@JsonProperty(required = true) int numJoinedMembers,
33-
@NonNull @JsonProperty(required = true) String roomId,
35+
@NonNull @JsonProperty(required = true) RoomID roomId,
3436
String roomType,
3537
String topic,
3638
@JsonProperty(required = true) boolean worldReadable) {}
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
package io.github.hikingc.matrixsdk.api.rooms.models;
22

3+
import io.github.hikingc.matrixsdk.api.identifiers.RoomID;
34
import java.util.List;
45

56
/// This record contains data when resolving a room alias.
67
///
78
/// @param roomId the room id for the room alias.
89
/// @param servers a list of servers aware of said alias.
9-
public record ResolvedAlias(String roomId, List<String> servers) {}
10+
public record ResolvedAlias(RoomID roomId, List<String> servers) {}

‎src/test/java/io/github/hikingc/matrixsdk/services/events/EventServiceTest.java‎

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
import com.github.tomakehurst.wiremock.junit5.WireMockTest;
1010
import io.github.hikingc.matrixsdk.api.MatrixClient;
1111
import io.github.hikingc.matrixsdk.api.events.*;
12+
import io.github.hikingc.matrixsdk.api.events.content.RoomJoinRules;
1213
import io.github.hikingc.matrixsdk.api.events.content.RoomMessage;
14+
import io.github.hikingc.matrixsdk.api.events.content.StateEventContent;
1315
import io.github.hikingc.matrixsdk.api.events.content.roommessages.FileContent;
1416
import io.github.hikingc.matrixsdk.api.events.content.roommessages.TextContent;
1517
import io.github.hikingc.matrixsdk.api.events.queries.ChronologicalDirection;
@@ -538,7 +540,9 @@ void getInitialSync_WithACorrectPayload_ThenReturnRoomInfo() {
538540

539541
@Test
540542
void sendStateEvent_WithACorrectPayload_ThenReturnAString() {
541-
// TODO pending interface to confirm fields to assert
543+
StateEventContent content = new RoomJoinRules(new RoomJoinRules.AllowCondition("EXAMPLE","TYPE"),"JOINRULE");
544+
var response = client.events().sendStateEvent(ROOM_ID,"",content);
545+
assertThat(response).isNotNull();
542546
}
543547

544548
@Test

‎src/test/java/io/github/hikingc/matrixsdk/services/rooms/RoomServiceTest.java‎

Lines changed: 36 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import io.github.hikingc.matrixsdk.api.events.content.RoomPowerLevels;
1010
import io.github.hikingc.matrixsdk.api.identifiers.RoomAlias;
1111
import io.github.hikingc.matrixsdk.api.identifiers.RoomID;
12+
import io.github.hikingc.matrixsdk.api.identifiers.UserID;
1213
import io.github.hikingc.matrixsdk.api.identifiers.Validator;
1314
import io.github.hikingc.matrixsdk.api.rooms.*;
1415
import io.github.hikingc.matrixsdk.api.rooms.queries.CreationRoomType;
@@ -17,7 +18,6 @@
1718
import io.github.hikingc.matrixsdk.context.DiscoveryResponse;
1819
import java.util.List;
1920
import java.util.Map;
20-
2121
import org.junit.jupiter.api.BeforeAll;
2222
import org.junit.jupiter.api.BeforeEach;
2323
import org.junit.jupiter.api.Test;
@@ -26,6 +26,7 @@
2626
class RoomServiceTest {
2727

2828
private static final RoomID ROOM_ID = RoomID.parse("!ekkTuJPNWnbuCJHvYB:kde.org");
29+
private static final UserID USER_ID = UserID.parse("@example:example.com");
2930
private static final String AUTH_TOKEN = "1234";
3031
private static MatrixClient client;
3132
private static DiscoveryResponse DISCOVERY_RESPONSE;
@@ -116,51 +117,40 @@ void sendCreateRequest_WithACorrectPayload_thenReturnARoomId() {
116117
{ "room_id": "%s" }
117118
"""
118119
.formatted(expectedRoomId))));
119-
InitialRoomConfiguration config = new InitialRoomConfiguration(
120+
InitialRoomConfiguration config =
121+
new InitialRoomConfiguration(
120122
new InitialRoomConfiguration.CreationContent(false), // m.federate: false
121123
List.of(
122-
new InitialRoomConfiguration.StateEvent(
123-
Map.of("join_rule", "public"),
124-
"",
125-
"m.room.join_rules"),
126-
new InitialRoomConfiguration.StateEvent(
127-
Map.of("history_visibility", "shared"),
128-
"",
129-
"m.room.history_visibility")
130-
),
124+
new InitialRoomConfiguration.StateEvent(
125+
Map.of("join_rule", "public"), "", "m.room.join_rules"),
126+
new InitialRoomConfiguration.StateEvent(
127+
Map.of("history_visibility", "shared"), "", "m.room.history_visibility")),
131128
List.of("@alice:example.com", "@bob:example.com"),
132129
List.of(
133-
new InitialRoomConfiguration.Invite3pid(
134-
"alice@example.com",
135-
"abc123_OpaqueString",
136-
"identity.example.com",
137-
"email")
138-
),
130+
new InitialRoomConfiguration.Invite3pid(
131+
"alice@example.com", "abc123_OpaqueString", "identity.example.com", "email")),
139132
false, // is_direct
140133
"The Grand Duke Pub",
141134
new RoomPowerLevels(
142-
50, // ban
143-
Map.of(
144-
"m.room.name", 50,
145-
"m.room.power_levels", 100
146-
), // events
147-
0, // eventsDefault
148-
0, // invite
149-
50, // kick
150-
null, // notifications — not present in source JSON
151-
50, // redact
152-
50, // stateDefault
153-
Map.of("@alice:example.com", 100), // users
154-
0 // users_default
155-
),
135+
50, // ban
136+
Map.of(
137+
"m.room.name", 50,
138+
"m.room.power_levels", 100), // events
139+
0, // eventsDefault
140+
0, // invite
141+
50, // kick
142+
null, // notifications — not present in source JSON
143+
50, // redact
144+
50, // stateDefault
145+
Map.of("@alice:example.com", 100), // users
146+
0 // users_default
147+
),
156148
CreationRoomType.PRIVATE_CHAT,
157149
"thepub",
158150
"11",
159151
"All about happy hour",
160-
VisibilityRoomType.PRIVATE
161-
);
162-
var response =
163-
client.room().create(config);
152+
VisibilityRoomType.PRIVATE);
153+
var response = client.room().create(config);
164154
assertEquals(expectedRoomId, response);
165155
}
166156

@@ -209,7 +199,7 @@ void sendResolveAliasRequest_WithCorrectPayload_thenReturnResolvedAlias() {
209199
var response = client.room().resolveAlias(alias);
210200

211201
assertNotNull(response);
212-
assertEquals(ROOM_ID.toString(), response.roomId());
202+
assertEquals(ROOM_ID, response.roomId());
213203
assertFalse(response.servers().isEmpty());
214204
}
215205

@@ -286,7 +276,8 @@ void sendInviteRequest_WithCorrectPayload_thenHitCorrectEndpoint() {
286276
true))
287277
.willReturn(okJson("{}")));
288278

289-
client.room().inviteUser(ROOM_ID, new RoomMembershipRequest("Welcome!", "@alice:example.com"));
279+
client.room().inviteUser(
280+
ROOM_ID, new RoomMembershipRequest("Welcome!", UserID.parse("@alice:example.com")));
290281

291282
verify(postRequestedFor(urlEqualTo("/_matrix/client/v3/rooms/" + ROOM_ID + "/invite")));
292283
}
@@ -373,14 +364,14 @@ void sendKickRequest_WithCorrectPayload_thenHitCorrectEndpoint() {
373364
"""
374365
{
375366
"reason": "Test reason",
376-
"user_id": "user"
367+
"user_id": "@example:example.com"
377368
}
378369
""",
379370
true,
380371
true))
381372
.willReturn(okJson("{}")));
382373

383-
client.room().kick(ROOM_ID, new RoomMembershipRequest("Test reason", "user"));
374+
client.room().kick(ROOM_ID, new RoomMembershipRequest("Test reason", USER_ID));
384375

385376
verify(postRequestedFor(urlEqualTo("/_matrix/client/v3/rooms/" + ROOM_ID + "/kick")));
386377
}
@@ -394,14 +385,14 @@ void sendBanRequest_WithCorrectPayload_thenHitCorrectEndpoint() {
394385
"""
395386
{
396387
"reason": "Test reason",
397-
"user_id": "user"
388+
"user_id": "@example:example.com"
398389
}
399390
""",
400391
true,
401392
true))
402393
.willReturn(okJson("{}")));
403394

404-
client.room().ban(ROOM_ID, new RoomMembershipRequest("Test reason", "user"));
395+
client.room().ban(ROOM_ID, new RoomMembershipRequest("Test reason", USER_ID));
405396

406397
verify(postRequestedFor(urlEqualTo("/_matrix/client/v3/rooms/" + ROOM_ID + "/ban")));
407398
}
@@ -415,14 +406,14 @@ void sendUnbanRequest_WithCorrectPayload_thenHitCorrectEndpoint() {
415406
"""
416407
{
417408
"reason": "Test reason",
418-
"user_id": "user"
409+
"user_id": "@example:example.com"
419410
}
420411
""",
421412
true,
422413
true))
423414
.willReturn(okJson("{}")));
424415

425-
client.room().unban(ROOM_ID, new RoomMembershipRequest("Test reason", "user"));
416+
client.room().unban(ROOM_ID, new RoomMembershipRequest("Test reason", USER_ID));
426417

427418
verify(postRequestedFor(urlEqualTo("/_matrix/client/v3/rooms/" + ROOM_ID + "/unban")));
428419
}
@@ -488,7 +479,7 @@ void sendGetPublicRoomDirRequest_WithQueryParams_thenReturnDirectory() {
488479

489480
assertNotNull(response);
490481
assertNotNull(response.chunk());
491-
assertEquals("!abc123:example.com", response.chunk().getFirst().roomId());
482+
assertEquals(RoomID.parse("!abc123:example.com"), response.chunk().getFirst().roomId());
492483
assertEquals("General", response.chunk().getFirst().name());
493484
assertEquals(1, response.totalRoomCountEstimate());
494485
}
@@ -520,7 +511,7 @@ void sendGetPublicRoomDirPostRequest_WithBody_thenReturnDirectory() {
520511

521512
assertNotNull(response);
522513
assertFalse(response.chunk().isEmpty());
523-
assertEquals("!abc123:example.com", response.chunk().getFirst().roomId());
514+
assertEquals(RoomID.parse("!abc123:example.com"), response.chunk().getFirst().roomId());
524515
}
525516

526517
@Test

0 commit comments

Comments
 (0)