PageRenderTime 132ms CodeModel.GetById 54ms app.highlight 14ms RepoModel.GetById 33ms app.codeStats 0ms

/apis/s3/src/test/java/org/jclouds/s3/xml/S3ParserTest.java

https://github.com/alasdairhodge/jclouds
Java | 189 lines | 143 code | 22 blank | 24 comment | 12 complexity | dbfdc88cb60a39d7cd596f8df9d8b9b0 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.net.URI;
 25import java.util.Date;
 26import java.util.Set;
 27import java.util.concurrent.Callable;
 28import java.util.concurrent.CompletionService;
 29import java.util.concurrent.ExecutionException;
 30import java.util.concurrent.ExecutorCompletionService;
 31
 32import javax.ws.rs.core.UriBuilder;
 33
 34import org.jclouds.PerformanceTest;
 35import org.jclouds.date.internal.SimpleDateFormatDateService;
 36import org.jclouds.http.HttpException;
 37import org.jclouds.http.HttpRequest;
 38import org.jclouds.http.functions.ParseSax;
 39import org.jclouds.http.functions.config.SaxParserModule;
 40import org.jclouds.s3.domain.BucketMetadata;
 41import org.jclouds.s3.domain.CanonicalUser;
 42import org.jclouds.s3.domain.ListBucketResponse;
 43import org.jclouds.s3.domain.ObjectMetadata;
 44import org.jclouds.s3.domain.ObjectMetadata.StorageClass;
 45import org.jclouds.util.Strings2;
 46import org.testng.annotations.AfterTest;
 47import org.testng.annotations.BeforeTest;
 48import org.testng.annotations.Test;
 49import org.xml.sax.SAXException;
 50
 51import com.google.common.collect.Iterables;
 52import com.google.inject.Guice;
 53import com.google.inject.Injector;
 54import com.sun.jersey.api.uri.UriBuilderImpl;
 55
 56/**
 57 * Tests parsing of S3 responses
 58 * 
 59 * @author Adrian Cole
 60 */
 61// NOTE:without testName, this will not call @Before* and fail w/NPE during surefire
 62@Test(groups = "performance", sequential = true, timeOut = 2 * 60 * 1000, testName = "S3ParserTest")
 63public class S3ParserTest extends PerformanceTest {
 64   Injector injector = null;
 65   ParseSax.Factory factory;
 66
 67   @BeforeTest
 68   protected void setUpInjector() {
 69      injector = Guice.createInjector(new SaxParserModule() {
 70         public void configure() {
 71            super.configure();
 72            bind(UriBuilder.class).to(UriBuilderImpl.class);
 73         }
 74      });
 75      factory = injector.getInstance(ParseSax.Factory.class);
 76      assert factory != null;
 77   }
 78
 79   @AfterTest
 80   protected void tearDownInjector() {
 81      factory = null;
 82      injector = null;
 83   }
 84
 85   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>";
 86
 87   @Test
 88   void testParseListAllMyBucketsSerialResponseTime() throws HttpException {
 89      for (int i = 0; i < LOOP_COUNT; i++)
 90         runParseListAllMyBuckets();
 91   }
 92
 93   private Set<BucketMetadata> runParseListAllMyBuckets() throws HttpException {
 94      return factory.create(injector.getInstance(ListAllMyBucketsHandler.class)).parse(
 95               Strings2.toInputStream(listAllMyBucketsResultOn200));
 96   }
 97
 98   @Test
 99   void testParseListAllMyBucketsParallelResponseTime() throws InterruptedException, ExecutionException {
100      CompletionService<Set<BucketMetadata>> completer = new ExecutorCompletionService<Set<BucketMetadata>>(exec);
101      for (int i = 0; i < LOOP_COUNT; i++)
102         completer.submit(new Callable<Set<BucketMetadata>>() {
103            public Set<BucketMetadata> call() throws IOException, SAXException, HttpException {
104               return runParseListAllMyBuckets();
105            }
106         });
107      for (int i = 0; i < LOOP_COUNT; i++)
108         assert completer.take().get() != null;
109   }
110
111   @Test
112   public void testCanParseListAllMyBuckets() throws HttpException {
113      Set<BucketMetadata> s3Buckets = runParseListAllMyBuckets();
114      BucketMetadata container1 = Iterables.get(s3Buckets, 0);
115      assert container1.getName().equals("adrianjbosstest");
116      Date expectedDate1 = new SimpleDateFormatDateService().iso8601DateParse("2009-03-12T02:00:07.000Z");
117      Date date1 = container1.getCreationDate();
118      assert date1.equals(expectedDate1);
119      BucketMetadata container2 = (BucketMetadata) s3Buckets.toArray()[1];
120      assert container2.getName().equals("adrianjbosstest2");
121      Date expectedDate2 = new SimpleDateFormatDateService().iso8601DateParse("2009-03-12T02:00:09.000Z");
122      Date date2 = container2.getCreationDate();
123      assert date2.equals(expectedDate2);
124      assert s3Buckets.size() == 2;
125      CanonicalUser owner = new CanonicalUser("e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0");
126      assert container1.getOwner().equals(owner);
127      assert container2.getOwner().equals(owner);
128   }
129
130   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>&quot;9d7bb64e8e18ee34eec06dd2cf37b766&quot;</ETag><Size>136</Size><Owner><ID>e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0</ID><DisplayName>ferncam</DisplayName></Owner><StorageClass>STANDARD</StorageClass></Contents></ListContainerHandler>";
131
132   public void testCanParseListContainerResult() throws HttpException {
133      ListBucketResponse container = runParseListContainerResult();
134      assert !container.isTruncated();
135      assert container.getName().equals("adrianjbosstest");
136      assert container.size() == 1;
137      ObjectMetadata object = container.iterator().next();
138      assert object.getKey().equals("3366");
139      Date expected = new SimpleDateFormatDateService().iso8601DateParse("2009-03-12T02:00:13.000Z");
140      assert object.getLastModified().equals(expected) : String.format("expected %1$s, but got %1$s", expected, object
141               .getLastModified());
142      assertEquals(object.getETag(), "\"9d7bb64e8e18ee34eec06dd2cf37b766\"");
143      assert object.getContentMetadata().getContentLength() == 136;
144      CanonicalUser owner = new CanonicalUser("e1a5f66a480ca99a4fdfe8e318c3020446c9989d7004e7778029fbcc5d990fa0");
145      owner.setDisplayName("ferncam");
146      assert object.getOwner().equals(owner);
147      assert object.getStorageClass().equals(StorageClass.STANDARD);
148   }
149
150   private ListBucketResponse runParseListContainerResult() throws HttpException {
151      return (ListBucketResponse) factory.create(injector.getInstance(ListBucketHandler.class)).setContext(
152               HttpRequest.builder().method("GET").endpoint(URI.create("http://bucket.com")).build()).parse(
153               Strings2.toInputStream(listContainerResult));
154   }
155
156   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>";
157
158   private ObjectMetadata runParseCopyObjectResult() throws HttpException {
159      return (ObjectMetadata) factory.create(injector.getInstance(CopyObjectHandler.class)).parse(
160               Strings2.toInputStream(successfulCopyObject200));
161   }
162
163   public void testCanParseCopyObjectResult() throws HttpException {
164      ObjectMetadata metadata = runParseCopyObjectResult();
165      Date expected = new SimpleDateFormatDateService().iso8601DateParse("2009-03-19T13:23:27.000Z");
166      assertEquals(metadata.getLastModified(), expected);
167      assertEquals(metadata.getETag(), "\"92836a3ea45a6984d1b4d23a747d46bb\"");
168   }
169
170   @Test
171   void testParseListContainerResultSerialResponseTime() throws HttpException {
172      for (int i = 0; i < LOOP_COUNT; i++)
173         runParseListContainerResult();
174   }
175
176   @Test
177   void testParseListContainerResultParallelResponseTime() throws InterruptedException, ExecutionException {
178      CompletionService<ListBucketResponse> completer = new ExecutorCompletionService<ListBucketResponse>(exec);
179      for (int i = 0; i < LOOP_COUNT; i++)
180         completer.submit(new Callable<ListBucketResponse>() {
181            public ListBucketResponse call() throws IOException, SAXException, HttpException {
182               return runParseListContainerResult();
183            }
184         });
185      for (int i = 0; i < LOOP_COUNT; i++)
186         assert completer.take().get() != null;
187   }
188
189}