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