PageRenderTime 2ms CodeModel.GetById 13ms app.highlight 7ms RepoModel.GetById 0ms app.codeStats 0ms

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

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