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

Test with homeserver #14

Merged
merged 14 commits into from
Nov 23, 2017
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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"
}

Expand Down Expand Up @@ -116,3 +133,9 @@ spotless {
junitPlatform {
enableStandardTestTask true
}

task testInt(type: Test) {
description = "Integration tests which require a Homeserver"
testClassesDir = sourceSets.testInt.output.classesDir
classpath = sourceSets.testInt.runtimeClasspath
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,84 +31,65 @@
import java.net.URISyntaxException;
import java.util.Optional;

import static com.github.tomakehurst.wiremock.client.WireMock.*;

import static org.hamcrest.core.Is.is;
import static org.hamcrest.core.IsEqual.equalTo;
import static org.junit.Assert.assertThat;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class MatrixHttpUserTest extends MatrixHttpTest {
private String nameUrl = String.format("/_matrix/client/r0/profile/%s/displayname", user.getId()) + tokenParameter;
private String nameResponse = String.format("{\"displayname\": \"%s\"}", nameOfUser);

private String avatarUrl = String.format("/_matrix/client/r0/profile/%s/avatar_url", user.getId()) + tokenParameter;
private String avatarMediaUrl = "mxc://matrix.org/wefh34uihSDRGhw34";
private String avatarResponse = String.format("{\"avatar_url\": \"%s\"}", avatarMediaUrl);
public abstract class AMatrixHttpUserTest extends MatrixHttpTest {
protected String avatarMediaUrl = "mxc://matrix.org/wefh34uihSDRGhw34";

@Test
public void getName() throws URISyntaxException {
stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(200).withBody(nameResponse)));
assertThat(createUserObject().getName(), is(equalTo(Optional.of(nameOfUser))));
assertThat(createUserObject().getName(), is(equalTo(Optional.of(username))));
}

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

@Test
public void getNameError403() throws URISyntaxException {
stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(403).withBody(error403Response)));

MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createUserObject()::getName);
checkErrorInfo403(e);
}

@Test
public void getNameError429() throws URISyntaxException {
stubFor(get(urlEqualTo(nameUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)));

MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class, createUserObject()::getName);
checkErrorInfo429(e);
}

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

@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);
}

@Test
public void getAvatarError429() throws URISyntaxException {
stubFor(get(urlEqualTo(avatarUrl)).willReturn(aResponse().withStatus(429).withBody(error429Response)));

MatrixClientRequestException e = assertThrows(MatrixClientRequestException.class,
createUserObject()::getAvatar);
checkErrorInfo429(e);
}

private MatrixHttpUser createUserObject() throws URISyntaxException {
MatrixClientContext context = createClientContext();
MatrixClientContext context = getOrCreateClientContext();
return new MatrixHttpUser(context, context.getUser().get());
}
}
92 changes: 92 additions & 0 deletions src/test/java/io/kamax/matrix/MatrixHttpUserWiremockTest.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

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();
}

}
149 changes: 149 additions & 0 deletions src/test/java/io/kamax/matrix/client/AMatrixHttpContentTest.java
Original file line number Diff line number Diff line change
@@ -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 <http://www.gnu.org/licenses/>.
*/

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);
}

}
Loading