Add GetDiff tests for binary files

Release-Notes: skip
Bug: Google b/304511785
Change-Id: I4578e3e58af7b6cb22b81210b979e87fba73176d
Signed-off-by: Edwin Kempin <ekempin@google.com>
diff --git a/javatests/com/google/gerrit/acceptance/api/revision/RevisionDiffIT.java b/javatests/com/google/gerrit/acceptance/api/revision/RevisionDiffIT.java
index b570466..5ab9fcb 100644
--- a/javatests/com/google/gerrit/acceptance/api/revision/RevisionDiffIT.java
+++ b/javatests/com/google/gerrit/acceptance/api/revision/RevisionDiffIT.java
@@ -24,6 +24,7 @@
 import static com.google.gerrit.extensions.common.testing.FileInfoSubject.assertThat;
 import static com.google.gerrit.git.ObjectIds.abbreviateName;
 import static com.google.gerrit.testing.GerritJUnit.assertThrows;
+import static java.nio.charset.StandardCharsets.UTF_8;
 import static java.util.stream.Collectors.joining;
 import static java.util.stream.Collectors.toMap;
 
@@ -36,8 +37,10 @@
 import com.google.gerrit.acceptance.GitUtil;
 import com.google.gerrit.acceptance.PushOneCommit;
 import com.google.gerrit.acceptance.PushOneCommit.Result;
+import com.google.gerrit.acceptance.testsuite.change.ChangeOperations;
 import com.google.gerrit.acceptance.testsuite.project.ProjectOperations;
 import com.google.gerrit.common.RawInputUtil;
+import com.google.gerrit.entities.Change;
 import com.google.gerrit.entities.Patch;
 import com.google.gerrit.entities.Permission;
 import com.google.gerrit.extensions.api.changes.FileApi;
@@ -47,6 +50,7 @@
 import com.google.gerrit.extensions.common.DiffInfo;
 import com.google.gerrit.extensions.common.FileInfo;
 import com.google.gerrit.extensions.common.WebLinkInfo;
+import com.google.gerrit.extensions.common.testing.ContentEntrySubject;
 import com.google.gerrit.extensions.restapi.BadRequestException;
 import com.google.gerrit.extensions.restapi.BinaryResult;
 import com.google.gerrit.extensions.webui.EditWebLink;
@@ -91,6 +95,7 @@
 
   @Inject private ExtensionRegistry extensionRegistry;
   @Inject private DiffOperations diffOperations;
+  @Inject private ChangeOperations changeOperations;
   @Inject private ProjectOperations projectOperations;
 
   private boolean intraline;
@@ -3034,6 +3039,102 @@
     assertThat(e).hasMessageThat().isEqualTo("edit not allowed as base");
   }
 
+  @Test
+  public void diffForAddedBinaryFile() throws Exception {
+    String imageFileName = "an_image.png";
+    byte[] imageBytes = createRgbImage(255, 0, 0);
+    Change.Id changeId =
+        changeOperations
+            .newChange()
+            .file(imageFileName)
+            .content(new String(imageBytes, UTF_8))
+            .create();
+
+    DiffInfo diffInfo = gApi.changes().id(changeId.get()).current().file(imageFileName).diff();
+
+    assertThat(diffInfo).binary().isTrue();
+    assertThat(diffInfo).content().isEmpty();
+    assertThat(diffInfo).diffHeader().contains("Binary files differ");
+    assertThat(diffInfo).metaA().isNull();
+    assertThat(diffInfo).metaB().isNotNull();
+    assertThat(diffInfo).webLinks().isNull();
+  }
+
+  @Test
+  public void diffForModifiedBinaryFile() throws Exception {
+    String imageFileName = "an_image.png";
+    byte[] imageBytes = createRgbImage(255, 0, 0);
+    Change.Id changeId1 =
+        changeOperations
+            .newChange()
+            .file(imageFileName)
+            .content(new String(imageBytes, UTF_8))
+            .create();
+
+    byte[] newImageBytes = createRgbImage(0, 255, 0);
+    Change.Id changeId2 =
+        changeOperations
+            .newChange()
+            .childOf()
+            .change(changeId1)
+            .file(imageFileName)
+            .content(new String(newImageBytes, UTF_8))
+            .create();
+
+    DiffInfo diffInfo = gApi.changes().id(changeId2.get()).current().file(imageFileName).diff();
+
+    assertThat(diffInfo).binary().isTrue();
+
+    // All fields in the contentEntry are null, except the 'skip' field. It's probably a bug that
+    // this is set for binary files.
+    ContentEntrySubject contentEntry = assertThat(diffInfo).content().onlyElement();
+    contentEntry.linesOfA().isNull();
+    contentEntry.linesOfB().isNull();
+    contentEntry.commonLines().isNull();
+
+    assertThat(diffInfo).diffHeader().contains("Binary files differ");
+    assertThat(diffInfo).metaA().isNotNull();
+    assertThat(diffInfo).metaB().isNotNull();
+    assertThat(diffInfo).webLinks().isNull();
+  }
+
+  @Test
+  public void diffForDeletedBinaryFile() throws Exception {
+    String imageFileName = "an_image.png";
+    byte[] imageBytes = createRgbImage(255, 0, 0);
+    Change.Id changeId1 =
+        changeOperations
+            .newChange()
+            .file(imageFileName)
+            .content(new String(imageBytes, UTF_8))
+            .create();
+
+    Change.Id changeId2 =
+        changeOperations
+            .newChange()
+            .childOf()
+            .change(changeId1)
+            .file(imageFileName)
+            .delete()
+            .create();
+
+    DiffInfo diffInfo = gApi.changes().id(changeId2.get()).current().file(imageFileName).diff();
+
+    assertThat(diffInfo).binary().isTrue();
+
+    // All fields in the contentEntry are null, except the 'skip' field. It's probably a bug that
+    // this is set for binary files.
+    ContentEntrySubject contentEntry = assertThat(diffInfo).content().onlyElement();
+    contentEntry.linesOfA().isNull();
+    contentEntry.linesOfB().isNull();
+    contentEntry.commonLines().isNull();
+
+    assertThat(diffInfo).diffHeader().contains("Binary files differ");
+    assertThat(diffInfo).metaA().isNotNull();
+    assertThat(diffInfo).metaB().isNull();
+    assertThat(diffInfo).webLinks().isNull();
+  }
+
   private Registration newEditWebLink() {
     EditWebLink webLink =
         new EditWebLink() {