Merge "GetArchive: Test getting tar/tar.gz archive"
diff --git a/javatests/com/google/gerrit/acceptance/rest/change/GetArchiveIT.java b/javatests/com/google/gerrit/acceptance/rest/change/GetArchiveIT.java
index 7abd75a..15e6360 100644
--- a/javatests/com/google/gerrit/acceptance/rest/change/GetArchiveIT.java
+++ b/javatests/com/google/gerrit/acceptance/rest/change/GetArchiveIT.java
@@ -18,19 +18,42 @@
 import static com.google.gerrit.testing.GerritJUnit.assertThrows;
 
 import com.google.gerrit.acceptance.AbstractDaemonTest;
+import com.google.gerrit.acceptance.PushOneCommit;
 import com.google.gerrit.acceptance.RestResponse;
 import com.google.gerrit.extensions.client.ArchiveFormat;
 import com.google.gerrit.extensions.restapi.BadRequestException;
+import com.google.gerrit.extensions.restapi.BinaryResult;
 import com.google.gerrit.extensions.restapi.MethodNotAllowedException;
+import com.google.gerrit.git.ObjectIds;
+import java.io.BufferedOutputStream;
+import java.io.ByteArrayInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.InputStream;
+import java.util.HashMap;
+import org.apache.commons.compress.archivers.tar.TarArchiveEntry;
+import org.apache.commons.compress.archivers.tar.TarArchiveInputStream;
+import org.apache.commons.compress.compressors.gzip.GzipCompressorInputStream;
+import org.eclipse.jgit.revwalk.RevCommit;
 import org.junit.Before;
 import org.junit.Test;
 
 public class GetArchiveIT extends AbstractDaemonTest {
+  private static final String DIRECTORY_NAME = "foo";
+  private static final String FILE_NAME = DIRECTORY_NAME + "/bar.txt";
+  private static final String FILE_CONTENT = "some content";
+
   private String changeId;
+  private RevCommit commit;
 
   @Before
   public void setUp() throws Exception {
-    changeId = createChange().getChangeId();
+    PushOneCommit push =
+        pushFactory.create(admin.newIdent(), testRepo, "My Change", FILE_NAME, FILE_CONTENT);
+    PushOneCommit.Result result = push.to("refs/for/master");
+    result.assertOkStatus();
+
+    changeId = result.getChangeId();
+    commit = result.getCommit();
   }
 
   @Test
@@ -61,4 +84,71 @@
             () -> gApi.changes().id(changeId).current().getArchive(ArchiveFormat.ZIP));
     assertThat(ex).hasMessageThat().isEqualTo("zip format is disabled");
   }
+
+  @Test
+  public void getTarArchive() throws Exception {
+    BinaryResult res = gApi.changes().id(changeId).current().getArchive(ArchiveFormat.TAR);
+    assertThat(res.getAttachmentName())
+        .isEqualTo(commit.abbreviate(ObjectIds.ABBREV_STR_LEN).name() + ".tar");
+    assertThat(res.getContentType()).isEqualTo("application/x-tar");
+    assertThat(res.canGzip()).isFalse();
+
+    byte[] archiveBytes = getBinaryContent(res);
+    try (ByteArrayInputStream in = new ByteArrayInputStream(archiveBytes)) {
+      HashMap<String, String> archiveEntries = getTarContent(in);
+      assertThat(archiveEntries)
+          .containsExactly(DIRECTORY_NAME + "/", null, FILE_NAME, FILE_CONTENT);
+    }
+  }
+
+  @Test
+  public void getTgzArchive() throws Exception {
+    BinaryResult res = gApi.changes().id(changeId).current().getArchive(ArchiveFormat.TGZ);
+    assertThat(res.getAttachmentName())
+        .isEqualTo(commit.abbreviate(ObjectIds.ABBREV_STR_LEN).name() + ".tar.gz");
+    assertThat(res.getContentType()).isEqualTo("application/x-gzip");
+    assertThat(res.canGzip()).isFalse();
+
+    byte[] archiveBytes = getBinaryContent(res);
+    try (ByteArrayInputStream in = new ByteArrayInputStream(archiveBytes);
+        GzipCompressorInputStream gzipIn = new GzipCompressorInputStream(in)) {
+      HashMap<String, String> archiveEntries = getTarContent(gzipIn);
+      assertThat(archiveEntries)
+          .containsExactly(DIRECTORY_NAME + "/", null, FILE_NAME, FILE_CONTENT);
+    }
+  }
+
+  private HashMap<String, String> getTarContent(InputStream in) throws Exception {
+    HashMap<String, String> archiveEntries = new HashMap<>();
+    int bufferSize = 100;
+    try (TarArchiveInputStream tarIn = new TarArchiveInputStream(in)) {
+      TarArchiveEntry entry;
+      while ((entry = tarIn.getNextTarEntry()) != null) {
+        if (entry.isDirectory()) {
+          archiveEntries.put(entry.getName(), null);
+        } else {
+          byte data[] = new byte[bufferSize];
+          try (ByteArrayOutputStream out = new ByteArrayOutputStream();
+              BufferedOutputStream bufferedOut = new BufferedOutputStream(out, bufferSize)) {
+            int count;
+            while ((count = tarIn.read(data, 0, bufferSize)) != -1) {
+              bufferedOut.write(data, 0, count);
+            }
+            bufferedOut.flush();
+            archiveEntries.put(entry.getName(), out.toString());
+          }
+        }
+      }
+    }
+    return archiveEntries;
+  }
+
+  private byte[] getBinaryContent(BinaryResult res) throws Exception {
+    try (ByteArrayOutputStream out = new ByteArrayOutputStream()) {
+      res.writeTo(out);
+      return out.toByteArray();
+    } finally {
+      res.close();
+    }
+  }
 }