2 * Copyright (C) The Arvados Authors. All rights reserved.
4 * SPDX-License-Identifier: AGPL-3.0 OR Apache-2.0
8 package org.arvados.client.facade;
10 import com.fasterxml.jackson.databind.ObjectMapper;
11 import com.fasterxml.jackson.databind.ObjectWriter;
12 import okhttp3.mockwebserver.MockResponse;
14 import org.apache.commons.io.FileUtils;
15 import org.arvados.client.api.model.Collection;
16 import org.arvados.client.api.model.KeepService;
17 import org.arvados.client.api.model.KeepServiceList;
18 import org.arvados.client.common.Characters;
19 import org.arvados.client.test.utils.ArvadosClientMockedWebServerTest;
20 import org.arvados.client.test.utils.FileTestUtils;
21 import org.junit.After;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.junit.Ignore;
27 import java.nio.charset.Charset;
28 import java.nio.file.Files;
29 import java.util.ArrayList;
30 import java.util.Arrays;
31 import java.util.List;
32 import java.util.stream.Collectors;
34 import static org.arvados.client.test.utils.ApiClientTestUtils.getResponse;
35 import static org.arvados.client.test.utils.FileTestUtils.*;
36 import static org.assertj.core.api.Assertions.assertThat;
37 import static org.junit.Assert.assertEquals;
38 import static org.junit.Assert.assertTrue;
40 public class ArvadosFacadeTest extends ArvadosClientMockedWebServerTest {
42 ArvadosFacade facade = new ArvadosFacade(CONFIG);
45 public void setUp() throws Exception {
46 FileTestUtils.createDirectory(FILE_SPLIT_TEST_DIR);
47 FileTestUtils.createDirectory(FILE_DOWNLOAD_TEST_DIR);
51 @Ignore("Failing test #15041")
52 public void uploadIsPerformedSuccessfullyUsingDiskOnlyKeepServices() throws Exception {
55 String keepServicesAccessible = setMockedServerPortToKeepServices("keep-services-accessible-disk-only");
56 server.enqueue(new MockResponse().setBody(keepServicesAccessible));
58 String blockLocator = "7df44272090cee6c0732382bba415ee9";
59 String signedBlockLocator = blockLocator + "+70+A189a93acda6e1fba18a9dffd42b6591cbd36d55d@5a1c17b6";
60 for (int i = 0; i < 8; i++) {
61 server.enqueue(new MockResponse().setBody(signedBlockLocator));
63 server.enqueue(getResponse("users-get"));
64 server.enqueue(getResponse("collections-create-manifest"));
66 FileTestUtils.generateFile(TEST_FILE, FileTestUtils.ONE_FOURTH_GB);
69 Collection actual = facade.upload(Arrays.asList(new File(TEST_FILE)), "Super Collection", null);
72 assertThat(actual.getName()).contains("Super Collection");
76 public void uploadIsPerformedSuccessfully() throws Exception {
79 String keepServicesAccessible = setMockedServerPortToKeepServices("keep-services-accessible");
80 server.enqueue(new MockResponse().setBody(keepServicesAccessible));
82 String blockLocator = "7df44272090cee6c0732382bba415ee9";
83 String signedBlockLocator = blockLocator + "+70+A189a93acda6e1fba18a9dffd42b6591cbd36d55d@5a1c17b6";
84 for (int i = 0; i < 4; i++) {
85 server.enqueue(new MockResponse().setBody(signedBlockLocator));
87 server.enqueue(getResponse("users-get"));
88 server.enqueue(getResponse("collections-create-manifest"));
90 FileTestUtils.generateFile(TEST_FILE, FileTestUtils.ONE_FOURTH_GB);
93 Collection actual = facade.upload(Arrays.asList(new File(TEST_FILE)), "Super Collection", null);
96 assertThat(actual.getName()).contains("Super Collection");
100 public void downloadOfWholeCollectionIsPerformedSuccessfully() throws Exception {
103 String collectionUuid = "ardev-4zz18-jk5vo4uo9u5vj52";
104 server.enqueue(getResponse("collections-download-file"));
106 String keepServicesAccessible = setMockedServerPortToKeepServices("keep-services-accessible");
107 server.enqueue(new MockResponse().setBody(keepServicesAccessible));
108 File collectionDestination = new File(FILE_DOWNLOAD_TEST_DIR + Characters.SLASH + collectionUuid);
110 List<File> files = generatePredefinedFiles();
111 List<byte[]> fileData = new ArrayList<>();
112 for (File f : files) {
113 fileData.add(Files.readAllBytes(f.toPath()));
115 byte[] filesDataChunk = fileData.stream().reduce(new byte[0], this::addAll);
117 server.enqueue(new MockResponse().setBody(new Buffer().write(filesDataChunk)));
120 List<File> downloadedFiles = facade.downloadCollectionFiles(collectionUuid, FILE_DOWNLOAD_TEST_DIR, false);
123 assertEquals(3, downloadedFiles.size());
124 assertTrue(collectionDestination.exists());
125 assertThat(downloadedFiles).allMatch(File::exists);
126 assertEquals(files.stream().map(File::getName).collect(Collectors.toList()), downloadedFiles.stream().map(File::getName).collect(Collectors.toList()));
127 assertEquals(files.stream().map(File::length).collect(Collectors.toList()), downloadedFiles.stream().map(File::length).collect(Collectors.toList()));
131 public void downloadOfWholeCollectionUsingKeepWebPerformedSuccessfully() throws Exception {
134 String collectionUuid = "ardev-4zz18-jk5vo4uo9u5vj52";
135 server.enqueue(getResponse("collections-download-file"));
137 List<File> files = generatePredefinedFiles();
138 for (File f : files) {
139 server.enqueue(new MockResponse().setBody(new Buffer().write(FileUtils.readFileToByteArray(f))));
143 List<File> downloadedFiles = facade.downloadCollectionFiles(collectionUuid, FILE_DOWNLOAD_TEST_DIR, true);
146 assertEquals(3, downloadedFiles.size());
147 assertThat(downloadedFiles).allMatch(File::exists);
148 assertEquals(files.stream().map(File::getName).collect(Collectors.toList()), downloadedFiles.stream().map(File::getName).collect(Collectors.toList()));
149 assertTrue(downloadedFiles.stream().map(File::length).collect(Collectors.toList()).containsAll(files.stream().map(File::length).collect(Collectors.toList())));
153 public void downloadOfSingleFilePerformedSuccessfully() throws Exception {
156 String collectionUuid = "ardev-4zz18-jk5vo4uo9u5vj52";
157 server.enqueue(getResponse("collections-download-file"));
159 File file = generatePredefinedFiles().get(0);
160 byte[] fileData = FileUtils.readFileToByteArray(file);
161 server.enqueue(new MockResponse().setBody(new Buffer().write(fileData)));
164 File downloadedFile = facade.downloadFile(file.getName(), collectionUuid, FILE_DOWNLOAD_TEST_DIR);
167 assertTrue(downloadedFile.exists());
168 assertEquals(file.getName(), downloadedFile.getName());
169 assertEquals(file.length(), downloadedFile.length());
172 private String setMockedServerPortToKeepServices(String jsonPath) throws Exception {
174 ObjectMapper mapper = new ObjectMapper().findAndRegisterModules();
175 String filePath = String.format("src/test/resources/org/arvados/client/api/client/%s.json", jsonPath);
176 File jsonFile = new File(filePath);
177 String json = FileUtils.readFileToString(jsonFile, Charset.defaultCharset());
178 KeepServiceList keepServiceList = mapper.readValue(json, KeepServiceList.class);
179 List<KeepService> items = keepServiceList.getItems();
180 for (KeepService keepService : items) {
181 keepService.setServicePort(server.getPort());
183 ObjectWriter writer = mapper.writer().withDefaultPrettyPrinter();
184 return writer.writeValueAsString(keepServiceList);
187 //Method to copy multiple byte[] arrays into one byte[] array
188 private byte[] addAll(byte[] array1, byte[] array2) {
189 byte[] joinedArray = new byte[array1.length + array2.length];
190 System.arraycopy(array1, 0, joinedArray, 0, array1.length);
191 System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
196 public void tearDown() throws Exception {
197 FileTestUtils.cleanDirectory(FILE_SPLIT_TEST_DIR);
198 FileTestUtils.cleanDirectory(FILE_DOWNLOAD_TEST_DIR);