PageRenderTime 42ms CodeModel.GetById 19ms RepoModel.GetById 0ms app.codeStats 0ms

/apis/s3/src/test/java/org/jclouds/s3/functions/ParseObjectMetadataFromHeadersTest.java

https://github.com/ddurnev/jclouds
Java | 162 lines | 117 code | 23 blank | 22 comment | 0 complexity | 91fac442733ef08791f9601adb45da4e 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.functions;
  20. import static org.easymock.classextension.EasyMock.createMock;
  21. import static org.jclouds.aws.reference.AWSConstants.PROPERTY_HEADER_TAG;
  22. import static org.jclouds.blobstore.reference.BlobStoreConstants.PROPERTY_USER_METADATA_PREFIX;
  23. import static org.testng.Assert.assertEquals;
  24. import java.util.Date;
  25. import java.util.Map;
  26. import javax.ws.rs.core.HttpHeaders;
  27. import javax.ws.rs.core.MediaType;
  28. import org.jclouds.crypto.CryptoStreams;
  29. import org.jclouds.date.internal.SimpleDateFormatDateService;
  30. import org.jclouds.http.HttpResponse;
  31. import org.jclouds.io.Payloads;
  32. import org.jclouds.rest.RequestSigner;
  33. import org.jclouds.s3.domain.MutableObjectMetadata;
  34. import org.jclouds.s3.domain.ObjectMetadata.StorageClass;
  35. import org.jclouds.s3.domain.internal.MutableObjectMetadataImpl;
  36. import org.jclouds.s3.reference.S3Headers;
  37. import org.testng.annotations.BeforeTest;
  38. import org.testng.annotations.Test;
  39. import com.google.common.collect.ImmutableMap;
  40. import com.google.common.collect.ImmutableMultimap;
  41. import com.google.inject.AbstractModule;
  42. import com.google.inject.Guice;
  43. import com.google.inject.name.Names;
  44. /**
  45. * @author Adrian Cole
  46. */
  47. @Test(testName = "s3.ParseObjectMetadataFromHeadersTest")
  48. public class ParseObjectMetadataFromHeadersTest {
  49. @Test
  50. void testNormalParsesETagIntoMD5AndMetadataHeaders() throws Exception {
  51. HttpResponse http = new HttpResponse(400, "boa", Payloads.newStringPayload(""), ImmutableMultimap.of(
  52. S3Headers.USER_METADATA_PREFIX + "foo", "bar", HttpHeaders.LAST_MODIFIED, lastModified,
  53. HttpHeaders.ETAG, "\"abcd\"", HttpHeaders.CACHE_CONTROL, "cacheControl"));
  54. http.getPayload().getContentMetadata().setContentLength(1025l);
  55. http.getPayload().getContentMetadata().setContentDisposition("contentDisposition");
  56. http.getPayload().getContentMetadata().setContentEncoding("encoding");
  57. http.getPayload().getContentMetadata().setContentType(MediaType.APPLICATION_OCTET_STREAM);
  58. MutableObjectMetadata response = parser.apply(http);
  59. MutableObjectMetadataImpl expects = new MutableObjectMetadataImpl();
  60. expects.setCacheControl("cacheControl");
  61. expects.getContentMetadata().setContentDisposition("contentDisposition");
  62. expects.getContentMetadata().setContentEncoding("encoding");
  63. expects.getContentMetadata().setContentType(MediaType.APPLICATION_OCTET_STREAM);
  64. expects.getContentMetadata().setContentLength(1025l);
  65. expects.getContentMetadata().setContentMD5(CryptoStreams.hex("abcd"));
  66. expects.setETag("\"abcd\"");
  67. expects.setKey("key");
  68. expects.setLastModified(now);
  69. expects.setOwner(null);
  70. expects.setStorageClass(StorageClass.STANDARD);
  71. expects.setUserMetadata(userMetadata);
  72. assertEquals(response, expects);
  73. }
  74. @Test
  75. void testMultipartDoesntAttemptToParseETagIntoMD5() throws Exception {
  76. HttpResponse http = new HttpResponse(400, "boa", Payloads.newStringPayload(""), ImmutableMultimap.of(
  77. S3Headers.USER_METADATA_PREFIX + "foo", "bar", HttpHeaders.LAST_MODIFIED, lastModified,
  78. HttpHeaders.ETAG, "\"abcd-1\"", HttpHeaders.CACHE_CONTROL, "cacheControl"));
  79. http.getPayload().getContentMetadata().setContentLength(1025l);
  80. http.getPayload().getContentMetadata().setContentDisposition("contentDisposition");
  81. http.getPayload().getContentMetadata().setContentEncoding("encoding");
  82. http.getPayload().getContentMetadata().setContentType(MediaType.APPLICATION_OCTET_STREAM);
  83. MutableObjectMetadata response = parser.apply(http);
  84. MutableObjectMetadataImpl expects = new MutableObjectMetadataImpl();
  85. expects.setCacheControl("cacheControl");
  86. expects.getContentMetadata().setContentDisposition("contentDisposition");
  87. expects.getContentMetadata().setContentEncoding("encoding");
  88. expects.getContentMetadata().setContentType(MediaType.APPLICATION_OCTET_STREAM);
  89. expects.getContentMetadata().setContentLength(1025l);
  90. expects.setETag("\"abcd-1\"");
  91. expects.setKey("key");
  92. expects.setLastModified(now);
  93. expects.setOwner(null);
  94. expects.setStorageClass(StorageClass.STANDARD);
  95. expects.setUserMetadata(userMetadata);
  96. assertEquals(response, expects);
  97. }
  98. @Test
  99. void testAmzEtagStillParsesToMD5AndDoesntMistakeAmzEtagForUserMetadata() throws Exception {
  100. HttpResponse http = new HttpResponse(400, "boa", Payloads.newStringPayload(""), ImmutableMultimap.of(
  101. S3Headers.USER_METADATA_PREFIX + "foo", "bar", HttpHeaders.LAST_MODIFIED, lastModified,
  102. HttpHeaders.CACHE_CONTROL, "cacheControl", S3Headers.AMZ_ETAG, "\"abcd\""));
  103. http.getPayload().getContentMetadata().setContentLength(1025l);
  104. http.getPayload().getContentMetadata().setContentDisposition("contentDisposition");
  105. http.getPayload().getContentMetadata().setContentEncoding("encoding");
  106. http.getPayload().getContentMetadata().setContentType(MediaType.APPLICATION_OCTET_STREAM);
  107. MutableObjectMetadata response = parser.apply(http);
  108. MutableObjectMetadataImpl expects = new MutableObjectMetadataImpl();
  109. expects.setCacheControl("cacheControl");
  110. expects.getContentMetadata().setContentDisposition("contentDisposition");
  111. expects.getContentMetadata().setContentEncoding("encoding");
  112. expects.getContentMetadata().setContentMD5(CryptoStreams.hex("abcd"));
  113. expects.getContentMetadata().setContentType(MediaType.APPLICATION_OCTET_STREAM);
  114. expects.getContentMetadata().setContentLength(1025l);
  115. expects.setETag("\"abcd\"");
  116. expects.setKey("key");
  117. expects.setLastModified(now);
  118. expects.setOwner(null);
  119. expects.setStorageClass(StorageClass.STANDARD);
  120. expects.setUserMetadata(userMetadata);
  121. assertEquals(response, expects);
  122. }
  123. String lastModified = new SimpleDateFormatDateService().rfc822DateFormat(new Date());
  124. // rfc isn't accurate down to nanos, so we'll parse back to ensure tests pass
  125. Date now = new SimpleDateFormatDateService().rfc822DateParse(lastModified);
  126. Map<String, String> userMetadata = ImmutableMap.of("foo", "bar");
  127. ParseObjectMetadataFromHeaders parser;
  128. @BeforeTest
  129. void setUp() {
  130. parser = Guice.createInjector(new AbstractModule() {
  131. @Override
  132. protected void configure() {
  133. bind(RequestSigner.class).toInstance(createMock(RequestSigner.class));
  134. bindConstant().annotatedWith(Names.named(PROPERTY_HEADER_TAG)).to(S3Headers.DEFAULT_AMAZON_HEADERTAG);
  135. bindConstant().annotatedWith(Names.named(PROPERTY_USER_METADATA_PREFIX)).to(S3Headers.USER_METADATA_PREFIX);
  136. }
  137. }).getInstance(ParseObjectMetadataFromHeaders.class).setKey("key");
  138. }
  139. }