Skip to content

Commit 18f9eb6

Browse files
committed
Add tests.
1 parent ded873b commit 18f9eb6

File tree

2 files changed

+83
-52
lines changed

2 files changed

+83
-52
lines changed

hub/src/test/java/cloud/katta/testsetup/HubTestUtilities.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,29 @@
44

55
package cloud.katta.testsetup;
66

7+
import ch.cyberduck.core.DisabledConnectionCallback;
8+
import ch.cyberduck.core.Path;
79
import ch.cyberduck.core.PreferencesUseragentProvider;
10+
import ch.cyberduck.core.Session;
11+
import ch.cyberduck.core.exception.BackgroundException;
12+
import ch.cyberduck.core.features.Bulk;
13+
import ch.cyberduck.core.features.Read;
14+
import ch.cyberduck.core.features.Write;
815
import ch.cyberduck.core.http.UserAgentHttpRequestInitializer;
16+
import ch.cyberduck.core.io.StatusOutputStream;
917
import ch.cyberduck.core.oauth.OAuth2AuthorizationService;
18+
import ch.cyberduck.core.transfer.Transfer;
19+
import ch.cyberduck.core.transfer.TransferItem;
20+
import ch.cyberduck.core.transfer.TransferStatus;
1021

22+
import org.apache.commons.io.IOUtils;
23+
24+
import java.io.ByteArrayInputStream;
25+
import java.io.ByteArrayOutputStream;
1126
import java.io.IOException;
27+
import java.io.InputStream;
1228
import java.net.URI;
29+
import java.util.Collections;
1330
import java.util.List;
1431
import java.util.Map;
1532

@@ -42,4 +59,27 @@ protected void updateParamsForAuth(final String[] authNames, final List<Pair> qu
4259
};
4360
return adminApiClient.setBasePath(setup.hubURL);
4461
}
62+
63+
public static byte[] write(final Session<?> session, final Path file, final byte[] content) throws BackgroundException, IOException {
64+
final TransferStatus status = new TransferStatus()
65+
.setLength(content.length);
66+
status.setChecksum(session.getFeature(Write.class).checksum(file, status).compute(new ByteArrayInputStream(content), status));
67+
session.getFeature(Bulk.class).pre(Transfer.Type.upload, Collections.singletonMap(new TransferItem(file), status), new DisabledConnectionCallback());
68+
try (final StatusOutputStream<?> out = session.getFeature(Write.class).write(file, status, new DisabledConnectionCallback())) {
69+
IOUtils.copyLarge(new ByteArrayInputStream(content), out);
70+
}
71+
return content;
72+
}
73+
74+
public static byte[] read(final Session<?> session, final Path file, final int length) throws BackgroundException, IOException {
75+
final TransferStatus status = new TransferStatus()
76+
.setRemote(file.attributes())
77+
.setLength(length);
78+
session.getFeature(Bulk.class).pre(Transfer.Type.download, Collections.singletonMap(new TransferItem(file), status), new DisabledConnectionCallback());
79+
final ByteArrayOutputStream buffer = new ByteArrayOutputStream(length);
80+
try (final InputStream in = session.getFeature(Read.class).read(file, status, new DisabledConnectionCallback())) {
81+
IOUtils.copyLarge(in, buffer);
82+
}
83+
return buffer.toByteArray();
84+
}
4585
}

hub/src/test/java/cloud/katta/workflows/AbstractHubSynchronizeTest.java

Lines changed: 43 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -9,33 +9,26 @@
99
import ch.cyberduck.core.DisabledConnectionCallback;
1010
import ch.cyberduck.core.DisabledListProgressListener;
1111
import ch.cyberduck.core.DisabledLoginCallback;
12+
import ch.cyberduck.core.DisabledPasswordCallback;
1213
import ch.cyberduck.core.ListService;
1314
import ch.cyberduck.core.OAuthTokens;
1415
import ch.cyberduck.core.Path;
15-
import ch.cyberduck.core.Session;
1616
import ch.cyberduck.core.SimplePathPredicate;
1717
import ch.cyberduck.core.UUIDRandomStringService;
1818
import ch.cyberduck.core.exception.AccessDeniedException;
19-
import ch.cyberduck.core.exception.BackgroundException;
2019
import ch.cyberduck.core.exception.NotfoundException;
2120
import ch.cyberduck.core.features.AttributesFinder;
22-
import ch.cyberduck.core.features.Bulk;
2321
import ch.cyberduck.core.features.Delete;
2422
import ch.cyberduck.core.features.Directory;
2523
import ch.cyberduck.core.features.Find;
2624
import ch.cyberduck.core.features.Home;
2725
import ch.cyberduck.core.features.Move;
28-
import ch.cyberduck.core.features.Read;
2926
import ch.cyberduck.core.features.Vault;
3027
import ch.cyberduck.core.features.Write;
31-
import ch.cyberduck.core.io.StatusOutputStream;
32-
import ch.cyberduck.core.transfer.Transfer;
33-
import ch.cyberduck.core.transfer.TransferItem;
3428
import ch.cyberduck.core.transfer.TransferStatus;
3529
import ch.cyberduck.core.vault.VaultCredentials;
3630
import ch.cyberduck.core.vault.VaultRegistry;
3731

