diff --git a/README.md b/README.md index 86a7394..beec186 100644 --- a/README.md +++ b/README.md @@ -38,6 +38,20 @@ dependencies { ``` **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. +## Tests +### Unit tests +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`. + +### Integration tests +The integration tests are located under `src/testInt` and are run against a homeserver. Therefore a server name +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 +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. + +To run the integration tests, please use the task testInt: `./gradlew testInt`. + +**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. + + ## Contribute Contributions and PRs are welcome to turn this into a fully fledged Matrix Java SDK. Your code will be licensed under AGPLv3 diff --git a/build.gradle b/build.gradle index 2c355a9..f94b7da 100644 --- a/build.gradle +++ b/build.gradle @@ -59,6 +59,14 @@ repositories { mavenCentral() } +sourceSets { + testInt { + compileClasspath += sourceSets.test.compileClasspath + runtimeClasspath += sourceSets.test.runtimeClasspath + } +} + + dependencies { compile 'org.slf4j:log4j-over-slf4j:1.7.25' compile 'commons-lang:commons-lang:2.6' @@ -72,6 +80,15 @@ dependencies { testCompile "com.github.tomakehurst:wiremock:2.8.0" testRuntimeOnly 'org.slf4j:slf4j-simple:1.7.25' + testIntCompile sourceSets.main.output + testIntCompile sourceSets.test.output + + testIntCompile configurations.compile + testIntCompile configurations.testCompile + + testIntRuntime configurations.runtime + testIntRuntime configurations.testRuntime + deployerJars "org.apache.maven.wagon:wagon-ssh:2.2" } @@ -116,3 +133,9 @@ spotless { junitPlatform { enableStandardTestTask true } + +task testInt(type: Test) { + description = "Integration tests which require a Homeserver" + testClassesDir = sourceSets.testInt.output.classesDir + classpath = sourceSets.testInt.runtimeClasspath +} diff --git a/src/test/java/io/kamax/matrix/MatrixHttpUserTest.java b/src/test/java/io/kamax/matrix/AMatrixHttpUserTest.java similarity index 66% rename from src/test/java/io/kamax/matrix/MatrixHttpUserTest.java rename to src/test/java/io/kamax/matrix/AMatrixHttpUserTest.java index fad41c9..ce5a3bd 100644 --- a/src/test/java/io/kamax/matrix/MatrixHttpUserTest.java +++ b/src/test/java/io/kamax/matrix/AMatrixHttpUserTest.java @@ -31,53 +31,39 @@ import java.net.URISyntaxException; import java.util.Optional; -import static com.github.tomakehurst.wiremock.client.WireMock.*; - import static org.hamcrest.core.Is.is; import static org.hamcrest.core.IsEqual.equalTo; import static org.junit.Assert.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertTrue; -public class MatrixHttpUserTest extends MatrixHttpTest { - private String nameUrl = String.format("/_matrix/client/r0/profile/%s/displayname", user.getId()) + tokenParameter; - private String nameResponse = String.format("{\"displayname\": \"%s\"}", nameOfUser); - - private String avatarUrl = String.format("/_matrix/client/r0/profile/%s/avatar_url", user.getId()) + tokenParameter; - private String avatarMediaUrl = "mxc://matrix.org/wefh34uihSDRGhw34"; - private String avatarResponse = String.format("{\"avatar_url\": \"%s\"}", avatarMediaUrl); +public abstract class AMatrixHttpUserTest extends MatrixHttpTest { + protected String avatarMediaUrl = "mxc://matrix.org/wefh34uihSDRGhw34"; @Test public void getName() throws URISyntaxException { - stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(200).withBody(nameResponse))); - assertThat(createUserObject().getName(), is(equalTo(Optional.of(nameOfUser)))); + assertThat(createUserObject().getName(), is(equalTo(Optional.of(username)))); } @Test public void getName404() throws URISyntaxException { - stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); assertThat(createUserObject().getName(), is(equalTo(Optional.empty()))); } @Test public void getNameError403() throws URISyntaxException { - stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); - MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createUserObject()::getName); checkErrorInfo403(e); } @Test public void getNameError429() throws URISyntaxException { - stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); - MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createUserObject()::getName); checkErrorInfo429(e); } @Test public void getAvatar() throws URISyntaxException { - stubFor(get(urlEqualTo(avatarUrl)).willReturn(aResponse().withStatus(200).withBody(avatarResponse))); Optional<_MatrixContent> matrixContent = createUserObject().getAvatar(); assertTrue(matrixContent.isPresent()); assertThat(matrixContent.get().getAddress(), is(equalTo(new URI(avatarMediaUrl)))); @@ -85,14 +71,11 @@ public void getAvatar() throws URISyntaxException { @Test public void getAvatar404() throws URISyntaxException { - stubFor(get(urlEqualTo(avatarUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); assertThat(createUserObject().getAvatar(), is(equalTo(Optional.empty()))); } @Test public void getAvatarError403() throws URISyntaxException { - stubFor(get(urlEqualTo(avatarUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); - MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createUserObject()::getAvatar); checkErrorInfo403(e); @@ -100,15 +83,13 @@ public void getAvatarError403() throws URISyntaxException { @Test public void getAvatarError429() throws URISyntaxException { - stubFor(get(urlEqualTo(avatarUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); - MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createUserObject()::getAvatar); checkErrorInfo429(e); } private MatrixHttpUser createUserObject() throws URISyntaxException { - MatrixClientContext context = createClientContext(); + MatrixClientContext context = getOrCreateClientContext(); return new MatrixHttpUser(context, context.getUser().get()); } } diff --git a/src/test/java/io/kamax/matrix/MatrixHttpUserWiremockTest.java b/src/test/java/io/kamax/matrix/MatrixHttpUserWiremockTest.java new file mode 100644 index 0000000..418c1ec --- /dev/null +++ b/src/test/java/io/kamax/matrix/MatrixHttpUserWiremockTest.java @@ -0,0 +1,92 @@ +/* + * 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 . + */ + +package io.kamax.matrix; + +import org.junit.Test; + +import java.net.URISyntaxException; + +import static com.github.tomakehurst.wiremock.client.WireMock.*; + +public class MatrixHttpUserWiremockTest extends AMatrixHttpUserTest { + private String nameUrl = String.format("/_matrix/client/r0/profile/%s/displayname", user.getId()) + tokenParameter; + private String nameResponse = String.format("{\"displayname\": \"%s\"}", username); + + private String avatarUrl = String.format("/_matrix/client/r0/profile/%s/avatar_url", user.getId()) + tokenParameter; + private String avatarResponse = String.format("{\"avatar_url\": \"%s\"}", avatarMediaUrl); + + @Override + public void login() throws URISyntaxException { + } + + @Override + public void logout() { + } + + @Test + public void getName() throws URISyntaxException { + stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(200).withBody(nameResponse))); + super.getName(); + } + + @Test + public void getName404() throws URISyntaxException { + stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); + super.getName404(); + } + + @Test + public void getNameError403() throws URISyntaxException { + stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); + super.getNameError403(); + } + + @Test + public void getNameError429() throws URISyntaxException { + stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); + super.getNameError429(); + } + + @Test + public void getAvatar() throws URISyntaxException { + stubFor(get(urlEqualTo(avatarUrl)).willReturn(aResponse().withStatus(200).withBody(avatarResponse))); + super.getAvatar(); + } + + @Test + public void getAvatar404() throws URISyntaxException { + stubFor(get(urlEqualTo(avatarUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); + super.getAvatar404(); + } + + @Test + public void getAvatarError403() throws URISyntaxException { + stubFor(get(urlEqualTo(avatarUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); + super.getAvatarError403(); + } + + @Test + public void getAvatarError429() throws URISyntaxException { + stubFor(get(urlEqualTo(avatarUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); + super.getAvatarError429(); + } + +} diff --git a/src/test/java/io/kamax/matrix/client/AMatrixHttpContentTest.java b/src/test/java/io/kamax/matrix/client/AMatrixHttpContentTest.java new file mode 100644 index 0000000..b813ec4 --- /dev/null +++ b/src/test/java/io/kamax/matrix/client/AMatrixHttpContentTest.java @@ -0,0 +1,149 @@ +/* + * 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 . + */ + +package io.kamax.matrix.client; + +import org.hamcrest.core.IsEqual; +import org.junit.Test; + +import java.io.File; +import java.io.IOException; +import java.net.URI; +import java.net.URISyntaxException; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.Optional; + +import static org.hamcrest.MatcherAssert.assertThat; +import static org.junit.jupiter.api.Assertions.*; + +/* + * TODO As the spec is outdated, I'm not sure if the error 403 can really happen in these test cases. This class has + * to be checked for correctness, when matrix's spec is updated. + */ +public abstract class AMatrixHttpContentTest extends MatrixHttpTest { + protected String bodyFilename = "textfile.txt"; + protected URI address = new URI("mxc://localhost/testpath/" + bodyFilename); + + public AMatrixHttpContentTest() throws URISyntaxException { + } + + @Test + public void isValid() throws URISyntaxException { + assertTrue(createContentObject().isValid()); + } + + @Test + public void isValidMissingContentType() throws URISyntaxException { + assertTrue(createContentObject().isValid()); + } + + @Test + public void isValidContentNotFound() throws URISyntaxException { + assertFalse(createContentObject().isValid()); + } + + @Test + public void isValidErrorAccessDenied() throws URISyntaxException { + assertFalse(createContentObject().isValid()); + } + + @Test + public void getType() throws URISyntaxException, IOException { + assertEquals(Optional.of("text/plain"), createContentObject().getType()); + } + + @Test + public void getTypeMissingContentType() throws URISyntaxException { + assertEquals(Optional.empty(), createContentObject().getType()); + } + + @Test + public void getTypeErrorContentNotFound() throws URISyntaxException, IOException { + MatrixHttpContent contentObject = createContentObject(); + assertFalse(contentObject.isValid()); + assertThrows(IllegalStateException.class, contentObject::getType); + } + + @Test + public void getTypeErrorAccessDenied() throws URISyntaxException, IOException { + MatrixHttpContent contentObject = createContentObject(); + assertFalse(contentObject.isValid()); + assertThrows(IllegalStateException.class, createContentObject()::getType); + } + + @Test + public void getData() throws URISyntaxException, IOException { + byte[] expectedResult = Files.readAllBytes(Paths.get(ClassLoader + .getSystemResource("wiremock" + File.separator + "__files" + File.separator + bodyFilename).toURI())); + assertThat(createContentObject().getData(), IsEqual.equalTo(expectedResult)); + } + + @Test + public void getDataMissingContentType() throws URISyntaxException, IOException { + byte[] expectedResult = Files.readAllBytes(Paths.get(ClassLoader + .getSystemResource("wiremock" + File.separator + "__files" + File.separator + bodyFilename).toURI())); + assertThat(createContentObject().getData(), IsEqual.equalTo(expectedResult)); + } + + @Test + public void getDataErrorContentNotFound() throws URISyntaxException, IOException { + MatrixHttpContent contentObject = createContentObject(); + assertFalse(contentObject.isValid()); + assertThrows(IllegalStateException.class, contentObject::getData); + } + + @Test + public void getDataErrorAccessDenied() throws URISyntaxException, IOException { + MatrixHttpContent contentObject = createContentObject(); + assertFalse(contentObject.isValid()); + assertThrows(IllegalStateException.class, contentObject::getData); + } + + @Test + public void getFilename() throws URISyntaxException, IOException { + assertEquals(Optional.of(bodyFilename), createContentObject().getFilename()); + } + + @Test + public void getFilenameMissingContentType() throws URISyntaxException { + assertEquals(Optional.empty(), createContentObject().getFilename()); + } + + @Test + public void getFilenameErrorContentNotFound() throws URISyntaxException, IOException { + MatrixHttpContent contentObject = createContentObject(); + assertFalse(contentObject.isValid()); + assertThrows(IllegalStateException.class, contentObject::getFilename); + } + + @Test + public void getFilenameErrorAccessDenied() throws URISyntaxException, IOException { + MatrixHttpContent contentObject = createContentObject(); + assertFalse(contentObject.isValid()); + assertThrows(IllegalStateException.class, contentObject::getFilename); + } + + private MatrixHttpContent createContentObject() throws URISyntaxException { + MatrixClientContext context = getOrCreateClientContext(); + return new MatrixHttpContent(context, address); + } + +} diff --git a/src/test/java/io/kamax/matrix/client/AMatrixHttpRoomTest.java b/src/test/java/io/kamax/matrix/client/AMatrixHttpRoomTest.java new file mode 100644 index 0000000..ca25ac3 --- /dev/null +++ b/src/test/java/io/kamax/matrix/client/AMatrixHttpRoomTest.java @@ -0,0 +1,189 @@ +/* + * 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 . + */ + +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 org.junit.Assert.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public abstract class AMatrixHttpRoomTest extends MatrixHttpTest { + protected String roomId = "roomId892347847"; + protected String eventId = "YUwRidLecu"; + + protected String nameOfRoom = "test room"; + protected String testTopic = "test topic"; + protected String testText = "test text"; + protected String joinedUser1 = "@test:testserver.org"; + protected String joinedUser2 = "@test2:testserver.org"; + + @Test + public void getName() throws URISyntaxException { + assertThat(createRoomObject().getName(), IsEqual.equalTo(Optional.of(nameOfRoom))); + } + + @Test + public void getEmptyName() throws URISyntaxException { + assertThat(createRoomObject().getName(), IsEqual.equalTo(Optional.empty())); + } + + @Test + public void getNameAccessDenied() throws URISyntaxException { + MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getName); + checkErrorInfo403(e); + } + + @Test + public void getNameRateLimited() throws URISyntaxException { + MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getName); + checkErrorInfo429(e); + } + + @Test + public void getTopic() throws URISyntaxException { + assertThat(createRoomObject().getTopic(), IsEqual.equalTo(Optional.of(testTopic))); + } + + @Test + public void getEmptyTopic() throws URISyntaxException { + assertThat(createRoomObject().getTopic(), IsEqual.equalTo(Optional.empty())); + } + + @Test + public void getTopicAccessDenied() throws URISyntaxException { + MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getTopic); + checkErrorInfo403(e); + } + + @Test + public void getTopicRateLimited() throws URISyntaxException { + MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getTopic); + checkErrorInfo429(e); + } + + @Test + public void join() throws URISyntaxException { + createRoomObject().join(); + } + + @Test + public void joinRoomNotFound() throws URISyntaxException { + MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::join); + checkErrorInfo404(e); + } + + @Test + public void joinAccessDenied() throws URISyntaxException { + MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::join); + checkErrorInfo403(e); + } + + @Test + public void joinRateLimited() throws URISyntaxException { + MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::join); + checkErrorInfo429(e); + } + + @Test + public void leave() throws URISyntaxException { + createRoomObject().leave(); + } + + @Test + public void leaveAccessDenied() throws URISyntaxException { + MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::leave); + checkErrorInfo403(e); + } + + @Test + public void leaveRoomNotFound() throws URISyntaxException { + createRoomObject().leave(); + } + + @Test + public void leaveRateLimited() throws URISyntaxException { + MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::leave); + checkErrorInfo429(e); + } + + @Test + public void sendText() throws URISyntaxException { + createRoomObject().sendText(testText); + } + + @Test + public void sendTextAccessDenied() throws URISyntaxException { + MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, + () -> createRoomObject().sendText(testText)); + checkErrorInfo403(e); + } + + @Test + public void sendTextRoomNotFound() throws URISyntaxException { + MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, + () -> createRoomObject().sendText(testText)); + checkErrorInfo404(e); + } + + @Test + public void sendTextRateLimited() throws URISyntaxException { + MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, + () -> createRoomObject().sendText(testText)); + checkErrorInfo429(e); + } + + @Test + public void getJoinedUsers() throws URISyntaxException { + List<_MatrixID> expectedResult = new ArrayList<>(); + expectedResult.add(new MatrixID(joinedUser1)); + expectedResult.add(new MatrixID(joinedUser2)); + + assertThat(createRoomObject().getJoinedUsers(), IsEqual.equalTo(expectedResult)); + } + + @Test + public void getJoinedUsersRoomNotFound() throws URISyntaxException { + MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, + createRoomObject()::getJoinedUsers); + checkErrorInfo404(e); + } + + @Test + public void getJoinedUsersRateLimited() throws URISyntaxException { + MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, + createRoomObject()::getJoinedUsers); + checkErrorInfo429(e); + } + + private MatrixHttpRoom createRoomObject() throws URISyntaxException { + MatrixClientContext context = getOrCreateClientContext(); + return new MatrixHttpRoom(context, roomId); + } +} diff --git a/src/test/java/io/kamax/matrix/client/MatrixHttpContentTest.java b/src/test/java/io/kamax/matrix/client/MatrixHttpContentWiremockTest.java similarity index 62% rename from src/test/java/io/kamax/matrix/client/MatrixHttpContentTest.java rename to src/test/java/io/kamax/matrix/client/MatrixHttpContentWiremockTest.java index 4a49213..54b3a73 100644 --- a/src/test/java/io/kamax/matrix/client/MatrixHttpContentTest.java +++ b/src/test/java/io/kamax/matrix/client/MatrixHttpContentWiremockTest.java @@ -20,123 +20,100 @@ package io.kamax.matrix.client; -import org.hamcrest.core.IsEqual; import org.junit.Test; -import java.io.File; import java.io.IOException; -import java.net.URI; import java.net.URISyntaxException; -import java.nio.file.Files; -import java.nio.file.Paths; -import java.util.Optional; import static com.github.tomakehurst.wiremock.client.WireMock.*; -import static org.hamcrest.MatcherAssert.assertThat; -import static org.junit.jupiter.api.Assertions.*; - -/* - * TODO As the spec is outdated, I'm not sure if the error 403 can really happen in these test cases. This class has - * to be checked for correctness, when matrix's spec is updated. - */ -public class MatrixHttpContentTest extends MatrixHttpTest { - private String bodyFilename = "textfile.txt"; - private URI address = new URI("mxc://localhost/testpath/" + bodyFilename); +public class MatrixHttpContentWiremockTest extends AMatrixHttpContentTest { private String downloadUrl = "/_matrix/media/v1/download/" + address.getHost() + address.getPath() + tokenParameter; - public MatrixHttpContentTest() throws URISyntaxException { + public MatrixHttpContentWiremockTest() throws URISyntaxException { + } + + @Override + public void login() throws URISyntaxException { + } + + @Override + public void logout() { } @Test public void isValid() throws URISyntaxException { stubFor(get(urlEqualTo(downloadUrl)).willReturn( aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain"))); - assertTrue(createContentObject().isValid()); + super.isValid(); } @Test public void isValidMissingContentType() throws URISyntaxException { stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(200).withBodyFile(bodyFilename))); - assertTrue(createContentObject().isValid()); + super.isValidMissingContentType(); } @Test public void isValidContentNotFound() throws URISyntaxException { stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); - assertFalse(createContentObject().isValid()); + super.isValidContentNotFound(); } @Test public void isValidErrorAccessDenied() throws URISyntaxException { stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); - assertFalse(createContentObject().isValid()); + super.isValidErrorAccessDenied(); } @Test public void getType() throws URISyntaxException, IOException { stubFor(get(urlEqualTo(downloadUrl)).willReturn( aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain"))); - assertEquals(Optional.of("text/plain"), createContentObject().getType()); + super.getType(); } @Test public void getTypeMissingContentType() throws URISyntaxException { stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(200).withBodyFile(bodyFilename))); - assertEquals(Optional.empty(), createContentObject().getType()); + super.getTypeMissingContentType(); } @Test public void getTypeErrorContentNotFound() throws URISyntaxException, IOException { stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); - MatrixHttpContent contentObject = createContentObject(); - assertFalse(contentObject.isValid()); - assertThrows(IllegalStateException.class, contentObject::getType); + super.getTypeErrorContentNotFound(); } @Test public void getTypeErrorAccessDenied() throws URISyntaxException, IOException { stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); - MatrixHttpContent contentObject = createContentObject(); - assertFalse(contentObject.isValid()); - assertThrows(IllegalStateException.class, createContentObject()::getType); + super.getTypeErrorAccessDenied(); } @Test public void getData() throws URISyntaxException, IOException { stubFor(get(urlEqualTo(downloadUrl)).willReturn( aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain"))); - - byte[] expectedResult = Files.readAllBytes(Paths.get(ClassLoader - .getSystemResource("wiremock" + File.separator + "__files" + File.separator + bodyFilename).toURI())); - assertThat(createContentObject().getData(), IsEqual.equalTo(expectedResult)); + super.getData(); } @Test public void getDataMissingContentType() throws URISyntaxException, IOException { stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(200).withBodyFile(bodyFilename))); - - byte[] expectedResult = Files.readAllBytes(Paths.get(ClassLoader - .getSystemResource("wiremock" + File.separator + "__files" + File.separator + bodyFilename).toURI())); - assertThat(createContentObject().getData(), IsEqual.equalTo(expectedResult)); + super.getDataMissingContentType(); } @Test public void getDataErrorContentNotFound() throws URISyntaxException, IOException { stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); - - MatrixHttpContent contentObject = createContentObject(); - assertFalse(contentObject.isValid()); - assertThrows(IllegalStateException.class, contentObject::getData); + super.getDataErrorContentNotFound(); } @Test public void getDataErrorAccessDenied() throws URISyntaxException, IOException { stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); - - MatrixHttpContent contentObject = createContentObject(); - assertFalse(contentObject.isValid()); - assertThrows(IllegalStateException.class, contentObject::getData); + super.getDataErrorAccessDenied(); } @Test @@ -144,57 +121,51 @@ public void getFilename() throws URISyntaxException, IOException { stubFor(get(urlEqualTo(downloadUrl)).willReturn( aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain") .withHeader("Content-Disposition", String.format("filename=%s;", bodyFilename)))); - assertEquals(Optional.of(bodyFilename), createContentObject().getFilename()); - - reset(); + super.getFilename(); + } + @Test + public void getFilename2() throws URISyntaxException, IOException { stubFor(get(urlEqualTo(downloadUrl)).willReturn( aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain") .withHeader("Content-Disposition", String.format("filename=\"%s\";", bodyFilename)))); - assertEquals(Optional.of(bodyFilename), createContentObject().getFilename()); + super.getFilename(); + } - reset(); + @Test + public void getFilename3() throws URISyntaxException, IOException { stubFor(get(urlEqualTo(downloadUrl)).willReturn( aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain") .withHeader("Content-Disposition", String.format("filename=\"%s\"", bodyFilename)))); - assertEquals(Optional.of(bodyFilename), createContentObject().getFilename()); - - reset(); + super.getFilename(); + } + @Test + public void getFilename4() throws URISyntaxException, IOException { stubFor(get(urlEqualTo(downloadUrl)).willReturn( aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain") .withHeader("Content-Disposition", String.format("filename=%s", bodyFilename)))); - assertEquals(Optional.of(bodyFilename), createContentObject().getFilename()); + super.getFilename(); } @Test public void getFilenameMissingContentType() throws URISyntaxException { stubFor(get(urlEqualTo(downloadUrl)).willReturn( aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain"))); - assertEquals(Optional.empty(), createContentObject().getFilename()); + super.getFilenameMissingContentType(); } @Test public void getFilenameErrorContentNotFound() throws URISyntaxException, IOException { stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); - - MatrixHttpContent contentObject = createContentObject(); - assertFalse(contentObject.isValid()); - assertThrows(IllegalStateException.class, contentObject::getFilename); + super.getFilenameErrorContentNotFound(); } @Test public void getFilenameErrorAccessDenied() throws URISyntaxException, IOException { stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); - MatrixHttpContent contentObject = createContentObject(); - assertFalse(contentObject.isValid()); - assertThrows(IllegalStateException.class, contentObject::getFilename); - } - - private MatrixHttpContent createContentObject() throws URISyntaxException { - MatrixClientContext context = createClientContext(); - return new MatrixHttpContent(context, address); + super.getFilenameErrorAccessDenied(); } } diff --git a/src/test/java/io/kamax/matrix/client/MatrixHttpRoomTest.java b/src/test/java/io/kamax/matrix/client/MatrixHttpRoomWiremockTest.java similarity index 63% rename from src/test/java/io/kamax/matrix/client/MatrixHttpRoomTest.java rename to src/test/java/io/kamax/matrix/client/MatrixHttpRoomWiremockTest.java index 1e9802b..cfae54e 100644 --- a/src/test/java/io/kamax/matrix/client/MatrixHttpRoomTest.java +++ b/src/test/java/io/kamax/matrix/client/MatrixHttpRoomWiremockTest.java @@ -20,32 +20,17 @@ 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"; - +public class MatrixHttpRoomWiremockTest extends AMatrixHttpRoomTest { 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; @@ -56,195 +41,157 @@ public class MatrixHttpRoomTest extends MatrixHttpTest { 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); + @Override + public void login() throws URISyntaxException { + } + + @Override + public void logout() { + } + @Test public void getName() throws URISyntaxException { stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(200).withBody(nameResponse))); - assertThat(createRoomObject().getName(), IsEqual.equalTo(Optional.of(nameOfRoom))); + super.getName(); } @Test public void getEmptyName() throws URISyntaxException { stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); - assertThat(createRoomObject().getName(), IsEqual.equalTo(Optional.empty())); + super.getEmptyName(); } @Test public void getNameAccessDenied() throws URISyntaxException { stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); - - MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getName); - checkErrorInfo403(e); + super.getNameAccessDenied(); } @Test public void getNameRateLimited() throws URISyntaxException { stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); - - MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getName); - checkErrorInfo429(e); + super.getNameRateLimited(); } @Test public void getTopic() throws URISyntaxException { stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(200).withBody(topicResponse))); - assertThat(createRoomObject().getTopic(), IsEqual.equalTo(Optional.of(testTopic))); + super.getTopic(); } @Test public void getEmptyTopic() throws URISyntaxException { stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); - assertThat(createRoomObject().getTopic(), IsEqual.equalTo(Optional.empty())); + super.getEmptyTopic(); } @Test public void getTopicAccessDenied() throws URISyntaxException { stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); - - MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getTopic); - checkErrorInfo403(e); + super.getTopicAccessDenied(); } @Test public void getTopicRateLimited() throws URISyntaxException { stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); - - MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getTopic); - checkErrorInfo429(e); + super.getTopicRateLimited(); } @Test public void join() throws URISyntaxException { stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(200).withBody(joinResponse))); - createRoomObject().join(); + super.join(); } @Test public void joinRoomNotFound() throws URISyntaxException { stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); - - MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::join); - checkErrorInfo404(e); + super.joinRoomNotFound(); } @Test public void joinAccessDenied() throws URISyntaxException { stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); - - MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::join); - checkErrorInfo403(e); + super.joinAccessDenied(); } @Test public void joinRateLimited() throws URISyntaxException { stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); - - MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::join); - checkErrorInfo429(e); + super.joinRateLimited(); } @Test public void leave() throws URISyntaxException { stubFor(post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(200).withBody(leaveResponse))); - createRoomObject().leave(); + super.leave(); } @Test public void leaveAccessDenied() throws URISyntaxException { stubFor(post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); - - MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::leave); - checkErrorInfo403(e); + super.leaveAccessDenied(); } @Test public void leaveRoomNotFound() throws URISyntaxException { stubFor(post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); - createRoomObject().leave(); + super.leaveRoomNotFound(); } @Test public void leaveRateLimited() throws URISyntaxException { stubFor(post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); - - MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::leave); - checkErrorInfo429(e); + super.leaveRateLimited(); } @Test public void sendText() throws URISyntaxException { stubFor(put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(200).withBody(sendTextResponse))); - createRoomObject().sendText(testText); + super.sendText(); } @Test public void sendTextAccessDenied() throws URISyntaxException { stubFor(put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); - - MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, - () -> createRoomObject().sendText(testText)); - checkErrorInfo403(e); + super.sendTextAccessDenied(); } @Test public void sendTextRoomNotFound() throws URISyntaxException { stubFor(put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); - - MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, - () -> createRoomObject().sendText(testText)); - checkErrorInfo404(e); + super.sendTextRoomNotFound(); } @Test public void sendTextRateLimited() throws URISyntaxException { stubFor(put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); - - MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, - () -> createRoomObject().sendText(testText)); - checkErrorInfo429(e); + super.sendTextRateLimited(); } @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)); + super.getJoinedUsers(); } @Test public void getJoinedUsersRoomNotFound() throws URISyntaxException { stubFor(get(urlEqualTo(getJoinedUsersUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); - - MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, - createRoomObject()::getJoinedUsers); - checkErrorInfo404(e); + super.getJoinedUsersRoomNotFound(); } @Test public void getJoinedUsersRateLimited() throws URISyntaxException { stubFor(get(urlEqualTo(getJoinedUsersUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); - - MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, - createRoomObject()::getJoinedUsers); - checkErrorInfo429(e); - } - - private MatrixHttpRoom createRoomObject() throws URISyntaxException { - MatrixClientContext context = createClientContext(); - return new MatrixHttpRoom(context, roomId); + super.getJoinedUsersRateLimited(); } } diff --git a/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java b/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java index 1e01e56..9664abb 100644 --- a/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java +++ b/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java @@ -24,12 +24,21 @@ import io.kamax.matrix.MatrixErrorInfo; import io.kamax.matrix.MatrixID; +import io.kamax.matrix.client.regular.MatrixHttpClient; import io.kamax.matrix.hs.MatrixHomeserver; +import org.junit.After; +import org.junit.Before; import org.junit.Rule; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.net.URISyntaxException; +import java.util.Map; import java.util.Optional; +import java.util.stream.Collectors; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; import static junit.framework.TestCase.assertEquals; @@ -45,8 +54,9 @@ public class MatrixHttpTest { protected String domain = "localhost"; protected String hostname = "localhost"; protected String baseUrl = "http://" + hostname + ":" + port; - protected String nameOfUser = "testuser"; - protected MatrixID user = new MatrixID(nameOfUser, domain); + protected String username = "testuser"; + protected String password = ""; + protected MatrixID user = new MatrixID(username, domain); private String errorResponseTemplate = "{\"errcode\": \"%s\", \"error\": \"%s\"}"; @@ -62,13 +72,65 @@ public class MatrixHttpTest { private String error429 = "Too many requests have been sent in a short period of time. Wait a while then try again."; protected String error429Response = String.format(errorResponseTemplate, errcode429, error429); - @Rule - public WireMockRule wireMockRule = new WireMockRule(options().port(port).usingFilesUnderDirectory(resourcePath)); + /** + * This method logs in to a homeserver, if the appropriate config file is present. It has to be commented out in + * Wiremock test cases. + * + * @throws URISyntaxException + */ + @Before + public void login() throws URISyntaxException { + InputStream configFile = this.getClass().getResourceAsStream("/HomeserverTest.conf"); + if (configFile != null) { + try (BufferedReader buffer = new BufferedReader(new InputStreamReader(configFile))) { + Map configValues = buffer.lines().filter(line -> !line.startsWith("#")).collect( + Collectors.toMap(line -> line.split("=")[0].trim(), line -> line.split("=")[1].trim())); + + port = Integer.valueOf(configValues.get("Port")); + hostname = configValues.get("Hostname"); + domain = configValues.get("Domain"); + baseUrl = "https://" + hostname + ":" + port; + username = configValues.get("Username"); + password = configValues.get("Password"); + user = new MatrixID(username, domain); + } catch (IOException e) { + throw new IllegalStateException(e); + } + try { + MatrixHomeserver homeserver = new MatrixHomeserver(domain, baseUrl); + MatrixClientContext context = new MatrixClientContext(homeserver); + MatrixPasswordLoginCredentials credentials = new MatrixPasswordLoginCredentials(username, password); + + client = new MatrixHttpClient(context); + client.login(credentials); + testToken = client.getAccessTokenOrThrow(); + } catch (URISyntaxException e) { + throw new IllegalStateException(e); + } + } + } - protected MatrixClientContext createClientContext() throws URISyntaxException { + /** + * This method logs out of a homeserver, if the appropriate config file is present. It has to be commented out in + * Wiremock test cases. + */ + @After + public void logout() { + client.logout(); + client = null; + } - MatrixHomeserver hs = new MatrixHomeserver(domain, baseUrl); - return new MatrixClientContext(hs, user, testToken); + @Rule + public WireMockRule wireMockRule = new WireMockRule(options().port(port).usingFilesUnderDirectory(resourcePath)); + private MatrixHttpClient client; + + protected MatrixClientContext getOrCreateClientContext() throws URISyntaxException { + if (client != null) { + return client.getContext(); + } else { + MatrixHomeserver hs = new MatrixHomeserver(domain, baseUrl); + return new MatrixClientContext(hs, user, testToken); + } } protected void checkErrorInfo403(MatrixClientRequestException e) { @@ -85,7 +147,7 @@ protected void checkErrorInfo429(MatrixClientRequestException e) { private void checkErrorInfo(String errcode, String error, Optional errorOptional) { assertTrue(errorOptional.isPresent()); - assertEquals(errorOptional.get().getErrcode(), errcode); - assertEquals(errorOptional.get().getError(), error); + assertEquals(errcode, errorOptional.get().getErrcode()); + assertEquals(error, errorOptional.get().getError()); } } diff --git a/src/test/java/io/kamax/matrix/client/as/MatrixApplicationServiceClientTest.java b/src/test/java/io/kamax/matrix/client/as/MatrixApplicationServiceClientTest.java index b2f9677..1b76aee 100644 --- a/src/test/java/io/kamax/matrix/client/as/MatrixApplicationServiceClientTest.java +++ b/src/test/java/io/kamax/matrix/client/as/MatrixApplicationServiceClientTest.java @@ -35,6 +35,14 @@ public class MatrixApplicationServiceClientTest extends MatrixHttpTest { private String createUserUrl = "/_matrix/client/r0/register" + tokenParameter; private String testUser = "testUser"; + @Override + public void login() throws URISyntaxException { + } + + @Override + public void logout() { + } + @Test public void createUser() throws URISyntaxException { stubFor(post(urlEqualTo(createUserUrl)).willReturn(aResponse().withStatus(200))); diff --git a/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientTest.java b/src/test/java/io/kamax/matrix/client/regular/AMatrixHttpClientLoginTest.java similarity index 54% rename from src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientTest.java rename to src/test/java/io/kamax/matrix/client/regular/AMatrixHttpClientLoginTest.java index 3ec43cf..3492fb7 100644 --- a/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientTest.java +++ b/src/test/java/io/kamax/matrix/client/regular/AMatrixHttpClientLoginTest.java @@ -17,7 +17,6 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ - package io.kamax.matrix.client.regular; import io.kamax.matrix.client.MatrixClientContext; @@ -33,37 +32,20 @@ import static com.github.tomakehurst.wiremock.client.WireMock.*; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; - -public class MatrixHttpClientTest extends MatrixHttpTest { - private String loginUrl = "/_matrix/client/r0/login"; - private String password = "MostSecretPasswordEver"; - private String deviceId = "testDeviceId_892377"; +import static org.junit.jupiter.api.Assertions.*; +public abstract class AMatrixHttpClientLoginTest extends MatrixHttpTest { + protected String loginUrl = "/_matrix/client/r0/login"; + protected String password = "MostSecretPasswordEver"; + protected String deviceId = "testDeviceId_892377"; private String logoutUrl = "/_matrix/client/r0/logout" + tokenParameter; - private String setDisplaynameUrl = String.format("/_matrix/client/r0/profile/%s/displayname", - createClientContext().getUser().get().getId()) + tokenParameter; - private String displayName = "display name"; - - public MatrixHttpClientTest() throws URISyntaxException { + @Override + public void logout() { } @Test public void loginWithDeviceId() throws URISyntaxException { - stubFor(post(urlEqualTo(loginUrl)) - .withRequestBody(equalToJson("{\"type\": \"m.login.password\"," + // - "\"user\": \"" + user.getLocalPart() + "\"," + // - "\"password\": \"" + password + "\"," + // - "\"device_id\": \"" + deviceId + "\"}")) - .willReturn(aResponse().withStatus(200) - .withBody("{\"user_id\": \"" + user.getId() + "\"," + // - "\"access_token\": \"" + testToken + "\"," + // - "\"home_server\": \"" + hostname + "\"," + // - "\"device_id\": \"" + deviceId + "\"}"))); - MatrixHomeserver hs = new MatrixHomeserver(domain, baseUrl); MatrixClientContext context = new MatrixClientContext(hs, deviceId); MatrixHttpClient client = new MatrixHttpClient(context); @@ -84,17 +66,6 @@ public void loginWithDeviceId() throws URISyntaxException { @Test public void loginWithDeviceIdAndLogout() throws URISyntaxException { - stubFor(post(urlEqualTo(loginUrl)) - .withRequestBody(equalToJson("{\"type\": \"m.login.password\"," + // - "\"user\": \"" + user.getLocalPart() + "\"," + // - "\"password\": \"" + password + "\"," + // - "\"device_id\": \"" + deviceId + "\"}")) - .willReturn(aResponse().withStatus(200) - .withBody("{\"user_id\": \"" + user.getId() + "\"," + // - "\"access_token\": \"" + testToken + "\"," + // - "\"home_server\": \"" + new MatrixHomeserver(domain, baseUrl) + "\"," + // - "\"device_id\": \"" + deviceId + "\"}"))); - MatrixHomeserver hs = new MatrixHomeserver(domain, baseUrl); MatrixClientContext context = new MatrixClientContext(hs, deviceId); MatrixHttpClient client = new MatrixHttpClient(context); @@ -118,16 +89,6 @@ public void loginWithDeviceIdAndLogout() throws URISyntaxException { @Test public void login() throws URISyntaxException { - stubFor(post(urlEqualTo(loginUrl)) - .withRequestBody(equalToJson("{\"type\": \"m.login.password\"," + // - "\"user\": \"" + user.getLocalPart() + "\"," + // - "\"password\": \"" + password + "\"}")) - .willReturn(aResponse().withStatus(200) - .withBody("{\"user_id\": \"" + user.getId() + "\"," + // - "\"access_token\": \"" + testToken + "\"," + // - "\"home_server\": \"" + new MatrixHomeserver(domain, baseUrl) + "\"," + // - "\"device_id\": \"" + deviceId + "\"}"))); - MatrixHomeserver hs = new MatrixHomeserver(domain, baseUrl); MatrixClientContext context = new MatrixClientContext(hs); MatrixHttpClient client = new MatrixHttpClient(context); @@ -148,12 +109,6 @@ public void login() throws URISyntaxException { @Test public void loginWrongPassword() throws URISyntaxException { - stubFor(post(urlEqualTo(loginUrl)) - .withRequestBody(equalToJson("{\"type\": \"m.login.password\"," + // - "\"user\": \"" + user.getLocalPart() + "\"," + // - "\"password\": \"" + password + "\"}")) - .willReturn(aResponse().withStatus(403).withBody(error403Response))); - MatrixHomeserver hs = new MatrixHomeserver(domain, baseUrl); MatrixClientContext context = new MatrixClientContext(hs); MatrixHttpClient client = new MatrixHttpClient(context); @@ -164,23 +119,4 @@ public void loginWrongPassword() throws URISyntaxException { checkErrorInfo403(e); } - @Test - public void setDisplayName() throws URISyntaxException { - stubFor(put(urlEqualTo(setDisplaynameUrl)).willReturn(aResponse().withStatus(200))); - createClientObject().setDisplayName(displayName); - } - - @Test - public void setDisplayNameErrorRateLimited() throws URISyntaxException { - stubFor(put(urlEqualTo(setDisplaynameUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); - - MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, - () -> createClientObject().setDisplayName(displayName)); - checkErrorInfo429(e); - } - - private MatrixHttpClient createClientObject() throws URISyntaxException { - MatrixClientContext context = createClientContext(); - return new MatrixHttpClient(context); - } } diff --git a/src/test/java/io/kamax/matrix/client/regular/AMatrixHttpClientTest.java b/src/test/java/io/kamax/matrix/client/regular/AMatrixHttpClientTest.java new file mode 100644 index 0000000..251824d --- /dev/null +++ b/src/test/java/io/kamax/matrix/client/regular/AMatrixHttpClientTest.java @@ -0,0 +1,52 @@ +/* + * 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 . + */ + +package io.kamax.matrix.client.regular; + +import io.kamax.matrix.client.MatrixClientContext; +import io.kamax.matrix.client.MatrixClientRequestException; +import io.kamax.matrix.client.MatrixHttpTest; + +import org.junit.Test; + +import java.net.URISyntaxException; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +public abstract class AMatrixHttpClientTest extends MatrixHttpTest { + protected String displayName = "display name"; + + @Test + public void setDisplayName() throws URISyntaxException { + createClientObject().setDisplayName(displayName); + } + + @Test + public void setDisplayNameErrorRateLimited() throws URISyntaxException { + MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, + () -> createClientObject().setDisplayName(displayName)); + checkErrorInfo429(e); + } + + private MatrixHttpClient createClientObject() throws URISyntaxException { + MatrixClientContext context = getOrCreateClientContext(); + return new MatrixHttpClient(context); + } +} diff --git a/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientLoginWiremockTest.java b/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientLoginWiremockTest.java new file mode 100644 index 0000000..738f092 --- /dev/null +++ b/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientLoginWiremockTest.java @@ -0,0 +1,88 @@ +/* + * 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 . + */ +package io.kamax.matrix.client.regular; + +import org.junit.Test; + +import java.net.URISyntaxException; + +import static com.github.tomakehurst.wiremock.client.WireMock.*; + +public class MatrixHttpClientLoginWiremockTest extends AMatrixHttpClientLoginTest { + + @Test + public void loginWithDeviceId() throws URISyntaxException { + stubFor(post(urlEqualTo(loginUrl)) + .withRequestBody(equalToJson("{\"type\": \"m.login.password\"," + // + "\"user\": \"" + user.getLocalPart() + "\"," + // + "\"password\": \"" + password + "\"," + // + "\"device_id\": \"" + deviceId + "\"}")) + .willReturn(aResponse().withStatus(200) + .withBody("{\"user_id\": \"" + user.getId() + "\"," + // + "\"access_token\": \"" + testToken + "\"," + // + "\"home_server\": \"" + hostname + "\"," + // + "\"device_id\": \"" + deviceId + "\"}"))); + + super.loginWithDeviceId(); + } + + @Test + public void loginWithDeviceIdAndLogout() throws URISyntaxException { + stubFor(post(urlEqualTo(loginUrl)) + .withRequestBody(equalToJson("{\"type\": \"m.login.password\"," + // + "\"user\": \"" + user.getLocalPart() + "\"," + // + "\"password\": \"" + password + "\"," + // + "\"device_id\": \"" + deviceId + "\"}")) + .willReturn(aResponse().withStatus(200) + .withBody("{\"user_id\": \"" + user.getId() + "\"," + // + "\"access_token\": \"" + testToken + "\"," + // + "\"home_server\": \"" + hostname + "\"," + // + "\"device_id\": \"" + deviceId + "\"}"))); + + super.loginWithDeviceIdAndLogout(); + } + + @Test + public void login() throws URISyntaxException { + stubFor(post(urlEqualTo(loginUrl)) + .withRequestBody(equalToJson("{\"type\": \"m.login.password\"," + // + "\"user\": \"" + user.getLocalPart() + "\"," + // + "\"password\": \"" + password + "\"}")) + .willReturn(aResponse().withStatus(200) + .withBody("{\"user_id\": \"" + user.getId() + "\"," + // + "\"access_token\": \"" + testToken + "\"," + // + "\"home_server\": \"" + hostname + "\"," + // + "\"device_id\": \"" + deviceId + "\"}"))); + + super.login(); + } + + @Test + public void loginWrongPassword() throws URISyntaxException { + stubFor(post(urlEqualTo(loginUrl)) + .withRequestBody(equalToJson("{\"type\": \"m.login.password\"," + // + "\"user\": \"" + user.getLocalPart() + "\"," + // + "\"password\": \"" + password + "\"}")) + .willReturn(aResponse().withStatus(403).withBody(error403Response))); + + super.loginWrongPassword(); + } + +} diff --git a/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientWiremockTest.java b/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientWiremockTest.java new file mode 100644 index 0000000..2251cf5 --- /dev/null +++ b/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientWiremockTest.java @@ -0,0 +1,55 @@ +/* + * 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 . + */ + +package io.kamax.matrix.client.regular; + +import org.junit.Test; + +import java.net.URISyntaxException; + +import static com.github.tomakehurst.wiremock.client.WireMock.*; + +public class MatrixHttpClientWiremockTest extends AMatrixHttpClientTest { + private String setDisplaynameUrl = String.format("/_matrix/client/r0/profile/%s/displayname", + getOrCreateClientContext().getUser().get().getId()) + tokenParameter; + + public MatrixHttpClientWiremockTest() throws URISyntaxException { + } + + @Override + public void login() throws URISyntaxException { + } + + @Override + public void logout() { + } + + @Test + public void setDisplayName() throws URISyntaxException { + stubFor(put(urlEqualTo(setDisplaynameUrl)).willReturn(aResponse().withStatus(200))); + super.setDisplayName(); + } + + @Test + public void setDisplayNameErrorRateLimited() throws URISyntaxException { + stubFor(put(urlEqualTo(setDisplaynameUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); + super.setDisplayNameErrorRateLimited(); + } +} diff --git a/src/testInt/java/io/kamax/matrix/MatrixHttpUserTest.java b/src/testInt/java/io/kamax/matrix/MatrixHttpUserTest.java new file mode 100644 index 0000000..6be3327 --- /dev/null +++ b/src/testInt/java/io/kamax/matrix/MatrixHttpUserTest.java @@ -0,0 +1,25 @@ +/* + * 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 . + */ + +package io.kamax.matrix; + +public class MatrixHttpUserTest extends AMatrixHttpUserTest { + +} diff --git a/src/testInt/java/io/kamax/matrix/client/MatrixHttpContentTest.java b/src/testInt/java/io/kamax/matrix/client/MatrixHttpContentTest.java new file mode 100644 index 0000000..32b5701 --- /dev/null +++ b/src/testInt/java/io/kamax/matrix/client/MatrixHttpContentTest.java @@ -0,0 +1,29 @@ +/* + * 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 . + */ + +package io.kamax.matrix.client; + +import java.net.URISyntaxException; + +public class MatrixHttpContentTest extends AMatrixHttpContentTest { + + public MatrixHttpContentTest() throws URISyntaxException { + } +} diff --git a/src/testInt/java/io/kamax/matrix/client/MatrixHttpRoomTest.java b/src/testInt/java/io/kamax/matrix/client/MatrixHttpRoomTest.java new file mode 100644 index 0000000..ecba7ff --- /dev/null +++ b/src/testInt/java/io/kamax/matrix/client/MatrixHttpRoomTest.java @@ -0,0 +1,25 @@ +/* + * 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 . + */ + +package io.kamax.matrix.client; + +public class MatrixHttpRoomTest extends AMatrixHttpRoomTest { + +} diff --git a/src/testInt/java/io/kamax/matrix/client/regular/MatrixHttpClientLoginTest.java b/src/testInt/java/io/kamax/matrix/client/regular/MatrixHttpClientLoginTest.java new file mode 100644 index 0000000..1722c22 --- /dev/null +++ b/src/testInt/java/io/kamax/matrix/client/regular/MatrixHttpClientLoginTest.java @@ -0,0 +1,24 @@ +/* + * 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 . + */ +package io.kamax.matrix.client.regular; + +public class MatrixHttpClientLoginTest extends AMatrixHttpClientLoginTest { + +} diff --git a/src/testInt/java/io/kamax/matrix/client/regular/MatrixHttpClientTest.java b/src/testInt/java/io/kamax/matrix/client/regular/MatrixHttpClientTest.java new file mode 100644 index 0000000..8aff701 --- /dev/null +++ b/src/testInt/java/io/kamax/matrix/client/regular/MatrixHttpClientTest.java @@ -0,0 +1,24 @@ +/* + * 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 . + */ + +package io.kamax.matrix.client.regular; + +public class MatrixHttpClientTest extends AMatrixHttpClientTest { +} diff --git a/src/testInt/resources/.gitignore b/src/testInt/resources/.gitignore new file mode 100644 index 0000000..d3e7d11 --- /dev/null +++ b/src/testInt/resources/.gitignore @@ -0,0 +1 @@ +HomeserverTest.conf diff --git a/src/testInt/resources/HomserverTest.conf_template b/src/testInt/resources/HomserverTest.conf_template new file mode 100644 index 0000000..2745e2f --- /dev/null +++ b/src/testInt/resources/HomserverTest.conf_template @@ -0,0 +1,11 @@ +# If you want to run these tests against a real homeserver, please fill out the necessary information below and +# rename this file to test.conf + +# Server information +Domain = localhost +Hostname = localhost +Port = 443 + +# User credentials +Username = xxx +Password = xxx \ No newline at end of file