Add timeouts as configuration parameters
[arvados.git] / sdk / java-v2 / src / test / java / org / arvados / client / facade / ArvadosFacadeIntegrationTest.java
1 /*
2  * Copyright (C) The Arvados Authors. All rights reserved.
3  *
4  * SPDX-License-Identifier: AGPL-3.0 OR Apache-2.0
5  *
6  */
7
8 package org.arvados.client.facade;
9
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;
23
24 import java.io.File;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.UUID;
28
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;
35
36 @Category(IntegrationTests.class)
37 public class ArvadosFacadeIntegrationTest extends ArvadosClientIntegrationTest {
38
39
40     private static final String COLLECTION_NAME = "Test collection " + UUID.randomUUID().toString();
41     private String collectionUuid;
42
43     @Before
44     public void setUp() throws Exception {
45         FileTestUtils.createDirectory(FILE_SPLIT_TEST_DIR);
46         FileTestUtils.createDirectory(FILE_DOWNLOAD_TEST_DIR);
47     }
48
49     @Test
50     public void uploadOfFileIsPerformedSuccessfully() throws Exception {
51         // given
52         File file = FileTestUtils.generateFile(TEST_FILE, FileTestUtils.ONE_FOURTH_GB / 200);
53
54         // when
55         Collection actual = FACADE.upload(Collections.singletonList(file), COLLECTION_NAME, PROJECT_UUID);
56         collectionUuid = actual.getUuid();
57
58         // then
59         assertThat(actual.getName()).contains("Test collection");
60         assertThat(actual.getManifestText()).contains(file.length() + Characters.COLON + file.getName());
61     }
62
63     @Test
64     public void uploadOfFilesIsPerformedSuccessfully() throws Exception {
65         // given
66         List<File> files = FileTestUtils.generatePredefinedFiles();
67         files.addAll(FileTestUtils.generatePredefinedFiles());
68
69         // when
70         Collection actual = FACADE.upload(files, COLLECTION_NAME, PROJECT_UUID);
71         collectionUuid = actual.getUuid();
72
73         // then
74         assertThat(actual.getName()).contains("Test collection");
75         files.forEach(f -> assertThat(actual.getManifestText()).contains(f.length() + Characters.COLON + f.getName().replace(" ", Characters.SPACE)));
76     }
77
78     @Test
79     public void uploadToExistingCollectionIsPerformedSuccessfully() throws Exception {
80         // given
81         File file = FileTestUtils.generateFile(TEST_FILE, FileTestUtils.ONE_EIGTH_GB / 500);
82         Collection existing = createTestCollection();
83
84         // when
85         Collection actual = FACADE.uploadToExistingCollection(Collections.singletonList(file), collectionUuid);
86
87         // then
88         assertEquals(collectionUuid, actual.getUuid());
89         assertThat(actual.getManifestText()).contains(file.length() + Characters.COLON + file.getName());
90     }
91
92     @Test
93     public void uploadWithExternalConfigProviderWorksProperly() throws Exception {
94         //given
95         ArvadosFacade facade = new ArvadosFacade(buildExternalConfig());
96         File file = FileTestUtils.generateFile(TEST_FILE, FileTestUtils.ONE_FOURTH_GB / 200);
97
98         //when
99         Collection actual = facade.upload(Collections.singletonList(file), COLLECTION_NAME, PROJECT_UUID);
100         collectionUuid = actual.getUuid();
101
102         //then
103         assertThat(actual.getName()).contains("Test collection");
104         assertThat(actual.getManifestText()).contains(file.length() + Characters.COLON + file.getName());
105     }
106
107     @Test
108     public void creationOfEmptyCollectionPerformedSuccesfully() {
109         // given
110         String collectionName = "Empty collection " + UUID.randomUUID().toString();
111
112         // when
113         Collection actual = FACADE.createEmptyCollection(collectionName, PROJECT_UUID);
114         collectionUuid = actual.getUuid();
115
116         // then
117         assertEquals(collectionName, actual.getName());
118         assertEquals(PROJECT_UUID, actual.getOwnerUuid());
119     }
120
121     @Test
122     public void fileTokensAreListedFromCollection() throws Exception {
123         //given
124         List<File> files = uploadTestFiles();
125
126         //when
127         List<FileToken> actual = FACADE.listFileInfoFromCollection(collectionUuid);
128
129         //then
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());
133         }
134     }
135
136     @Test
137     public void downloadOfFilesPerformedSuccessfully() throws Exception {
138         //given
139         List<File> files = uploadTestFiles();
140         File destination = new File(FILE_DOWNLOAD_TEST_DIR + Characters.SLASH + collectionUuid);
141
142         //when
143         List<File> actual = FACADE.downloadCollectionFiles(collectionUuid, FILE_DOWNLOAD_TEST_DIR, false);
144
145         //then
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());
151         }
152     }
153
154     @Test
155     public void downloadOfFilesPerformedSuccessfullyUsingKeepWeb() throws Exception {
156         //given
157         List<File> files = uploadTestFiles();
158         File destination = new File(FILE_DOWNLOAD_TEST_DIR + Characters.SLASH + collectionUuid);
159
160         //when
161         List<File> actual = FACADE.downloadCollectionFiles(collectionUuid, FILE_DOWNLOAD_TEST_DIR, true);
162
163         //then
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());
169         }
170     }
171
172     @Test
173     public void singleFileIsDownloadedSuccessfullyUsingKeepWeb() throws Exception {
174         //given
175         File file = uploadSingleTestFile(false);
176
177         //when
178         File actual = FACADE.downloadFile(file.getName(), collectionUuid, FILE_DOWNLOAD_TEST_DIR);
179
180         //then
181         assertThat(actual).exists();
182         assertThat(actual.length()).isEqualTo(file.length());
183     }
184
185     @Test
186     public void downloadOfOneFileSplittedToMultipleLocatorsPerformedSuccesfully() throws Exception {
187         //given
188         File file = uploadSingleTestFile(true);
189
190         List<File> actual = FACADE.downloadCollectionFiles(collectionUuid, FILE_DOWNLOAD_TEST_DIR, false);
191
192         Assert.assertEquals(1, actual.size());
193         assertThat(actual.get(0).length()).isEqualTo(file.length());
194     }
195
196     @Test
197     public void downloadWithExternalConfigProviderWorksProperly() throws Exception {
198         //given
199         ArvadosFacade facade = new ArvadosFacade(buildExternalConfig());
200         List<File> files = uploadTestFiles();
201         //when
202         List<File> actual = facade.downloadCollectionFiles(collectionUuid, FILE_DOWNLOAD_TEST_DIR, false);
203
204         //then
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());
209         }
210     }
211
212     private ExternalConfigProvider buildExternalConfig() {
213         return ExternalConfigProvider
214                 .builder()
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())
229                 .build();
230     }
231
232     private Collection createTestCollection() {
233         Collection collection = FACADE.createEmptyCollection(COLLECTION_NAME, PROJECT_UUID);
234         collectionUuid = collection.getUuid();
235         return collection;
236     }
237
238     private List<File> uploadTestFiles() throws Exception{
239         createTestCollection();
240         List<File> files = FileTestUtils.generatePredefinedFiles();
241         FACADE.uploadToExistingCollection(files, collectionUuid);
242         return files;
243     }
244
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);
250         return file;
251     }
252
253     @After
254     public void tearDown() throws Exception {
255         FileTestUtils.cleanDirectory(FILE_SPLIT_TEST_DIR);
256         FileTestUtils.cleanDirectory(FILE_DOWNLOAD_TEST_DIR);
257
258         if(collectionUuid != null)
259         FACADE.deleteCollection(collectionUuid);
260     }
261 }