diff --git a/scm-it/pom.xml b/scm-it/pom.xml index ab68517a9d..fa4c0b1295 100644 --- a/scm-it/pom.xml +++ b/scm-it/pom.xml @@ -27,6 +27,17 @@ 3.1.0 test + + javax + javaee-api + 7.0 + + + org.glassfish + javax.json + 1.0.4 + runtime + diff --git a/scm-it/src/test/java/sonia/scm/it/RepositoriesITCase.java b/scm-it/src/test/java/sonia/scm/it/RepositoriesITCase.java index f4d0b7586b..4b8d3924a3 100644 --- a/scm-it/src/test/java/sonia/scm/it/RepositoriesITCase.java +++ b/scm-it/src/test/java/sonia/scm/it/RepositoriesITCase.java @@ -35,12 +35,14 @@ package sonia.scm.it; import org.apache.http.HttpStatus; import org.junit.After; +import org.junit.Before; import org.junit.Test; import org.junit.runner.RunWith; import org.junit.runners.Parameterized; import org.junit.runners.Parameterized.Parameters; import sonia.scm.web.VndMediaType; +import javax.json.Json; import java.io.IOException; import java.util.Collection; @@ -50,13 +52,14 @@ import static org.hamcrest.Matchers.nullValue; import static sonia.scm.it.RegExMatcher.matchesPattern; import static sonia.scm.it.RestUtil.createResourceUrl; import static sonia.scm.it.RestUtil.given; -import static sonia.scm.it.RestUtil.readJson; @RunWith(Parameterized.class) public class RepositoriesITCase { private final String repositoryType; + private String repositoryUrl; + public RepositoriesITCase(String repositoryType) { this.repositoryType = repositoryType; } @@ -66,6 +69,20 @@ public class RepositoriesITCase { return ScmParameterizedIntegrationTestUtil.createParameters(); } + @Before + public void createRepository() { + this.repositoryUrl = given(VndMediaType.REPOSITORY) + .body(repositoryJson()) + + .when() + .post(createResourceUrl("repositories")) + + .then() + .statusCode(HttpStatus.SC_CREATED) + .extract() + .header("location"); + } + @After public void cleanup() { TestData.cleanup(); @@ -73,8 +90,6 @@ public class RepositoriesITCase { @Test public void shouldCreateSuccessfully() throws IOException { - String repositoryUrl = createRepository(); - given(VndMediaType.REPOSITORY) .when() @@ -92,9 +107,7 @@ public class RepositoriesITCase { } @Test - public void shouldDeleteSuccessfully() throws IOException { - String repositoryUrl = createRepository(); - + public void shouldDeleteSuccessfully() { given(VndMediaType.REPOSITORY) .when() @@ -113,10 +126,8 @@ public class RepositoriesITCase { } @Test - public void shouldRejectMultipleCreations() throws IOException { - String repositoryJson = readJson("repository-" + repositoryType + ".json"); - createRepository(); - + public void shouldRejectMultipleCreations() { + String repositoryJson = repositoryJson(); given(VndMediaType.REPOSITORY) .body(repositoryJson) @@ -127,16 +138,13 @@ public class RepositoriesITCase { .statusCode(HttpStatus.SC_CONFLICT); } - private String createRepository() throws IOException { - return given(VndMediaType.REPOSITORY) - .body(readJson("repository-" + repositoryType + ".json")) - - .when() - .post(createResourceUrl("repositories")) - - .then() - .statusCode(HttpStatus.SC_CREATED) - .extract() - .header("location"); + private String repositoryJson() { + return Json.createObjectBuilder() + .add("contact", "zaphod.beeblebrox@hitchhiker.com") + .add("description", "Heart of Gold") + .add("name", "HeartOfGold-" + repositoryType) + .add("archived", false) + .add("type", repositoryType) + .build().toString(); } } diff --git a/scm-it/src/test/java/sonia/scm/it/RestUtil.java b/scm-it/src/test/java/sonia/scm/it/RestUtil.java index a902c8985c..1458ab6d0b 100644 --- a/scm-it/src/test/java/sonia/scm/it/RestUtil.java +++ b/scm-it/src/test/java/sonia/scm/it/RestUtil.java @@ -1,68 +1,25 @@ package sonia.scm.it; -import com.google.common.io.Resources; import io.restassured.RestAssured; -import io.restassured.response.Response; import io.restassured.specification.RequestSpecification; -import java.io.IOException; -import java.lang.reflect.InvocationHandler; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.lang.reflect.Proxy; import java.net.URI; -import java.net.URL; -import java.nio.charset.Charset; -import static java.util.Arrays.asList; +import static java.net.URI.create; public class RestUtil { - public static final String BASE_URL = "http://localhost:8081/scm/"; - public static final String REST_BASE_URL = BASE_URL.concat("api/rest/v2/"); + public static final URI BASE_URL = create("http://localhost:8081/scm/"); + public static final URI REST_BASE_URL = BASE_URL.resolve("api/rest/v2/"); - public static Response lastResponse; - - public static URI createResourceUrl(String url) - { - return URI.create(REST_BASE_URL).resolve(url); - } - - public static String readJson(String jsonFileName) throws IOException { - URL url = Resources.getResource(jsonFileName); - return Resources.toString(url, Charset.forName("UTF-8")); + public static URI createResourceUrl(String path) { + return REST_BASE_URL.resolve(path); } public static RequestSpecification given(String mediaType) { - RequestSpecification requestSpecification = RestAssured.given() + return RestAssured.given() .contentType(mediaType) .accept(mediaType) .auth().preemptive().basic("scmadmin", "scmadmin"); - return wrapRequestSpecification(requestSpecification); - } - - private static RequestSpecification wrapRequestSpecification(RequestSpecification requestSpecification) { - return (RequestSpecification) Proxy.newProxyInstance(RestUtil.class.getClassLoader(), new Class[]{RequestSpecification.class}, new RequestSpecificationWrapper(requestSpecification)); - } - - private static class RequestSpecificationWrapper implements InvocationHandler { - - private final RequestSpecification delegate; - - private RequestSpecificationWrapper(RequestSpecification delegate) { - this.delegate = delegate; - } - - @Override - public Object invoke(Object proxy, Method method, Object[] args) throws InvocationTargetException, IllegalAccessException { - if (asList("get", "put", "post", "delete").contains(method.getName())) { - lastResponse = (Response) method.invoke(delegate, args); - return lastResponse; - } else if (method.getReturnType().equals(RequestSpecification.class)) { - return wrapRequestSpecification((RequestSpecification) method.invoke(delegate, args)); - } else { - return method.invoke(delegate, args); - } - } } } diff --git a/scm-it/src/test/resources/repository-git.json b/scm-it/src/test/resources/repository-git.json deleted file mode 100644 index 3fc491ac01..0000000000 --- a/scm-it/src/test/resources/repository-git.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "contact": "zaphod.beeblebrox@hitchhiker.com", - "description": "Heart of Gold is the first prototype ship to successfully utilise the revolutionary Infinite Improbability Drive", - "name": "HeartOfGold-git", - "archived": false, - "type": "git" -} diff --git a/scm-it/src/test/resources/repository-hg.json b/scm-it/src/test/resources/repository-hg.json deleted file mode 100644 index cf1be4cc2e..0000000000 --- a/scm-it/src/test/resources/repository-hg.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "contact": "zaphod.beeblebrox@hitchhiker.com", - "description": "Heart of Gold is the first prototype ship to successfully utilise the revolutionary Infinite Improbability Drive", - "name": "HeartOfGold-hg", - "archived": false, - "type": "hg" -} diff --git a/scm-it/src/test/resources/repository-svn.json b/scm-it/src/test/resources/repository-svn.json deleted file mode 100644 index 97e2aa6074..0000000000 --- a/scm-it/src/test/resources/repository-svn.json +++ /dev/null @@ -1,7 +0,0 @@ -{ - "contact": "zaphod.beeblebrox@hitchhiker.com", - "description": "Heart of Gold is the first prototype ship to successfully utilise the revolutionary Infinite Improbability Drive", - "name": "HeartOfGold-svn", - "archived": false, - "type": "svn" -}