Download file with resume
[arvados.git] / sdk / java-v2 / src / test / java / org / arvados / client / api / client / KeepWebApiClientTest.java
index e796c690321c846060ceccbd4278d986516c70d1..9b6b4fa17fe094f55935004414bcf9ddbb5d75d7 100644 (file)
@@ -10,7 +10,10 @@ package org.arvados.client.api.client;
 import org.arvados.client.test.utils.ArvadosClientMockedWebServerTest;
 import org.junit.Test;
 
+import java.io.ByteArrayOutputStream;
 import java.io.File;
+import java.io.IOException;
+import java.io.InputStream;
 import java.nio.file.Files;
 
 import okhttp3.mockwebserver.MockResponse;
@@ -18,7 +21,7 @@ import okio.Buffer;
 
 import static org.arvados.client.test.utils.ApiClientTestUtils.getResponse;
 import static org.assertj.core.api.Assertions.assertThat;
-import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertArrayEquals;
 import static org.junit.Assert.assertNotNull;
 
 public class KeepWebApiClientTest extends ArvadosClientMockedWebServerTest {
@@ -46,7 +49,8 @@ public class KeepWebApiClientTest extends ArvadosClientMockedWebServerTest {
         // given
         String collectionUuid = "some-collection-uuid";
         String filePathName = "sample-file-path";
-        long offset = 1024;
+        long start = 1024;
+        Long end = null;
 
         byte[] expectedData = "test data".getBytes();
 
@@ -54,12 +58,24 @@ public class KeepWebApiClientTest extends ArvadosClientMockedWebServerTest {
             server.enqueue(new MockResponse().setBody(buffer));
 
             // when
-            byte[] actualData = client.downloadPartial(collectionUuid, filePathName, offset);
+            InputStream inputStream = client.get(collectionUuid, filePathName, start, end);
+            byte[] actualData = inputStreamToByteArray(inputStream);
 
             // then
             assertNotNull(actualData);
-            assertEquals(new String(expectedData), new String(actualData));
+            assertArrayEquals(expectedData, actualData);
         }
     }
 
+    private byte[] inputStreamToByteArray(InputStream inputStream) throws IOException {
+        ByteArrayOutputStream buffer = new ByteArrayOutputStream();
+        int nRead;
+        byte[] data = new byte[1024];
+        while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
+            buffer.write(data, 0, nRead);
+        }
+        buffer.flush();
+        return buffer.toByteArray();
+    }
+
 }