mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-12 16:35:45 +01:00
Move test data creation to class TestData
This commit is contained in:
@@ -34,7 +34,6 @@ package sonia.scm.it;
|
|||||||
//~--- non-JDK imports --------------------------------------------------------
|
//~--- non-JDK imports --------------------------------------------------------
|
||||||
|
|
||||||
import org.apache.http.HttpStatus;
|
import org.apache.http.HttpStatus;
|
||||||
import org.junit.After;
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Rule;
|
import org.junit.Rule;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
@@ -48,7 +47,6 @@ import sonia.scm.repository.client.api.RepositoryClient;
|
|||||||
import sonia.scm.repository.client.api.RepositoryClientFactory;
|
import sonia.scm.repository.client.api.RepositoryClientFactory;
|
||||||
import sonia.scm.web.VndMediaType;
|
import sonia.scm.web.VndMediaType;
|
||||||
|
|
||||||
import javax.json.Json;
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.FileOutputStream;
|
import java.io.FileOutputStream;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
@@ -62,6 +60,8 @@ import static org.junit.Assert.assertEquals;
|
|||||||
import static sonia.scm.it.RegExMatcher.matchesPattern;
|
import static sonia.scm.it.RegExMatcher.matchesPattern;
|
||||||
import static sonia.scm.it.RestUtil.createResourceUrl;
|
import static sonia.scm.it.RestUtil.createResourceUrl;
|
||||||
import static sonia.scm.it.RestUtil.given;
|
import static sonia.scm.it.RestUtil.given;
|
||||||
|
import static sonia.scm.it.ScmTypes.availableScmTypes;
|
||||||
|
import static sonia.scm.it.TestData.repositoryJson;
|
||||||
|
|
||||||
@RunWith(Parameterized.class)
|
@RunWith(Parameterized.class)
|
||||||
public class RepositoriesITCase {
|
public class RepositoriesITCase {
|
||||||
@@ -77,34 +77,21 @@ public class RepositoriesITCase {
|
|||||||
|
|
||||||
public RepositoriesITCase(String repositoryType) {
|
public RepositoriesITCase(String repositoryType) {
|
||||||
this.repositoryType = repositoryType;
|
this.repositoryType = repositoryType;
|
||||||
|
this.repositoryUrl = TestData.getDefaultRepositoryUrl(repositoryType);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Parameters(name = "{0}")
|
@Parameters(name = "{0}")
|
||||||
public static Collection<String> createParameters() {
|
public static Collection<String> createParameters() {
|
||||||
return ScmParameterizedIntegrationTestUtil.createParameters();
|
return availableScmTypes();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Before
|
@Before
|
||||||
public void createRepository() {
|
public void createRepository() {
|
||||||
this.repositoryUrl = given(VndMediaType.REPOSITORY)
|
TestData.createDefault();
|
||||||
.body(repositoryJson())
|
|
||||||
|
|
||||||
.when()
|
|
||||||
.post(createResourceUrl("repositories"))
|
|
||||||
|
|
||||||
.then()
|
|
||||||
.statusCode(HttpStatus.SC_CREATED)
|
|
||||||
.extract()
|
|
||||||
.header("location");
|
|
||||||
}
|
|
||||||
|
|
||||||
@After
|
|
||||||
public void cleanup() {
|
|
||||||
TestData.cleanup();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldCreateSuccessfully() throws IOException {
|
public void shouldCreateSuccessfully() {
|
||||||
given(VndMediaType.REPOSITORY)
|
given(VndMediaType.REPOSITORY)
|
||||||
|
|
||||||
.when()
|
.when()
|
||||||
@@ -142,7 +129,7 @@ public class RepositoriesITCase {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void shouldRejectMultipleCreations() {
|
public void shouldRejectMultipleCreations() {
|
||||||
String repositoryJson = repositoryJson();
|
String repositoryJson = repositoryJson(repositoryType);
|
||||||
given(VndMediaType.REPOSITORY)
|
given(VndMediaType.REPOSITORY)
|
||||||
.body(repositoryJson)
|
.body(repositoryJson)
|
||||||
|
|
||||||
@@ -208,14 +195,4 @@ public class RepositoriesITCase {
|
|||||||
.extract()
|
.extract()
|
||||||
.path("_links.httpProtocol.href");
|
.path("_links.httpProtocol.href");
|
||||||
}
|
}
|
||||||
|
|
||||||
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();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -5,8 +5,8 @@ import sonia.scm.util.IOUtil;
|
|||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Collection;
|
import java.util.Collection;
|
||||||
|
|
||||||
class ScmParameterizedIntegrationTestUtil {
|
class ScmTypes {
|
||||||
static Collection<String> createParameters() {
|
static Collection<String> availableScmTypes() {
|
||||||
Collection<String> params = new ArrayList<>();
|
Collection<String> params = new ArrayList<>();
|
||||||
|
|
||||||
params.add("git");
|
params.add("git");
|
||||||
@@ -5,11 +5,15 @@ import org.slf4j.Logger;
|
|||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import sonia.scm.web.VndMediaType;
|
import sonia.scm.web.VndMediaType;
|
||||||
|
|
||||||
|
import javax.json.Json;
|
||||||
|
import java.util.HashMap;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
import java.util.Map;
|
||||||
|
|
||||||
import static java.util.Arrays.asList;
|
import static java.util.Arrays.asList;
|
||||||
import static sonia.scm.it.RestUtil.createResourceUrl;
|
import static sonia.scm.it.RestUtil.createResourceUrl;
|
||||||
import static sonia.scm.it.RestUtil.given;
|
import static sonia.scm.it.RestUtil.given;
|
||||||
|
import static sonia.scm.it.ScmTypes.availableScmTypes;
|
||||||
|
|
||||||
public class TestData {
|
public class TestData {
|
||||||
|
|
||||||
@@ -17,13 +21,24 @@ public class TestData {
|
|||||||
|
|
||||||
private static final List<String> PROTECTED_USERS = asList("scmadmin", "anonymous");
|
private static final List<String> PROTECTED_USERS = asList("scmadmin", "anonymous");
|
||||||
|
|
||||||
|
private static Map<String, String> DEFAULT_REPOSITORIES = new HashMap<>();
|
||||||
|
|
||||||
|
public static void createDefault() {
|
||||||
|
cleanup();
|
||||||
|
createDefaultRepositories();
|
||||||
|
}
|
||||||
|
|
||||||
public static void cleanup() {
|
public static void cleanup() {
|
||||||
cleanupRepositories();
|
cleanupRepositories();
|
||||||
cleanupGroups();
|
cleanupGroups();
|
||||||
cleanupUsers();
|
cleanupUsers();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void cleanupRepositories() {
|
public static String getDefaultRepositoryUrl(String repositoryType) {
|
||||||
|
return DEFAULT_REPOSITORIES.get(repositoryType);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static void cleanupRepositories() {
|
||||||
List<String> repositories = given(VndMediaType.REPOSITORY_COLLECTION)
|
List<String> repositories = given(VndMediaType.REPOSITORY_COLLECTION)
|
||||||
.when()
|
.when()
|
||||||
.get(createResourceUrl("repositories"))
|
.get(createResourceUrl("repositories"))
|
||||||
@@ -33,9 +48,10 @@ public class TestData {
|
|||||||
.body().jsonPath().getList("_embedded.repositories._links.self.href");
|
.body().jsonPath().getList("_embedded.repositories._links.self.href");
|
||||||
LOG.info("about to delete {} repositories", repositories.size());
|
LOG.info("about to delete {} repositories", repositories.size());
|
||||||
repositories.forEach(TestData::delete);
|
repositories.forEach(TestData::delete);
|
||||||
|
DEFAULT_REPOSITORIES.clear();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void cleanupGroups() {
|
private static void cleanupGroups() {
|
||||||
List<String> groups = given(VndMediaType.GROUP_COLLECTION)
|
List<String> groups = given(VndMediaType.GROUP_COLLECTION)
|
||||||
.when()
|
.when()
|
||||||
.get(createResourceUrl("groups"))
|
.get(createResourceUrl("groups"))
|
||||||
@@ -47,7 +63,7 @@ public class TestData {
|
|||||||
groups.forEach(TestData::delete);
|
groups.forEach(TestData::delete);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void cleanupUsers() {
|
private static void cleanupUsers() {
|
||||||
List<String> users = given(VndMediaType.USER_COLLECTION)
|
List<String> users = given(VndMediaType.USER_COLLECTION)
|
||||||
.when()
|
.when()
|
||||||
.get(createResourceUrl("users"))
|
.get(createResourceUrl("users"))
|
||||||
@@ -59,7 +75,7 @@ public class TestData {
|
|||||||
users.stream().filter(url -> PROTECTED_USERS.stream().noneMatch(url::contains)).forEach(TestData::delete);
|
users.stream().filter(url -> PROTECTED_USERS.stream().noneMatch(url::contains)).forEach(TestData::delete);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void delete(String url) {
|
private static void delete(String url) {
|
||||||
given(VndMediaType.REPOSITORY)
|
given(VndMediaType.REPOSITORY)
|
||||||
.when()
|
.when()
|
||||||
.delete(url)
|
.delete(url)
|
||||||
@@ -68,6 +84,32 @@ public class TestData {
|
|||||||
LOG.info("deleted {}", url);
|
LOG.info("deleted {}", url);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static void createDefaultRepositories() {
|
||||||
|
for (String repositoryType : availableScmTypes()) {
|
||||||
|
String url = given(VndMediaType.REPOSITORY)
|
||||||
|
.body(repositoryJson(repositoryType))
|
||||||
|
|
||||||
|
.when()
|
||||||
|
.post(createResourceUrl("repositories"))
|
||||||
|
|
||||||
|
.then()
|
||||||
|
.statusCode(HttpStatus.SC_CREATED)
|
||||||
|
.extract()
|
||||||
|
.header("location");
|
||||||
|
DEFAULT_REPOSITORIES.put(repositoryType, url);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String repositoryJson(String repositoryType) {
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
cleanup();
|
cleanup();
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user