Files
SCM-Manager/scm-it/src/test/java/sonia/scm/it/RepositoryAccessITCase.java

190 lines
5.7 KiB
Java
Raw Normal View History

package sonia.scm.it;
import org.apache.http.HttpStatus;
import org.junit.Assume;
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 sonia.scm.repository.Changeset;
import sonia.scm.repository.client.api.ClientCommand;
2018-08-22 09:18:17 +02:00
import sonia.scm.repository.client.api.RepositoryClient;
2018-08-22 09:18:17 +02:00
import java.io.File;
import java.io.IOException;
import java.util.Collection;
2018-08-29 13:34:53 +02:00
import java.util.List;
2018-08-17 09:33:45 +02:00
import static java.lang.Thread.sleep;
import static org.assertj.core.api.Assertions.assertThat;
2018-08-17 09:33:45 +02:00
import static org.hamcrest.Matchers.equalTo;
import static org.junit.Assert.assertNotNull;
import static sonia.scm.it.RestUtil.ADMIN_PASSWORD;
import static sonia.scm.it.RestUtil.ADMIN_USERNAME;
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;
2018-08-22 09:18:17 +02:00
private File folder;
private RepositoryRequests.AppliedRepositoryGetRequest repositoryGetRequest;
public RepositoryAccessITCase(String repositoryType) {
this.repositoryType = repositoryType;
}
@Parameterized.Parameters(name = "{0}")
public static Collection<String> createParameters() {
return availableScmTypes();
}
@Before
public void init() {
TestData.createDefault();
2018-08-22 09:18:17 +02:00
folder = tempFolder.getRoot();
repositoryGetRequest = RepositoryRequests.start()
.given()
.url(TestData.getDefaultRepositoryUrl(repositoryType))
.usernameAndPassword(ADMIN_USERNAME, ADMIN_PASSWORD)
.get()
.assertStatusCode(HttpStatus.SC_OK);
}
@Test
public void shouldFindBranches() throws IOException {
2018-08-22 10:59:46 +02:00
RepositoryClient repositoryClient = RepositoryUtil.createRepositoryClient(repositoryType, folder);
Assume.assumeTrue("There are no branches for " + repositoryType, repositoryClient.isCommandSupported(ClientCommand.BRANCH));
2018-08-22 10:59:46 +02:00
RepositoryUtil.createAndCommitFile(repositoryClient, "scmadmin", "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);
}
2018-08-17 09:33:45 +02:00
@Test
public void shouldReadContent() throws IOException, InterruptedException {
2018-08-28 12:43:56 +02:00
RepositoryClient repositoryClient = RepositoryUtil.createRepositoryClient(repositoryType, folder);
RepositoryUtil.createAndCommitFile(repositoryClient, "scmadmin", "a.txt", "a");
tempFolder.newFolder("subfolder");
2018-08-28 12:43:56 +02:00
RepositoryUtil.createAndCommitFile(repositoryClient, "scmadmin", "subfolder/a.txt", "sub-a");
2018-08-17 09:33:45 +02:00
sleep(1000);
String sourcesUrl = given()
.when()
.get(TestData.getDefaultRepositoryUrl(repositoryType))
.then()
.statusCode(HttpStatus.SC_OK)
.extract()
.path("_links.sources.href");
String rootContentUrl = given()
2018-08-17 09:33:45 +02:00
.when()
.get(sourcesUrl)
.then()
.statusCode(HttpStatus.SC_OK)
.extract()
.path("files.find{it.name=='a.txt'}._links.self.href");
2018-08-17 09:33:45 +02:00
given()
.when()
.get(rootContentUrl)
2018-08-17 09:33:45 +02:00
.then()
.statusCode(HttpStatus.SC_OK)
.body(equalTo("a"));
String subfolderSourceUrl = given()
.when()
.get(sourcesUrl)
.then()
.statusCode(HttpStatus.SC_OK)
.extract()
.path("files.find{it.name=='subfolder'}._links.self.href");
String subfolderContentUrl= given()
.when()
.get(subfolderSourceUrl)
.then()
.statusCode(HttpStatus.SC_OK)
.extract()
.path("files[0]._links.self.href");
given()
.when()
.get(subfolderContentUrl)
.then()
.statusCode(HttpStatus.SC_OK)
.body(equalTo("sub-a"));
2018-08-17 09:33:45 +02:00
}
2018-08-29 13:34:53 +02:00
@Test
public void shouldFindChangesets() throws IOException {
RepositoryClient repositoryClient = RepositoryUtil.createRepositoryClient(repositoryType, folder);
RepositoryUtil.createAndCommitFile(repositoryClient, "scmadmin", "a.txt", "a");
RepositoryUtil.createAndCommitFile(repositoryClient, "scmadmin", "b.txt", "b");
String changesetsUrl = given()
.when()
.get(TestData.getDefaultRepositoryUrl(repositoryType))
.then()
.statusCode(HttpStatus.SC_OK)
.extract()
.path("_links.changesets.href");
List changesets = given()
.when()
.get(changesetsUrl)
.then()
.statusCode(HttpStatus.SC_OK)
.extract()
.path("_embedded.changesets.id");
assertThat(changesets).size().isBetween(2, 3); // svn has an implicit root revision '0' that is extra to the two commits
}
@Test
@SuppressWarnings("unchecked")
public void shouldFindFileHistory() throws IOException {
RepositoryClient repositoryClient = RepositoryUtil.createRepositoryClient(repositoryType, folder);
String fileName_1 = "a.txt";
Changeset changeset_1 = RepositoryUtil.createAndCommitFile(repositoryClient, ADMIN_USERNAME, fileName_1, "a");
repositoryGetRequest
.usingRepositoryResponse()
.requestSources()
.usingSourcesResponse()
.requestFileHistory(fileName_1)
.assertStatusCode(HttpStatus.SC_OK)
.usingChangesetsResponse()
.assertChangesets(changesets -> {
assertThat(changesets).hasSize(1);
assertThat(changesets.get(0)).containsEntry("id", changeset_1.getId());
assertThat(changesets.get(0)).containsEntry("description", changeset_1.getDescription());
}
);
}
}
2018-08-29 13:34:53 +02:00