forked from kamax-matrix/matrix-java-sdk
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMatrixHttpRoomTest.java
266 lines (212 loc) · 11.5 KB
/
MatrixHttpRoomTest.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
/*
* matrix-java-sdk - Matrix Client SDK for Java
* Copyright (C) 2017 Arne Augenstein
*
* https://max.kamax.io/
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as
* published by the Free Software Foundation, either version 3 of the
* License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package io.kamax.matrix.client;
import io.kamax.matrix.MatrixID;
import io.kamax.matrix._MatrixID;
import org.hamcrest.core.IsEqual;
import org.junit.Test;
import java.net.URISyntaxException;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import static com.github.tomakehurst.wiremock.client.WireMock.*;
import static org.junit.Assert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
public class MatrixHttpRoomTest extends MatrixHttpTest {
private String roomId = "roomId892347847";
private String eventId = "YUwRidLecu";
private String nameUrl = String.format("/_matrix/client/r0/rooms/%s/state/m.room.name", roomId) + tokenParameter;
private String nameOfRoom = "test room";
private String nameResponse = String.format("{\"name\": \"%s\"}", nameOfRoom);
private String topicUrl = String.format("/_matrix/client/r0/rooms/%s/state/m.room.topic", roomId) + tokenParameter;
private String testTopic = "test topic";
private String topicResponse = String.format("{\"topic\": \"%s\"}", testTopic);
private String joinUrl = String.format("/_matrix/client/r0/rooms/%s/join", roomId) + tokenParameter;
private String joinResponse = String.format("{\"roomId\": \"%s\"}", roomId);
private String leaveUrl = String.format("/_matrix/client/r0/rooms/%s/leave", roomId) + tokenParameter;
private String leaveResponse = String.format("{\"roomId\": \"%s\"}", roomId);
private String sendTextUrl = String.format("/_matrix/client/r0/rooms/%s/send/m.room.message/([0-9.]+)\\", roomId)
+ tokenParameter;
private String testText = "test text";
private String sendTextResponse = String.format("{\"event_id\": \"%s\"}", eventId) + tokenParameter;
private String getJoinedUsersUrl = String.format("/_matrix/client/r0/rooms/%s/joined_members", roomId)
+ tokenParameter;
private String joinedUser1 = "@test:testserver.org";
private String joinedUser2 = "@test2:testserver.org";
private String getJoinedUsersResponse = String.format("{\"joined\": {\"%s\": \"1\", \"%s\": \"2\"}}", joinedUser1,
joinedUser2);
@Test
public void getName() throws URISyntaxException {
stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(200).withBody(nameResponse)));
assertThat(createRoomObject().getName(), IsEqual.equalTo(Optional.of(nameOfRoom)));
}
@Test
public void getName404() throws URISyntaxException {
stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)));
assertThat(createRoomObject().getName(), IsEqual.equalTo(Optional.empty()));
}
@Test
public void getNameError403() throws URISyntaxException {
stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)));
MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getName);
// TODO After the refactoring, the exception will contain a proper MatrixErrorInfo object
// checkErrorInfo403(e);
}
@Test
public void getNameError429() throws URISyntaxException {
stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(429).withBody(error403Response)));
MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getName);
// TODO After the refactoring, the exception will contain a proper MatrixErrorInfo object
// checkErrorInfo429(e);
}
@Test
public void getTopic() throws URISyntaxException {
stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(200).withBody(topicResponse)));
assertThat(createRoomObject().getTopic(), IsEqual.equalTo(Optional.of(testTopic)));
}
@Test
public void getTopicError404() throws URISyntaxException {
stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(404).withBody(topicResponse)));
assertThat(createRoomObject().getTopic(), IsEqual.equalTo(Optional.empty()));
}
@Test
public void getTopicError403() throws URISyntaxException {
stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)));
MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getTopic);
checkErrorInfo403(e);
}
@Test
public void getTopicError429() throws URISyntaxException {
stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)));
MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getTopic);
checkErrorInfo429(e);
}
@Test
public void join() throws URISyntaxException {
stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(200).withBody(joinResponse)));
createRoomObject().join();
}
@Test
public void joinError404() throws URISyntaxException {
stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)));
MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::join);
checkErrorInfo404(e);
}
@Test
public void joinError403() throws URISyntaxException {
stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)));
// TODO After the refactoring, this test will throw a MatrixClientRequestException
createRoomObject().join();
// MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::join);
// checkErrorInfo403(e);
}
@Test
public void joinError429() throws URISyntaxException {
stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)));
MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::join);
checkErrorInfo429(e);
}
@Test
public void leave() throws URISyntaxException {
stubFor(post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(200).withBody(leaveResponse)));
createRoomObject().leave();
}
@Test
public void leaveError403() throws URISyntaxException {
stubFor(post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)));
// TODO After the refactoring, this test will throw a MatrixClientRequestException
createRoomObject().leave();
// MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::leave);
// checkErrorInfo403(e);
}
@Test
public void leaveError404() throws URISyntaxException {
stubFor(post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)));
// TODO After the refactoring, this test will throw a MatrixClientRequestException
createRoomObject().leave();
// MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::leave);
// checkErrorInfo404(e);
}
@Test
public void leaveError429() throws URISyntaxException {
stubFor(post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)));
MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::leave);
checkErrorInfo429(e);
}
@Test
public void sendText() throws URISyntaxException {
stubFor(put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(200).withBody(sendTextResponse)));
createRoomObject().sendText(testText);
}
@Test
public void sendTextError403() throws URISyntaxException {
stubFor(put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)));
// TODO After the refactoring, this test will throw a MatrixClientRequestException
createRoomObject().sendText(testText);
// MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class,
// () -> createRoomObject().sendText(testText));
// checkErrorInfo404(e);
}
@Test
public void sendTextError404() throws URISyntaxException {
stubFor(put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)));
MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class,
() -> createRoomObject().sendText(testText));
// TODO After the refactoring, the exception will contain a proper MatrixErrorInfo object
// checkErrorInfo404(e);
}
@Test
public void sendTextError429() throws URISyntaxException {
stubFor(put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)));
MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class,
() -> createRoomObject().sendText(testText));
// TODO After the refactoring, the exception will contain a proper MatrixErrorInfo object
// checkErrorInfo429(e);
}
@Test
public void getJoinedUsers() throws URISyntaxException {
stubFor(get(urlEqualTo(getJoinedUsersUrl))
.willReturn(aResponse().withStatus(200).withBody(getJoinedUsersResponse)));
List<_MatrixID> expectedResult = new ArrayList<>();
expectedResult.add(new MatrixID(joinedUser1));
expectedResult.add(new MatrixID(joinedUser2));
assertThat(createRoomObject().getJoinedUsers(), IsEqual.equalTo(expectedResult));
}
@Test
public void getJoinedUsersError404() throws URISyntaxException {
stubFor(get(urlEqualTo(getJoinedUsersUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)));
MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class,
createRoomObject()::getJoinedUsers);
// TODO After the refactoring, the exception will contain a proper MatrixErrorInfo object
// checkErrorInfo404(e);
}
@Test
public void getJoinedUsers429() throws URISyntaxException {
stubFor(get(urlEqualTo(getJoinedUsersUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)));
MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class,
createRoomObject()::getJoinedUsers);
// TODO After the refactoring, the exception will contain a proper MatrixErrorInfo object
// checkErrorInfo404(e);
}
private MatrixHttpRoom createRoomObject() throws URISyntaxException {
MatrixClientContext context = createClientContext();
return new MatrixHttpRoom(context, roomId);
}
}