Tweak test script no issue #
[arvados.git] / src / test / java / org / arvados / client / facade / ArvadosFacadeTest.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 com.fasterxml.jackson.databind.ObjectMapper;
11 import com.fasterxml.jackson.databind.ObjectWriter;
12 import okhttp3.mockwebserver.MockResponse;
13 import okio.Buffer;
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
25 import java.io.File;
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;
32
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;
38
39 public class ArvadosFacadeTest extends ArvadosClientMockedWebServerTest {
40
41     ArvadosFacade facade = new ArvadosFacade(CONFIG);
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 uploadIsPerformedSuccessfullyUsingDiskOnlyKeepServices() throws Exception {
51
52         // given
53         String keepServicesAccessible = setMockedServerPortToKeepServices("keep-services-accessible-disk-only");
54         server.enqueue(new MockResponse().setBody(keepServicesAccessible));
55
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));
60         }
61         server.enqueue(getResponse("users-get"));
62         server.enqueue(getResponse("collections-create-manifest"));
63
64         FileTestUtils.generateFile(TEST_FILE, FileTestUtils.ONE_FOURTH_GB);
65
66         // when
67         Collection actual = facade.upload(Arrays.asList(new File(TEST_FILE)), "Super Collection", null);
68
69         // then
70         assertThat(actual.getName()).contains("Super Collection");
71     }
72
73     @Test
74     public void uploadIsPerformedSuccessfully() throws Exception {
75
76         // given
77         String keepServicesAccessible = setMockedServerPortToKeepServices("keep-services-accessible");
78         server.enqueue(new MockResponse().setBody(keepServicesAccessible));
79
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));
84         }
85         server.enqueue(getResponse("users-get"));
86         server.enqueue(getResponse("collections-create-manifest"));
87
88         FileTestUtils.generateFile(TEST_FILE, FileTestUtils.ONE_FOURTH_GB);
89
90         // when
91         Collection actual = facade.upload(Arrays.asList(new File(TEST_FILE)), "Super Collection", null);
92
93         // then
94         assertThat(actual.getName()).contains("Super Collection");
95     }
96
97     @Test
98     public void downloadOfWholeCollectionIsPerformedSuccessfully() throws Exception {
99
100         //given
101         String collectionUuid = "ardev-4zz18-jk5vo4uo9u5vj52";
102         server.enqueue(getResponse("collections-download-file"));
103
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);
107
108         List<File> files = generatePredefinedFiles();
109         List<byte[]> fileData = new ArrayList<>();
110         for (File f : files) {
111             fileData.add(Files.readAllBytes(f.toPath()));
112         }
113         byte[] filesDataChunk = fileData.stream().reduce(new byte[0], this::addAll);
114
115         server.enqueue(new MockResponse().setBody(new Buffer().write(filesDataChunk)));
116
117         //when
118         List<File> downloadedFiles = facade.downloadCollectionFiles(collectionUuid, FILE_DOWNLOAD_TEST_DIR, false);
119
120         //then
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()));
126     }
127
128     @Test
129     public void downloadOfWholeCollectionUsingKeepWebPerformedSuccessfully() throws Exception {
130
131         //given
132         String collectionUuid = "ardev-4zz18-jk5vo4uo9u5vj52";
133         server.enqueue(getResponse("collections-download-file"));
134
135         List<File> files = generatePredefinedFiles();
136         for (File f : files) {
137             server.enqueue(new MockResponse().setBody(new Buffer().write(FileUtils.readFileToByteArray(f))));
138         }
139
140         //when
141         List<File> downloadedFiles = facade.downloadCollectionFiles(collectionUuid, FILE_DOWNLOAD_TEST_DIR, true);
142
143         //then
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())));
148     }
149
150     @Test
151     public void downloadOfSingleFilePerformedSuccessfully() throws Exception {
152
153         //given
154         String collectionUuid = "ardev-4zz18-jk5vo4uo9u5vj52";
155         server.enqueue(getResponse("collections-download-file"));
156
157         File file = generatePredefinedFiles().get(0);
158         byte[] fileData = FileUtils.readFileToByteArray(file);
159         server.enqueue(new MockResponse().setBody(new Buffer().write(fileData)));
160
161         //when
162         File downloadedFile = facade.downloadFile(file.getName(), collectionUuid, FILE_DOWNLOAD_TEST_DIR);
163
164         //then
165         assertTrue(downloadedFile.exists());
166         assertEquals(file.getName(), downloadedFile.getName());
167         assertEquals(file.length(), downloadedFile.length());
168     }
169
170     private String setMockedServerPortToKeepServices(String jsonPath) throws Exception {
171
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());
180         }
181         ObjectWriter writer = mapper.writer().withDefaultPrettyPrinter();
182         return writer.writeValueAsString(keepServiceList);
183     }
184
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);
190         return joinedArray;
191     }
192
193     @After
194     public void tearDown() throws Exception {
195         FileTestUtils.cleanDirectory(FILE_SPLIT_TEST_DIR);
196         FileTestUtils.cleanDirectory(FILE_DOWNLOAD_TEST_DIR);
197     }
198 }