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 org.apache.commons.io.FileUtils;
11 import org.arvados.client.api.model.Collection;
12 import org.arvados.client.common.Characters;
13 import org.arvados.client.config.ExternalConfigProvider;
14 import org.arvados.client.junit.categories.IntegrationTests;
15 import org.arvados.client.logic.collection.FileToken;
16 import org.arvados.client.test.utils.ArvadosClientIntegrationTest;
17 import org.arvados.client.test.utils.FileTestUtils;
18 import org.junit.After;
19 import org.junit.Assert;
20 import org.junit.Before;
21 import org.junit.Test;
22 import org.junit.experimental.categories.Category;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.UUID;
29 import static org.arvados.client.test.utils.FileTestUtils.FILE_DOWNLOAD_TEST_DIR;
30 import static org.arvados.client.test.utils.FileTestUtils.FILE_SPLIT_TEST_DIR;
31 import static org.arvados.client.test.utils.FileTestUtils.TEST_FILE;
32 import static org.assertj.core.api.Assertions.assertThat;
33 import static org.junit.Assert.assertEquals;
34 import static org.junit.Assert.assertTrue;
36 @Category(IntegrationTests.class)
37 public class ArvadosFacadeIntegrationTest extends ArvadosClientIntegrationTest {
40 private static final String COLLECTION_NAME = "Test collection " + UUID.randomUUID().toString();
41 private String collectionUuid;
44 public void setUp() throws Exception {
45 FileTestUtils.createDirectory(FILE_SPLIT_TEST_DIR);
46 FileTestUtils.createDirectory(FILE_DOWNLOAD_TEST_DIR);
50 public void uploadOfFileIsPerformedSuccessfully() throws Exception {
52 File file = FileTestUtils.generateFile(TEST_FILE, FileTestUtils.ONE_FOURTH_GB / 200);
55 Collection actual = FACADE.upload(Collections.singletonList(file), COLLECTION_NAME, PROJECT_UUID);
56 collectionUuid = actual.getUuid();
59 assertThat(actual.getName()).contains("Test collection");
60 assertThat(actual.getManifestText()).contains(file.length() + Characters.COLON + file.getName());
64 public void uploadOfFilesIsPerformedSuccessfully() throws Exception {
66 List<File> files = FileTestUtils.generatePredefinedFiles();
67 files.addAll(FileTestUtils.generatePredefinedFiles());
70 Collection actual = FACADE.upload(files, COLLECTION_NAME, PROJECT_UUID);
71 collectionUuid = actual.getUuid();
74 assertThat(actual.getName()).contains("Test collection");
75 files.forEach(f -> assertThat(actual.getManifestText()).contains(f.length() + Characters.COLON + f.getName().replace(" ", Characters.SPACE)));
79 public void uploadToExistingCollectionIsPerformedSuccessfully() throws Exception {
81 File file = FileTestUtils.generateFile(TEST_FILE, FileTestUtils.ONE_EIGTH_GB / 500);
82 Collection existing = createTestCollection();
85 Collection actual = FACADE.uploadToExistingCollection(Collections.singletonList(file), collectionUuid);
88 assertEquals(collectionUuid, actual.getUuid());
89 assertThat(actual.getManifestText()).contains(file.length() + Characters.COLON + file.getName());
93 public void uploadWithExternalConfigProviderWorksProperly() throws Exception {
95 ArvadosFacade facade = new ArvadosFacade(buildExternalConfig());
96 File file = FileTestUtils.generateFile(TEST_FILE, FileTestUtils.ONE_FOURTH_GB / 200);
99 Collection actual = facade.upload(Collections.singletonList(file), COLLECTION_NAME, PROJECT_UUID);
100 collectionUuid = actual.getUuid();
103 assertThat(actual.getName()).contains("Test collection");
104 assertThat(actual.getManifestText()).contains(file.length() + Characters.COLON + file.getName());
108 public void creationOfEmptyCollectionPerformedSuccesfully() {
110 String collectionName = "Empty collection " + UUID.randomUUID().toString();
113 Collection actual = FACADE.createEmptyCollection(collectionName, PROJECT_UUID);
114 collectionUuid = actual.getUuid();
117 assertEquals(collectionName, actual.getName());
118 assertEquals(PROJECT_UUID, actual.getOwnerUuid());
122 public void fileTokensAreListedFromCollection() throws Exception {
124 List<File> files = uploadTestFiles();
127 List<FileToken> actual = FACADE.listFileInfoFromCollection(collectionUuid);
130 assertEquals(files.size(), actual.size());
131 for (int i = 0; i < files.size(); i++) {
132 assertEquals(files.get(i).length(), actual.get(i).getFileSize());
137 public void downloadOfFilesPerformedSuccessfully() throws Exception {
139 List<File> files = uploadTestFiles();
140 File destination = new File(FILE_DOWNLOAD_TEST_DIR + Characters.SLASH + collectionUuid);
143 List<File> actual = FACADE.downloadCollectionFiles(collectionUuid, FILE_DOWNLOAD_TEST_DIR, false);
146 assertEquals(files.size(), actual.size());
147 assertTrue(destination.exists());
148 assertThat(actual).allMatch(File::exists);
149 for (int i = 0; i < files.size(); i++) {
150 assertEquals(files.get(i).length(), actual.get(i).length());
155 public void downloadOfFilesPerformedSuccessfullyUsingKeepWeb() throws Exception {
157 List<File> files = uploadTestFiles();
158 File destination = new File(FILE_DOWNLOAD_TEST_DIR + Characters.SLASH + collectionUuid);
161 List<File> actual = FACADE.downloadCollectionFiles(collectionUuid, FILE_DOWNLOAD_TEST_DIR, true);
164 assertEquals(files.size(), actual.size());
165 assertTrue(destination.exists());
166 assertThat(actual).allMatch(File::exists);
167 for (int i = 0; i < files.size(); i++) {
168 assertEquals(files.get(i).length(), actual.get(i).length());
173 public void singleFileIsDownloadedSuccessfullyUsingKeepWeb() throws Exception {
175 File file = uploadSingleTestFile(false);
178 File actual = FACADE.downloadFile(file.getName(), collectionUuid, FILE_DOWNLOAD_TEST_DIR);
181 assertThat(actual).exists();
182 assertThat(actual.length()).isEqualTo(file.length());
186 public void downloadOfOneFileSplittedToMultipleLocatorsPerformedSuccesfully() throws Exception {
188 File file = uploadSingleTestFile(true);
190 List<File> actual = FACADE.downloadCollectionFiles(collectionUuid, FILE_DOWNLOAD_TEST_DIR, false);
192 Assert.assertEquals(1, actual.size());
193 assertThat(actual.get(0).length()).isEqualTo(file.length());
197 public void downloadWithExternalConfigProviderWorksProperly() throws Exception {
199 ArvadosFacade facade = new ArvadosFacade(buildExternalConfig());
200 List<File> files = uploadTestFiles();
202 List<File> actual = facade.downloadCollectionFiles(collectionUuid, FILE_DOWNLOAD_TEST_DIR, false);
205 assertEquals(files.size(), actual.size());
206 assertThat(actual).allMatch(File::exists);
207 for (int i = 0; i < files.size(); i++) {
208 assertEquals(files.get(i).length(), actual.get(i).length());
212 private ExternalConfigProvider buildExternalConfig() {
213 return ExternalConfigProvider
215 .apiHostInsecure(CONFIG.isApiHostInsecure())
216 .keepWebHost(CONFIG.getKeepWebHost())
217 .keepWebPort(CONFIG.getKeepWebPort())
218 .apiHost(CONFIG.getApiHost())
219 .apiPort(CONFIG.getApiPort())
220 .apiToken(CONFIG.getApiToken())
221 .apiProtocol(CONFIG.getApiProtocol())
222 .fileSplitSize(CONFIG.getFileSplitSize())
223 .fileSplitDirectory(CONFIG.getFileSplitDirectory())
224 .numberOfCopies(CONFIG.getNumberOfCopies())
225 .numberOfRetries(CONFIG.getNumberOfRetries())
226 .connectTimeout(CONFIG.getConnectTimeout())
227 .readTimeout(CONFIG.getReadTimeout())
228 .writeTimeout(CONFIG.getWriteTimeout())
232 private Collection createTestCollection() {
233 Collection collection = FACADE.createEmptyCollection(COLLECTION_NAME, PROJECT_UUID);
234 collectionUuid = collection.getUuid();
238 private List<File> uploadTestFiles() throws Exception{
239 createTestCollection();
240 List<File> files = FileTestUtils.generatePredefinedFiles();
241 FACADE.uploadToExistingCollection(files, collectionUuid);
245 private File uploadSingleTestFile(boolean bigFile) throws Exception{
246 createTestCollection();
247 Long fileSize = bigFile ? FileUtils.ONE_MB * 70 : FileTestUtils.ONE_EIGTH_GB / 100;
248 File file = FileTestUtils.generateFile(TEST_FILE, fileSize);
249 FACADE.uploadToExistingCollection(Collections.singletonList(file), collectionUuid);
254 public void tearDown() throws Exception {
255 FileTestUtils.cleanDirectory(FILE_SPLIT_TEST_DIR);
256 FileTestUtils.cleanDirectory(FILE_DOWNLOAD_TEST_DIR);
258 if(collectionUuid != null)
259 FACADE.deleteCollection(collectionUuid);