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;
26 import java.nio.charset.Charset;
27 import java.nio.file.Files;
28 import java.util.ArrayList;
29 import java.util.Arrays;
30 import java.util.List;
31 import java.util.stream.Collectors;
33 import static org.arvados.client.test.utils.ApiClientTestUtils.getResponse;
34 import static org.arvados.client.test.utils.FileTestUtils.*;
35 import static org.assertj.core.api.Assertions.assertThat;
36 import static org.junit.Assert.assertEquals;
37 import static org.junit.Assert.assertTrue;
39 public class ArvadosFacadeTest extends ArvadosClientMockedWebServerTest {
41 ArvadosFacade facade = new ArvadosFacade(CONFIG);
44 public void setUp() throws Exception {
45 FileTestUtils.createDirectory(FILE_SPLIT_TEST_DIR);
46 FileTestUtils.createDirectory(FILE_DOWNLOAD_TEST_DIR);
50 public void uploadIsPerformedSuccessfullyUsingDiskOnlyKeepServices() throws Exception {
53 String keepServicesAccessible = setMockedServerPortToKeepServices("keep-services-accessible-disk-only");
54 server.enqueue(new MockResponse().setBody(keepServicesAccessible));
56 String blockLocator = "7df44272090cee6c0732382bba415ee9";
57 String signedBlockLocator = blockLocator + "+70+A189a93acda6e1fba18a9dffd42b6591cbd36d55d@5a1c17b6";
58 for (int i = 0; i < 8; i++) {
59 server.enqueue(new MockResponse().setBody(signedBlockLocator));
61 server.enqueue(getResponse("users-get"));
62 server.enqueue(getResponse("collections-create-manifest"));
64 FileTestUtils.generateFile(TEST_FILE, FileTestUtils.ONE_FOURTH_GB);
67 Collection actual = facade.upload(Arrays.asList(new File(TEST_FILE)), "Super Collection", null);
70 assertThat(actual.getName()).contains("Super Collection");
74 public void uploadIsPerformedSuccessfully() throws Exception {
77 String keepServicesAccessible = setMockedServerPortToKeepServices("keep-services-accessible");
78 server.enqueue(new MockResponse().setBody(keepServicesAccessible));
80 String blockLocator = "7df44272090cee6c0732382bba415ee9";
81 String signedBlockLocator = blockLocator + "+70+A189a93acda6e1fba18a9dffd42b6591cbd36d55d@5a1c17b6";
82 for (int i = 0; i < 4; i++) {
83 server.enqueue(new MockResponse().setBody(signedBlockLocator));
85 server.enqueue(getResponse("users-get"));
86 server.enqueue(getResponse("collections-create-manifest"));
88 FileTestUtils.generateFile(TEST_FILE, FileTestUtils.ONE_FOURTH_GB);
91 Collection actual = facade.upload(Arrays.asList(new File(TEST_FILE)), "Super Collection", null);
94 assertThat(actual.getName()).contains("Super Collection");
98 public void downloadOfWholeCollectionIsPerformedSuccessfully() throws Exception {
101 String collectionUuid = "ardev-4zz18-jk5vo4uo9u5vj52";
102 server.enqueue(getResponse("collections-download-file"));
104 String keepServicesAccessible = setMockedServerPortToKeepServices("keep-services-accessible");
105 server.enqueue(new MockResponse().setBody(keepServicesAccessible));
106 File collectionDestination = new File(FILE_DOWNLOAD_TEST_DIR + Characters.SLASH + collectionUuid);
108 List<File> files = generatePredefinedFiles();
109 List<byte[]> fileData = new ArrayList<>();
110 for (File f : files) {
111 fileData.add(Files.readAllBytes(f.toPath()));
113 byte[] filesDataChunk = fileData.stream().reduce(new byte[0], this::addAll);
115 server.enqueue(new MockResponse().setBody(new Buffer().write(filesDataChunk)));
118 List<File> downloadedFiles = facade.downloadCollectionFiles(collectionUuid, FILE_DOWNLOAD_TEST_DIR, false);
121 assertEquals(3, downloadedFiles.size());
122 assertTrue(collectionDestination.exists());
123 assertThat(downloadedFiles).allMatch(File::exists);
124 assertEquals(files.stream().map(File::getName).collect(Collectors.toList()), downloadedFiles.stream().map(File::getName).collect(Collectors.toList()));
125 assertEquals(files.stream().map(File::length).collect(Collectors.toList()), downloadedFiles.stream().map(File::length).collect(Collectors.toList()));
129 public void downloadOfWholeCollectionUsingKeepWebPerformedSuccessfully() throws Exception {
132 String collectionUuid = "ardev-4zz18-jk5vo4uo9u5vj52";
133 server.enqueue(getResponse("collections-download-file"));
135 List<File> files = generatePredefinedFiles();
136 for (File f : files) {
137 server.enqueue(new MockResponse().setBody(new Buffer().write(FileUtils.readFileToByteArray(f))));
141 List<File> downloadedFiles = facade.downloadCollectionFiles(collectionUuid, FILE_DOWNLOAD_TEST_DIR, true);
144 assertEquals(3, downloadedFiles.size());
145 assertThat(downloadedFiles).allMatch(File::exists);
146 assertEquals(files.stream().map(File::getName).collect(Collectors.toList()), downloadedFiles.stream().map(File::getName).collect(Collectors.toList()));
147 assertTrue(downloadedFiles.stream().map(File::length).collect(Collectors.toList()).containsAll(files.stream().map(File::length).collect(Collectors.toList())));
151 public void downloadOfSingleFilePerformedSuccessfully() throws Exception {
154 String collectionUuid = "ardev-4zz18-jk5vo4uo9u5vj52";
155 server.enqueue(getResponse("collections-download-file"));
157 File file = generatePredefinedFiles().get(0);
158 byte[] fileData = FileUtils.readFileToByteArray(file);
159 server.enqueue(new MockResponse().setBody(new Buffer().write(fileData)));
162 File downloadedFile = facade.downloadFile(file.getName(), collectionUuid, FILE_DOWNLOAD_TEST_DIR);
165 assertTrue(downloadedFile.exists());
166 assertEquals(file.getName(), downloadedFile.getName());
167 assertEquals(file.length(), downloadedFile.length());
170 private String setMockedServerPortToKeepServices(String jsonPath) throws Exception {
172 ObjectMapper mapper = new ObjectMapper().findAndRegisterModules();
173 String filePath = String.format("src/test/resources/org/arvados/client/api/client/%s.json", jsonPath);
174 File jsonFile = new File(filePath);
175 String json = FileUtils.readFileToString(jsonFile, Charset.defaultCharset());
176 KeepServiceList keepServiceList = mapper.readValue(json, KeepServiceList.class);
177 List<KeepService> items = keepServiceList.getItems();
178 for (KeepService keepService : items) {
179 keepService.setServicePort(server.getPort());
181 ObjectWriter writer = mapper.writer().withDefaultPrettyPrinter();
182 return writer.writeValueAsString(keepServiceList);
185 //Method to copy multiple byte[] arrays into one byte[] array
186 private byte[] addAll(byte[] array1, byte[] array2) {
187 byte[] joinedArray = new byte[array1.length + array2.length];
188 System.arraycopy(array1, 0, joinedArray, 0, array1.length);
189 System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
194 public void tearDown() throws Exception {
195 FileTestUtils.cleanDirectory(FILE_SPLIT_TEST_DIR);
196 FileTestUtils.cleanDirectory(FILE_DOWNLOAD_TEST_DIR);