PageRenderTime 46ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

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