integration tests for modifications

This commit is contained in:
Mohamed Karray
2018-09-13 17:29:52 +02:00
parent 6c53675e2c
commit 188d2d8cef
7 changed files with 277 additions and 9 deletions

View File

@@ -3,6 +3,8 @@ package sonia.scm.it;
import io.restassured.response.ExtractableResponse;
import io.restassured.response.Response;
import org.apache.http.HttpStatus;
import org.assertj.core.util.Lists;
import org.assertj.core.util.Maps;
import org.junit.Assume;
import org.junit.Before;
import org.junit.Rule;
@@ -17,8 +19,11 @@ import sonia.scm.web.VndMediaType;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import static java.lang.Thread.sleep;
import static org.assertj.core.api.Assertions.assertThat;
@@ -312,5 +317,163 @@ public class RepositoryAccessITCase {
}
);
}
@Test
@SuppressWarnings("unchecked")
public void shouldFindAddedModifications() throws IOException {
RepositoryClient repositoryClient = RepositoryUtil.createRepositoryClient(repositoryType, folder);
String fileName = "a.txt";
Changeset changeset = RepositoryUtil.createAndCommitFile(repositoryClient, ADMIN_USERNAME, fileName, "a");
String revision = changeset.getId();
repositoryGetRequest
.usingRepositoryResponse()
.requestChangesets()
.assertStatusCode(HttpStatus.SC_OK)
.usingChangesetsResponse()
.requestModifications(revision)
.assertStatusCode(HttpStatus.SC_OK)
.usingModificationsResponse()
.assertRevision(actualRevision -> assertThat(actualRevision).isEqualTo(revision))
.assertAdded(addedFiles -> assertThat(addedFiles)
.hasSize(1)
.containsExactly(fileName))
.assertRemoved(removedFiles -> assertThat(removedFiles)
.hasSize(0))
.assertModified(files -> assertThat(files)
.hasSize(0));
}
@Test
@SuppressWarnings("unchecked")
public void shouldFindRemovedModifications() throws IOException {
RepositoryClient repositoryClient = RepositoryUtil.createRepositoryClient(repositoryType, folder);
String fileName = "a.txt";
RepositoryUtil.createAndCommitFile(repositoryClient, ADMIN_USERNAME, fileName, "a");
Changeset changeset = RepositoryUtil.removeAndCommitFile(repositoryClient, ADMIN_USERNAME, fileName);
String revision = changeset.getId();
repositoryGetRequest
.usingRepositoryResponse()
.requestChangesets()
.assertStatusCode(HttpStatus.SC_OK)
.usingChangesetsResponse()
.requestModifications(revision)
.assertStatusCode(HttpStatus.SC_OK)
.usingModificationsResponse()
.assertRevision(actualRevision -> assertThat(actualRevision).isEqualTo(revision))
.assertRemoved(removedFiles -> assertThat(removedFiles)
.hasSize(1)
.containsExactly(fileName))
.assertAdded(addedFiles -> assertThat(addedFiles)
.hasSize(0))
.assertModified(files -> assertThat(files)
.hasSize(0));
}
@Test
@SuppressWarnings("unchecked")
public void shouldFindUpdateModifications() throws IOException {
RepositoryClient repositoryClient = RepositoryUtil.createRepositoryClient(repositoryType, folder);
String fileName = "a.txt";
RepositoryUtil.createAndCommitFile(repositoryClient, ADMIN_USERNAME, fileName, "a");
Changeset changeset = RepositoryUtil.createAndCommitFile(repositoryClient, ADMIN_USERNAME, fileName, "new Content");
String revision = changeset.getId();
repositoryGetRequest
.usingRepositoryResponse()
.requestChangesets()
.assertStatusCode(HttpStatus.SC_OK)
.usingChangesetsResponse()
.requestModifications(revision)
.assertStatusCode(HttpStatus.SC_OK)
.usingModificationsResponse()
.assertRevision(actualRevision -> assertThat(actualRevision).isEqualTo(revision))
.assertModified(modifiedFiles -> assertThat(modifiedFiles)
.hasSize(1)
.containsExactly(fileName))
.assertRemoved(removedFiles -> assertThat(removedFiles)
.hasSize(0))
.assertAdded(addedFiles -> assertThat(addedFiles)
.hasSize(0));
}
@Test
@SuppressWarnings("unchecked")
public void shouldFindMultipleModifications() throws IOException {
RepositoryClient repositoryClient = RepositoryUtil.createRepositoryClient(repositoryType, folder);
RepositoryUtil.createAndCommitFile(repositoryClient, ADMIN_USERNAME, "b.txt", "b");
RepositoryUtil.createAndCommitFile(repositoryClient, ADMIN_USERNAME, "c.txt", "c");
RepositoryUtil.createAndCommitFile(repositoryClient, ADMIN_USERNAME, "d.txt", "d");
Map<String, String> addedFiles = new HashMap<String, String>()
{{
put("a.txt", "bla bla");
}};
Map<String, String> modifiedFiles = new HashMap<String, String>()
{{
put("b.txt", "new content");
}};
ArrayList<String> removedFiles = Lists.newArrayList("c.txt", "d.txt");
Changeset changeset = RepositoryUtil.commitMultipleFileModifications(repositoryClient, ADMIN_USERNAME, addedFiles, modifiedFiles, removedFiles);
String revision = changeset.getId();
repositoryGetRequest
.usingRepositoryResponse()
.requestChangesets()
.assertStatusCode(HttpStatus.SC_OK)
.usingChangesetsResponse()
.requestModifications(revision)
.assertStatusCode(HttpStatus.SC_OK)
.usingModificationsResponse()
.assertRevision(actualRevision -> assertThat(actualRevision).isEqualTo(revision))
.assertAdded(a -> assertThat(a)
.hasSize(1)
.containsExactly("a.txt"))
.assertModified(m-> assertThat(m)
.hasSize(1)
.containsExactly("b.txt"))
.assertRemoved(r -> assertThat(r)
.hasSize(2)
.containsExactly("c.txt", "d.txt"));
}
@Test
@SuppressWarnings("unchecked")
public void svnShouldCreateOneModificationPerFolder() throws IOException {
Assume.assumeThat(repositoryType, equalTo("svn"));
RepositoryClient repositoryClient = RepositoryUtil.createRepositoryClient(repositoryType, folder);
RepositoryUtil.createAndCommitFile(repositoryClient, ADMIN_USERNAME, "bbb/bb/b.txt", "b");
RepositoryUtil.createAndCommitFile(repositoryClient, ADMIN_USERNAME, "ccc/cc/c.txt", "c");
RepositoryUtil.createAndCommitFile(repositoryClient, ADMIN_USERNAME, "ddd/dd/d.txt", "d");
Map<String, String> addedFiles = new HashMap<String, String>()
{{
put("aaa/aa/a.txt", "bla bla");
}};
Map<String, String> modifiedFiles = new HashMap<String, String>()
{{
put("bbb/bb/b.txt", "new content");
}};
ArrayList<String> removedFiles = Lists.newArrayList("ccc/cc/c.txt", "ddd/dd/d.txt");
Changeset changeset = RepositoryUtil.commitMultipleFileModifications(repositoryClient, ADMIN_USERNAME, addedFiles, modifiedFiles, removedFiles);
String revision = changeset.getId();
repositoryGetRequest
.usingRepositoryResponse()
.requestChangesets()
.assertStatusCode(HttpStatus.SC_OK)
.usingChangesetsResponse()
.requestModifications(revision)
.assertStatusCode(HttpStatus.SC_OK)
.usingModificationsResponse()
.assertRevision(actualRevision -> assertThat(actualRevision).isEqualTo(revision))
.assertAdded(a -> assertThat(a)
.hasSize(3)
.containsExactly("aaa/aa/a.txt", "aaa", "aaa/aa"))
.assertModified(m-> assertThat(m)
.hasSize(1)
.containsExactly("bbb/bb/b.txt"))
.assertRemoved(r -> assertThat(r)
.hasSize(2)
.containsExactly("ccc/cc/c.txt", "ddd/dd/d.txt"));
}
}

