Add more test coverage for ListFiles

This change adds two tests for ListFiles, which covers the code paths
when 'base' or 'query' field is set.

Bug: Issue 6189
Change-Id: I6afd2881abb46732ea9a973d1ba7f21d6b23506a
This commit is contained in:
Changcheng Xiao
2017-07-04 15:35:33 +02:00
parent 0bfd454d3e
commit ac5d9c0229
4 changed files with 72 additions and 0 deletions

View File

@@ -887,6 +887,54 @@ public class RevisionIT extends AbstractDaemonTest {
.containsExactly(COMMIT_MSG, MERGE_LIST, "foo");
}
@Test
public void listFilesOnDifferentBases() throws Exception {
PushOneCommit.Result result1 = createChange();
String changeId = result1.getChangeId();
PushOneCommit.Result result2 = amendChange(changeId, SUBJECT, "b.txt", "b");
PushOneCommit.Result result3 = amendChange(changeId, SUBJECT, "c.txt", "c");
String revId1 = result1.getCommit().name();
String revId2 = result2.getCommit().name();
String revId3 = result3.getCommit().name();
assertThat(gApi.changes().id(changeId).revision(revId1).files(null).keySet())
.containsExactly(COMMIT_MSG, "a.txt");
assertThat(gApi.changes().id(changeId).revision(revId2).files(null).keySet())
.containsExactly(COMMIT_MSG, "a.txt", "b.txt");
assertThat(gApi.changes().id(changeId).revision(revId3).files(null).keySet())
.containsExactly(COMMIT_MSG, "a.txt", "b.txt", "c.txt");
assertThat(gApi.changes().id(changeId).revision(revId2).files(revId1).keySet())
.containsExactly(COMMIT_MSG, "b.txt");
assertThat(gApi.changes().id(changeId).revision(revId3).files(revId1).keySet())
.containsExactly(COMMIT_MSG, "b.txt", "c.txt");
assertThat(gApi.changes().id(changeId).revision(revId3).files(revId2).keySet())
.containsExactly(COMMIT_MSG, "c.txt");
}
@Test
public void queryRevisionFiles() throws Exception {
Map<String, String> files = ImmutableMap.of("file1.txt", "content 1", "file2.txt", "content 2");
PushOneCommit.Result result =
pushFactory.create(db, admin.getIdent(), testRepo, SUBJECT, files).to("refs/for/master");
result.assertOkStatus();
String changeId = result.getChangeId();
assertThat(gApi.changes().id(changeId).current().queryFiles("file1.txt"))
.containsExactly("file1.txt");
assertThat(gApi.changes().id(changeId).current().queryFiles("file2.txt"))
.containsExactly("file2.txt");
assertThat(gApi.changes().id(changeId).current().queryFiles("file1"))
.containsExactly("file1.txt");
assertThat(gApi.changes().id(changeId).current().queryFiles("file2"))
.containsExactly("file2.txt");
assertThat(gApi.changes().id(changeId).current().queryFiles("file"))
.containsExactly("file1.txt", "file2.txt");
assertThat(gApi.changes().id(changeId).current().queryFiles(""))
.containsExactly("file1.txt", "file2.txt");
}
@Test
public void description() throws Exception {
PushOneCommit.Result r = createChange();

View File

@@ -69,6 +69,8 @@ public interface RevisionApi {
Map<String, FileInfo> files(int parentNum) throws RestApiException;
List<String> queryFiles(String query) throws RestApiException;
FileApi file(String path);
CommitInfo commit(boolean addLinks) throws RestApiException;
@@ -238,6 +240,11 @@ public interface RevisionApi {
throw new NotImplementedException();
}
@Override
public List<String> queryFiles(String query) throws RestApiException {
throw new NotImplementedException();
}
@Override
public FileApi file(String path) {
throw new NotImplementedException();

View File

@@ -14,6 +14,7 @@
package com.google.gerrit.server.api.changes;
import static com.google.common.base.Preconditions.checkArgument;
import static com.google.gerrit.server.api.ApiUtil.asRestApiException;
import com.google.common.collect.ImmutableSet;
@@ -387,6 +388,17 @@ class RevisionApiImpl implements RevisionApi {
}
}
@SuppressWarnings("unchecked")
@Override
public List<String> queryFiles(String query) throws RestApiException {
try {
checkArgument(query != null, "no query provided");
return (List<String>) listFiles.setQuery(query).apply(revision).value();
} catch (Exception e) {
throw asRestApiException("Cannot retrieve files", e);
}
}
@Override
public FileApi file(String path) {
return fileApi.create(files.parse(revision, IdString.fromDecoded(path)));

View File

@@ -316,6 +316,11 @@ public class Files implements ChildCollection<RevisionResource, FileResource> {
}
}
public ListFiles setQuery(String query) {
this.query = query;
return this;
}
public ListFiles setBase(String base) {
this.base = base;
return this;