mirror of
https://github.com/scm-manager/scm-manager.git
synced 2026-07-07 12:33:35 +02:00
Build json in integration test via java ee builder
This commit is contained in:
@@ -27,6 +27,17 @@
|
||||
<version>3.1.0</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax</groupId>
|
||||
<artifactId>javaee-api</artifactId>
|
||||
<version>7.0</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.glassfish</groupId>
|
||||
<artifactId>javax.json</artifactId>
|
||||
<version>1.0.4</version>
|
||||
<scope>runtime</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<build>
|
||||
|
||||
@@ -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();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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"
|
||||
}
|
||||
@@ -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"
|
||||
}
|
||||
@@ -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"
|
||||
}
|
||||
Reference in New Issue
Block a user