View File

@@ -156,7 +156,7 @@ public class RepositoryRequests {
return new AppliedGetSourcesRequest(getResponseFromLink(repositoryResponse, "_links.sources.href"));
}
AppliedGetChangesetsRequest requestChangesets(String fileName) {
AppliedGetChangesetsRequest requestChangesets() {
return new AppliedGetChangesetsRequest(getResponseFromLink(repositoryResponse, "_links.changesets.href"));
}
}
@@ -189,6 +189,9 @@ public class RepositoryRequests {
return new AppliedGetDiffRequest(getResponseFromLink(changesetsResponse, "_embedded.changesets.find{it.id=='" + revision + "'}._links.diff.href"));
}
public AppliedGetModificationsRequest requestModifications(String revision) {
return new AppliedGetModificationsRequest(getResponseFromLink(changesetsResponse, "_embedded.changesets.find{it.id=='" + revision + "'}._links.modifications.href"));
}
}
class AppliedGetSourcesRequest extends AppliedGetRequest<AppliedGetSourcesRequest> {
@@ -246,4 +249,45 @@ public class RepositoryRequests {
return new GivenWithUrlAndAuth();
}
}
class AppliedGetModificationsRequest extends AppliedGetRequest<AppliedGetModificationsRequest> {
public AppliedGetModificationsRequest(Response response) { super(response); }
ModificationsResponse usingModificationsResponse() {
return new ModificationsResponse(super.response);
}
}
class ModificationsResponse {
private Response resource;
public ModificationsResponse(Response resource) {
this.resource = resource;
}
ModificationsResponse assertRevision(Consumer<String> assertRevision) {
String revision = resource.then().extract().path("revision");
assertRevision.accept(revision);
return this;
}
ModificationsResponse assertAdded(Consumer<List<String>> assertAdded) {
List<String > added = resource.then().extract().path("added");
assertAdded.accept(added);
return this;
}
ModificationsResponse assertRemoved(Consumer<List<String>> assertRemoved) {
List<String > removed = resource.then().extract().path("removed");
assertRemoved.accept(removed);
return this;
}
ModificationsResponse assertModified(Consumer<List<String>> assertModified) {
List<String > modified = resource.then().extract().path("modified");
assertModified.accept(modified);
return this;
}
}
}

