Skip to content
This repository was archived by the owner on Jun 21, 2019. It is now read-only.

Commit eacd67a

Browse files
MrCustomizerMax Dor
MrCustomizer
authored and
Max Dor
committed
Test with homeserver (#14)
Refactor unit tests to allow integration tests against a real Homeserver
1 parent 9130cc4 commit eacd67a

21 files changed

+961
-255
lines changed

README.md

+14
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,20 @@ dependencies {
3838
```
3939
**WARNING:** This SDK was originally created to support [Kamax.io projects](https://github.com/kamax-io) and is therefore not necessarly complete. It will be built as the various projects evolve and grow. The SDK is therefore still in Alpha.
4040

41+
## Tests
42+
### Unit tests
43+
The unit tests of this project are located under `src/test`. In these tests the http calls against the homeserver are mocked with [Wiremock](http://wiremock.org/). The tests can be run by executing Gradle's test task: `./gradlew test`.
44+
45+
### Integration tests
46+
The integration tests are located under `src/testInt` and are run against a homeserver. Therefore a server name
47+
and user credentials have to be provided in the config file with the name `src/testInt/resources/HomeserverTest.conf` to run these tests. A template configuration file exists in the
48+
same directory with the name `HomeserverTest.conf_template`. The configuration file is ignored by Git and will not be checked in when comitting to the repository.
49+
50+
To run the integration tests, please use the task testInt: `./gradlew testInt`.
51+
52+
**WARNING:** At the moment, most of the integration tests fail as the test cases are not yet adjusted to be run against a real homeserver.
53+
54+
4155
## Contribute
4256
Contributions and PRs are welcome to turn this into a fully fledged Matrix Java SDK.
4357
Your code will be licensed under AGPLv3

build.gradle

+23
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,14 @@ repositories {
5959
mavenCentral()
6060
}
6161

62+
sourceSets {
63+
testInt {
64+
compileClasspath += sourceSets.test.compileClasspath
65+
runtimeClasspath += sourceSets.test.runtimeClasspath
66+
}
67+
}
68+
69+
6270
dependencies {
6371
compile 'org.slf4j:log4j-over-slf4j:1.7.25'
6472
compile 'commons-lang:commons-lang:2.6'
@@ -72,6 +80,15 @@ dependencies {
7280
testCompile "com.github.tomakehurst:wiremock:2.8.0"
7381
testRuntimeOnly 'org.slf4j:slf4j-simple:1.7.25'
7482

83+
testIntCompile sourceSets.main.output
84+
testIntCompile sourceSets.test.output
85+
86+
testIntCompile configurations.compile
87+
testIntCompile configurations.testCompile
88+
89+
testIntRuntime configurations.runtime
90+
testIntRuntime configurations.testRuntime
91+
7592
deployerJars "org.apache.maven.wagon:wagon-ssh:2.2"
7693
}
7794

@@ -116,3 +133,9 @@ spotless {
116133
junitPlatform {
117134
enableStandardTestTask true
118135
}
136+
137+
task testInt(type: Test) {
138+
description = "Integration tests which require a Homeserver"
139+
testClassesDir = sourceSets.testInt.output.classesDir
140+
classpath = sourceSets.testInt.runtimeClasspath
141+
}

src/test/java/io/kamax/matrix/MatrixHttpUserTest.java src/test/java/io/kamax/matrix/AMatrixHttpUserTest.java

+4-23
Original file line numberDiff line numberDiff line change
@@ -31,84 +31,65 @@
3131
import java.net.URISyntaxException;
3232
import java.util.Optional;
3333

34-
import static com.github.tomakehurst.wiremock.client.WireMock.*;
35-
3634
import static org.hamcrest.core.Is.is;
3735
import static org.hamcrest.core.IsEqual.equalTo;
3836
import static org.junit.Assert.assertThat;
3937
import static org.junit.jupiter.api.Assertions.assertThrows;
4038
import static org.junit.jupiter.api.Assertions.assertTrue;
4139

42-
public class MatrixHttpUserTest extends MatrixHttpTest {
43-
private String nameUrl = String.format("/_matrix/client/r0/profile/%s/displayname", user.getId()) + tokenParameter;
44-
private String nameResponse = String.format("{\"displayname\": \"%s\"}", nameOfUser);
45-
46-
private String avatarUrl = String.format("/_matrix/client/r0/profile/%s/avatar_url", user.getId()) + tokenParameter;
47-
private String avatarMediaUrl = "mxc://matrix.org/wefh34uihSDRGhw34";
48-
private String avatarResponse = String.format("{\"avatar_url\": \"%s\"}", avatarMediaUrl);
40+
public abstract class AMatrixHttpUserTest extends MatrixHttpTest {
41+
protected String avatarMediaUrl = "mxc://matrix.org/wefh34uihSDRGhw34";
4942

5043
@Test
5144
public void getName() throws URISyntaxException {
52-
stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(200).withBody(nameResponse)));
53-
assertThat(createUserObject().getName(), is(equalTo(Optional.of(nameOfUser))));
45+
assertThat(createUserObject().getName(), is(equalTo(Optional.of(username))));
5446
}
5547

5648
@Test
5749
public void getName404() throws URISyntaxException {
58-
stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)));
5950
assertThat(createUserObject().getName(), is(equalTo(Optional.empty())));
6051
}
6152

6253
@Test
6354
public void getNameError403() throws URISyntaxException {
64-
stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)));
65-
6655
MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createUserObject()::getName);
6756
checkErrorInfo403(e);
6857
}
6958

7059
@Test
7160
public void getNameError429() throws URISyntaxException {
72-
stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)));
73-
7461
MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createUserObject()::getName);
7562
checkErrorInfo429(e);
7663
}
7764

7865
@Test
7966
public void getAvatar() throws URISyntaxException {
80-
stubFor(get(urlEqualTo(avatarUrl)).willReturn(aResponse().withStatus(200).withBody(avatarResponse)));
8167
Optional<_MatrixContent> matrixContent = createUserObject().getAvatar();
8268
assertTrue(matrixContent.isPresent());
8369
assertThat(matrixContent.get().getAddress(), is(equalTo(new URI(avatarMediaUrl))));
8470
}
8571

8672
@Test
8773
public void getAvatar404() throws URISyntaxException {
88-
stubFor(get(urlEqualTo(avatarUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)));
8974
assertThat(createUserObject().getAvatar(), is(equalTo(Optional.empty())));
9075
}
9176

9277
@Test
9378
public void getAvatarError403() throws URISyntaxException {
94-
stubFor(get(urlEqualTo(avatarUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)));
95-
9679
MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class,
9780
createUserObject()::getAvatar);
9881
checkErrorInfo403(e);
9982
}
10083

10184
@Test
10285
public void getAvatarError429() throws URISyntaxException {
103-
stubFor(get(urlEqualTo(avatarUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)));
104-
10586
MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class,
10687
createUserObject()::getAvatar);
10788
checkErrorInfo429(e);
10889
}
10990

11091
private MatrixHttpUser createUserObject() throws URISyntaxException {
111-
MatrixClientContext context = createClientContext();
92+
MatrixClientContext context = getOrCreateClientContext();
11293
return new MatrixHttpUser(context, context.getUser().get());
11394
}
11495
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
* matrix-java-sdk - Matrix Client SDK for Java
3+
* Copyright (C) 2017 Arne Augenstein
4+
*
5+
* https://max.kamax.io/
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Affero General Public License as
9+
* published by the Free Software Foundation, either version 3 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
21+
package io.kamax.matrix;
22+
23+
import org.junit.Test;
24+
25+
import java.net.URISyntaxException;
26+
27+
import static com.github.tomakehurst.wiremock.client.WireMock.*;
28+
29+
public class MatrixHttpUserWiremockTest extends AMatrixHttpUserTest {
30+
private String nameUrl = String.format("/_matrix/client/r0/profile/%s/displayname", user.getId()) + tokenParameter;
31+
private String nameResponse = String.format("{\"displayname\": \"%s\"}", username);
32+
33+
private String avatarUrl = String.format("/_matrix/client/r0/profile/%s/avatar_url", user.getId()) + tokenParameter;
34+
private String avatarResponse = String.format("{\"avatar_url\": \"%s\"}", avatarMediaUrl);
35+
36+
@Override
37+
public void login() throws URISyntaxException {
38+
}
39+
40+
@Override
41+
public void logout() {
42+
}
43+
44+
@Test
45+
public void getName() throws URISyntaxException {
46+
stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(200).withBody(nameResponse)));
47+
super.getName();
48+
}
49+
50+
@Test
51+
public void getName404() throws URISyntaxException {
52+
stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)));
53+
super.getName404();
54+
}
55+
56+
@Test
57+
public void getNameError403() throws URISyntaxException {
58+
stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)));
59+
super.getNameError403();
60+
}
61+
62+
@Test
63+
public void getNameError429() throws URISyntaxException {
64+
stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)));
65+
super.getNameError429();
66+
}
67+
68+
@Test
69+
public void getAvatar() throws URISyntaxException {
70+
stubFor(get(urlEqualTo(avatarUrl)).willReturn(aResponse().withStatus(200).withBody(avatarResponse)));
71+
super.getAvatar();
72+
}
73+
74+
@Test
75+
public void getAvatar404() throws URISyntaxException {
76+
stubFor(get(urlEqualTo(avatarUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)));
77+
super.getAvatar404();
78+
}
79+
80+
@Test
81+
public void getAvatarError403() throws URISyntaxException {
82+
stubFor(get(urlEqualTo(avatarUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)));
83+
super.getAvatarError403();
84+
}
85+
86+
@Test
87+
public void getAvatarError429() throws URISyntaxException {
88+
stubFor(get(urlEqualTo(avatarUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)));
89+
super.getAvatarError429();
90+
}
91+
92+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*
2+
* matrix-java-sdk - Matrix Client SDK for Java
3+
* Copyright (C) 2017 Arne Augenstein
4+
*
5+
* https://max.kamax.io/
6+
*
7+
* This program is free software: you can redistribute it and/or modify
8+
* it under the terms of the GNU Affero General Public License as
9+
* published by the Free Software Foundation, either version 3 of the
10+
* License, or (at your option) any later version.
11+
*
12+
* This program is distributed in the hope that it will be useful,
13+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
14+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15+
* GNU Affero General Public License for more details.
16+
*
17+
* You should have received a copy of the GNU Affero General Public License
18+
* along with this program. If not, see <http://www.gnu.org/licenses/>.
19+
*/
20+
21+
package io.kamax.matrix.client;
22+
23+
import org.hamcrest.core.IsEqual;
24+
import org.junit.Test;
25+
26+
import java.io.File;
27+
import java.io.IOException;
28+
import java.net.URI;
29+
import java.net.URISyntaxException;
30+
import java.nio.file.Files;
31+
import java.nio.file.Paths;
32+
import java.util.Optional;
33+
34+
import static org.hamcrest.MatcherAssert.assertThat;
35+
import static org.junit.jupiter.api.Assertions.*;
36+
37+
/*
38+
* TODO As the spec is outdated, I'm not sure if the error 403 can really happen in these test cases. This class has
39+
* to be checked for correctness, when matrix's spec is updated.
40+
*/
41+
public abstract class AMatrixHttpContentTest extends MatrixHttpTest {
42+
protected String bodyFilename = "textfile.txt";
43+
protected URI address = new URI("mxc://localhost/testpath/" + bodyFilename);
44+
45+
public AMatrixHttpContentTest() throws URISyntaxException {
46+
}
47+
48+
@Test
49+
public void isValid() throws URISyntaxException {
50+
assertTrue(createContentObject().isValid());
51+
}
52+
53+
@Test
54+
public void isValidMissingContentType() throws URISyntaxException {
55+
assertTrue(createContentObject().isValid());
56+
}
57+
58+
@Test
59+
public void isValidContentNotFound() throws URISyntaxException {
60+
assertFalse(createContentObject().isValid());
61+
}
62+
63+
@Test
64+
public void isValidErrorAccessDenied() throws URISyntaxException {
65+
assertFalse(createContentObject().isValid());
66+
}
67+
68+
@Test
69+
public void getType() throws URISyntaxException, IOException {
70+
assertEquals(Optional.of("text/plain"), createContentObject().getType());
71+
}
72+
73+
@Test
74+
public void getTypeMissingContentType() throws URISyntaxException {
75+
assertEquals(Optional.empty(), createContentObject().getType());
76+
}
77+
78+
@Test
79+
public void getTypeErrorContentNotFound() throws URISyntaxException, IOException {
80+
MatrixHttpContent contentObject = createContentObject();
81+
assertFalse(contentObject.isValid());
82+
assertThrows(IllegalStateException.class, contentObject::getType);
83+
}
84+
85+
@Test
86+
public void getTypeErrorAccessDenied() throws URISyntaxException, IOException {
87+
MatrixHttpContent contentObject = createContentObject();
88+
assertFalse(contentObject.isValid());
89+
assertThrows(IllegalStateException.class, createContentObject()::getType);
90+
}
91+
92+
@Test
93+
public void getData() throws URISyntaxException, IOException {
94+
byte[] expectedResult = Files.readAllBytes(Paths.get(ClassLoader
95+
.getSystemResource("wiremock" + File.separator + "__files" + File.separator + bodyFilename).toURI()));
96+
assertThat(createContentObject().getData(), IsEqual.equalTo(expectedResult));
97+
}
98+
99+
@Test
100+
public void getDataMissingContentType() throws URISyntaxException, IOException {
101+
byte[] expectedResult = Files.readAllBytes(Paths.get(ClassLoader
102+
.getSystemResource("wiremock" + File.separator + "__files" + File.separator + bodyFilename).toURI()));
103+
assertThat(createContentObject().getData(), IsEqual.equalTo(expectedResult));
104+
}
105+
106+
@Test
107+
public void getDataErrorContentNotFound() throws URISyntaxException, IOException {
108+
MatrixHttpContent contentObject = createContentObject();
109+
assertFalse(contentObject.isValid());
110+
assertThrows(IllegalStateException.class, contentObject::getData);
111+
}
112+
113+
@Test
114+
public void getDataErrorAccessDenied() throws URISyntaxException, IOException {
115+
MatrixHttpContent contentObject = createContentObject();
116+
assertFalse(contentObject.isValid());
117+
assertThrows(IllegalStateException.class, contentObject::getData);
118+
}
119+
120+
@Test
121+
public void getFilename() throws URISyntaxException, IOException {
122+
assertEquals(Optional.of(bodyFilename), createContentObject().getFilename());
123+
}
124+
125+
@Test
126+
public void getFilenameMissingContentType() throws URISyntaxException {
127+
assertEquals(Optional.empty(), createContentObject().getFilename());
128+
}
129+
130+
@Test
131+
public void getFilenameErrorContentNotFound() throws URISyntaxException, IOException {
132+
MatrixHttpContent contentObject = createContentObject();
133+
assertFalse(contentObject.isValid());
134+
assertThrows(IllegalStateException.class, contentObject::getFilename);
135+
}
136+
137+
@Test
138+
public void getFilenameErrorAccessDenied() throws URISyntaxException, IOException {
139+
MatrixHttpContent contentObject = createContentObject();
140+
assertFalse(contentObject.isValid());
141+
assertThrows(IllegalStateException.class, contentObject::getFilename);
142+
}
143+
144+
private MatrixHttpContent createContentObject() throws URISyntaxException {
145+
MatrixClientContext context = getOrCreateClientContext();
146+
return new MatrixHttpContent(context, address);
147+
}
148+
149+
}

0 commit comments

Comments
 (0)