From 5c33780d50fa3cb6bb10864cce8a58491f31c2ea Mon Sep 17 00:00:00 2001 From: Arne Augenstein Date: Wed, 15 Nov 2017 18:04:07 +0100 Subject: [PATCH 01/14] Fix error in test case --- .../io/kamax/matrix/client/regular/MatrixHttpClientTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientTest.java b/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientTest.java index 3ec43cf..02c31fb 100644 --- a/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientTest.java +++ b/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientTest.java @@ -92,7 +92,7 @@ public void loginWithDeviceIdAndLogout() throws URISyntaxException { .willReturn(aResponse().withStatus(200) .withBody("{\"user_id\": \"" + user.getId() + "\"," + // "\"access_token\": \"" + testToken + "\"," + // - "\"home_server\": \"" + new MatrixHomeserver(domain, baseUrl) + "\"," + // + "\"home_server\": \"" + hostname + "\"," + // "\"device_id\": \"" + deviceId + "\"}"))); MatrixHomeserver hs = new MatrixHomeserver(domain, baseUrl); @@ -125,7 +125,7 @@ public void login() throws URISyntaxException { .willReturn(aResponse().withStatus(200) .withBody("{\"user_id\": \"" + user.getId() + "\"," + // "\"access_token\": \"" + testToken + "\"," + // - "\"home_server\": \"" + new MatrixHomeserver(domain, baseUrl) + "\"," + // + "\"home_server\": \"" + hostname + "\"," + // "\"device_id\": \"" + deviceId + "\"}"))); MatrixHomeserver hs = new MatrixHomeserver(domain, baseUrl); From 4953b1ceb4e7a47899756635f1eb2b8c2ff43796 Mon Sep 17 00:00:00 2001 From: Arne Augenstein Date: Wed, 15 Nov 2017 22:41:07 +0100 Subject: [PATCH 02/14] Add ability to login from test cases --- .../io/kamax/matrix/MatrixHttpUserTest.java | 4 +- .../matrix/client/MatrixHttpRoomTest.java | 72 ++++++++++++------- .../kamax/matrix/client/MatrixHttpTest.java | 72 +++++++++++++++++-- src/test/resources/.gitignore | 1 + src/test/resources/test.conf_template | 11 +++ 5 files changed, 126 insertions(+), 34 deletions(-) create mode 100644 src/test/resources/.gitignore create mode 100644 src/test/resources/test.conf_template diff --git a/src/test/java/io/kamax/matrix/MatrixHttpUserTest.java b/src/test/java/io/kamax/matrix/MatrixHttpUserTest.java index fad41c9..95e4d8f 100644 --- a/src/test/java/io/kamax/matrix/MatrixHttpUserTest.java +++ b/src/test/java/io/kamax/matrix/MatrixHttpUserTest.java @@ -41,7 +41,7 @@ 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 nameResponse = String.format("{\"displayname\": \"%s\"}", username); private String avatarUrl = String.format("/_matrix/client/r0/profile/%s/avatar_url", user.getId()) + tokenParameter; private String avatarMediaUrl = "mxc://matrix.org/wefh34uihSDRGhw34"; @@ -50,7 +50,7 @@ public class MatrixHttpUserTest extends MatrixHttpTest { @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 diff --git a/src/test/java/io/kamax/matrix/client/MatrixHttpRoomTest.java b/src/test/java/io/kamax/matrix/client/MatrixHttpRoomTest.java index 1e9802b..aefba71 100644 --- a/src/test/java/io/kamax/matrix/client/MatrixHttpRoomTest.java +++ b/src/test/java/io/kamax/matrix/client/MatrixHttpRoomTest.java @@ -24,7 +24,7 @@ import io.kamax.matrix._MatrixID; import org.hamcrest.core.IsEqual; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.net.URISyntaxException; import java.util.ArrayList; @@ -68,19 +68,22 @@ public class MatrixHttpRoomTest extends MatrixHttpTest { @Test public void getName() throws URISyntaxException { - stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(200).withBody(nameResponse))); + configureWiremock(s -> s + .stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(200).withBody(nameResponse)))); assertThat(createRoomObject().getName(), IsEqual.equalTo(Optional.of(nameOfRoom))); } @Test public void getEmptyName() throws URISyntaxException { - stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); + configureWiremock(s -> s + .stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)))); assertThat(createRoomObject().getName(), IsEqual.equalTo(Optional.empty())); } @Test public void getNameAccessDenied() throws URISyntaxException { - stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); + configureWiremock(s -> s + .stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getName); checkErrorInfo403(e); @@ -88,7 +91,8 @@ public void getNameAccessDenied() throws URISyntaxException { @Test public void getNameRateLimited() throws URISyntaxException { - stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); + configureWiremock(s -> s + .stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getName); checkErrorInfo429(e); @@ -96,19 +100,22 @@ public void getNameRateLimited() throws URISyntaxException { @Test public void getTopic() throws URISyntaxException { - stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(200).withBody(topicResponse))); + configureWiremock(s -> s + .stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(200).withBody(topicResponse)))); assertThat(createRoomObject().getTopic(), IsEqual.equalTo(Optional.of(testTopic))); } @Test public void getEmptyTopic() throws URISyntaxException { - stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); + configureWiremock(s -> s + .stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)))); assertThat(createRoomObject().getTopic(), IsEqual.equalTo(Optional.empty())); } @Test public void getTopicAccessDenied() throws URISyntaxException { - stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); + configureWiremock(s -> s + .stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getTopic); checkErrorInfo403(e); @@ -116,7 +123,8 @@ public void getTopicAccessDenied() throws URISyntaxException { @Test public void getTopicRateLimited() throws URISyntaxException { - stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); + configureWiremock(s -> s + .stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getTopic); checkErrorInfo429(e); @@ -124,13 +132,15 @@ public void getTopicRateLimited() throws URISyntaxException { @Test public void join() throws URISyntaxException { - stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(200).withBody(joinResponse))); + configureWiremock(s -> s + .stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(200).withBody(joinResponse)))); createRoomObject().join(); } @Test public void joinRoomNotFound() throws URISyntaxException { - stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); + configureWiremock(s -> s + .stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::join); checkErrorInfo404(e); @@ -138,7 +148,8 @@ public void joinRoomNotFound() throws URISyntaxException { @Test public void joinAccessDenied() throws URISyntaxException { - stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); + configureWiremock(s -> s + .stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::join); checkErrorInfo403(e); @@ -146,7 +157,8 @@ public void joinAccessDenied() throws URISyntaxException { @Test public void joinRateLimited() throws URISyntaxException { - stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); + configureWiremock(s -> s + .stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::join); checkErrorInfo429(e); @@ -154,13 +166,15 @@ public void joinRateLimited() throws URISyntaxException { @Test public void leave() throws URISyntaxException { - stubFor(post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(200).withBody(leaveResponse))); + configureWiremock(s -> s + .stubFor(post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(200).withBody(leaveResponse)))); createRoomObject().leave(); } @Test public void leaveAccessDenied() throws URISyntaxException { - stubFor(post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); + configureWiremock(s -> s.stubFor( + post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::leave); checkErrorInfo403(e); @@ -168,13 +182,15 @@ public void leaveAccessDenied() throws URISyntaxException { @Test public void leaveRoomNotFound() throws URISyntaxException { - stubFor(post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); + configureWiremock(s -> s.stubFor( + post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)))); createRoomObject().leave(); } @Test public void leaveRateLimited() throws URISyntaxException { - stubFor(post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); + configureWiremock(s -> s.stubFor( + post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::leave); checkErrorInfo429(e); @@ -182,13 +198,15 @@ public void leaveRateLimited() throws URISyntaxException { @Test public void sendText() throws URISyntaxException { - stubFor(put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(200).withBody(sendTextResponse))); + configureWiremock(s -> s.stubFor( + put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(200).withBody(sendTextResponse)))); createRoomObject().sendText(testText); } @Test public void sendTextAccessDenied() throws URISyntaxException { - stubFor(put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); + configureWiremock(s -> s.stubFor( + put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, () -> createRoomObject().sendText(testText)); @@ -197,7 +215,8 @@ public void sendTextAccessDenied() throws URISyntaxException { @Test public void sendTextRoomNotFound() throws URISyntaxException { - stubFor(put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); + configureWiremock(s -> s.stubFor( + put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, () -> createRoomObject().sendText(testText)); @@ -206,7 +225,8 @@ public void sendTextRoomNotFound() throws URISyntaxException { @Test public void sendTextRateLimited() throws URISyntaxException { - stubFor(put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); + configureWiremock(s -> s.stubFor( + put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, () -> createRoomObject().sendText(testText)); @@ -215,8 +235,8 @@ public void sendTextRateLimited() throws URISyntaxException { @Test public void getJoinedUsers() throws URISyntaxException { - stubFor(get(urlEqualTo(getJoinedUsersUrl)) - .willReturn(aResponse().withStatus(200).withBody(getJoinedUsersResponse))); + configureWiremock(s -> s.stubFor(get(urlEqualTo(getJoinedUsersUrl)) + .willReturn(aResponse().withStatus(200).withBody(getJoinedUsersResponse)))); List<_MatrixID> expectedResult = new ArrayList<>(); expectedResult.add(new MatrixID(joinedUser1)); @@ -227,7 +247,8 @@ public void getJoinedUsers() throws URISyntaxException { @Test public void getJoinedUsersRoomNotFound() throws URISyntaxException { - stubFor(get(urlEqualTo(getJoinedUsersUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); + configureWiremock(s -> s.stubFor( + get(urlEqualTo(getJoinedUsersUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getJoinedUsers); @@ -236,7 +257,8 @@ public void getJoinedUsersRoomNotFound() throws URISyntaxException { @Test public void getJoinedUsersRateLimited() throws URISyntaxException { - stubFor(get(urlEqualTo(getJoinedUsersUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); + configureWiremock(s -> s.stubFor( + get(urlEqualTo(getJoinedUsersUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getJoinedUsers); diff --git a/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java b/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java index 1e01e56..fbcdb88 100644 --- a/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java +++ b/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java @@ -20,16 +20,26 @@ package io.kamax.matrix.client; -import com.github.tomakehurst.wiremock.junit.WireMockRule; +import com.github.tomakehurst.wiremock.WireMockServer; 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.Rule; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.extension.ParameterResolutionException; +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.function.Consumer; +import java.util.stream.Collectors; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; import static junit.framework.TestCase.assertEquals; @@ -45,8 +55,8 @@ 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 MatrixID user = new MatrixID(username, domain); private String errorResponseTemplate = "{\"errcode\": \"%s\", \"error\": \"%s\"}"; @@ -62,11 +72,10 @@ 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)); + protected WireMockServer server; + private MatrixHttpClient client; protected MatrixClientContext createClientContext() throws URISyntaxException { - MatrixHomeserver hs = new MatrixHomeserver(domain, baseUrl); return new MatrixClientContext(hs, user, testToken); } @@ -88,4 +97,53 @@ private void checkErrorInfo(String errcode, String error, Optional configValues = buffer.lines().filter(line -> !line.startsWith("#")).collect( + Collectors.toMap(line -> line.split("=")[0].trim(), line -> line.split("=")[1].trim())); + + String portKey = "Port"; + port = Integer.valueOf(configValues.get(portKey)); + hostname = configValues.get("Hostname"); + domain = configValues.get("Domain"); + baseUrl = "https://" + hostname + ":" + port; + MatrixHomeserver homeserver = new MatrixHomeserver(domain, baseUrl); + + username = configValues.get("Username"); + MatrixClientContext context = new MatrixClientContext(homeserver); + MatrixPasswordLoginCredentials credentials = new MatrixPasswordLoginCredentials(username, + configValues.get("Password")); + + client = new MatrixHttpClient(context); + client.login(credentials); + testToken = client.getAccessTokenOrThrow(); + } catch (IOException e) { + throw new ParameterResolutionException("Config file could not be read", e); + } + } else { + server = new WireMockServer(options().port(port).usingFilesUnderDirectory(resourcePath)); + server.start(); + } + } + + @AfterEach + protected void tearDown() { + if (server != null) { + server.resetAll(); + server.stop(); + } else { + client.logout(); + } + } + + public void configureWiremock(Consumer var1) { + if (server != null) { + var1.accept(server); + } + } } diff --git a/src/test/resources/.gitignore b/src/test/resources/.gitignore new file mode 100644 index 0000000..9eb8893 --- /dev/null +++ b/src/test/resources/.gitignore @@ -0,0 +1 @@ +test.conf diff --git a/src/test/resources/test.conf_template b/src/test/resources/test.conf_template new file mode 100644 index 0000000..2745e2f --- /dev/null +++ b/src/test/resources/test.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 From 5c4146dc0e246f6f79bc5936c8e2fcddd83c36e7 Mon Sep 17 00:00:00 2001 From: Arne Augenstein Date: Wed, 15 Nov 2017 22:52:20 +0100 Subject: [PATCH 03/14] Move other tests to new method --- .../io/kamax/matrix/MatrixHttpUserTest.java | 26 ++++--- .../matrix/client/MatrixHttpContentTest.java | 73 +++++++++++-------- .../matrix/client/MatrixHttpRoomTest.java | 46 ++++++------ .../kamax/matrix/client/MatrixHttpTest.java | 2 +- .../MatrixApplicationServiceClientTest.java | 10 ++- .../client/regular/MatrixHttpClientTest.java | 29 ++++---- 6 files changed, 103 insertions(+), 83 deletions(-) diff --git a/src/test/java/io/kamax/matrix/MatrixHttpUserTest.java b/src/test/java/io/kamax/matrix/MatrixHttpUserTest.java index 95e4d8f..7b4315e 100644 --- a/src/test/java/io/kamax/matrix/MatrixHttpUserTest.java +++ b/src/test/java/io/kamax/matrix/MatrixHttpUserTest.java @@ -25,7 +25,7 @@ import io.kamax.matrix.client.MatrixHttpTest; import io.kamax.matrix.client.MatrixHttpUser; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.net.URI; import java.net.URISyntaxException; @@ -49,19 +49,22 @@ public class MatrixHttpUserTest extends MatrixHttpTest { @Test public void getName() throws URISyntaxException { - stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(200).withBody(nameResponse))); + callIfNotNull(s -> s + .stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(200).withBody(nameResponse)))); assertThat(createUserObject().getName(), is(equalTo(Optional.of(username)))); } @Test public void getName404() throws URISyntaxException { - stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); + callIfNotNull(s -> s + .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))); + callIfNotNull(s -> s + .stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createUserObject()::getName); checkErrorInfo403(e); @@ -69,7 +72,8 @@ public void getNameError403() throws URISyntaxException { @Test public void getNameError429() throws URISyntaxException { - stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); + callIfNotNull(s -> s + .stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createUserObject()::getName); checkErrorInfo429(e); @@ -77,7 +81,8 @@ public void getNameError429() throws URISyntaxException { @Test public void getAvatar() throws URISyntaxException { - stubFor(get(urlEqualTo(avatarUrl)).willReturn(aResponse().withStatus(200).withBody(avatarResponse))); + callIfNotNull(s -> s + .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,13 +90,15 @@ public void getAvatar() throws URISyntaxException { @Test public void getAvatar404() throws URISyntaxException { - stubFor(get(urlEqualTo(avatarUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); + callIfNotNull(s -> s.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))); + callIfNotNull(s -> s.stubFor( + get(urlEqualTo(avatarUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createUserObject()::getAvatar); @@ -100,7 +107,8 @@ public void getAvatarError403() throws URISyntaxException { @Test public void getAvatarError429() throws URISyntaxException { - stubFor(get(urlEqualTo(avatarUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); + callIfNotNull(s -> s.stubFor( + get(urlEqualTo(avatarUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createUserObject()::getAvatar); diff --git a/src/test/java/io/kamax/matrix/client/MatrixHttpContentTest.java b/src/test/java/io/kamax/matrix/client/MatrixHttpContentTest.java index 4a49213..212ddc8 100644 --- a/src/test/java/io/kamax/matrix/client/MatrixHttpContentTest.java +++ b/src/test/java/io/kamax/matrix/client/MatrixHttpContentTest.java @@ -21,7 +21,7 @@ package io.kamax.matrix.client; import org.hamcrest.core.IsEqual; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.io.File; import java.io.IOException; @@ -50,45 +50,50 @@ 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"))); + callIfNotNull(s -> s.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))); + callIfNotNull(s -> s.stubFor( + get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(200).withBodyFile(bodyFilename)))); assertTrue(createContentObject().isValid()); } @Test public void isValidContentNotFound() throws URISyntaxException { - stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); + callIfNotNull(s -> s.stubFor( + get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)))); assertFalse(createContentObject().isValid()); } @Test public void isValidErrorAccessDenied() throws URISyntaxException { - stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); + callIfNotNull(s -> s.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"))); + callIfNotNull(s -> s.stubFor(get(urlEqualTo(downloadUrl)).willReturn( + aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain")))); assertEquals(Optional.of("text/plain"), createContentObject().getType()); } @Test public void getTypeMissingContentType() throws URISyntaxException { - stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(200).withBodyFile(bodyFilename))); + callIfNotNull(s -> s.stubFor( + get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(200).withBodyFile(bodyFilename)))); assertEquals(Optional.empty(), createContentObject().getType()); } @Test public void getTypeErrorContentNotFound() throws URISyntaxException, IOException { - stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); + callIfNotNull(s -> s.stubFor( + get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)))); MatrixHttpContent contentObject = createContentObject(); assertFalse(contentObject.isValid()); assertThrows(IllegalStateException.class, contentObject::getType); @@ -96,7 +101,8 @@ public void getTypeErrorContentNotFound() throws URISyntaxException, IOException @Test public void getTypeErrorAccessDenied() throws URISyntaxException, IOException { - stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); + callIfNotNull(s -> s.stubFor( + get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)))); MatrixHttpContent contentObject = createContentObject(); assertFalse(contentObject.isValid()); assertThrows(IllegalStateException.class, createContentObject()::getType); @@ -104,8 +110,8 @@ public void getTypeErrorAccessDenied() throws URISyntaxException, IOException { @Test public void getData() throws URISyntaxException, IOException { - stubFor(get(urlEqualTo(downloadUrl)).willReturn( - aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain"))); + callIfNotNull(s -> s.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())); @@ -114,7 +120,8 @@ public void getData() throws URISyntaxException, IOException { @Test public void getDataMissingContentType() throws URISyntaxException, IOException { - stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(200).withBodyFile(bodyFilename))); + callIfNotNull(s -> s.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())); @@ -123,7 +130,8 @@ public void getDataMissingContentType() throws URISyntaxException, IOException { @Test public void getDataErrorContentNotFound() throws URISyntaxException, IOException { - stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); + callIfNotNull(s -> s.stubFor( + get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)))); MatrixHttpContent contentObject = createContentObject(); assertFalse(contentObject.isValid()); @@ -132,7 +140,8 @@ public void getDataErrorContentNotFound() throws URISyntaxException, IOException @Test public void getDataErrorAccessDenied() throws URISyntaxException, IOException { - stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); + callIfNotNull(s -> s.stubFor( + get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)))); MatrixHttpContent contentObject = createContentObject(); assertFalse(contentObject.isValid()); @@ -141,43 +150,44 @@ public void getDataErrorAccessDenied() throws URISyntaxException, IOException { @Test public void getFilename() throws URISyntaxException, IOException { - stubFor(get(urlEqualTo(downloadUrl)).willReturn( + callIfNotNull(s -> s.stubFor(get(urlEqualTo(downloadUrl)).willReturn( aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain") - .withHeader("Content-Disposition", String.format("filename=%s;", bodyFilename)))); + .withHeader("Content-Disposition", String.format("filename=%s;", bodyFilename))))); assertEquals(Optional.of(bodyFilename), createContentObject().getFilename()); - reset(); + callIfNotNull(s -> s.resetAll()); - stubFor(get(urlEqualTo(downloadUrl)).willReturn( + callIfNotNull(s -> s.stubFor(get(urlEqualTo(downloadUrl)).willReturn( aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain") - .withHeader("Content-Disposition", String.format("filename=\"%s\";", bodyFilename)))); + .withHeader("Content-Disposition", String.format("filename=\"%s\";", bodyFilename))))); assertEquals(Optional.of(bodyFilename), createContentObject().getFilename()); - reset(); + callIfNotNull(s -> s.resetAll()); - stubFor(get(urlEqualTo(downloadUrl)).willReturn( + callIfNotNull(s -> s.stubFor(get(urlEqualTo(downloadUrl)).willReturn( aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain") - .withHeader("Content-Disposition", String.format("filename=\"%s\"", bodyFilename)))); + .withHeader("Content-Disposition", String.format("filename=\"%s\"", bodyFilename))))); assertEquals(Optional.of(bodyFilename), createContentObject().getFilename()); - reset(); + callIfNotNull(s -> s.resetAll()); - stubFor(get(urlEqualTo(downloadUrl)).willReturn( + callIfNotNull(s -> s.stubFor(get(urlEqualTo(downloadUrl)).willReturn( aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain") - .withHeader("Content-Disposition", String.format("filename=%s", bodyFilename)))); + .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"))); + callIfNotNull(s -> s.stubFor(get(urlEqualTo(downloadUrl)).willReturn( + aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain")))); assertEquals(Optional.empty(), createContentObject().getFilename()); } @Test public void getFilenameErrorContentNotFound() throws URISyntaxException, IOException { - stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); + callIfNotNull(s -> s.stubFor( + get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)))); MatrixHttpContent contentObject = createContentObject(); assertFalse(contentObject.isValid()); @@ -186,7 +196,8 @@ public void getFilenameErrorContentNotFound() throws URISyntaxException, IOExcep @Test public void getFilenameErrorAccessDenied() throws URISyntaxException, IOException { - stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); + callIfNotNull(s -> s.stubFor( + get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)))); MatrixHttpContent contentObject = createContentObject(); assertFalse(contentObject.isValid()); assertThrows(IllegalStateException.class, contentObject::getFilename); diff --git a/src/test/java/io/kamax/matrix/client/MatrixHttpRoomTest.java b/src/test/java/io/kamax/matrix/client/MatrixHttpRoomTest.java index aefba71..b659822 100644 --- a/src/test/java/io/kamax/matrix/client/MatrixHttpRoomTest.java +++ b/src/test/java/io/kamax/matrix/client/MatrixHttpRoomTest.java @@ -68,21 +68,21 @@ public class MatrixHttpRoomTest extends MatrixHttpTest { @Test public void getName() throws URISyntaxException { - configureWiremock(s -> s + callIfNotNull(s -> s .stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(200).withBody(nameResponse)))); assertThat(createRoomObject().getName(), IsEqual.equalTo(Optional.of(nameOfRoom))); } @Test public void getEmptyName() throws URISyntaxException { - configureWiremock(s -> s + callIfNotNull(s -> s .stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)))); assertThat(createRoomObject().getName(), IsEqual.equalTo(Optional.empty())); } @Test public void getNameAccessDenied() throws URISyntaxException { - configureWiremock(s -> s + callIfNotNull(s -> s .stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getName); @@ -91,7 +91,7 @@ public void getNameAccessDenied() throws URISyntaxException { @Test public void getNameRateLimited() throws URISyntaxException { - configureWiremock(s -> s + callIfNotNull(s -> s .stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getName); @@ -100,21 +100,21 @@ public void getNameRateLimited() throws URISyntaxException { @Test public void getTopic() throws URISyntaxException { - configureWiremock(s -> s + callIfNotNull(s -> s .stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(200).withBody(topicResponse)))); assertThat(createRoomObject().getTopic(), IsEqual.equalTo(Optional.of(testTopic))); } @Test public void getEmptyTopic() throws URISyntaxException { - configureWiremock(s -> s + callIfNotNull(s -> s .stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)))); assertThat(createRoomObject().getTopic(), IsEqual.equalTo(Optional.empty())); } @Test public void getTopicAccessDenied() throws URISyntaxException { - configureWiremock(s -> s + callIfNotNull(s -> s .stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getTopic); @@ -123,7 +123,7 @@ public void getTopicAccessDenied() throws URISyntaxException { @Test public void getTopicRateLimited() throws URISyntaxException { - configureWiremock(s -> s + callIfNotNull(s -> s .stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getTopic); @@ -132,14 +132,14 @@ public void getTopicRateLimited() throws URISyntaxException { @Test public void join() throws URISyntaxException { - configureWiremock(s -> s + callIfNotNull(s -> s .stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(200).withBody(joinResponse)))); createRoomObject().join(); } @Test public void joinRoomNotFound() throws URISyntaxException { - configureWiremock(s -> s + callIfNotNull(s -> s .stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::join); @@ -148,7 +148,7 @@ public void joinRoomNotFound() throws URISyntaxException { @Test public void joinAccessDenied() throws URISyntaxException { - configureWiremock(s -> s + callIfNotNull(s -> s .stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::join); @@ -157,7 +157,7 @@ public void joinAccessDenied() throws URISyntaxException { @Test public void joinRateLimited() throws URISyntaxException { - configureWiremock(s -> s + callIfNotNull(s -> s .stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::join); @@ -166,14 +166,14 @@ public void joinRateLimited() throws URISyntaxException { @Test public void leave() throws URISyntaxException { - configureWiremock(s -> s + callIfNotNull(s -> s .stubFor(post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(200).withBody(leaveResponse)))); createRoomObject().leave(); } @Test public void leaveAccessDenied() throws URISyntaxException { - configureWiremock(s -> s.stubFor( + callIfNotNull(s -> s.stubFor( post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::leave); @@ -182,14 +182,14 @@ public void leaveAccessDenied() throws URISyntaxException { @Test public void leaveRoomNotFound() throws URISyntaxException { - configureWiremock(s -> s.stubFor( + callIfNotNull(s -> s.stubFor( post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)))); createRoomObject().leave(); } @Test public void leaveRateLimited() throws URISyntaxException { - configureWiremock(s -> s.stubFor( + callIfNotNull(s -> s.stubFor( post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::leave); @@ -198,14 +198,14 @@ public void leaveRateLimited() throws URISyntaxException { @Test public void sendText() throws URISyntaxException { - configureWiremock(s -> s.stubFor( + callIfNotNull(s -> s.stubFor( put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(200).withBody(sendTextResponse)))); createRoomObject().sendText(testText); } @Test public void sendTextAccessDenied() throws URISyntaxException { - configureWiremock(s -> s.stubFor( + callIfNotNull(s -> s.stubFor( put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, @@ -215,7 +215,7 @@ public void sendTextAccessDenied() throws URISyntaxException { @Test public void sendTextRoomNotFound() throws URISyntaxException { - configureWiremock(s -> s.stubFor( + callIfNotNull(s -> s.stubFor( put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, @@ -225,7 +225,7 @@ public void sendTextRoomNotFound() throws URISyntaxException { @Test public void sendTextRateLimited() throws URISyntaxException { - configureWiremock(s -> s.stubFor( + callIfNotNull(s -> s.stubFor( put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, @@ -235,7 +235,7 @@ public void sendTextRateLimited() throws URISyntaxException { @Test public void getJoinedUsers() throws URISyntaxException { - configureWiremock(s -> s.stubFor(get(urlEqualTo(getJoinedUsersUrl)) + callIfNotNull(s -> s.stubFor(get(urlEqualTo(getJoinedUsersUrl)) .willReturn(aResponse().withStatus(200).withBody(getJoinedUsersResponse)))); List<_MatrixID> expectedResult = new ArrayList<>(); @@ -247,7 +247,7 @@ public void getJoinedUsers() throws URISyntaxException { @Test public void getJoinedUsersRoomNotFound() throws URISyntaxException { - configureWiremock(s -> s.stubFor( + callIfNotNull(s -> s.stubFor( get(urlEqualTo(getJoinedUsersUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, @@ -257,7 +257,7 @@ public void getJoinedUsersRoomNotFound() throws URISyntaxException { @Test public void getJoinedUsersRateLimited() throws URISyntaxException { - configureWiremock(s -> s.stubFor( + callIfNotNull(s -> s.stubFor( get(urlEqualTo(getJoinedUsersUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, diff --git a/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java b/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java index fbcdb88..d6854a5 100644 --- a/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java +++ b/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java @@ -141,7 +141,7 @@ protected void tearDown() { } } - public void configureWiremock(Consumer var1) { + public void callIfNotNull(Consumer var1) { if (server != null) { var1.accept(server); } 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..7126f14 100644 --- a/src/test/java/io/kamax/matrix/client/as/MatrixApplicationServiceClientTest.java +++ b/src/test/java/io/kamax/matrix/client/as/MatrixApplicationServiceClientTest.java @@ -20,10 +20,11 @@ package io.kamax.matrix.client.as; -import io.kamax.matrix.client.*; +import io.kamax.matrix.client.MatrixClientRequestException; +import io.kamax.matrix.client.MatrixHttpTest; import io.kamax.matrix.hs.MatrixHomeserver; -import org.junit.Test; +import org.junit.jupiter.api.Test; import java.net.URISyntaxException; @@ -37,13 +38,14 @@ public class MatrixApplicationServiceClientTest extends MatrixHttpTest { @Test public void createUser() throws URISyntaxException { - stubFor(post(urlEqualTo(createUserUrl)).willReturn(aResponse().withStatus(200))); + callIfNotNull(s -> s.stubFor(post(urlEqualTo(createUserUrl)).willReturn(aResponse().withStatus(200)))); createClientObject().createUser(testUser); } @Test public void createUserErrorRateLimited() throws URISyntaxException { - stubFor(post(urlEqualTo(createUserUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); + callIfNotNull(s -> s.stubFor( + post(urlEqualTo(createUserUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, () -> createClientObject().createUser(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 index 02c31fb..e6aa791 100644 --- a/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientTest.java +++ b/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientTest.java @@ -26,16 +26,14 @@ import io.kamax.matrix.client.MatrixPasswordLoginCredentials; import io.kamax.matrix.hs.MatrixHomeserver; -import org.junit.Test; +import org.junit.jupiter.api.Test; import org.junit.platform.commons.util.StringUtils; import java.net.URISyntaxException; 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; +import static org.junit.jupiter.api.Assertions.*; public class MatrixHttpClientTest extends MatrixHttpTest { private String loginUrl = "/_matrix/client/r0/login"; @@ -53,7 +51,7 @@ public MatrixHttpClientTest() throws URISyntaxException { @Test public void loginWithDeviceId() throws URISyntaxException { - stubFor(post(urlEqualTo(loginUrl)) + callIfNotNull(s -> s.stubFor(post(urlEqualTo(loginUrl)) .withRequestBody(equalToJson("{\"type\": \"m.login.password\"," + // "\"user\": \"" + user.getLocalPart() + "\"," + // "\"password\": \"" + password + "\"," + // @@ -62,7 +60,7 @@ public void loginWithDeviceId() throws URISyntaxException { .withBody("{\"user_id\": \"" + user.getId() + "\"," + // "\"access_token\": \"" + testToken + "\"," + // "\"home_server\": \"" + hostname + "\"," + // - "\"device_id\": \"" + deviceId + "\"}"))); + "\"device_id\": \"" + deviceId + "\"}")))); MatrixHomeserver hs = new MatrixHomeserver(domain, baseUrl); MatrixClientContext context = new MatrixClientContext(hs, deviceId); @@ -84,7 +82,7 @@ public void loginWithDeviceId() throws URISyntaxException { @Test public void loginWithDeviceIdAndLogout() throws URISyntaxException { - stubFor(post(urlEqualTo(loginUrl)) + callIfNotNull(s -> s.stubFor(post(urlEqualTo(loginUrl)) .withRequestBody(equalToJson("{\"type\": \"m.login.password\"," + // "\"user\": \"" + user.getLocalPart() + "\"," + // "\"password\": \"" + password + "\"," + // @@ -93,7 +91,7 @@ public void loginWithDeviceIdAndLogout() throws URISyntaxException { .withBody("{\"user_id\": \"" + user.getId() + "\"," + // "\"access_token\": \"" + testToken + "\"," + // "\"home_server\": \"" + hostname + "\"," + // - "\"device_id\": \"" + deviceId + "\"}"))); + "\"device_id\": \"" + deviceId + "\"}")))); MatrixHomeserver hs = new MatrixHomeserver(domain, baseUrl); MatrixClientContext context = new MatrixClientContext(hs, deviceId); @@ -112,13 +110,13 @@ public void loginWithDeviceIdAndLogout() throws URISyntaxException { */ assertEquals(deviceId, client.getDeviceId().get()); - stubFor(post(urlEqualTo(logoutUrl))); + callIfNotNull(s -> s.stubFor(post(urlEqualTo(logoutUrl)))); client.logout(); } @Test public void login() throws URISyntaxException { - stubFor(post(urlEqualTo(loginUrl)) + callIfNotNull(s -> s.stubFor(post(urlEqualTo(loginUrl)) .withRequestBody(equalToJson("{\"type\": \"m.login.password\"," + // "\"user\": \"" + user.getLocalPart() + "\"," + // "\"password\": \"" + password + "\"}")) @@ -126,7 +124,7 @@ public void login() throws URISyntaxException { .withBody("{\"user_id\": \"" + user.getId() + "\"," + // "\"access_token\": \"" + testToken + "\"," + // "\"home_server\": \"" + hostname + "\"," + // - "\"device_id\": \"" + deviceId + "\"}"))); + "\"device_id\": \"" + deviceId + "\"}")))); MatrixHomeserver hs = new MatrixHomeserver(domain, baseUrl); MatrixClientContext context = new MatrixClientContext(hs); @@ -148,11 +146,11 @@ public void login() throws URISyntaxException { @Test public void loginWrongPassword() throws URISyntaxException { - stubFor(post(urlEqualTo(loginUrl)) + callIfNotNull(s -> s.stubFor(post(urlEqualTo(loginUrl)) .withRequestBody(equalToJson("{\"type\": \"m.login.password\"," + // "\"user\": \"" + user.getLocalPart() + "\"," + // "\"password\": \"" + password + "\"}")) - .willReturn(aResponse().withStatus(403).withBody(error403Response))); + .willReturn(aResponse().withStatus(403).withBody(error403Response)))); MatrixHomeserver hs = new MatrixHomeserver(domain, baseUrl); MatrixClientContext context = new MatrixClientContext(hs); @@ -166,13 +164,14 @@ public void loginWrongPassword() throws URISyntaxException { @Test public void setDisplayName() throws URISyntaxException { - stubFor(put(urlEqualTo(setDisplaynameUrl)).willReturn(aResponse().withStatus(200))); + callIfNotNull(s -> s.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))); + callIfNotNull(s -> s.stubFor( + put(urlEqualTo(setDisplaynameUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, () -> createClientObject().setDisplayName(displayName)); From 3ae9805ae625ec6973cacbc1ae181f0941ab46bd Mon Sep 17 00:00:00 2001 From: Arne Augenstein Date: Sun, 19 Nov 2017 00:02:26 +0100 Subject: [PATCH 04/14] Change to saner approach for testing real homeservers --- .../io/kamax/matrix/MatrixHttpUserTest.java | 26 ++-- .../matrix/client/MatrixHttpContentTest.java | 73 +++++----- .../matrix/client/MatrixHttpRoomTest.java | 126 +++++++++++------- .../kamax/matrix/client/MatrixHttpTest.java | 71 +++++----- .../MatrixApplicationServiceClientTest.java | 10 +- .../client/regular/MatrixHttpClientTest.java | 29 ++-- 6 files changed, 174 insertions(+), 161 deletions(-) diff --git a/src/test/java/io/kamax/matrix/MatrixHttpUserTest.java b/src/test/java/io/kamax/matrix/MatrixHttpUserTest.java index 7b4315e..95e4d8f 100644 --- a/src/test/java/io/kamax/matrix/MatrixHttpUserTest.java +++ b/src/test/java/io/kamax/matrix/MatrixHttpUserTest.java @@ -25,7 +25,7 @@ import io.kamax.matrix.client.MatrixHttpTest; import io.kamax.matrix.client.MatrixHttpUser; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.net.URI; import java.net.URISyntaxException; @@ -49,22 +49,19 @@ public class MatrixHttpUserTest extends MatrixHttpTest { @Test public void getName() throws URISyntaxException { - callIfNotNull(s -> s - .stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(200).withBody(nameResponse)))); + stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(200).withBody(nameResponse))); assertThat(createUserObject().getName(), is(equalTo(Optional.of(username)))); } @Test public void getName404() throws URISyntaxException { - callIfNotNull(s -> s - .stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)))); + stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); assertThat(createUserObject().getName(), is(equalTo(Optional.empty()))); } @Test public void getNameError403() throws URISyntaxException { - callIfNotNull(s -> s - .stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)))); + stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createUserObject()::getName); checkErrorInfo403(e); @@ -72,8 +69,7 @@ public void getNameError403() throws URISyntaxException { @Test public void getNameError429() throws URISyntaxException { - callIfNotNull(s -> s - .stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)))); + stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createUserObject()::getName); checkErrorInfo429(e); @@ -81,8 +77,7 @@ public void getNameError429() throws URISyntaxException { @Test public void getAvatar() throws URISyntaxException { - callIfNotNull(s -> s - .stubFor(get(urlEqualTo(avatarUrl)).willReturn(aResponse().withStatus(200).withBody(avatarResponse)))); + 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)))); @@ -90,15 +85,13 @@ public void getAvatar() throws URISyntaxException { @Test public void getAvatar404() throws URISyntaxException { - callIfNotNull(s -> s.stubFor( - get(urlEqualTo(avatarUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)))); + stubFor(get(urlEqualTo(avatarUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); assertThat(createUserObject().getAvatar(), is(equalTo(Optional.empty()))); } @Test public void getAvatarError403() throws URISyntaxException { - callIfNotNull(s -> s.stubFor( - get(urlEqualTo(avatarUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)))); + stubFor(get(urlEqualTo(avatarUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createUserObject()::getAvatar); @@ -107,8 +100,7 @@ public void getAvatarError403() throws URISyntaxException { @Test public void getAvatarError429() throws URISyntaxException { - callIfNotNull(s -> s.stubFor( - get(urlEqualTo(avatarUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)))); + stubFor(get(urlEqualTo(avatarUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createUserObject()::getAvatar); diff --git a/src/test/java/io/kamax/matrix/client/MatrixHttpContentTest.java b/src/test/java/io/kamax/matrix/client/MatrixHttpContentTest.java index 212ddc8..4a49213 100644 --- a/src/test/java/io/kamax/matrix/client/MatrixHttpContentTest.java +++ b/src/test/java/io/kamax/matrix/client/MatrixHttpContentTest.java @@ -21,7 +21,7 @@ package io.kamax.matrix.client; import org.hamcrest.core.IsEqual; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.io.File; import java.io.IOException; @@ -50,50 +50,45 @@ public MatrixHttpContentTest() throws URISyntaxException { @Test public void isValid() throws URISyntaxException { - callIfNotNull(s -> s.stubFor(get(urlEqualTo(downloadUrl)).willReturn( - aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain")))); + stubFor(get(urlEqualTo(downloadUrl)).willReturn( + aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain"))); assertTrue(createContentObject().isValid()); } @Test public void isValidMissingContentType() throws URISyntaxException { - callIfNotNull(s -> s.stubFor( - get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(200).withBodyFile(bodyFilename)))); + stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(200).withBodyFile(bodyFilename))); assertTrue(createContentObject().isValid()); } @Test public void isValidContentNotFound() throws URISyntaxException { - callIfNotNull(s -> s.stubFor( - get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)))); + stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); assertFalse(createContentObject().isValid()); } @Test public void isValidErrorAccessDenied() throws URISyntaxException { - callIfNotNull(s -> s.stubFor( - get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)))); + stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); assertFalse(createContentObject().isValid()); } @Test public void getType() throws URISyntaxException, IOException { - callIfNotNull(s -> s.stubFor(get(urlEqualTo(downloadUrl)).willReturn( - aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain")))); + stubFor(get(urlEqualTo(downloadUrl)).willReturn( + aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain"))); assertEquals(Optional.of("text/plain"), createContentObject().getType()); } @Test public void getTypeMissingContentType() throws URISyntaxException { - callIfNotNull(s -> s.stubFor( - get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(200).withBodyFile(bodyFilename)))); + stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(200).withBodyFile(bodyFilename))); assertEquals(Optional.empty(), createContentObject().getType()); } @Test public void getTypeErrorContentNotFound() throws URISyntaxException, IOException { - callIfNotNull(s -> s.stubFor( - get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)))); + stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); MatrixHttpContent contentObject = createContentObject(); assertFalse(contentObject.isValid()); assertThrows(IllegalStateException.class, contentObject::getType); @@ -101,8 +96,7 @@ public void getTypeErrorContentNotFound() throws URISyntaxException, IOException @Test public void getTypeErrorAccessDenied() throws URISyntaxException, IOException { - callIfNotNull(s -> s.stubFor( - get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)))); + stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); MatrixHttpContent contentObject = createContentObject(); assertFalse(contentObject.isValid()); assertThrows(IllegalStateException.class, createContentObject()::getType); @@ -110,8 +104,8 @@ public void getTypeErrorAccessDenied() throws URISyntaxException, IOException { @Test public void getData() throws URISyntaxException, IOException { - callIfNotNull(s -> s.stubFor(get(urlEqualTo(downloadUrl)).willReturn( - aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain")))); + 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())); @@ -120,8 +114,7 @@ public void getData() throws URISyntaxException, IOException { @Test public void getDataMissingContentType() throws URISyntaxException, IOException { - callIfNotNull(s -> s.stubFor( - get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(200).withBodyFile(bodyFilename)))); + 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())); @@ -130,8 +123,7 @@ public void getDataMissingContentType() throws URISyntaxException, IOException { @Test public void getDataErrorContentNotFound() throws URISyntaxException, IOException { - callIfNotNull(s -> s.stubFor( - get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)))); + stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); MatrixHttpContent contentObject = createContentObject(); assertFalse(contentObject.isValid()); @@ -140,8 +132,7 @@ public void getDataErrorContentNotFound() throws URISyntaxException, IOException @Test public void getDataErrorAccessDenied() throws URISyntaxException, IOException { - callIfNotNull(s -> s.stubFor( - get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)))); + stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); MatrixHttpContent contentObject = createContentObject(); assertFalse(contentObject.isValid()); @@ -150,44 +141,43 @@ public void getDataErrorAccessDenied() throws URISyntaxException, IOException { @Test public void getFilename() throws URISyntaxException, IOException { - callIfNotNull(s -> s.stubFor(get(urlEqualTo(downloadUrl)).willReturn( + stubFor(get(urlEqualTo(downloadUrl)).willReturn( aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain") - .withHeader("Content-Disposition", String.format("filename=%s;", bodyFilename))))); + .withHeader("Content-Disposition", String.format("filename=%s;", bodyFilename)))); assertEquals(Optional.of(bodyFilename), createContentObject().getFilename()); - callIfNotNull(s -> s.resetAll()); + reset(); - callIfNotNull(s -> s.stubFor(get(urlEqualTo(downloadUrl)).willReturn( + stubFor(get(urlEqualTo(downloadUrl)).willReturn( aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain") - .withHeader("Content-Disposition", String.format("filename=\"%s\";", bodyFilename))))); + .withHeader("Content-Disposition", String.format("filename=\"%s\";", bodyFilename)))); assertEquals(Optional.of(bodyFilename), createContentObject().getFilename()); - callIfNotNull(s -> s.resetAll()); + reset(); - callIfNotNull(s -> s.stubFor(get(urlEqualTo(downloadUrl)).willReturn( + stubFor(get(urlEqualTo(downloadUrl)).willReturn( aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain") - .withHeader("Content-Disposition", String.format("filename=\"%s\"", bodyFilename))))); + .withHeader("Content-Disposition", String.format("filename=\"%s\"", bodyFilename)))); assertEquals(Optional.of(bodyFilename), createContentObject().getFilename()); - callIfNotNull(s -> s.resetAll()); + reset(); - callIfNotNull(s -> s.stubFor(get(urlEqualTo(downloadUrl)).willReturn( + stubFor(get(urlEqualTo(downloadUrl)).willReturn( aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain") - .withHeader("Content-Disposition", String.format("filename=%s", bodyFilename))))); + .withHeader("Content-Disposition", String.format("filename=%s", bodyFilename)))); assertEquals(Optional.of(bodyFilename), createContentObject().getFilename()); } @Test public void getFilenameMissingContentType() throws URISyntaxException { - callIfNotNull(s -> s.stubFor(get(urlEqualTo(downloadUrl)).willReturn( - aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain")))); + stubFor(get(urlEqualTo(downloadUrl)).willReturn( + aResponse().withStatus(200).withBodyFile(bodyFilename).withHeader("Content-Type", "text/plain"))); assertEquals(Optional.empty(), createContentObject().getFilename()); } @Test public void getFilenameErrorContentNotFound() throws URISyntaxException, IOException { - callIfNotNull(s -> s.stubFor( - get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)))); + stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); MatrixHttpContent contentObject = createContentObject(); assertFalse(contentObject.isValid()); @@ -196,8 +186,7 @@ public void getFilenameErrorContentNotFound() throws URISyntaxException, IOExcep @Test public void getFilenameErrorAccessDenied() throws URISyntaxException, IOException { - callIfNotNull(s -> s.stubFor( - get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)))); + stubFor(get(urlEqualTo(downloadUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); MatrixHttpContent contentObject = createContentObject(); assertFalse(contentObject.isValid()); assertThrows(IllegalStateException.class, contentObject::getFilename); diff --git a/src/test/java/io/kamax/matrix/client/MatrixHttpRoomTest.java b/src/test/java/io/kamax/matrix/client/MatrixHttpRoomTest.java index b659822..2022dd5 100644 --- a/src/test/java/io/kamax/matrix/client/MatrixHttpRoomTest.java +++ b/src/test/java/io/kamax/matrix/client/MatrixHttpRoomTest.java @@ -24,7 +24,7 @@ import io.kamax.matrix._MatrixID; import org.hamcrest.core.IsEqual; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.net.URISyntaxException; import java.util.ArrayList; @@ -68,201 +68,233 @@ public class MatrixHttpRoomTest extends MatrixHttpTest { @Test public void getName() throws URISyntaxException { - callIfNotNull(s -> s - .stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(200).withBody(nameResponse)))); + stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(200).withBody(nameResponse))); + + login(); assertThat(createRoomObject().getName(), IsEqual.equalTo(Optional.of(nameOfRoom))); + logout(); } @Test public void getEmptyName() throws URISyntaxException { - callIfNotNull(s -> s - .stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)))); + stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); + + login(); assertThat(createRoomObject().getName(), IsEqual.equalTo(Optional.empty())); + logout(); } @Test public void getNameAccessDenied() throws URISyntaxException { - callIfNotNull(s -> s - .stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)))); + stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); + login(); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getName); checkErrorInfo403(e); + logout(); } @Test public void getNameRateLimited() throws URISyntaxException { - callIfNotNull(s -> s - .stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)))); + stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); + login(); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getName); checkErrorInfo429(e); + logout(); } @Test public void getTopic() throws URISyntaxException { - callIfNotNull(s -> s - .stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(200).withBody(topicResponse)))); + stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(200).withBody(topicResponse))); + + login(); assertThat(createRoomObject().getTopic(), IsEqual.equalTo(Optional.of(testTopic))); + logout(); } @Test public void getEmptyTopic() throws URISyntaxException { - callIfNotNull(s -> s - .stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)))); + stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); + + login(); assertThat(createRoomObject().getTopic(), IsEqual.equalTo(Optional.empty())); + logout(); } @Test public void getTopicAccessDenied() throws URISyntaxException { - callIfNotNull(s -> s - .stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)))); + stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); + login(); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getTopic); checkErrorInfo403(e); + logout(); } @Test public void getTopicRateLimited() throws URISyntaxException { - callIfNotNull(s -> s - .stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)))); + stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); + login(); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getTopic); checkErrorInfo429(e); + logout(); } @Test public void join() throws URISyntaxException { - callIfNotNull(s -> s - .stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(200).withBody(joinResponse)))); + stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(200).withBody(joinResponse))); + + login(); createRoomObject().join(); + logout(); } @Test public void joinRoomNotFound() throws URISyntaxException { - callIfNotNull(s -> s - .stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)))); + stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); + login(); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::join); checkErrorInfo404(e); + logout(); } @Test public void joinAccessDenied() throws URISyntaxException { - callIfNotNull(s -> s - .stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)))); + stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); + login(); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::join); checkErrorInfo403(e); + logout(); } @Test public void joinRateLimited() throws URISyntaxException { - callIfNotNull(s -> s - .stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)))); + stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); + login(); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::join); checkErrorInfo429(e); + logout(); } @Test public void leave() throws URISyntaxException { - callIfNotNull(s -> s - .stubFor(post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(200).withBody(leaveResponse)))); + stubFor(post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(200).withBody(leaveResponse))); + + login(); createRoomObject().leave(); + logout(); } @Test public void leaveAccessDenied() throws URISyntaxException { - callIfNotNull(s -> s.stubFor( - post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)))); + stubFor(post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); + login(); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::leave); checkErrorInfo403(e); + logout(); } @Test public void leaveRoomNotFound() throws URISyntaxException { - callIfNotNull(s -> s.stubFor( - post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)))); + stubFor(post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); + + login(); createRoomObject().leave(); + logout(); } @Test public void leaveRateLimited() throws URISyntaxException { - callIfNotNull(s -> s.stubFor( - post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)))); + stubFor(post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); + login(); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::leave); checkErrorInfo429(e); + logout(); } @Test public void sendText() throws URISyntaxException { - callIfNotNull(s -> s.stubFor( - put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(200).withBody(sendTextResponse)))); + stubFor(put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(200).withBody(sendTextResponse))); + + login(); createRoomObject().sendText(testText); + logout(); } @Test public void sendTextAccessDenied() throws URISyntaxException { - callIfNotNull(s -> s.stubFor( - put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)))); + stubFor(put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); + login(); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, () -> createRoomObject().sendText(testText)); checkErrorInfo403(e); + logout(); } @Test public void sendTextRoomNotFound() throws URISyntaxException { - callIfNotNull(s -> s.stubFor( - put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)))); + stubFor(put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); + login(); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, () -> createRoomObject().sendText(testText)); checkErrorInfo404(e); + logout(); } @Test public void sendTextRateLimited() throws URISyntaxException { - callIfNotNull(s -> s.stubFor( - put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)))); + stubFor(put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); + login(); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, () -> createRoomObject().sendText(testText)); checkErrorInfo429(e); + logout(); } @Test public void getJoinedUsers() throws URISyntaxException { - callIfNotNull(s -> s.stubFor(get(urlEqualTo(getJoinedUsersUrl)) - .willReturn(aResponse().withStatus(200).withBody(getJoinedUsersResponse)))); + 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)); + login(); assertThat(createRoomObject().getJoinedUsers(), IsEqual.equalTo(expectedResult)); + logout(); } @Test public void getJoinedUsersRoomNotFound() throws URISyntaxException { - callIfNotNull(s -> s.stubFor( - get(urlEqualTo(getJoinedUsersUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response)))); + stubFor(get(urlEqualTo(getJoinedUsersUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); + login(); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getJoinedUsers); checkErrorInfo404(e); + logout(); } @Test public void getJoinedUsersRateLimited() throws URISyntaxException { - callIfNotNull(s -> s.stubFor( - get(urlEqualTo(getJoinedUsersUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)))); + stubFor(get(urlEqualTo(getJoinedUsersUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); + login(); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getJoinedUsers); checkErrorInfo429(e); + logout(); } private MatrixHttpRoom createRoomObject() throws URISyntaxException { diff --git a/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java b/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java index d6854a5..9ade01d 100644 --- a/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java +++ b/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java @@ -20,16 +20,16 @@ package io.kamax.matrix.client; -import com.github.tomakehurst.wiremock.WireMockServer; +import com.github.tomakehurst.wiremock.junit.WireMockRule; 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.jupiter.api.AfterEach; -import org.junit.jupiter.api.BeforeEach; -import org.junit.jupiter.api.extension.ParameterResolutionException; +import org.junit.After; +import org.junit.Before; +import org.junit.Rule; import java.io.BufferedReader; import java.io.IOException; @@ -38,7 +38,6 @@ import java.net.URISyntaxException; import java.util.Map; import java.util.Optional; -import java.util.function.Consumer; import java.util.stream.Collectors; import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.options; @@ -56,6 +55,7 @@ public class MatrixHttpTest { protected String hostname = "localhost"; protected String baseUrl = "http://" + hostname + ":" + port; protected String username = "testuser"; + protected String password = ""; protected MatrixID user = new MatrixID(username, domain); private String errorResponseTemplate = "{\"errcode\": \"%s\", \"error\": \"%s\"}"; @@ -72,7 +72,8 @@ 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); - protected WireMockServer server; + @Rule + public WireMockRule wireMockRule = new WireMockRule(options().port(port).usingFilesUnderDirectory(resourcePath)); private MatrixHttpClient client; protected MatrixClientContext createClientContext() throws URISyntaxException { @@ -98,52 +99,52 @@ private void checkErrorInfo(String errcode, String error, Optional { 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())); - String portKey = "Port"; - port = Integer.valueOf(configValues.get(portKey)); + port = Integer.valueOf(configValues.get("Port")); hostname = configValues.get("Hostname"); domain = configValues.get("Domain"); baseUrl = "https://" + hostname + ":" + port; - MatrixHomeserver homeserver = new MatrixHomeserver(domain, baseUrl); - username = configValues.get("Username"); + password = configValues.get("Password"); + } catch (IOException e) { + throw new IllegalStateException(e); + } + }); + } + + private Optional getConfigFileStream() { + // TODO Is the location of the config file alright? + return Optional.ofNullable(this.getClass().getResourceAsStream("/test.conf")); + } + + protected void login() throws URISyntaxException { + getConfigFileStream().ifPresent((InputStream configFile) -> { + try { + MatrixHomeserver homeserver = new MatrixHomeserver(domain, baseUrl); MatrixClientContext context = new MatrixClientContext(homeserver); - MatrixPasswordLoginCredentials credentials = new MatrixPasswordLoginCredentials(username, - configValues.get("Password")); + createClientContext(); + MatrixPasswordLoginCredentials credentials = new MatrixPasswordLoginCredentials(username, password); client = new MatrixHttpClient(context); client.login(credentials); testToken = client.getAccessTokenOrThrow(); - } catch (IOException e) { - throw new ParameterResolutionException("Config file could not be read", e); + } catch (URISyntaxException e) { + throw new IllegalStateException(e); } - } else { - server = new WireMockServer(options().port(port).usingFilesUnderDirectory(resourcePath)); - server.start(); - } + }); } - @AfterEach - protected void tearDown() { - if (server != null) { - server.resetAll(); - server.stop(); - } else { + @After + public void logout() { + if (client != null) { client.logout(); - } - } - - public void callIfNotNull(Consumer var1) { - if (server != null) { - var1.accept(server); + client = null; } } } 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 7126f14..b2f9677 100644 --- a/src/test/java/io/kamax/matrix/client/as/MatrixApplicationServiceClientTest.java +++ b/src/test/java/io/kamax/matrix/client/as/MatrixApplicationServiceClientTest.java @@ -20,11 +20,10 @@ package io.kamax.matrix.client.as; -import io.kamax.matrix.client.MatrixClientRequestException; -import io.kamax.matrix.client.MatrixHttpTest; +import io.kamax.matrix.client.*; import io.kamax.matrix.hs.MatrixHomeserver; -import org.junit.jupiter.api.Test; +import org.junit.Test; import java.net.URISyntaxException; @@ -38,14 +37,13 @@ public class MatrixApplicationServiceClientTest extends MatrixHttpTest { @Test public void createUser() throws URISyntaxException { - callIfNotNull(s -> s.stubFor(post(urlEqualTo(createUserUrl)).willReturn(aResponse().withStatus(200)))); + stubFor(post(urlEqualTo(createUserUrl)).willReturn(aResponse().withStatus(200))); createClientObject().createUser(testUser); } @Test public void createUserErrorRateLimited() throws URISyntaxException { - callIfNotNull(s -> s.stubFor( - post(urlEqualTo(createUserUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)))); + stubFor(post(urlEqualTo(createUserUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, () -> createClientObject().createUser(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 index e6aa791..02c31fb 100644 --- a/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientTest.java +++ b/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientTest.java @@ -26,14 +26,16 @@ import io.kamax.matrix.client.MatrixPasswordLoginCredentials; import io.kamax.matrix.hs.MatrixHomeserver; -import org.junit.jupiter.api.Test; +import org.junit.Test; import org.junit.platform.commons.util.StringUtils; import java.net.URISyntaxException; import static com.github.tomakehurst.wiremock.client.WireMock.*; -import static org.junit.jupiter.api.Assertions.*; +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"; @@ -51,7 +53,7 @@ public MatrixHttpClientTest() throws URISyntaxException { @Test public void loginWithDeviceId() throws URISyntaxException { - callIfNotNull(s -> s.stubFor(post(urlEqualTo(loginUrl)) + stubFor(post(urlEqualTo(loginUrl)) .withRequestBody(equalToJson("{\"type\": \"m.login.password\"," + // "\"user\": \"" + user.getLocalPart() + "\"," + // "\"password\": \"" + password + "\"," + // @@ -60,7 +62,7 @@ public void loginWithDeviceId() throws URISyntaxException { .withBody("{\"user_id\": \"" + user.getId() + "\"," + // "\"access_token\": \"" + testToken + "\"," + // "\"home_server\": \"" + hostname + "\"," + // - "\"device_id\": \"" + deviceId + "\"}")))); + "\"device_id\": \"" + deviceId + "\"}"))); MatrixHomeserver hs = new MatrixHomeserver(domain, baseUrl); MatrixClientContext context = new MatrixClientContext(hs, deviceId); @@ -82,7 +84,7 @@ public void loginWithDeviceId() throws URISyntaxException { @Test public void loginWithDeviceIdAndLogout() throws URISyntaxException { - callIfNotNull(s -> s.stubFor(post(urlEqualTo(loginUrl)) + stubFor(post(urlEqualTo(loginUrl)) .withRequestBody(equalToJson("{\"type\": \"m.login.password\"," + // "\"user\": \"" + user.getLocalPart() + "\"," + // "\"password\": \"" + password + "\"," + // @@ -91,7 +93,7 @@ public void loginWithDeviceIdAndLogout() throws URISyntaxException { .withBody("{\"user_id\": \"" + user.getId() + "\"," + // "\"access_token\": \"" + testToken + "\"," + // "\"home_server\": \"" + hostname + "\"," + // - "\"device_id\": \"" + deviceId + "\"}")))); + "\"device_id\": \"" + deviceId + "\"}"))); MatrixHomeserver hs = new MatrixHomeserver(domain, baseUrl); MatrixClientContext context = new MatrixClientContext(hs, deviceId); @@ -110,13 +112,13 @@ public void loginWithDeviceIdAndLogout() throws URISyntaxException { */ assertEquals(deviceId, client.getDeviceId().get()); - callIfNotNull(s -> s.stubFor(post(urlEqualTo(logoutUrl)))); + stubFor(post(urlEqualTo(logoutUrl))); client.logout(); } @Test public void login() throws URISyntaxException { - callIfNotNull(s -> s.stubFor(post(urlEqualTo(loginUrl)) + stubFor(post(urlEqualTo(loginUrl)) .withRequestBody(equalToJson("{\"type\": \"m.login.password\"," + // "\"user\": \"" + user.getLocalPart() + "\"," + // "\"password\": \"" + password + "\"}")) @@ -124,7 +126,7 @@ public void login() throws URISyntaxException { .withBody("{\"user_id\": \"" + user.getId() + "\"," + // "\"access_token\": \"" + testToken + "\"," + // "\"home_server\": \"" + hostname + "\"," + // - "\"device_id\": \"" + deviceId + "\"}")))); + "\"device_id\": \"" + deviceId + "\"}"))); MatrixHomeserver hs = new MatrixHomeserver(domain, baseUrl); MatrixClientContext context = new MatrixClientContext(hs); @@ -146,11 +148,11 @@ public void login() throws URISyntaxException { @Test public void loginWrongPassword() throws URISyntaxException { - callIfNotNull(s -> s.stubFor(post(urlEqualTo(loginUrl)) + stubFor(post(urlEqualTo(loginUrl)) .withRequestBody(equalToJson("{\"type\": \"m.login.password\"," + // "\"user\": \"" + user.getLocalPart() + "\"," + // "\"password\": \"" + password + "\"}")) - .willReturn(aResponse().withStatus(403).withBody(error403Response)))); + .willReturn(aResponse().withStatus(403).withBody(error403Response))); MatrixHomeserver hs = new MatrixHomeserver(domain, baseUrl); MatrixClientContext context = new MatrixClientContext(hs); @@ -164,14 +166,13 @@ public void loginWrongPassword() throws URISyntaxException { @Test public void setDisplayName() throws URISyntaxException { - callIfNotNull(s -> s.stubFor(put(urlEqualTo(setDisplaynameUrl)).willReturn(aResponse().withStatus(200)))); + stubFor(put(urlEqualTo(setDisplaynameUrl)).willReturn(aResponse().withStatus(200))); createClientObject().setDisplayName(displayName); } @Test public void setDisplayNameErrorRateLimited() throws URISyntaxException { - callIfNotNull(s -> s.stubFor( - put(urlEqualTo(setDisplaynameUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)))); + stubFor(put(urlEqualTo(setDisplaynameUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, () -> createClientObject().setDisplayName(displayName)); From 16bcbc6adfad3dc527f9d7f2e1fee18d578da07c Mon Sep 17 00:00:00 2001 From: Arne Augenstein Date: Sun, 19 Nov 2017 00:05:27 +0100 Subject: [PATCH 05/14] Set MatrixID to new values, too --- src/test/java/io/kamax/matrix/client/MatrixHttpTest.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java b/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java index 9ade01d..00acc86 100644 --- a/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java +++ b/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java @@ -112,6 +112,7 @@ public void setUp() { baseUrl = "https://" + hostname + ":" + port; username = configValues.get("Username"); password = configValues.get("Password"); + user = new MatrixID(username, domain); } catch (IOException e) { throw new IllegalStateException(e); } From 59cd68f7ac6b5984ccf6ea932605cda6d405eec1 Mon Sep 17 00:00:00 2001 From: Arne Augenstein Date: Sun, 19 Nov 2017 13:07:45 +0100 Subject: [PATCH 06/14] Move login tests to separate class --- .../regular/MatrixHttpClientLoginTest.java | 159 ++++++++++++++++++ .../client/regular/MatrixHttpClientTest.java | 124 -------------- 2 files changed, 159 insertions(+), 124 deletions(-) create mode 100644 src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientLoginTest.java diff --git a/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientLoginTest.java b/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientLoginTest.java new file mode 100644 index 0000000..1a68dc0 --- /dev/null +++ b/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientLoginTest.java @@ -0,0 +1,159 @@ +/* + * 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 io.kamax.matrix.client.MatrixPasswordLoginCredentials; +import io.kamax.matrix.hs.MatrixHomeserver; + +import org.junit.Test; +import org.junit.platform.commons.util.StringUtils; + +import java.net.URISyntaxException; + +import static com.github.tomakehurst.wiremock.client.WireMock.*; +import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; + +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 MatrixHttpClientLoginTest extends MatrixHttpTest { + private String loginUrl = "/_matrix/client/r0/login"; + private String password = "MostSecretPasswordEver"; + private String deviceId = "testDeviceId_892377"; + private String logoutUrl = "/_matrix/client/r0/logout" + tokenParameter; + + @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); + + MatrixPasswordLoginCredentials credentials = new MatrixPasswordLoginCredentials(user.getLocalPart(), password); + client.login(credentials); + + assertTrue(StringUtils.isNotBlank(client.getAccessToken().get())); + assertTrue(StringUtils.isNotBlank(client.getDeviceId().get())); + assertTrue(StringUtils.isNotBlank(client.getUser().get().getId())); + + /* + * TODO The spec is not clear if the returned device id is always the same as the one you pass to the server. + * If it can be different this assertion has to be removed. + */ + assertEquals(deviceId, client.getDeviceId().get()); + } + + @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 + "\"}"))); + + MatrixHomeserver hs = new MatrixHomeserver(domain, baseUrl); + MatrixClientContext context = new MatrixClientContext(hs, deviceId); + MatrixHttpClient client = new MatrixHttpClient(context); + + MatrixPasswordLoginCredentials credentials = new MatrixPasswordLoginCredentials(user.getLocalPart(), password); + client.login(credentials); + + assertTrue(StringUtils.isNotBlank(client.getAccessToken().get())); + assertTrue(StringUtils.isNotBlank(client.getDeviceId().get())); + assertTrue(StringUtils.isNotBlank(client.getUser().get().getId())); + + /* + * TODO The spec is not clear if the returned device id is always the same as the one you pass to the server. + * If it can be different this assertion has to be removed. + */ + assertEquals(deviceId, client.getDeviceId().get()); + + stubFor(post(urlEqualTo(logoutUrl))); + client.logout(); + } + + @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 + "\"}"))); + + MatrixHomeserver hs = new MatrixHomeserver(domain, baseUrl); + MatrixClientContext context = new MatrixClientContext(hs); + MatrixHttpClient client = new MatrixHttpClient(context); + + MatrixPasswordLoginCredentials credentials = new MatrixPasswordLoginCredentials(user.getLocalPart(), password); + client.login(credentials); + + assertTrue(StringUtils.isNotBlank(client.getAccessToken().get())); + assertTrue(StringUtils.isNotBlank(client.getDeviceId().get())); + assertTrue(StringUtils.isNotBlank(client.getUser().get().getId())); + + /* + * TODO The spec is not clear if the returned device id is always the same as the one you pass to the server. + * If it can be different this assertion has to be removed. + */ + assertEquals(deviceId, client.getDeviceId().get()); + } + + @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); + + MatrixPasswordLoginCredentials credentials = new MatrixPasswordLoginCredentials(user.getLocalPart(), password); + MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, + () -> client.login(credentials)); + checkErrorInfo403(e); + } + +} diff --git a/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientTest.java b/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientTest.java index 02c31fb..447c5e7 100644 --- a/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientTest.java +++ b/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientTest.java @@ -23,27 +23,16 @@ import io.kamax.matrix.client.MatrixClientContext; import io.kamax.matrix.client.MatrixClientRequestException; import io.kamax.matrix.client.MatrixHttpTest; -import io.kamax.matrix.client.MatrixPasswordLoginCredentials; -import io.kamax.matrix.hs.MatrixHomeserver; import org.junit.Test; -import org.junit.platform.commons.util.StringUtils; import java.net.URISyntaxException; 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"; - - 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"; @@ -51,119 +40,6 @@ public class MatrixHttpClientTest extends MatrixHttpTest { public MatrixHttpClientTest() throws URISyntaxException { } - @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); - - MatrixPasswordLoginCredentials credentials = new MatrixPasswordLoginCredentials(user.getLocalPart(), password); - client.login(credentials); - - assertTrue(StringUtils.isNotBlank(client.getAccessToken().get())); - assertTrue(StringUtils.isNotBlank(client.getDeviceId().get())); - assertTrue(StringUtils.isNotBlank(client.getUser().get().getId())); - - /* - * TODO The spec is not clear if the returned device id is always the same as the one you pass to the server. - * If it can be different this assertion has to be removed. - */ - assertEquals(deviceId, client.getDeviceId().get()); - } - - @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 + "\"}"))); - - MatrixHomeserver hs = new MatrixHomeserver(domain, baseUrl); - MatrixClientContext context = new MatrixClientContext(hs, deviceId); - MatrixHttpClient client = new MatrixHttpClient(context); - - MatrixPasswordLoginCredentials credentials = new MatrixPasswordLoginCredentials(user.getLocalPart(), password); - client.login(credentials); - - assertTrue(StringUtils.isNotBlank(client.getAccessToken().get())); - assertTrue(StringUtils.isNotBlank(client.getDeviceId().get())); - assertTrue(StringUtils.isNotBlank(client.getUser().get().getId())); - - /* - * TODO The spec is not clear if the returned device id is always the same as the one you pass to the server. - * If it can be different this assertion has to be removed. - */ - assertEquals(deviceId, client.getDeviceId().get()); - - stubFor(post(urlEqualTo(logoutUrl))); - client.logout(); - } - - @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 + "\"}"))); - - MatrixHomeserver hs = new MatrixHomeserver(domain, baseUrl); - MatrixClientContext context = new MatrixClientContext(hs); - MatrixHttpClient client = new MatrixHttpClient(context); - - MatrixPasswordLoginCredentials credentials = new MatrixPasswordLoginCredentials(user.getLocalPart(), password); - client.login(credentials); - - assertTrue(StringUtils.isNotBlank(client.getAccessToken().get())); - assertTrue(StringUtils.isNotBlank(client.getDeviceId().get())); - assertTrue(StringUtils.isNotBlank(client.getUser().get().getId())); - - /* - * TODO The spec is not clear if the returned device id is always the same as the one you pass to the server. - * If it can be different this assertion has to be removed. - */ - assertEquals(deviceId, client.getDeviceId().get()); - } - - @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); - - MatrixPasswordLoginCredentials credentials = new MatrixPasswordLoginCredentials(user.getLocalPart(), password); - MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, - () -> client.login(credentials)); - checkErrorInfo403(e); - } - @Test public void setDisplayName() throws URISyntaxException { stubFor(put(urlEqualTo(setDisplaynameUrl)).willReturn(aResponse().withStatus(200))); From 36720cabe256bd46b7f782b971ae9f34856f3f0b Mon Sep 17 00:00:00 2001 From: Arne Augenstein Date: Sun, 19 Nov 2017 13:58:40 +0100 Subject: [PATCH 07/14] Use subclassing for differentiation of test case categories --- build.gradle | 9 + .../matrix/client/MatrixHttpRoomTest.java | 129 +----------- .../client/MatrixHttpRoomWiremockTest.java | 197 ++++++++++++++++++ .../kamax/matrix/client/MatrixHttpTest.java | 88 ++++---- .../MatrixApplicationServiceClientTest.java | 8 + .../client/regular/MatrixHttpClientTest.java | 12 +- .../regular/MatrixHttpClientWiremockTest.java | 55 +++++ 7 files changed, 321 insertions(+), 177 deletions(-) create mode 100644 src/test/java/io/kamax/matrix/client/MatrixHttpRoomWiremockTest.java create mode 100644 src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientWiremockTest.java diff --git a/build.gradle b/build.gradle index 2c355a9..20ea7ea 100644 --- a/build.gradle +++ b/build.gradle @@ -116,3 +116,12 @@ spotless { junitPlatform { enableStandardTestTask true } + +test { + // Exclude homeserver tests + exclude 'io/kamax/matrix/MatrixHttpUserTest*' + exclude 'io/kamax/matrix/client/MatrixHttpRoomTest*' + exclude 'io/kamax/matrix/client/MatrixHttpContentTest*' + exclude 'io/kamax/matrix/client/regular/MatrixHttpClientLoginTest*' + exclude 'io/kamax/matrix/client/regular/MatrixHttpClientTest*' +} diff --git a/src/test/java/io/kamax/matrix/client/MatrixHttpRoomTest.java b/src/test/java/io/kamax/matrix/client/MatrixHttpRoomTest.java index 2022dd5..cabad88 100644 --- a/src/test/java/io/kamax/matrix/client/MatrixHttpRoomTest.java +++ b/src/test/java/io/kamax/matrix/client/MatrixHttpRoomTest.java @@ -31,270 +31,155 @@ 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); + protected String roomId = "roomId892347847"; + protected String eventId = "YUwRidLecu"; - 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); + 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 { - stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(200).withBody(nameResponse))); - - login(); assertThat(createRoomObject().getName(), IsEqual.equalTo(Optional.of(nameOfRoom))); - logout(); } @Test public void getEmptyName() throws URISyntaxException { - stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); - - login(); assertThat(createRoomObject().getName(), IsEqual.equalTo(Optional.empty())); - logout(); } @Test public void getNameAccessDenied() throws URISyntaxException { - stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); - - login(); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getName); checkErrorInfo403(e); - logout(); } @Test public void getNameRateLimited() throws URISyntaxException { - stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); - - login(); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getName); checkErrorInfo429(e); - logout(); } @Test public void getTopic() throws URISyntaxException { - stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(200).withBody(topicResponse))); - - login(); assertThat(createRoomObject().getTopic(), IsEqual.equalTo(Optional.of(testTopic))); - logout(); } @Test public void getEmptyTopic() throws URISyntaxException { - stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); - - login(); assertThat(createRoomObject().getTopic(), IsEqual.equalTo(Optional.empty())); - logout(); } @Test public void getTopicAccessDenied() throws URISyntaxException { - stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); - - login(); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getTopic); checkErrorInfo403(e); - logout(); } @Test public void getTopicRateLimited() throws URISyntaxException { - stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); - - login(); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getTopic); checkErrorInfo429(e); - logout(); } @Test public void join() throws URISyntaxException { - stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(200).withBody(joinResponse))); - - login(); createRoomObject().join(); - logout(); } @Test public void joinRoomNotFound() throws URISyntaxException { - stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); - - login(); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::join); checkErrorInfo404(e); - logout(); } @Test public void joinAccessDenied() throws URISyntaxException { - stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); - - login(); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::join); checkErrorInfo403(e); - logout(); } @Test public void joinRateLimited() throws URISyntaxException { - stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); - - login(); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::join); checkErrorInfo429(e); - logout(); } @Test public void leave() throws URISyntaxException { - stubFor(post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(200).withBody(leaveResponse))); - - login(); createRoomObject().leave(); - logout(); } @Test public void leaveAccessDenied() throws URISyntaxException { - stubFor(post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); - - login(); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::leave); checkErrorInfo403(e); - logout(); } @Test public void leaveRoomNotFound() throws URISyntaxException { - stubFor(post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); - - login(); createRoomObject().leave(); - logout(); } @Test public void leaveRateLimited() throws URISyntaxException { - stubFor(post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); - - login(); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::leave); checkErrorInfo429(e); - logout(); } @Test public void sendText() throws URISyntaxException { - stubFor(put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(200).withBody(sendTextResponse))); - - login(); createRoomObject().sendText(testText); - logout(); } @Test public void sendTextAccessDenied() throws URISyntaxException { - stubFor(put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); - - login(); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, () -> createRoomObject().sendText(testText)); checkErrorInfo403(e); - logout(); } @Test public void sendTextRoomNotFound() throws URISyntaxException { - stubFor(put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); - - login(); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, () -> createRoomObject().sendText(testText)); checkErrorInfo404(e); - logout(); } @Test public void sendTextRateLimited() throws URISyntaxException { - stubFor(put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); - - login(); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, () -> createRoomObject().sendText(testText)); checkErrorInfo429(e); - logout(); } @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)); - login(); assertThat(createRoomObject().getJoinedUsers(), IsEqual.equalTo(expectedResult)); - logout(); } @Test public void getJoinedUsersRoomNotFound() throws URISyntaxException { - stubFor(get(urlEqualTo(getJoinedUsersUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); - - login(); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getJoinedUsers); checkErrorInfo404(e); - logout(); } @Test public void getJoinedUsersRateLimited() throws URISyntaxException { - stubFor(get(urlEqualTo(getJoinedUsersUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); - - login(); MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createRoomObject()::getJoinedUsers); checkErrorInfo429(e); - logout(); } private MatrixHttpRoom createRoomObject() throws URISyntaxException { diff --git a/src/test/java/io/kamax/matrix/client/MatrixHttpRoomWiremockTest.java b/src/test/java/io/kamax/matrix/client/MatrixHttpRoomWiremockTest.java new file mode 100644 index 0000000..a504a7f --- /dev/null +++ b/src/test/java/io/kamax/matrix/client/MatrixHttpRoomWiremockTest.java @@ -0,0 +1,197 @@ +/* + * 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.junit.Test; + +import java.net.URISyntaxException; + +import static com.github.tomakehurst.wiremock.client.WireMock.*; + +public class MatrixHttpRoomWiremockTest extends MatrixHttpRoomTest { + private String nameUrl = String.format("/_matrix/client/r0/rooms/%s/state/m.room.name", roomId) + tokenParameter; + 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 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 sendTextResponse = String.format("{\"event_id\": \"%s\"}", eventId) + tokenParameter; + + private String getJoinedUsersUrl = String.format("/_matrix/client/r0/rooms/%s/joined_members", roomId) + + tokenParameter; + 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))); + super.getName(); + } + + @Test + public void getEmptyName() throws URISyntaxException { + stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); + super.getEmptyName(); + } + + @Test + public void getNameAccessDenied() throws URISyntaxException { + stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); + super.getNameAccessDenied(); + } + + @Test + public void getNameRateLimited() throws URISyntaxException { + stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); + super.getNameRateLimited(); + } + + @Test + public void getTopic() throws URISyntaxException { + stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(200).withBody(topicResponse))); + super.getTopic(); + } + + @Test + public void getEmptyTopic() throws URISyntaxException { + stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); + super.getEmptyTopic(); + } + + @Test + public void getTopicAccessDenied() throws URISyntaxException { + stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); + super.getTopicAccessDenied(); + } + + @Test + public void getTopicRateLimited() throws URISyntaxException { + stubFor(get(urlEqualTo(topicUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); + super.getTopicRateLimited(); + } + + @Test + public void join() throws URISyntaxException { + stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(200).withBody(joinResponse))); + super.join(); + } + + @Test + public void joinRoomNotFound() throws URISyntaxException { + stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); + super.joinRoomNotFound(); + } + + @Test + public void joinAccessDenied() throws URISyntaxException { + stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); + super.joinAccessDenied(); + } + + @Test + public void joinRateLimited() throws URISyntaxException { + stubFor(post(urlEqualTo(joinUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); + super.joinRateLimited(); + } + + @Test + public void leave() throws URISyntaxException { + stubFor(post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(200).withBody(leaveResponse))); + super.leave(); + } + + @Test + public void leaveAccessDenied() throws URISyntaxException { + stubFor(post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); + super.leaveAccessDenied(); + } + + @Test + public void leaveRoomNotFound() throws URISyntaxException { + stubFor(post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); + super.leaveRoomNotFound(); + } + + @Test + public void leaveRateLimited() throws URISyntaxException { + stubFor(post(urlEqualTo(leaveUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); + super.leaveRateLimited(); + } + + @Test + public void sendText() throws URISyntaxException { + stubFor(put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(200).withBody(sendTextResponse))); + super.sendText(); + } + + @Test + public void sendTextAccessDenied() throws URISyntaxException { + stubFor(put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response))); + super.sendTextAccessDenied(); + } + + @Test + public void sendTextRoomNotFound() throws URISyntaxException { + stubFor(put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); + super.sendTextRoomNotFound(); + } + + @Test + public void sendTextRateLimited() throws URISyntaxException { + stubFor(put(urlMatching(sendTextUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); + super.sendTextRateLimited(); + } + + @Test + public void getJoinedUsers() throws URISyntaxException { + stubFor(get(urlEqualTo(getJoinedUsersUrl)) + .willReturn(aResponse().withStatus(200).withBody(getJoinedUsersResponse))); + super.getJoinedUsers(); + } + + @Test + public void getJoinedUsersRoomNotFound() throws URISyntaxException { + stubFor(get(urlEqualTo(getJoinedUsersUrl)).willReturn(aResponse().withStatus(404).withBody(error404Response))); + super.getJoinedUsersRoomNotFound(); + } + + @Test + public void getJoinedUsersRateLimited() throws URISyntaxException { + stubFor(get(urlEqualTo(getJoinedUsersUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response))); + 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 00acc86..b30e838 100644 --- a/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java +++ b/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java @@ -72,36 +72,17 @@ 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)); - private MatrixHttpClient client; - - protected MatrixClientContext createClientContext() throws URISyntaxException { - MatrixHomeserver hs = new MatrixHomeserver(domain, baseUrl); - return new MatrixClientContext(hs, user, 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); - } - + /** + * 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 setUp() { - getConfigFileStream().ifPresent(configFile -> { + public void login() throws URISyntaxException { + // TODO Is the location of the config file alright? + InputStream configFile = this.getClass().getResourceAsStream("/test.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())); @@ -116,16 +97,6 @@ public void setUp() { } catch (IOException e) { throw new IllegalStateException(e); } - }); - } - - private Optional getConfigFileStream() { - // TODO Is the location of the config file alright? - return Optional.ofNullable(this.getClass().getResourceAsStream("/test.conf")); - } - - protected void login() throws URISyntaxException { - getConfigFileStream().ifPresent((InputStream configFile) -> { try { MatrixHomeserver homeserver = new MatrixHomeserver(domain, baseUrl); MatrixClientContext context = new MatrixClientContext(homeserver); @@ -138,14 +109,43 @@ protected void login() throws URISyntaxException { } catch (URISyntaxException e) { throw new IllegalStateException(e); } - }); + } } + /** + * 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() { - if (client != null) { - client.logout(); - client = null; - } + client.logout(); + client = null; + } + + @Rule + public WireMockRule wireMockRule = new WireMockRule(options().port(port).usingFilesUnderDirectory(resourcePath)); + private MatrixHttpClient client; + + protected MatrixClientContext createClientContext() throws URISyntaxException { + MatrixHomeserver hs = new MatrixHomeserver(domain, baseUrl); + return new MatrixClientContext(hs, user, 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 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/MatrixHttpClientTest.java index 447c5e7..4d7c4c7 100644 --- a/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientTest.java +++ b/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientTest.java @@ -28,28 +28,18 @@ 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().get().getId()) + tokenParameter; - private String displayName = "display name"; - - public MatrixHttpClientTest() throws URISyntaxException { - } + protected String displayName = "display name"; @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); 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..dfad2dd --- /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 MatrixHttpClientTest { + private String setDisplaynameUrl = String.format("/_matrix/client/r0/profile/%s/displayname", + createClientContext().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(); + } +} From 8687969ba7d2b270a3661012c6e94b2588fecb92 Mon Sep 17 00:00:00 2001 From: Arne Augenstein Date: Sun, 19 Nov 2017 14:09:15 +0100 Subject: [PATCH 08/14] Fix bug in checkErrorInfo --- src/test/java/io/kamax/matrix/client/MatrixHttpTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java b/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java index b30e838..703e925 100644 --- a/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java +++ b/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java @@ -145,7 +145,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()); } } From 095df542ea99b92560c29396ba8e1eabd0ef2623 Mon Sep 17 00:00:00 2001 From: Arne Augenstein Date: Sun, 19 Nov 2017 14:24:22 +0100 Subject: [PATCH 09/14] Fix bug in context handling --- src/test/java/io/kamax/matrix/MatrixHttpUserTest.java | 2 +- .../io/kamax/matrix/client/MatrixHttpContentTest.java | 2 +- .../io/kamax/matrix/client/MatrixHttpRoomTest.java | 2 +- .../java/io/kamax/matrix/client/MatrixHttpTest.java | 11 +++++++---- .../matrix/client/regular/MatrixHttpClientTest.java | 2 +- .../client/regular/MatrixHttpClientWiremockTest.java | 2 +- 6 files changed, 12 insertions(+), 9 deletions(-) diff --git a/src/test/java/io/kamax/matrix/MatrixHttpUserTest.java b/src/test/java/io/kamax/matrix/MatrixHttpUserTest.java index 95e4d8f..6ecc3d4 100644 --- a/src/test/java/io/kamax/matrix/MatrixHttpUserTest.java +++ b/src/test/java/io/kamax/matrix/MatrixHttpUserTest.java @@ -108,7 +108,7 @@ public void getAvatarError429() throws URISyntaxException { } 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/client/MatrixHttpContentTest.java b/src/test/java/io/kamax/matrix/client/MatrixHttpContentTest.java index 4a49213..f92c432 100644 --- a/src/test/java/io/kamax/matrix/client/MatrixHttpContentTest.java +++ b/src/test/java/io/kamax/matrix/client/MatrixHttpContentTest.java @@ -193,7 +193,7 @@ public void getFilenameErrorAccessDenied() throws URISyntaxException, IOExceptio } private MatrixHttpContent createContentObject() throws URISyntaxException { - MatrixClientContext context = createClientContext(); + MatrixClientContext context = getOrCreateClientContext(); 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 index cabad88..d11d024 100644 --- a/src/test/java/io/kamax/matrix/client/MatrixHttpRoomTest.java +++ b/src/test/java/io/kamax/matrix/client/MatrixHttpRoomTest.java @@ -183,7 +183,7 @@ public void getJoinedUsersRateLimited() throws URISyntaxException { } private MatrixHttpRoom createRoomObject() throws URISyntaxException { - MatrixClientContext context = createClientContext(); + MatrixClientContext context = getOrCreateClientContext(); 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 index 703e925..82ab15b 100644 --- a/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java +++ b/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java @@ -100,7 +100,6 @@ public void login() throws URISyntaxException { try { MatrixHomeserver homeserver = new MatrixHomeserver(domain, baseUrl); MatrixClientContext context = new MatrixClientContext(homeserver); - createClientContext(); MatrixPasswordLoginCredentials credentials = new MatrixPasswordLoginCredentials(username, password); client = new MatrixHttpClient(context); @@ -126,9 +125,13 @@ public void logout() { public WireMockRule wireMockRule = new WireMockRule(options().port(port).usingFilesUnderDirectory(resourcePath)); private MatrixHttpClient client; - protected MatrixClientContext createClientContext() throws URISyntaxException { - MatrixHomeserver hs = new MatrixHomeserver(domain, baseUrl); - return new MatrixClientContext(hs, user, testToken); + 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) { diff --git a/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientTest.java b/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientTest.java index 4d7c4c7..f5125da 100644 --- a/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientTest.java +++ b/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientTest.java @@ -46,7 +46,7 @@ public void setDisplayNameErrorRateLimited() throws URISyntaxException { } private MatrixHttpClient createClientObject() throws URISyntaxException { - MatrixClientContext context = createClientContext(); + MatrixClientContext context = getOrCreateClientContext(); return new MatrixHttpClient(context); } } diff --git a/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientWiremockTest.java b/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientWiremockTest.java index dfad2dd..1706962 100644 --- a/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientWiremockTest.java +++ b/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientWiremockTest.java @@ -28,7 +28,7 @@ public class MatrixHttpClientWiremockTest extends MatrixHttpClientTest { private String setDisplaynameUrl = String.format("/_matrix/client/r0/profile/%s/displayname", - createClientContext().getUser().get().getId()) + tokenParameter; + getOrCreateClientContext().getUser().get().getId()) + tokenParameter; public MatrixHttpClientWiremockTest() throws URISyntaxException { } From cb0e0a1f1efbeb319f5714d08d4cf3decb30b17e Mon Sep 17 00:00:00 2001 From: Arne Augenstein Date: Mon, 20 Nov 2017 20:38:12 +0100 Subject: [PATCH 10/14] Add new source set for integration tests --- build.gradle | 28 +++++++++++++++++++++------- 1 file changed, 21 insertions(+), 7 deletions(-) diff --git a/build.gradle b/build.gradle index 20ea7ea..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" } @@ -117,11 +134,8 @@ junitPlatform { enableStandardTestTask true } -test { - // Exclude homeserver tests - exclude 'io/kamax/matrix/MatrixHttpUserTest*' - exclude 'io/kamax/matrix/client/MatrixHttpRoomTest*' - exclude 'io/kamax/matrix/client/MatrixHttpContentTest*' - exclude 'io/kamax/matrix/client/regular/MatrixHttpClientLoginTest*' - exclude 'io/kamax/matrix/client/regular/MatrixHttpClientTest*' +task testInt(type: Test) { + description = "Integration tests which require a Homeserver" + testClassesDir = sourceSets.testInt.output.classesDir + classpath = sourceSets.testInt.runtimeClasspath } From 80499a208febb4329d3e045eb4d954450b914d59 Mon Sep 17 00:00:00 2001 From: Arne Augenstein Date: Mon, 20 Nov 2017 21:15:24 +0100 Subject: [PATCH 11/14] Change to new test layout --- ...UserTest.java => AMatrixHttpUserTest.java} | 23 +-- .../matrix/MatrixHttpUserWiremockTest.java | 92 +++++++++++ .../matrix/client/AMatrixHttpContentTest.java | 149 ++++++++++++++++++ ...RoomTest.java => AMatrixHttpRoomTest.java} | 2 +- ...ava => MatrixHttpContentWiremockTest.java} | 105 +++++------- .../client/MatrixHttpRoomWiremockTest.java | 2 +- ...t.java => AMatrixHttpClientLoginTest.java} | 55 ++----- ...ntTest.java => AMatrixHttpClientTest.java} | 2 +- .../MatrixHttpClientLoginWiremockTest.java | 88 +++++++++++ .../regular/MatrixHttpClientWiremockTest.java | 2 +- .../io/kamax/matrix/MatrixHttpUserTest.java | 25 +++ .../matrix/client/MatrixHttpContentTest.java | 29 ++++ .../matrix/client/MatrixHttpRoomTest.java | 25 +++ .../regular/MatrixHttpClientLoginTest.java | 24 +++ .../client/regular/MatrixHttpClientTest.java | 24 +++ 15 files changed, 509 insertions(+), 138 deletions(-) rename src/test/java/io/kamax/matrix/{MatrixHttpUserTest.java => AMatrixHttpUserTest.java} (68%) create mode 100644 src/test/java/io/kamax/matrix/MatrixHttpUserWiremockTest.java create mode 100644 src/test/java/io/kamax/matrix/client/AMatrixHttpContentTest.java rename src/test/java/io/kamax/matrix/client/{MatrixHttpRoomTest.java => AMatrixHttpRoomTest.java} (98%) rename src/test/java/io/kamax/matrix/client/{MatrixHttpContentTest.java => MatrixHttpContentWiremockTest.java} (62%) rename src/test/java/io/kamax/matrix/client/regular/{MatrixHttpClientLoginTest.java => AMatrixHttpClientLoginTest.java} (62%) rename src/test/java/io/kamax/matrix/client/regular/{MatrixHttpClientTest.java => AMatrixHttpClientTest.java} (96%) create mode 100644 src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientLoginWiremockTest.java create mode 100644 src/testInt/java/io/kamax/matrix/MatrixHttpUserTest.java create mode 100644 src/testInt/java/io/kamax/matrix/client/MatrixHttpContentTest.java create mode 100644 src/testInt/java/io/kamax/matrix/client/MatrixHttpRoomTest.java create mode 100644 src/testInt/java/io/kamax/matrix/client/regular/MatrixHttpClientLoginTest.java create mode 100644 src/testInt/java/io/kamax/matrix/client/regular/MatrixHttpClientTest.java diff --git a/src/test/java/io/kamax/matrix/MatrixHttpUserTest.java b/src/test/java/io/kamax/matrix/AMatrixHttpUserTest.java similarity index 68% rename from src/test/java/io/kamax/matrix/MatrixHttpUserTest.java rename to src/test/java/io/kamax/matrix/AMatrixHttpUserTest.java index 6ecc3d4..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\"}", username); - - 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(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,8 +83,6 @@ 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); 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/MatrixHttpRoomTest.java b/src/test/java/io/kamax/matrix/client/AMatrixHttpRoomTest.java similarity index 98% rename from src/test/java/io/kamax/matrix/client/MatrixHttpRoomTest.java rename to src/test/java/io/kamax/matrix/client/AMatrixHttpRoomTest.java index d11d024..ca25ac3 100644 --- a/src/test/java/io/kamax/matrix/client/MatrixHttpRoomTest.java +++ b/src/test/java/io/kamax/matrix/client/AMatrixHttpRoomTest.java @@ -34,7 +34,7 @@ import static org.junit.Assert.assertThat; import static org.junit.jupiter.api.Assertions.assertThrows; -public class MatrixHttpRoomTest extends MatrixHttpTest { +public abstract class AMatrixHttpRoomTest extends MatrixHttpTest { protected String roomId = "roomId892347847"; protected String eventId = "YUwRidLecu"; 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 f92c432..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 = getOrCreateClientContext(); - return new MatrixHttpContent(context, address); + super.getFilenameErrorAccessDenied(); } } diff --git a/src/test/java/io/kamax/matrix/client/MatrixHttpRoomWiremockTest.java b/src/test/java/io/kamax/matrix/client/MatrixHttpRoomWiremockTest.java index a504a7f..cfae54e 100644 --- a/src/test/java/io/kamax/matrix/client/MatrixHttpRoomWiremockTest.java +++ b/src/test/java/io/kamax/matrix/client/MatrixHttpRoomWiremockTest.java @@ -26,7 +26,7 @@ import static com.github.tomakehurst.wiremock.client.WireMock.*; -public class MatrixHttpRoomWiremockTest extends MatrixHttpRoomTest { +public class MatrixHttpRoomWiremockTest extends AMatrixHttpRoomTest { private String nameUrl = String.format("/_matrix/client/r0/rooms/%s/state/m.room.name", roomId) + tokenParameter; private String nameResponse = String.format("{\"name\": \"%s\"}", nameOfRoom); diff --git a/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientLoginTest.java b/src/test/java/io/kamax/matrix/client/regular/AMatrixHttpClientLoginTest.java similarity index 62% rename from src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientLoginTest.java rename to src/test/java/io/kamax/matrix/client/regular/AMatrixHttpClientLoginTest.java index 1a68dc0..3492fb7 100644 --- a/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientLoginTest.java +++ b/src/test/java/io/kamax/matrix/client/regular/AMatrixHttpClientLoginTest.java @@ -31,31 +31,21 @@ import java.net.URISyntaxException; import static com.github.tomakehurst.wiremock.client.WireMock.*; -import static com.github.tomakehurst.wiremock.client.WireMock.aResponse; -import static org.junit.jupiter.api.Assertions.assertEquals; -import static org.junit.jupiter.api.Assertions.assertThrows; -import static org.junit.jupiter.api.Assertions.assertTrue; +import static org.junit.jupiter.api.Assertions.*; -public class MatrixHttpClientLoginTest extends MatrixHttpTest { - private String loginUrl = "/_matrix/client/r0/login"; - private String password = "MostSecretPasswordEver"; - private String deviceId = "testDeviceId_892377"; +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; + @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); @@ -76,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\": \"" + hostname + "\"," + // - "\"device_id\": \"" + deviceId + "\"}"))); - MatrixHomeserver hs = new MatrixHomeserver(domain, baseUrl); MatrixClientContext context = new MatrixClientContext(hs, deviceId); MatrixHttpClient client = new MatrixHttpClient(context); @@ -110,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\": \"" + hostname + "\"," + // - "\"device_id\": \"" + deviceId + "\"}"))); - MatrixHomeserver hs = new MatrixHomeserver(domain, baseUrl); MatrixClientContext context = new MatrixClientContext(hs); MatrixHttpClient client = new MatrixHttpClient(context); @@ -140,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); diff --git a/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientTest.java b/src/test/java/io/kamax/matrix/client/regular/AMatrixHttpClientTest.java similarity index 96% rename from src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientTest.java rename to src/test/java/io/kamax/matrix/client/regular/AMatrixHttpClientTest.java index f5125da..251824d 100644 --- a/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientTest.java +++ b/src/test/java/io/kamax/matrix/client/regular/AMatrixHttpClientTest.java @@ -30,7 +30,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows; -public class MatrixHttpClientTest extends MatrixHttpTest { +public abstract class AMatrixHttpClientTest extends MatrixHttpTest { protected String displayName = "display name"; @Test 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 index 1706962..2251cf5 100644 --- a/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientWiremockTest.java +++ b/src/test/java/io/kamax/matrix/client/regular/MatrixHttpClientWiremockTest.java @@ -26,7 +26,7 @@ import static com.github.tomakehurst.wiremock.client.WireMock.*; -public class MatrixHttpClientWiremockTest extends MatrixHttpClientTest { +public class MatrixHttpClientWiremockTest extends AMatrixHttpClientTest { private String setDisplaynameUrl = String.format("/_matrix/client/r0/profile/%s/displayname", getOrCreateClientContext().getUser().get().getId()) + tokenParameter; 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 { +} From fbb5dc47194e706c2f8cc9721f21fe0bb3fc290d Mon Sep 17 00:00:00 2001 From: Arne Augenstein Date: Tue, 21 Nov 2017 22:52:44 +0100 Subject: [PATCH 12/14] Move config file --- src/test/java/io/kamax/matrix/client/MatrixHttpTest.java | 1 - src/{test => testInt}/resources/.gitignore | 0 src/{test => testInt}/resources/test.conf_template | 0 3 files changed, 1 deletion(-) rename src/{test => testInt}/resources/.gitignore (100%) rename src/{test => testInt}/resources/test.conf_template (100%) diff --git a/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java b/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java index 82ab15b..35c9b74 100644 --- a/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java +++ b/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java @@ -80,7 +80,6 @@ public class MatrixHttpTest { */ @Before public void login() throws URISyntaxException { - // TODO Is the location of the config file alright? InputStream configFile = this.getClass().getResourceAsStream("/test.conf"); if (configFile != null) { try (BufferedReader buffer = new BufferedReader(new InputStreamReader(configFile))) { diff --git a/src/test/resources/.gitignore b/src/testInt/resources/.gitignore similarity index 100% rename from src/test/resources/.gitignore rename to src/testInt/resources/.gitignore diff --git a/src/test/resources/test.conf_template b/src/testInt/resources/test.conf_template similarity index 100% rename from src/test/resources/test.conf_template rename to src/testInt/resources/test.conf_template From 03082f7fb0c80e16526744a7abbcec57805ce024 Mon Sep 17 00:00:00 2001 From: Arne Augenstein Date: Tue, 21 Nov 2017 22:55:59 +0100 Subject: [PATCH 13/14] Rename config file --- src/test/java/io/kamax/matrix/client/MatrixHttpTest.java | 2 +- src/testInt/resources/.gitignore | 2 +- .../{test.conf_template => HomserverTest.conf_template} | 0 3 files changed, 2 insertions(+), 2 deletions(-) rename src/testInt/resources/{test.conf_template => HomserverTest.conf_template} (100%) diff --git a/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java b/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java index 35c9b74..9664abb 100644 --- a/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java +++ b/src/test/java/io/kamax/matrix/client/MatrixHttpTest.java @@ -80,7 +80,7 @@ public class MatrixHttpTest { */ @Before public void login() throws URISyntaxException { - InputStream configFile = this.getClass().getResourceAsStream("/test.conf"); + 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( diff --git a/src/testInt/resources/.gitignore b/src/testInt/resources/.gitignore index 9eb8893..d3e7d11 100644 --- a/src/testInt/resources/.gitignore +++ b/src/testInt/resources/.gitignore @@ -1 +1 @@ -test.conf +HomeserverTest.conf diff --git a/src/testInt/resources/test.conf_template b/src/testInt/resources/HomserverTest.conf_template similarity index 100% rename from src/testInt/resources/test.conf_template rename to src/testInt/resources/HomserverTest.conf_template From edf8e011a399f4303459caef82ce78e629520833 Mon Sep 17 00:00:00 2001 From: Arne Augenstein Date: Thu, 23 Nov 2017 21:45:28 +0100 Subject: [PATCH 14/14] Add a short explanation of the tests in the README --- README.md | 14 ++++++++++++++ 1 file changed, 14 insertions(+) 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