mirror of
https://github.com/scm-manager/scm-manager.git
synced 2025-11-08 14:35:45 +01:00
Add basic integration test for interactions with repositories
This commit is contained in:
@@ -0,0 +1,67 @@
|
||||
package sonia.scm.it;
|
||||
|
||||
import org.apache.http.HttpStatus;
|
||||
import org.junit.Before;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.TemporaryFolder;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.junit.runners.Parameterized;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
|
||||
import static org.junit.Assert.assertNotNull;
|
||||
import static org.junit.Assume.assumeFalse;
|
||||
import static sonia.scm.it.RestUtil.given;
|
||||
import static sonia.scm.it.ScmTypes.availableScmTypes;
|
||||
|
||||
@RunWith(Parameterized.class)
|
||||
public class RepositoryAccessITCase {
|
||||
|
||||
@Rule
|
||||
public TemporaryFolder tempFolder = new TemporaryFolder();
|
||||
|
||||
private final String repositoryType;
|
||||
private RepositoryUtil repositoryUtil;
|
||||
|
||||
public RepositoryAccessITCase(String repositoryType) {
|
||||
this.repositoryType = repositoryType;
|
||||
}
|
||||
|
||||
@Parameterized.Parameters(name = "{0}")
|
||||
public static Collection<String> createParameters() {
|
||||
return availableScmTypes();
|
||||
}
|
||||
|
||||
@Before
|
||||
public void initClient() throws IOException {
|
||||
TestData.createDefault();
|
||||
repositoryUtil = new RepositoryUtil(repositoryType, tempFolder.getRoot());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void shouldFindBranches() throws IOException {
|
||||
assumeFalse("There are no branches for SVN", repositoryType.equals("svn"));
|
||||
|
||||
repositoryUtil.createAndCommitFile("a.txt", "a");
|
||||
|
||||
String branchesUrl = given()
|
||||
.when()
|
||||
.get(TestData.getDefaultRepositoryUrl(repositoryType))
|
||||
.then()
|
||||
.statusCode(HttpStatus.SC_OK)
|
||||
.extract()
|
||||
.path("_links.branches.href");
|
||||
|
||||
Object branchName = given()
|
||||
.when()
|
||||
.get(branchesUrl)
|
||||
.then()
|
||||
.statusCode(HttpStatus.SC_OK)
|
||||
.extract()
|
||||
.path("_embedded.branches[0].name");
|
||||
|
||||
assertNotNull(branchName);
|
||||
}
|
||||
}
|
||||
62
scm-it/src/test/java/sonia/scm/it/RepositoryUtil.java
Normal file
62
scm-it/src/test/java/sonia/scm/it/RepositoryUtil.java
Normal file
@@ -0,0 +1,62 @@
|
||||
package sonia.scm.it;
|
||||
|
||||
import com.google.common.base.Charsets;
|
||||
import com.google.common.io.Files;
|
||||
import org.apache.http.HttpStatus;
|
||||
import sonia.scm.repository.Changeset;
|
||||
import sonia.scm.repository.Person;
|
||||
import sonia.scm.repository.client.api.ClientCommand;
|
||||
import sonia.scm.repository.client.api.RepositoryClient;
|
||||
import sonia.scm.repository.client.api.RepositoryClientFactory;
|
||||
import sonia.scm.web.VndMediaType;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
import static sonia.scm.it.RestUtil.ADMIN_PASSWORD;
|
||||
import static sonia.scm.it.RestUtil.ADMIN_USERNAME;
|
||||
import static sonia.scm.it.RestUtil.given;
|
||||
|
||||
public class RepositoryUtil {
|
||||
|
||||
private static final RepositoryClientFactory REPOSITORY_CLIENT_FACTORY = new RepositoryClientFactory();
|
||||
|
||||
private final RepositoryClient repositoryClient;
|
||||
private final File folder;
|
||||
|
||||
RepositoryUtil(String repositoryType, File folder) throws IOException {
|
||||
this.repositoryClient = createRepositoryClient(repositoryType, folder);
|
||||
this.folder = folder;
|
||||
}
|
||||
|
||||
static RepositoryClient createRepositoryClient(String repositoryType, File folder) throws IOException {
|
||||
String httpProtocolUrl = given(VndMediaType.REPOSITORY)
|
||||
|
||||
.when()
|
||||
.get(TestData.getDefaultRepositoryUrl(repositoryType))
|
||||
|
||||
.then()
|
||||
.statusCode(HttpStatus.SC_OK)
|
||||
.extract()
|
||||
.path("_links.httpProtocol.href");
|
||||
|
||||
|
||||
return REPOSITORY_CLIENT_FACTORY.create(repositoryType, httpProtocolUrl, ADMIN_USERNAME, ADMIN_PASSWORD, folder);
|
||||
}
|
||||
|
||||
void createAndCommitFile(String fileName, String content) throws IOException {
|
||||
Files.write(content, new File(folder, fileName), Charsets.UTF_8);
|
||||
repositoryClient.getAddCommand().add(fileName);
|
||||
commit("added " + fileName);
|
||||
}
|
||||
|
||||
Changeset commit(String message) throws IOException {
|
||||
Changeset changeset = repositoryClient.getCommitCommand().commit(
|
||||
new Person("scmadmin", "scmadmin@scm-manager.org"), message
|
||||
);
|
||||
if (repositoryClient.isCommandSupported(ClientCommand.PUSH)) {
|
||||
repositoryClient.getPushCommand().push();
|
||||
}
|
||||
return changeset;
|
||||
}
|
||||
}
|
||||
@@ -16,10 +16,18 @@ public class RestUtil {
|
||||
return REST_BASE_URL.resolve(path);
|
||||
}
|
||||
|
||||
public static final String ADMIN_USERNAME = "scmadmin";
|
||||
public static final String ADMIN_PASSWORD = "scmadmin";
|
||||
|
||||
public static RequestSpecification given(String mediaType) {
|
||||
return RestAssured.given()
|
||||
.contentType(mediaType)
|
||||
.accept(mediaType)
|
||||
.auth().preemptive().basic("scmadmin", "scmadmin");
|
||||
.auth().preemptive().basic(ADMIN_USERNAME, ADMIN_PASSWORD);
|
||||
}
|
||||
|
||||
public static RequestSpecification given() {
|
||||
return RestAssured.given()
|
||||
.auth().preemptive().basic(ADMIN_USERNAME, ADMIN_PASSWORD);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user