diff --git a/build.gradle b/build.gradle index 2f8fdfc..8f351c7 100644 --- a/build.gradle +++ b/build.gradle @@ -17,6 +17,14 @@ * You should have received a copy of the GNU Affero General Public License * along with this program. If not, see . */ +buildscript { + repositories { + mavenCentral() + } + dependencies { + classpath 'org.junit.platform:junit-platform-gradle-plugin:1.0.1' + } +} plugins { id "com.diffplug.gradle.spotless" version "3.5.2" @@ -27,6 +35,7 @@ version = '0.0.2' apply plugin: 'java' apply plugin: 'maven' +apply plugin: 'org.junit.platform.gradle.plugin' configurations { deployerJars @@ -43,7 +52,9 @@ dependencies { compile 'org.apache.httpcomponents:fluent-hc:4.5.3' compile 'com.google.code.gson:gson:2.8.0' - testCompile 'junit:junit:4.12' + testCompile "org.junit.jupiter:junit-jupiter-api:5.0.0" + testRuntime "org.junit.jupiter:junit-jupiter-engine:5.0.0" + testCompile "com.github.tomakehurst:wiremock:2.8.0" testRuntimeOnly 'org.slf4j:slf4j-simple:1.7.25' deployerJars "org.apache.maven.wagon:wagon-ssh:2.2" @@ -86,3 +97,7 @@ spotless { importOrderFile 'spotless.importorder' } } + +junitPlatform { + enableStandardTestTask true +} diff --git a/src/main/java/io/kamax/matrix/client/MatrixHttpContent.java b/src/main/java/io/kamax/matrix/client/MatrixHttpContent.java index 0aac443..7316e8b 100644 --- a/src/main/java/io/kamax/matrix/client/MatrixHttpContent.java +++ b/src/main/java/io/kamax/matrix/client/MatrixHttpContent.java @@ -45,7 +45,7 @@ public class MatrixHttpContent extends AMatrixHttpClient implements _MatrixConte private Logger log = LoggerFactory.getLogger(MatrixHttpContent.class); - private final Pattern filenamePattern = Pattern.compile("filename=\"?(?.+)\"?;?"); + private final Pattern filenamePattern = Pattern.compile("filename=\"?(?[^\";]+)"); private URI address; private String type; diff --git a/src/test/java/io/kamax/matrix/client/MatrixHttpContentTest.java b/src/test/java/io/kamax/matrix/client/MatrixHttpContentTest.java new file mode 100644 index 0000000..81ccd97 --- /dev/null +++ b/src/test/java/io/kamax/matrix/client/MatrixHttpContentTest.java @@ -0,0 +1,184 @@ +/* + * 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 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); + private String downloadUrl = "/_matrix/media/v1/download/" + address.getHost() + address.getPath() + tokenParameter; + + public MatrixHttpContentTest() throws URISyntaxException { + } + + @Test + public void isValid() throws URISyntaxException { + stubFor(get(urlEqualTo(downloadUrl)).willReturn( + aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain"))); + assertTrue(createContentObject().isValid()); + } + + @Test + public void isValidMissingContentType() throws URISyntaxException { + stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(200).withBodyFile(bodyFilename))); + assertFalse(createContentObject().isValid()); + } + + @Test + public void isValidError404() throws URISyntaxException { + stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); + assertFalse(createContentObject().isValid()); + } + + @Test + public void isValidError403() throws URISyntaxException { + stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); + assertFalse(createContentObject().isValid()); + } + + @Test + public void getType() throws URISyntaxException, IOException { + stubFor(get(urlEqualTo(downloadUrl)).willReturn( + aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain"))); + assertEquals("text/plain", createContentObject().getType()); + } + + @Test + public void getTypeMissingContentType() throws URISyntaxException { + stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(200).withBodyFile(bodyFilename))); + assertNull(createContentObject().getType()); + } + + @Test + public void getTypeError404() throws URISyntaxException, IOException { + stubFor(get(urlEqualTo(downloadUrl)) + .willReturn(aResponse().withStatus(404).withStatus(404).withBody(error404Response))); + assertNull(createContentObject().getType()); + } + + @Test + public void getTypeError403() throws URISyntaxException, IOException { + stubFor(get(urlEqualTo(downloadUrl)) + .willReturn(aResponse().withStatus(403).withStatus(403).withBody(error403Response))); + assertNull(createContentObject().getType()); + } + + @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)); + } + + @Test + public void getDataMissingContentType() throws URISyntaxException { + stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(200).withBodyFile(bodyFilename))); + assertNull(createContentObject().getData()); + } + + @Test + public void getDataError404() throws URISyntaxException, IOException { + stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); + assertNull(createContentObject().getData()); + } + + @Test + public void getDataError403() throws URISyntaxException, IOException { + stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); + assertNull(createContentObject().getData()); + } + + @Test + 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(); + + 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(); + + 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(); + + 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()); + } + + @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()); + } + + @Test + public void getFilenameError404() throws URISyntaxException, IOException { + stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); + assertEquals(Optional.empty(), createContentObject().getFilename()); + } + + @Test + public void getFilenameError403() throws URISyntaxException, IOException { + stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); + assertEquals(Optional.empty(), createContentObject().getFilename()); + } + + private MatrixHttpContent createContentObject() throws URISyntaxException { + MatrixClientContext context = createClientContext(); + return new MatrixHttpContent(context, address); + } + +} diff --git a/src/test/java/io/kamax/matrix/client/MatrixHttpRoomTest.java b/src/test/java/io/kamax/matrix/client/MatrixHttpRoomTest.java new file mode 100644 index 0000000..f2410e5 --- /dev/null +++ b/src/test/java/io/kamax/matrix/client/MatrixHttpRoomTest.java @@ -0,0 +1,266 @@ +/* + * matrix-java-sdk - Matrix Client SDK for Java + * Copyright (C) 2017 Arne Augenstein + * + * https://max.kamax.io/ + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU Affero General Public License as + * published by the Free Software Foundation, either version 3 of the + * License, or (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU Affero General Public License for more details. + * + * You should have received a copy of the GNU Affero General Public License + * along with this program. If not, see . + */ + +package io.kamax.matrix.client; + +import io.kamax.matrix.MatrixID; +import io.kamax.matrix._MatrixID; + +import org.hamcrest.core.IsEqual; +import org.junit.Test; + +import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.List; +import java.util.Optional; + +import static com.github.tomakehurst.wiremock.client.WireMock.*; + +import static org.junit.Assert.assertThat; +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class MatrixHttpRoomTest extends MatrixHttpTest { + private String roomId = "roomId892347847"; + private String eventId = "YUwRidLecu"; + + private String nameUrl = String.format("/_matrix/client/r0/rooms/%s/state/m.room.name", roomId) + tokenParameter; + private String nameOfRoom = "test room"; + private String nameResponse = String.format("{\"name\": \"%s\"}", nameOfRoom); + + private String topicUrl = String.format("/_matrix/client/r0/rooms/%s/state/m.room.topic", roomId) + tokenParameter; + private String testTopic = "test topic"; + private String topicResponse = String.format("{\"topic\": \"%s\"}", testTopic); + + private String joinUrl = String.format("/_matrix/client/r0/rooms/%s/join", roomId) + tokenParameter; + private String joinResponse = String.format("{\"roomId\": \"%s\"}", roomId); + + private String leaveUrl = String.format("/_matrix/client/r0/rooms/%s/leave", roomId) + tokenParameter; + private String leaveResponse = String.format("{\"roomId\": \"%s\"}", roomId); + + private String sendTextUrl = String.format("/_matrix/client/r0/rooms/%s/send/m.room.message/([0-9.]+)\\", roomId) + + tokenParameter; + private String testText = "test text"; + private String sendTextResponse = String.format("{\"event_id\": \"%s\"}", eventId) + tokenParameter; + + private String getJoinedUsersUrl = String.format("/_matrix/client/r0/rooms/%s/joined_members", roomId) + + tokenParameter; + private String joinedUser1 = "@test:testserver.org"; + private String joinedUser2 = "@test2:testserver.org"; + private String getJoinedUsersResponse = String.format("{\"joined\": {\"%s\": \"1\", \"%s\": \"2\"}}", joinedUser1, + joinedUser2); + + @Test + public void getName() throws URISyntaxException { + stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(200).withBody(nameResponse))); + assertThat(createRoomObject().getName(), IsEqual.equalTo(Optional.of(nameOfRoom))); + } + + @Test + public void getName404() throws URISyntaxException { + stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(404).withBody(nameResponse))); + assertThat(createRoomObject().getName(), IsEqual.equalTo(Optional.empty())); + } + + @Test + public void getNameError403() throws URISyntaxException { + stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); + + MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getName); + // TODO After the refactoring, the exception will contain a proper MatrixErrorInfo object + // checkErrorInfo403(e); + } + + @Test + public void getNameError429() throws URISyntaxException { + stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(429).withBody(error403Response))); + + MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getName); + // TODO After the refactoring, the exception will contain a proper MatrixErrorInfo object + // checkErrorInfo429(e); + } + + @Test + public void getTopic() throws URISyntaxException { + stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(200).withBody(topicResponse))); + assertThat(createRoomObject().getTopic(), IsEqual.equalTo(Optional.of(testTopic))); + } + + @Test + public void getTopicError404() throws URISyntaxException { + stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(404).withBody(topicResponse))); + assertThat(createRoomObject().getTopic(), IsEqual.equalTo(Optional.empty())); + } + + @Test + public void getTopicError403() throws URISyntaxException { + stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); + + MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getTopic); + checkErrorInfo403(e); + } + + @Test + public void getTopicError429() throws URISyntaxException { + stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); + + MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getTopic); + checkErrorInfo429(e); + } + + @Test + public void join() throws URISyntaxException { + stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(200).withBody(joinResponse))); + createRoomObject().join(); + } + + @Test + public void joinError404() throws URISyntaxException { + stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); + + MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::join); + checkErrorInfo404(e); + } + + @Test + public void joinError403() throws URISyntaxException { + stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); + + // TODO After the refactoring, this test will throw a MatrixClientRequestException + createRoomObject().join(); + // MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::join); + // checkErrorInfo403(e); + } + + @Test + public void joinError429() throws URISyntaxException { + stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); + + MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::join); + checkErrorInfo429(e); + } + + @Test + public void leave() throws URISyntaxException { + stubFor(post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(200).withBody(leaveResponse))); + createRoomObject().leave(); + } + + @Test + public void leaveError403() throws URISyntaxException { + stubFor(post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); + + // TODO After the refactoring, this test will throw a MatrixClientRequestException + createRoomObject().leave(); + // MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::leave); + // checkErrorInfo403(e); + } + + @Test + public void leaveError404() throws URISyntaxException { + stubFor(post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); + + // TODO After the refactoring, this test will throw a MatrixClientRequestException + createRoomObject().leave(); + // MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::leave); + // checkErrorInfo404(e); + } + + @Test + public void leaveError429() throws URISyntaxException { + stubFor(post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); + + MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::leave); + checkErrorInfo429(e); + } + + @Test + public void sendText() throws URISyntaxException { + stubFor(put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(200).withBody(sendTextResponse))); + createRoomObject().sendText(testText); + } + + @Test + public void sendTextError403() throws URISyntaxException { + stubFor(put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); + + // TODO After the refactoring, this test will throw a MatrixClientRequestException + createRoomObject().sendText(testText); + // MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, + // () -> createRoomObject().sendText(testText)); + // checkErrorInfo404(e); + } + + @Test + public void sendTextError404() throws URISyntaxException { + stubFor(put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); + + MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, + () -> createRoomObject().sendText(testText)); + // TODO After the refactoring, the exception will contain a proper MatrixErrorInfo object + // checkErrorInfo404(e); + } + + @Test + public void sendTextError429() throws URISyntaxException { + stubFor(put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); + + MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, + () -> createRoomObject().sendText(testText)); + // TODO After the refactoring, the exception will contain a proper MatrixErrorInfo object + // checkErrorInfo429(e); + } + + @Test + public void getJoinedUsers() throws URISyntaxException { + stubFor(get(urlEqualTo(getJoinedUsersUrl)) + .willReturn(aResponse().withStatus(200).withBody(getJoinedUsersResponse))); + + List<_MatrixID> expectedResult = new ArrayList<>(); + expectedResult.add(new MatrixID(joinedUser1)); + expectedResult.add(new MatrixID(joinedUser2)); + + assertThat(createRoomObject().getJoinedUsers(), IsEqual.equalTo(expectedResult)); + } + + @Test + public void getJoinedUsersError404() throws URISyntaxException { + stubFor(get(urlEqualTo(getJoinedUsersUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); + + MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, + createRoomObject()::getJoinedUsers); + // TODO After the refactoring, the exception will contain a proper MatrixErrorInfo object + // checkErrorInfo404(e); + } + + @Test + public void getJoinedUsers429() throws URISyntaxException { + stubFor(get(urlEqualTo(getJoinedUsersUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); + + MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, + createRoomObject()::getJoinedUsers); + // TODO After the refactoring, the exception will contain a proper MatrixErrorInfo object + // checkErrorInfo404(e); + } + + private MatrixHttpRoom createRoomObject() throws URISyntaxException { + MatrixClientContext context = createClientContext(); + return new MatrixHttpRoom(context, roomId); + } +} diff --git a/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java b/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java new file mode 100644 index 0000000..4336583 --- /dev/null +++ b/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java @@ -0,0 +1,90 @@ +/* + * 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 com.github.tomakehurst.wiremock.junit.WireMockRule; + +import io.kamax.matrix.MatrixErrorInfo; +import io.kamax.matrix.MatrixID; +import io.kamax.matrix.hs.MatrixHomeserver; + +import org.junit.Rule; + +import java.net.URISyntaxException; +import java.util.Optional; + +import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; +import static junit.framework.TestCase.assertEquals; + +import static org.junit.Assert.assertTrue; + +public class MatrixHttpTest { + protected String testToken = "testToken"; + protected String tokenParameter = "?access_token=" + testToken; + protected int port = 8098; + protected String resourcePath = "src/test/resources/wiremock"; + + private String errorResponseTemplate = "{\"errcode\": \"%s\", \"error\": \"%s\"}"; + + private String errcode403 = "M_FORBIDDEN"; + private String error403 = "Access denied."; + protected String error403Response = String.format(errorResponseTemplate, errcode403, error403); + + private String errcode404 = "M_NOT_FOUND"; + private String error404 = "Element not found."; + protected String error404Response = String.format(errorResponseTemplate, errcode404, error404); + + private String errcode429 = "M_LIMIT_EXCEEDED"; + 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)); + + protected MatrixClientContext createClientContext() throws URISyntaxException { + String domain = "localhost"; + String baseUrl = "http://localhost:" + port; + MatrixHomeserver hs = new MatrixHomeserver(domain, baseUrl); + return new MatrixClientContext(hs, new MatrixID("testuser", domain), testToken); + } + + protected String getAcessTokenParameter() { + return "?access_token=" + testToken; + } + + protected void checkErrorInfo403(MatrixClientRequestException e) { + checkErrorInfo(errcode403, error403, e.getError()); + } + + protected void checkErrorInfo404(MatrixClientRequestException e) { + checkErrorInfo(errcode404, error404, e.getError()); + } + + protected void checkErrorInfo429(MatrixClientRequestException e) { + checkErrorInfo(errcode429, error429, e.getError()); + } + + private void checkErrorInfo(String errcode, String error, Optional errorOptional) { + assertTrue(errorOptional.isPresent()); + assertEquals(errorOptional.get().getErrcode(), errcode); + assertEquals(errorOptional.get().getError(), error); + } +} diff --git a/src/test/java/io/kamax/matrix/client/as/MatrixApplicationServiceClientTest.java b/src/test/java/io/kamax/matrix/client/as/MatrixApplicationServiceClientTest.java new file mode 100644 index 0000000..f0d1e82 --- /dev/null +++ b/src/test/java/io/kamax/matrix/client/as/MatrixApplicationServiceClientTest.java @@ -0,0 +1,60 @@ +/* + * 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.as; + +import io.kamax.matrix.client.*; +import io.kamax.matrix.hs.MatrixHomeserver; + +import org.junit.Test; + +import java.net.URISyntaxException; + +import static com.github.tomakehurst.wiremock.client.WireMock.*; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class MatrixApplicationServiceClientTest extends MatrixHttpTest { + private String createUserUrl = "/_matrix/client/r0/register" + tokenParameter; + private String testUser = "testUser"; + + @Test + public void createUser() throws URISyntaxException { + stubFor(post(urlEqualTo(createUserUrl)).willReturn(aResponse().withStatus(200))); + createClientObject().createUser(testUser); + } + + @Test + public void createUserError429() throws URISyntaxException { + stubFor(post(urlEqualTo(createUserUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); + + MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, + () -> createClientObject().createUser(testUser)); + checkErrorInfo429(e); + } + + private MatrixApplicationServiceClient createClientObject() throws URISyntaxException { + String domain = "localhost"; + String baseUrl = "http://localhost:" + port; + MatrixHomeserver hs = new MatrixHomeserver(domain, baseUrl); + return new MatrixApplicationServiceClient(hs, testToken, "testuser"); + } + +} diff --git a/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientTest.java b/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientTest.java new file mode 100644 index 0000000..712cbc0 --- /dev/null +++ b/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientTest.java @@ -0,0 +1,60 @@ +/* + * 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.*; + +import org.junit.Test; + +import java.net.URISyntaxException; + +import static com.github.tomakehurst.wiremock.client.WireMock.*; + +import static org.junit.jupiter.api.Assertions.assertThrows; + +public class MatrixHttpClientTest extends MatrixHttpTest { + private String setDisplaynameUrl = String.format("/_matrix/client/r0/profile/%s/displayname", + createClientContext().getUser().getId()) + tokenParameter; + private String displayName = "display name"; + + public MatrixHttpClientTest() throws URISyntaxException { + } + + @Test + public void setDisplayName() throws URISyntaxException { + stubFor(put(urlEqualTo(setDisplaynameUrl)).willReturn(aResponse().withStatus(200))); + createClientObject().setDisplayName(displayName); + } + + @Test + public void setDisplayNameError429() 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/resources/wiremock/__files/textfile.txt b/src/test/resources/wiremock/__files/textfile.txt new file mode 100644 index 0000000..53b68c0 --- /dev/null +++ b/src/test/resources/wiremock/__files/textfile.txt @@ -0,0 +1 @@ +text file with test text \ No newline at end of file