/apis/s3/src/test/java/org/jclouds/s3/xml/S3ParserTest.java
Java | 190 lines | 144 code | 22 blank | 24 comment | 12 complexity | eb8d73e429b131f6f95d4d2ed0621777 MD5 | raw file
1/**
2 * Licensed to jclouds, Inc. (jclouds) under one or more
3 * contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. jclouds licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19package org.jclouds.s3.xml;
20
21import static org.testng.Assert.assertEquals;
22
23import java.io.IOException;
24import java.io.UnsupportedEncodingException;
25import java.net.URI;
26import java.util.Date;
27import java.util.Set;
28import java.util.concurrent.Callable;
29import java.util.concurrent.CompletionService;
30import java.util.concurrent.ExecutionException;
31import java.util.concurrent.ExecutorCompletionService;
32
33import javax.ws.rs.core.UriBuilder;
34
35import org.jclouds.PerformanceTest;
36import org.jclouds.date.internal.SimpleDateFormatDateService;
37import org.jclouds.http.HttpException;
38import org.jclouds.http.HttpRequest;
39import org.jclouds.http.functions.ParseSax;
40import org.jclouds.http.functions.config.SaxParserModule;
41import org.jclouds.s3.domain.BucketMetadata;
42import org.jclouds.s3.domain.CanonicalUser;
43import org.jclouds.s3.domain.ListBucketResponse;
44import org.jclouds.s3.domain.ObjectMetadata;
45import org.jclouds.s3.domain.ObjectMetadata.StorageClass;
46import org.jclouds.util.Strings2;
47import org.testng.annotations.AfterTest;
48import org.testng.annotations.BeforeTest;
49import org.testng.annotations.Test;
50import org.xml.sax.SAXException;
51
52import com.google.common.collect.Iterables;
53import com.google.inject.Guice;
54import com.google.inject.Injector;
55import com.sun.jersey.api.uri.UriBuilderImpl;
56
57/**
58 * Tests parsing of S3 responses
59 *
60 * @author Adrian Cole
61 */
62// NOTE:without testName, this will not call @Before* and fail w/NPE during surefire
63@Test(groups = "performance", sequential = true, timeOut = 2 * 60 * 1000, testName = "S3ParserTest")
64public class S3ParserTest extends PerformanceTest {
65 Injector injector = null;
66 ParseSax.Factory factory;
67
68 @BeforeTest
69 protected void setUpInjector() {
70 injector = Guice.createInjector(new SaxParserModule() {
71 public void configure() {
72 super.configure();
73 bind(UriBuilder.class).to(UriBuilderImpl.class);
74 }
75 });
76 factory = injector.getInstance(ParseSax.Factory.class);
77 assert factory != null;
78 }
79
80 @AfterTest
81 protected void tearDownInjector() {
82 factory = null;
83 injector = null;
84 }
85
86 public static final String listAllMyBucketsResultOn200 = "<ListAllMyBucketsResult xmlns=\"http://s3.amazonaws.com/doc/callables/\"><Owner><ID>e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0</ID></Owner><Buckets><Bucket><Name>adrianjbosstest</Name><CreationDate>2009-03-12T02:00:07.000Z</CreationDate></Bucket><Bucket><Name>adrianjbosstest2</Name><CreationDate>2009-03-12T02:00:09.000Z</CreationDate></Bucket></Buckets></ListAllMyBucketsResult>";
87
88 @Test
89 void testParseListAllMyBucketsSerialResponseTime() throws HttpException {
90 for (int i = 0; i < LOOP_COUNT; i++)
91 runParseListAllMyBuckets();
92 }
93
94 private Set<BucketMetadata> runParseListAllMyBuckets() throws HttpException {
95 return factory.create(injector.getInstance(ListAllMyBucketsHandler.class)).parse(
96 Strings2.toInputStream(listAllMyBucketsResultOn200));
97 }
98
99 @Test
100 void testParseListAllMyBucketsParallelResponseTime() throws InterruptedException, ExecutionException {
101 CompletionService<Set<BucketMetadata>> completer = new ExecutorCompletionService<Set<BucketMetadata>>(exec);
102 for (int i = 0; i < LOOP_COUNT; i++)
103 completer.submit(new Callable<Set<BucketMetadata>>() {
104 public Set<BucketMetadata> call() throws IOException, SAXException, HttpException {
105 return runParseListAllMyBuckets();
106 }
107 });
108 for (int i = 0; i < LOOP_COUNT; i++)
109 assert completer.take().get() != null;
110 }
111
112 @Test
113 public void testCanParseListAllMyBuckets() throws HttpException {
114 Set<BucketMetadata> s3Buckets = runParseListAllMyBuckets();
115 BucketMetadata container1 = Iterables.get(s3Buckets, 0);
116 assert container1.getName().equals("adrianjbosstest");
117 Date expectedDate1 = new SimpleDateFormatDateService().iso8601DateParse("2009-03-12T02:00:07.000Z");
118 Date date1 = container1.getCreationDate();
119 assert date1.equals(expectedDate1);
120 BucketMetadata container2 = (BucketMetadata) s3Buckets.toArray()[1];
121 assert container2.getName().equals("adrianjbosstest2");
122 Date expectedDate2 = new SimpleDateFormatDateService().iso8601DateParse("2009-03-12T02:00:09.000Z");
123 Date date2 = container2.getCreationDate();
124 assert date2.equals(expectedDate2);
125 assert s3Buckets.size() == 2;
126 CanonicalUser owner = new CanonicalUser("e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0");
127 assert container1.getOwner().equals(owner);
128 assert container2.getOwner().equals(owner);
129 }
130
131 public static final String listContainerResult = "<ListContainerHandler xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><Name>adrianjbosstest</Name><Prefix></Prefix><Marker></Marker><MaxKeys>1000</MaxKeys><IsTruncated>false</IsTruncated><Contents><Key>3366</Key><LastModified>2009-03-12T02:00:13.000Z</LastModified><ETag>"9d7bb64e8e18ee34eec06dd2cf37b766"</ETag><Size>136</Size><Owner><ID>e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0</ID><DisplayName>ferncam</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents></ListContainerHandler>";
132
133 public void testCanParseListContainerResult() throws HttpException, UnsupportedEncodingException {
134 ListBucketResponse container = runParseListContainerResult();
135 assert !container.isTruncated();
136 assert container.getName().equals("adrianjbosstest");
137 assert container.size() == 1;
138 ObjectMetadata object = container.iterator().next();
139 assert object.getKey().equals("3366");
140 Date expected = new SimpleDateFormatDateService().iso8601DateParse("2009-03-12T02:00:13.000Z");
141 assert object.getLastModified().equals(expected) : String.format("expected %1$s, but got %1$s", expected, object
142 .getLastModified());
143 assertEquals(object.getETag(), "\"9d7bb64e8e18ee34eec06dd2cf37b766\"");
144 assert object.getContentMetadata().getContentLength() == 136;
145 CanonicalUser owner = new CanonicalUser("e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0");
146 owner.setDisplayName("ferncam");
147 assert object.getOwner().equals(owner);
148 assert object.getStorageClass().equals(StorageClass.STANDARD);
149 }
150
151 private ListBucketResponse runParseListContainerResult() throws HttpException {
152 return (ListBucketResponse) factory.create(injector.getInstance(ListBucketHandler.class)).setContext(
153 HttpRequest.builder().method("GET").endpoint(URI.create("http://bucket.com")).build()).parse(
154 Strings2.toInputStream(listContainerResult));
155 }
156
157 public static final String successfulCopyObject200 = "<CopyObjectResult xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\"><LastModified>2009-03-19T13:23:27.000Z</LastModified><ETag>\"92836a3ea45a6984d1b4d23a747d46bb\"</ETag></CopyObjectResult>";
158
159 private ObjectMetadata runParseCopyObjectResult() throws HttpException {
160 return (ObjectMetadata) factory.create(injector.getInstance(CopyObjectHandler.class)).parse(
161 Strings2.toInputStream(successfulCopyObject200));
162 }
163
164 public void testCanParseCopyObjectResult() throws HttpException, UnsupportedEncodingException {
165 ObjectMetadata metadata = runParseCopyObjectResult();
166 Date expected = new SimpleDateFormatDateService().iso8601DateParse("2009-03-19T13:23:27.000Z");
167 assertEquals(metadata.getLastModified(), expected);
168 assertEquals(metadata.getETag(), "\"92836a3ea45a6984d1b4d23a747d46bb\"");
169 }
170
171 @Test
172 void testParseListContainerResultSerialResponseTime() throws HttpException {
173 for (int i = 0; i < LOOP_COUNT; i++)
174 runParseListContainerResult();
175 }
176
177 @Test
178 void testParseListContainerResultParallelResponseTime() throws InterruptedException, ExecutionException {
179 CompletionService<ListBucketResponse> completer = new ExecutorCompletionService<ListBucketResponse>(exec);
180 for (int i = 0; i < LOOP_COUNT; i++)
181 completer.submit(new Callable<ListBucketResponse>() {
182 public ListBucketResponse call() throws IOException, SAXException, HttpException {
183 return runParseListContainerResult();
184 }
185 });
186 for (int i = 0; i < LOOP_COUNT; i++)
187 assert completer.take().get() != null;
188 }
189
190}