Add callback for delete operation.
[cyberduck.git] / test / ch / cyberduck / core / s3 / S3ReadFeatureTest.java
blob5960291d91189fe6b6bac5db707ed4ec53a3f199
1 package ch.cyberduck.core.s3;
3 import ch.cyberduck.core.AbstractTestCase;
4 import ch.cyberduck.core.Credentials;
5 import ch.cyberduck.core.DisabledCancelCallback;
6 import ch.cyberduck.core.DisabledHostKeyCallback;
7 import ch.cyberduck.core.DisabledLoginCallback;
8 import ch.cyberduck.core.DisabledPasswordStore;
9 import ch.cyberduck.core.DisabledTranscriptListener;
10 import ch.cyberduck.core.Host;
11 import ch.cyberduck.core.Path;
12 import ch.cyberduck.core.exception.NotfoundException;
13 import ch.cyberduck.core.features.Delete;
14 import ch.cyberduck.core.io.SHA256ChecksumCompute;
15 import ch.cyberduck.core.io.StreamCopier;
16 import ch.cyberduck.core.transfer.TransferStatus;
18 import org.apache.commons.io.IOUtils;
19 import org.apache.commons.io.output.NullOutputStream;
20 import org.apache.commons.lang3.RandomStringUtils;
21 import org.junit.Test;
23 import java.io.ByteArrayInputStream;
24 import java.io.ByteArrayOutputStream;
25 import java.io.InputStream;
26 import java.io.OutputStream;
27 import java.util.Collections;
28 import java.util.EnumSet;
29 import java.util.UUID;
31 import static org.junit.Assert.*;
33 /**
34 * @version $Id$
36 public class S3ReadFeatureTest extends AbstractTestCase {
38 @Test(expected = NotfoundException.class)
39 public void testReadNotFound() throws Exception {
40 final Host host = new Host(new S3Protocol(), new S3Protocol().getDefaultHostname(), new Credentials(
41 properties.getProperty("s3.key"), properties.getProperty("s3.secret")
42 ));
43 final S3Session session = new S3Session(host);
44 session.open(new DisabledHostKeyCallback(), new DisabledTranscriptListener());
45 session.login(new DisabledPasswordStore(), new DisabledLoginCallback(), new DisabledCancelCallback());
46 final TransferStatus status = new TransferStatus();
47 final Path container = new Path("test.cyberduck.ch", EnumSet.of(Path.Type.directory, Path.Type.volume));
48 new S3ReadFeature(session).read(new Path(container, "nosuchname", EnumSet.of(Path.Type.file)), status);
51 @Test
52 public void testReadRange() throws Exception {
53 final Host host = new Host(new S3Protocol(), new S3Protocol().getDefaultHostname(), new Credentials(
54 properties.getProperty("s3.key"), properties.getProperty("s3.secret")
55 ));
56 final S3Session session = new S3Session(host);
57 session.open(new DisabledHostKeyCallback(), new DisabledTranscriptListener());
58 session.login(new DisabledPasswordStore(), new DisabledLoginCallback(), new DisabledCancelCallback());
59 final Path container = new Path("test.cyberduck.ch", EnumSet.of(Path.Type.directory, Path.Type.volume));
60 final Path test = new Path(container, UUID.randomUUID().toString(), EnumSet.of(Path.Type.file));
61 new S3TouchFeature(session).touch(test);
62 final byte[] content = RandomStringUtils.random(1000).getBytes();
63 final TransferStatus status = new TransferStatus().length(content.length);
64 status.setChecksum(new SHA256ChecksumCompute().compute(new ByteArrayInputStream(content)));
65 final OutputStream out = new S3WriteFeature(session).write(test, status);
66 assertNotNull(out);
67 new StreamCopier(new TransferStatus(), new TransferStatus()).transfer(new ByteArrayInputStream(content), out);
68 IOUtils.closeQuietly(out);
69 status.setAppend(true);
70 status.setOffset(100L);
71 final InputStream in = new S3ReadFeature(session).read(test, status);
72 assertNotNull(in);
73 final ByteArrayOutputStream buffer = new ByteArrayOutputStream(content.length - 100);
74 new StreamCopier(status, status).transfer(in, buffer);
75 final byte[] reference = new byte[content.length - 100];
76 System.arraycopy(content, 100, reference, 0, content.length - 100);
77 assertArrayEquals(reference, buffer.toByteArray());
78 in.close();
79 new S3DefaultDeleteFeature(session).delete(Collections.<Path>singletonList(test), new DisabledLoginCallback(), new Delete.Callback() {
80 @Override
81 public void delete(final Path file) {
83 });
84 session.close();
87 @Test
88 public void testDownloadGzip() throws Exception {
89 final Host host = new Host(new S3Protocol(), new S3Protocol().getDefaultHostname(), new Credentials(
90 properties.getProperty("s3.key"), properties.getProperty("s3.secret")
91 ));
92 final S3Session session = new S3Session(host);
93 session.open(new DisabledHostKeyCallback(), new DisabledTranscriptListener());
94 session.login(new DisabledPasswordStore(), new DisabledLoginCallback(), new DisabledCancelCallback());
95 final TransferStatus status = new TransferStatus();
96 status.setLength(1457L);
97 final Path container = new Path("test.cyberduck.ch", EnumSet.of(Path.Type.directory, Path.Type.volume));
98 final InputStream in = new S3ReadFeature(session).read(new Path(container,
99 "189584543480_CloudTrail_us-east-1_20141017T0910Z_CoraJxmlIWYQI2wc.json.gz",
100 EnumSet.of(Path.Type.file)), status);
101 assertNotNull(in);
102 new StreamCopier(status, status).transfer(in, new NullOutputStream());
103 assertEquals(1457L, status.getOffset());
104 assertEquals(1457L, status.getLength());
105 in.close();
106 session.close();