Add callback for delete operation.
[cyberduck.git] / test / ch / cyberduck / core / openstack / SwiftLargeObjectUploadFeatureTest.java
blob8a4b0b86bb08f5ac43b4d5c3c9af8a7b5c34113b
1 package ch.cyberduck.core.openstack;
3 import ch.cyberduck.core.AbstractTestCase;
4 import ch.cyberduck.core.Credentials;
5 import ch.cyberduck.core.DisabledCancelCallback;
6 import ch.cyberduck.core.DisabledConnectionCallback;
7 import ch.cyberduck.core.DisabledHostKeyCallback;
8 import ch.cyberduck.core.DisabledLoginCallback;
9 import ch.cyberduck.core.DisabledPasswordStore;
10 import ch.cyberduck.core.DisabledTranscriptListener;
11 import ch.cyberduck.core.Host;
12 import ch.cyberduck.core.Local;
13 import ch.cyberduck.core.Path;
14 import ch.cyberduck.core.exception.NotfoundException;
15 import ch.cyberduck.core.features.Delete;
16 import ch.cyberduck.core.io.BandwidthThrottle;
17 import ch.cyberduck.core.io.Checksum;
18 import ch.cyberduck.core.io.DisabledStreamListener;
19 import ch.cyberduck.core.io.StreamCopier;
20 import ch.cyberduck.core.shared.DefaultAttributesFeature;
21 import ch.cyberduck.core.transfer.TransferStatus;
23 import org.apache.commons.io.IOUtils;
24 import org.junit.Test;
26 import java.io.ByteArrayOutputStream;
27 import java.io.InputStream;
28 import java.io.OutputStream;
29 import java.util.Collections;
30 import java.util.EnumSet;
31 import java.util.List;
32 import java.util.Map;
33 import java.util.Random;
34 import java.util.UUID;
36 import ch.iterate.openstack.swift.model.StorageObject;
38 import static org.junit.Assert.*;
40 /**
41 * @version $Id$
43 public class SwiftLargeObjectUploadFeatureTest extends AbstractTestCase {
45 @Test
46 public void testUploadHP() throws Exception {
47 final Host host = new Host(new SwiftProtocol() {
48 @Override
49 public String getContext() {
50 return "/v2.0/tokens";
52 }, "region-a.geo-1.identity.hpcloudsvc.com", 35357, new Credentials(
53 properties.getProperty("hpcloud.key"), properties.getProperty("hpcloud.secret")
54 ));
55 final Path container = new Path("test.cyberduck.ch", EnumSet.of(Path.Type.directory, Path.Type.volume));
56 container.attributes().setRegion("region-a.geo-1");
57 this.test(host, container);
60 @Test(expected = NotfoundException.class)
61 public void testUploadHPNotFound() throws Exception {
62 final Host host = new Host(new SwiftProtocol() {
63 @Override
64 public String getContext() {
65 return "/v2.0/tokens";
67 }, "region-a.geo-1.identity.hpcloudsvc.com", 35357, new Credentials(
68 properties.getProperty("hpcloud.key"), properties.getProperty("hpcloud.secret")
69 ));
70 final Path container = new Path("t.cyberduck.ch", EnumSet.of(Path.Type.directory, Path.Type.volume));
71 container.attributes().setRegion("region-b.geo-1");
72 this.test(host, container);
75 @Test
76 public void testUploadRax() throws Exception {
77 final Host host = new Host(new SwiftProtocol(), "identity.api.rackspacecloud.com",
78 new Credentials(
79 properties.getProperty("rackspace.key"), properties.getProperty("rackspace.secret")));
80 final Path container = new Path("test.cyberduck.ch", EnumSet.of(Path.Type.directory, Path.Type.volume));
81 container.attributes().setRegion("DFW");
82 this.test(host, container);
85 private void test(final Host host, final Path container) throws Exception {
86 final SwiftSession session = new SwiftSession(host);
87 session.open(new DisabledHostKeyCallback(), new DisabledTranscriptListener());
88 session.login(new DisabledPasswordStore(), new DisabledLoginCallback(), new DisabledCancelCallback());
90 final Path test = new Path(container, UUID.randomUUID().toString() + ".txt", EnumSet.of(Path.Type.file));
92 final Local local = new Local(System.getProperty("java.io.tmpdir"), UUID.randomUUID().toString());
94 // Each segment, except the last, must be larger than 1048576 bytes.
95 //2MB + 1
96 final byte[] content = new byte[1048576 + 1048576 + 1];
97 new Random().nextBytes(content);
99 final OutputStream out = local.getOutputStream(false);
100 IOUtils.write(content, out);
101 IOUtils.closeQuietly(out);
102 final TransferStatus status = new TransferStatus();
103 status.setLength(content.length);
105 final SwiftLargeObjectUploadFeature upload = new SwiftLargeObjectUploadFeature(session,
106 new SwiftObjectListService(session),
107 new SwiftSegmentService(session, ".segments-test/"), new SwiftWriteFeature(session), (long) (content.length / 2), 4);
109 final StorageObject object = upload.upload(test, local, new BandwidthThrottle(BandwidthThrottle.UNLIMITED), new DisabledStreamListener(),
110 status, new DisabledConnectionCallback());
111 assertNull(Checksum.parse(object.getMd5sum()));
112 assertNull(new SwiftAttributesFeature(session).find(test).getChecksum());
113 assertNull(new DefaultAttributesFeature(session).find(test).getChecksum());
115 assertEquals(1048576 + 1048576 + 1, status.getOffset());
116 assertTrue(status.isComplete());
117 assertFalse(status.isCanceled());
119 assertTrue(new SwiftFindFeature(session).find(test));
120 final InputStream in = new SwiftReadFeature(session).read(test, new TransferStatus());
121 final ByteArrayOutputStream buffer = new ByteArrayOutputStream(content.length);
122 new StreamCopier(status, status).transfer(in, buffer);
123 IOUtils.closeQuietly(in);
124 assertArrayEquals(content, buffer.toByteArray());
125 final Map<String, String> metadata = new SwiftMetadataFeature(session).getMetadata(test);
126 assertFalse(metadata.isEmpty());
127 assertEquals("text/plain", metadata.get("Content-Type"));
128 final List<Path> segments = new SwiftSegmentService(session).list(test);
129 assertFalse(segments.isEmpty());
130 assertEquals(3, segments.size());
131 assertEquals(1048576L, segments.get(0).attributes().getSize());
132 assertEquals(1048576L, segments.get(1).attributes().getSize());
133 assertEquals(1L, segments.get(2).attributes().getSize());
134 new SwiftDeleteFeature(session).delete(Collections.<Path>singletonList(test), new DisabledLoginCallback(), new Delete.Callback() {
135 @Override
136 public void delete(final Path file) {
139 session.close();