View File

@@ -14,6 +14,8 @@ import sonia.scm.repository.client.api.RepositoryClientFactory;
import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;
public class RepositoryUtil {
@@ -42,11 +44,53 @@ public class RepositoryUtil {
}
static Changeset createAndCommitFile(RepositoryClient repositoryClient, String username, String fileName, String content) throws IOException {
writeAndAddFile(repositoryClient, fileName, content);
return commit(repositoryClient, username, "added " + fileName);
}
/**
* Bundle multiple File modification in one changeset
*
* @param repositoryClient
* @param username
* @param addedFiles map.key: path of the file, value: the file content
* @param modifiedFiles map.key: path of the file, value: the file content
* @param removedFiles list of file paths to be removed
* @return the changeset with all modifications
* @throws IOException
*/
static Changeset commitMultipleFileModifications(RepositoryClient repositoryClient, String username, Map<String, String> addedFiles, Map<String, String> modifiedFiles, List<String> removedFiles) throws IOException {
for (String fileName : addedFiles.keySet()) {
writeAndAddFile(repositoryClient, fileName, addedFiles.get(fileName));
}
for (String fileName : modifiedFiles.keySet()) {
writeAndAddFile(repositoryClient, fileName, modifiedFiles.get(fileName));
}
for (String fileName : removedFiles) {
deleteFileAndApplyRemoveCommand(repositoryClient, fileName);
}
return commit(repositoryClient, username, "multiple file modifications" );
}
private static File writeAndAddFile(RepositoryClient repositoryClient, String fileName, String content) throws IOException {
File file = new File(repositoryClient.getWorkingCopy(), fileName);
Files.createParentDirs(file);
Files.write(content, file, Charsets.UTF_8);
addWithParentDirectories(repositoryClient, file);
return commit(repositoryClient, username, "added " + fileName);
return file;
}
static Changeset removeAndCommitFile(RepositoryClient repositoryClient, String username, String fileName) throws IOException {
deleteFileAndApplyRemoveCommand(repositoryClient, fileName);
return commit(repositoryClient, username, "removed " + fileName);
}
private static void deleteFileAndApplyRemoveCommand(RepositoryClient repositoryClient, String fileName) throws IOException {
File file = new File(repositoryClient.getWorkingCopy(), fileName);
if (repositoryClient.isCommandSupported(ClientCommand.REMOVE)) {
repositoryClient.getRemoveCommand().remove(fileName);
}
file.delete();
}
private static String addWithParentDirectories(RepositoryClient repositoryClient, File file) throws IOException {

View File

@@ -53,7 +53,7 @@ public class GitModificationsCommandTest extends AbstractRemoteCommandTestBase {
commit(outgoing, "add file");
File file = new File(outgoingDirectory, fileName);
file.delete();
outgoing.add().setUpdate(true).addFilepattern(".").call();
outgoing.rm().addFilepattern(fileName).call();
RevCommit removedFileCommit = commit(outgoing, "remove file");
String revision = removedFileCommit.getName();
Consumer<Modifications> assertModifications = assertRemovedFiles(fileName);

View File

@@ -42,6 +42,7 @@ import sonia.scm.repository.HgHookManager;
import sonia.scm.repository.HgRepositoryHandler;
import sonia.scm.repository.Repository;
import sonia.scm.repository.api.Command;
import sonia.scm.repository.api.CommandNotSupportedException;
//~--- JDK imports ------------------------------------------------------------
@@ -201,6 +202,16 @@ public class HgRepositoryServiceProvider extends RepositoryServiceProvider
return new HgLogCommand(context, repository);
}
/**
* Get the corresponding {@link ModificationsCommand} implemented from the Plugins
*
* @return the corresponding {@link ModificationsCommand} implemented from the Plugins
* @throws CommandNotSupportedException if there is no Implementation
*/
public ModificationsCommand getModificationsCommand() {
return new HgModificationsCommand(context,repository);
}
/**
* Method description
*

View File

@@ -167,6 +167,10 @@ public class SvnRepositoryServiceProvider extends RepositoryServiceProvider
return new SvnLogCommand(context, repository);
}
public ModificationsCommand getModificationsCommand() {
return new SvnModificationsCommand(context, repository);
}
/**
* Method description
*

View File

@@ -70,13 +70,15 @@ class SvnChangeWorker {
SVNWCClient wClient = client.getWCClient();
// add files
try {
wClient.doAdd(addedFiles.toArray(new File[0]), true, false, false,
SVNDepth.INFINITY, false, false, false);
addedFiles.clear();
if (!addedFiles.isEmpty()){
try {
wClient.doAdd(addedFiles.toArray(new File[0]), true, false, false,
SVNDepth.INFINITY, false, false, false);
addedFiles.clear();
} catch (SVNException ex) {
throw new RepositoryClientException("failed to add files", ex);
} catch (SVNException ex) {
throw new RepositoryClientException("failed to add files", ex);
}
}
// remove files