38-
import org.apache.commons.io.IOUtils;
3932
import org.apache.commons.lang3.RandomUtils;
4033
import org.apache.commons.lang3.StringUtils;
4134
import org.apache.logging.log4j.LogManager;
@@ -46,10 +39,6 @@
4639
import org.junit.jupiter.params.provider.MethodSource;
4740
import org.openapitools.jackson.nullable.JsonNullableModule;
4841

49-
import java.io.ByteArrayInputStream;
50-
import java.io.IOException;
51-
import java.io.InputStream;
52-
import java.util.Arrays;
5342
import java.util.Collections;
5443
import java.util.EnumSet;
5544
import java.util.HashMap;
@@ -61,7 +50,6 @@
6150
import cloud.katta.client.ApiClient;
6251
import cloud.katta.client.ApiException;
6352
import cloud.katta.client.api.StorageProfileResourceApi;
64-
import cloud.katta.client.api.UsersResourceApi;
6553
import cloud.katta.client.model.S3SERVERSIDEENCRYPTION;
6654
import cloud.katta.client.model.S3STORAGECLASSES;
6755
import cloud.katta.client.model.StorageProfileDto;
@@ -77,6 +65,7 @@
7765
import cloud.katta.protocols.hub.HubVaultRegistry;
7866
import cloud.katta.testsetup.AbstractHubTest;
7967
import cloud.katta.testsetup.HubTestConfig;
68+
import cloud.katta.testsetup.HubTestUtilities;
8069
import cloud.katta.testsetup.MethodIgnorableSource;
8170
import com.fasterxml.jackson.annotation.JsonInclude;
8271
import com.fasterxml.jackson.databind.ObjectMapper;
@@ -306,50 +295,34 @@ void test03AddVault(final HubTestConfig config) throws Exception {
306295
}
307296
{
308297
// encrypted file upload
309-
final Path file = new Path(vault, new AlphanumericRandomStringService(25).random(), EnumSet.of(Path.Type.file));
310-
byte[] content = writeRandomFile(hubSession, file, 234);
298+
final Path file = new Path(vault, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file));
299+
final byte[] content = HubTestUtilities.write(hubSession, file, RandomUtils.nextBytes(234));
311300
final AttributedList<Path> list = hubSession.getFeature(ListService.class).list(vault, new DisabledListProgressListener());
312301
assertEquals(1, list.size());
313302
assertEquals(file.getName(), list.get(0).getName());
314-
315-
byte[] actual = new byte[300];
316-
try (final InputStream inputStream = hubSession.getFeature(Read.class).read(file, new TransferStatus(), new DisabledConnectionCallback())) {
317-
int l = inputStream.read(actual);
318-
assert l == 234;
319-
assertArrayEquals(content, Arrays.copyOfRange(actual, 0, l));
320-
}
303+
assertArrayEquals(content, HubTestUtilities.read(hubSession, file, content.length));
321304
}
322305
{
323306
// directory creation and listing
324-
final Path folder = new Path(vault, new AlphanumericRandomStringService(25).random(), EnumSet.of(Path.Type.directory));
325-
307+
final Path folder = new Path(vault, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory));
326308
hubSession.getFeature(Directory.class).mkdir(hubSession.getFeature(Write.class), folder, new TransferStatus());
327309
final AttributedList<Path> list = hubSession.getFeature(ListService.class).list(vault, new DisabledListProgressListener());
328310
assertEquals(2, list.size()); // a file and a folder
329-
330311
{
331312
// file upload in subfolder
332-
final Path file = new Path(folder, new AlphanumericRandomStringService(25).random(), EnumSet.of(Path.Type.file));
333-
final byte[] content = writeRandomFile(hubSession, file, 555);
313+
final Path file = new Path(folder, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file));
314+
final byte[] content = HubTestUtilities.write(hubSession, file, RandomUtils.nextBytes(555));
334315
final AttributedList<Path> sublist = hubSession.getFeature(ListService.class).list(folder, new DisabledListProgressListener());
335316
assertEquals(1, sublist.size());
336317
assertEquals(file.getName(), sublist.get(0).getName());
337-
338-
byte[] actual = new byte[600];
339-
try (final InputStream inputStream = hubSession.getFeature(Read.class).read(file, new TransferStatus(), new DisabledConnectionCallback())) {
340-
int l = inputStream.read(actual);
341-
assert l == 555;
342-
assertArrayEquals(content, Arrays.copyOfRange(actual, 0, l));
343-
}
344-
318+
assertArrayEquals(content, HubTestUtilities.read(hubSession, file, content.length));
345319
// move operation to root folder and read again
346-
hubSession.getFeature(Move.class).move(file, new Path(vault, file.getName(), EnumSet.of(Path.Type.file)), new TransferStatus(), new Delete.DisabledCallback(), new DisabledConnectionCallback());
347-
320+
hubSession.getFeature(Move.class).move(file, new Path(vault, file.getName(), EnumSet.of(Path.Type.file)), new TransferStatus(),
321+
new Delete.DisabledCallback(), new DisabledConnectionCallback());
348322
final AttributedList<Path> list2 = hubSession.getFeature(ListService.class).list(vault, new DisabledListProgressListener());
349323
assertEquals(3, list2.size()); // 1 subfolder and 2 files
350-
351-
assertEquals(1, list2.toStream().map(Path::isDirectory).filter(Boolean::booleanValue).count());
352-
assertEquals(2, list2.toStream().map(Path::isFile).filter(Boolean::booleanValue).count());
324+
assertEquals(1, list2.filter(Path::isDirectory).size());
325+
assertEquals(2, list2.filter(Path::isFile).size());
353326
}
354327
}
355328
vaultRegistry.close(vault);
@@ -374,18 +347,36 @@ void test04SetupCode(final HubTestConfig config) throws Exception {
374347
assertEquals(vaults, feature.list(Home.root(), new DisabledListProgressListener()));
375348
for(final Path vault : vaults) {
376349
assertTrue(hubSession.getFeature(Find.class).find(vault));
350+
final AttributedList<Path> list = hubSession.getFeature(ListService.class).list(vault, new DisabledListProgressListener());
351+
assertEquals(3, list.size()); // 1 subfolder and 2 files
352+
assertEquals(1, list.toStream().filter(Path::isDirectory).count());
353+
assertEquals(2, list.toStream().filter(Path::isFile).count());
354+
for(Path f : list.filter(Path::isFile)) {
355+
final long length = f.attributes().getSize();
356+
HubTestUtilities.read(hubSession, f, (int) length);
357+
}
358+
for(Path d : list.filter(Path::isDirectory)) {
359+
{
360+
// New file
361+
final Path file = new Path(d, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.file));
362+
final byte[] content = HubTestUtilities.write(hubSession, file, RandomUtils.nextBytes(247));
363+
assertArrayEquals(content, HubTestUtilities.read(hubSession, file, content.length));
364+
hubSession.getFeature(Delete.class).delete(Collections.singletonList(file), new DisabledPasswordCallback(), new Delete.DisabledCallback());
365+
assertFalse(hubSession.getFeature(Find.class).find(file));
366+
}
367+
{
368+
// New directory
369+
final Path folder = new Path(d, new AlphanumericRandomStringService().random(), EnumSet.of(Path.Type.directory));
370+
hubSession.getFeature(Directory.class).mkdir(hubSession.getFeature(Write.class), folder, new TransferStatus());
371+
hubSession.getFeature(Delete.class).delete(Collections.singletonList(folder), new DisabledPasswordCallback(), new Delete.DisabledCallback());
372+
assertFalse(hubSession.getFeature(Find.class).find(folder));
373+
}
374+
final AttributedList<Path> sublist = hubSession.getFeature(ListService.class).list(d, new DisabledListProgressListener());
375+
for(Path f : sublist.filter(Path::isFile)) {
376+
final long length = f.attributes().getSize();
377+
HubTestUtilities.read(hubSession, f, (int) length);
378+
}
379+
}
377380
}
378-
new UsersResourceApi(hubSession.getClient()).apiUsersMeGet(true, false);
379-
}
380-
381-
private static byte[] writeRandomFile(final Session<?> session, final Path file, int size) throws BackgroundException, IOException {
382-
final byte[] content = RandomUtils.nextBytes(size);
383-
final TransferStatus transferStatus = new TransferStatus().setLength(content.length);
384-
transferStatus.setChecksum(session.getFeature(Write.class).checksum(file, transferStatus).compute(new ByteArrayInputStream(content), transferStatus));
385-
session.getFeature(Bulk.class).pre(Transfer.Type.upload, Collections.singletonMap(new TransferItem(file), transferStatus), new DisabledConnectionCallback());
386-
final StatusOutputStream<?> out = session.getFeature(Write.class).write(file, transferStatus, new DisabledConnectionCallback());
387-
IOUtils.copyLarge(new ByteArrayInputStream(content), out);
388-
out.close();
389-
return content;
390381
}
391382
}

0 commit comments

Comments
 (0)