PageRenderTime 98ms CodeModel.GetById 24ms RepoModel.GetById 1ms app.codeStats 0ms

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

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