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

Commit 0160179

Browse files
MrCustomizerMax Dor
MrCustomizer
authored and
Max Dor
committed
Add first round of unit tests
* Create first wiremock tests * Add tests for MatrixApplicationServiceClient * Refactor test class structure * Create builders and add some more tests * Fix regular expression * Fix error in runner * Add more tests for MatrixHttpContent * Refactor test runners * Add more tests * Add sendText test cases * Add tests for getting joined users * Refactor handling of strings * Rename builder classes * Fix formatting issue * Refactor topic tests * Refactor test of getName * Correct regular expression * Correct comment * Move to JUnit 5 * Revert to JUnit 5.0.0 * Port MatrixHttpRoomTest to JUnit 5 * Port the rest of the tests to JUnit 5
1 parent b1057e7 commit 0160179

File tree

8 files changed

+678
-2
lines changed

8 files changed

+678
-2
lines changed

build.gradle

+16-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@
1717
* You should have received a copy of the GNU Affero General Public License
1818
* along with this program. If not, see <http://www.gnu.org/licenses/>.
1919
*/
20+
buildscript {
21+
repositories {
22+
mavenCentral()
23+
}
24+
dependencies {
25+
classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.1'
26+
}
27+
}
2028

2129
plugins {
2230
id "com.diffplug.gradle.spotless" version "3.5.2"
@@ -27,6 +35,7 @@ version = '0.0.2'
2735

2836
apply plugin: 'java'
2937
apply plugin: 'maven'
38+
apply plugin: 'org.junit.platform.gradle.plugin'
3039

3140
configurations {
3241
deployerJars
@@ -43,7 +52,9 @@ dependencies {
4352
compile 'org.apache.httpcomponents:fluent-hc:4.5.3'
4453
compile 'com.google.code.gson:gson:2.8.0'
4554

46-
testCompile 'junit:junit:4.12'
55+
testCompile "org.junit.jupiter:junit-jupiter-api:5.0.0"
56+
testRuntime "org.junit.jupiter:junit-jupiter-engine:5.0.0"
57+
testCompile "com.github.tomakehurst:wiremock:2.8.0"
4758
testRuntimeOnly 'org.slf4j:slf4j-simple:1.7.25'
4859

4960
deployerJars "org.apache.maven.wagon:wagon-ssh:2.2"
@@ -86,3 +97,7 @@ spotless {
8697
importOrderFile 'spotless.importorder'
8798
}
8899
}
100+
101+
junitPlatform {
102+
enableStandardTestTask true
103+
}

src/main/java/io/kamax/matrix/client/MatrixHttpContent.java

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ public class MatrixHttpContent extends AMatrixHttpClient implements _MatrixConte
4545

4646
private Logger log = LoggerFactory.getLogger(MatrixHttpContent.class);
4747

48-
private final Pattern filenamePattern = Pattern.compile("filename=\"?(?<filename>.+)\"?;?");
48+
private final Pattern filenamePattern = Pattern.compile("filename=\"?(?<filename>[^\";]+)");
4949

5050
private URI address;
5151
private String type;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,184 @@
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 com.github.tomakehurst.wiremock.client.WireMock.*;
35+
36+
import static org.hamcrest.MatcherAssert.assertThat;
37+
import static org.junit.jupiter.api.Assertions.*;
38+
39+
/*
40+
* TODO As the spec is outdated, I'm not sure if the error 403 can really happen in these test cases. This class has
41+
* to be checked for correctness, when matrix's spec is updated.
42+
*/
43+
public class MatrixHttpContentTest extends MatrixHttpTest {
44+
private String bodyFilename = "textfile.txt";
45+
private URI address = new URI("mxc://localhost/testpath/" + bodyFilename);
46+
private String downloadUrl = "/_matrix/media/v1/download/" + address.getHost() + address.getPath() + tokenParameter;
47+
48+
public MatrixHttpContentTest() throws URISyntaxException {
49+
}
50+
51+
@Test
52+
public void isValid() throws URISyntaxException {
53+
stubFor(get(urlEqualTo(downloadUrl)).willReturn(
54+
aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain")));
55+
assertTrue(createContentObject().isValid());
56+
}
57+
58+
@Test
59+
public void isValidMissingContentType() throws URISyntaxException {
60+
stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(200).withBodyFile(bodyFilename)));
61+
assertFalse(createContentObject().isValid());
62+
}
63+
64+
@Test
65+
public void isValidError404() throws URISyntaxException {
66+
stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)));
67+
assertFalse(createContentObject().isValid());
68+
}
69+
70+
@Test
71+
public void isValidError403() throws URISyntaxException {
72+
stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)));
73+
assertFalse(createContentObject().isValid());
74+
}
75+
76+
@Test
77+
public void getType() throws URISyntaxException, IOException {
78+
stubFor(get(urlEqualTo(downloadUrl)).willReturn(
79+
aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain")));
80+
assertEquals("text/plain", createContentObject().getType());
81+
}
82+
83+
@Test
84+
public void getTypeMissingContentType() throws URISyntaxException {
85+
stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(200).withBodyFile(bodyFilename)));
86+
assertNull(createContentObject().getType());
87+
}
88+
89+
@Test
90+
public void getTypeError404() throws URISyntaxException, IOException {
91+
stubFor(get(urlEqualTo(downloadUrl))
92+
.willReturn(aResponse().withStatus(404).withStatus(404).withBody(error404Response)));
93+
assertNull(createContentObject().getType());
94+
}
95+
96+
@Test
97+
public void getTypeError403() throws URISyntaxException, IOException {
98+
stubFor(get(urlEqualTo(downloadUrl))
99+
.willReturn(aResponse().withStatus(403).withStatus(403).withBody(error403Response)));
100+
assertNull(createContentObject().getType());
101+
}
102+
103+
@Test
104+
public void getData() throws URISyntaxException, IOException {
105+
stubFor(get(urlEqualTo(downloadUrl)).willReturn(
106+
aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain")));
107+
108+
byte[] expectedResult = Files.readAllBytes(Paths.get(ClassLoader
109+
.getSystemResource("wiremock" + File.separator + "__files" + File.separator + bodyFilename).toURI()));
110+
assertThat(createContentObject().getData(), IsEqual.equalTo(expectedResult));
111+
}
112+
113+
@Test
114+
public void getDataMissingContentType() throws URISyntaxException {
115+
stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(200).withBodyFile(bodyFilename)));
116+
assertNull(createContentObject().getData());
117+
}
118+
119+
@Test
120+
public void getDataError404() throws URISyntaxException, IOException {
121+
stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)));
122+
assertNull(createContentObject().getData());
123+
}
124+
125+
@Test
126+
public void getDataError403() throws URISyntaxException, IOException {
127+
stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)));
128+
assertNull(createContentObject().getData());
129+
}
130+
131+
@Test
132+
public void getFilename() throws URISyntaxException, IOException {
133+
stubFor(get(urlEqualTo(downloadUrl)).willReturn(
134+
aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain")
135+
.withHeader("Content-Disposition", String.format("filename=%s;", bodyFilename))));
136+
assertEquals(Optional.of(bodyFilename), createContentObject().getFilename());
137+
138+
reset();
139+
140+
stubFor(get(urlEqualTo(downloadUrl)).willReturn(
141+
aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain")
142+
.withHeader("Content-Disposition", String.format("filename=\"%s\";", bodyFilename))));
143+
assertEquals(Optional.of(bodyFilename), createContentObject().getFilename());
144+
145+
reset();
146+
147+
stubFor(get(urlEqualTo(downloadUrl)).willReturn(
148+
aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain")
149+
.withHeader("Content-Disposition", String.format("filename=\"%s\"", bodyFilename))));
150+
assertEquals(Optional.of(bodyFilename), createContentObject().getFilename());
151+
152+
reset();
153+
154+
stubFor(get(urlEqualTo(downloadUrl)).willReturn(
155+
aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain")
156+
.withHeader("Content-Disposition", String.format("filename=%s", bodyFilename))));
157+
assertEquals(Optional.of(bodyFilename), createContentObject().getFilename());
158+
}
159+
160+
@Test
161+
public void getFilenameMissingContentType() throws URISyntaxException {
162+
stubFor(get(urlEqualTo(downloadUrl)).willReturn(
163+
aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain")));
164+
assertEquals(Optional.empty(), createContentObject().getFilename());
165+
}
166+
167+
@Test
168+
public void getFilenameError404() throws URISyntaxException, IOException {
169+
stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)));
170+
assertEquals(Optional.empty(), createContentObject().getFilename());
171+
}
172+
173+
@Test
174+
public void getFilenameError403() throws URISyntaxException, IOException {
175+
stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)));
176+
assertEquals(Optional.empty(), createContentObject().getFilename());
177+
}
178+
179+
private MatrixHttpContent createContentObject() throws URISyntaxException {
180+
MatrixClientContext context = createClientContext();
181+
return new MatrixHttpContent(context, address);
182+
}
183+
184+
}

0 commit comments

Comments
 (0)