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 --------------------------------------------------------
|
||||
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.junit.After;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
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.web.VndMediaType;
|
||||
|
||||
import javax.json.Json;
|
||||
import java.io.File;
|
||||
import java.io.FileOutputStream;
|
||||
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.RestUtil.createResourceUrl;
|
||||
import static sonia.scm.it.RestUtil.given;
|
||||
import static sonia.scm.it.ScmTypes.availableScmTypes;
|
||||
import static sonia.scm.it.TestData.repositoryJson;
|
||||
|
||||
@RunWith(Parameterized.class)
|
||||
public class RepositoriesITCase {
|
||||
@@ -77,34 +77,21 @@ public class RepositoriesITCase {
|
||||
|
||||
public RepositoriesITCase(String repositoryType) {
|
||||
this.repositoryType = repositoryType;
|
||||
this.repositoryUrl = TestData.getDefaultRepositoryUrl(repositoryType);
|
||||
}
|
||||
|
||||
@Parameters(name = "{0}")
|
||||
public static Collection<String> createParameters() {
|
||||
return ScmParameterizedIntegrationTestUtil.createParameters();
|
||||
return availableScmTypes();
|
||||
}
|
||||
|
||||
@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();
|
||||
TestData.createDefault();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldCreateSuccessfully() throws IOException {
|
||||
public void shouldCreateSuccessfully() {
|
||||
given(VndMediaType.REPOSITORY)
|
||||
|
||||
.when()
|
||||
@@ -142,7 +129,7 @@ public class RepositoriesITCase {
|
||||
|
||||
@Test
|
||||
public void shouldRejectMultipleCreations() {
|
||||
String repositoryJson = repositoryJson();
|
||||
String repositoryJson = repositoryJson(repositoryType);
|
||||
given(VndMediaType.REPOSITORY)
|
||||
.body(repositoryJson)
|
||||
|
||||
@@ -208,14 +195,4 @@ public class RepositoriesITCase {
|
||||
.extract()
|
||||
.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.Collection;
|
||||
|
||||
class ScmParameterizedIntegrationTestUtil {
|
||||
static Collection<String> createParameters() {
|
||||
class ScmTypes {
|
||||
static Collection<String> availableScmTypes() {
|
||||
Collection<String> params = new ArrayList<>();
|
||||
|
||||
params.add("git");
|
||||
@@ -5,11 +5,15 @@ import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
import sonia.scm.web.VndMediaType;
|
||||
|
||||
import javax.json.Json;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import static java.util.Arrays.asList;
|
||||
import static sonia.scm.it.RestUtil.createResourceUrl;
|
||||
import static sonia.scm.it.RestUtil.given;
|
||||
import static sonia.scm.it.ScmTypes.availableScmTypes;
|
||||
|
||||
public class TestData {
|
||||
|
||||
@@ -17,13 +21,24 @@ public class TestData {
|
||||
|
||||
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() {
|
||||
cleanupRepositories();
|
||||
cleanupGroups();
|
||||
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)
|
||||
.when()
|
||||
.get(createResourceUrl("repositories"))
|
||||
@@ -33,9 +48,10 @@ public class TestData {
|
||||
.body().jsonPath().getList("_embedded.repositories._links.self.href");
|
||||
LOG.info("about to delete {} repositories", repositories.size());
|
||||
repositories.forEach(TestData::delete);
|
||||
DEFAULT_REPOSITORIES.clear();
|
||||
}
|
||||
|
||||
public static void cleanupGroups() {
|
||||
private static void cleanupGroups() {
|
||||
List<String> groups = given(VndMediaType.GROUP_COLLECTION)
|
||||
.when()
|
||||
.get(createResourceUrl("groups"))
|
||||
@@ -47,7 +63,7 @@ public class TestData {
|
||||
groups.forEach(TestData::delete);
|
||||
}
|
||||
|
||||
public static void cleanupUsers() {
|
||||
private static void cleanupUsers() {
|
||||
List<String> users = given(VndMediaType.USER_COLLECTION)
|
||||
.when()
|
||||
.get(createResourceUrl("users"))
|
||||
@@ -59,7 +75,7 @@ public class TestData {
|
||||
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)
|
||||
.when()
|
||||
.delete(url)
|
||||
@@ -68,6 +84,32 @@ public class TestData {
|
||||
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) {
|
||||
cleanup();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user