PageRenderTime 50ms CodeModel.GetById 15ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 0ms

/apis/s3/src/test/java/org/jclouds/s3/handlers/ParseS3ErrorFromXmlContentTest.java

https://github.com/regularfry/jclouds
Java | 123 lines | 80 code | 21 blank | 22 comment | 1 complexity | 00b1f4049ab9e897ac20dbc0ade863ae 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.handlers;
 20
 21import static org.easymock.EasyMock.expect;
 22import static org.easymock.EasyMock.reportMatcher;
 23import static org.easymock.classextension.EasyMock.createMock;
 24import static org.easymock.classextension.EasyMock.replay;
 25import static org.easymock.classextension.EasyMock.verify;
 26import static org.jclouds.aws.reference.AWSConstants.PROPERTY_HEADER_TAG;
 27import static org.jclouds.s3.reference.S3Constants.PROPERTY_S3_SERVICE_PATH;
 28import static org.jclouds.s3.reference.S3Constants.PROPERTY_S3_VIRTUAL_HOST_BUCKETS;
 29
 30import java.net.URI;
 31
 32import org.easymock.IArgumentMatcher;
 33import org.jclouds.blobstore.ContainerNotFoundException;
 34import org.jclouds.blobstore.KeyNotFoundException;
 35import org.jclouds.http.HttpCommand;
 36import org.jclouds.http.HttpRequest;
 37import org.jclouds.http.HttpResponse;
 38import org.jclouds.http.functions.config.SaxParserModule;
 39import org.jclouds.io.Payloads;
 40import org.jclouds.rest.RequestSigner;
 41import org.jclouds.s3.reference.S3Headers;
 42import org.jclouds.util.Strings2;
 43import org.testng.annotations.Test;
 44
 45import com.google.inject.AbstractModule;
 46import com.google.inject.Guice;
 47import com.google.inject.name.Names;
 48
 49/**
 50 * 
 51 * @author Adrian Cole
 52 */
 53@Test(groups = { "unit" })
 54public class ParseS3ErrorFromXmlContentTest {
 55   private static final String SERVICE_PATH = "/services/Walrus";
 56
 57   @Test
 58   public void test404ContainerNotFoundExceptionPath() {
 59      assertCodeMakes("GET", URI
 60               .create("http://partnercloud.eucalyptus.com:8773/services/Walrus/adriancole-blobstore58/"), 404,
 61               "HTTP/1.1 404 Not Found", false, "<Error><Code>Monster.NotFound</Code></Error>",
 62               ContainerNotFoundException.class);
 63   }
 64
 65   @Test
 66   public void test404KeyNotFoundExceptionPath() {
 67      assertCodeMakes("GET", URI
 68               .create("http://partnercloud.eucalyptus.com:8773/services/Walrus/adriancole-blobstore58/apples"), 404,
 69               "HTTP/1.1 404 Not Found", false, "<Error><Code>Monster.NotFound</Code></Error>",
 70               KeyNotFoundException.class);
 71   }
 72
 73   private void assertCodeMakes(String method, URI uri, int statusCode, String message, final boolean virtualHost,
 74            String content, Class<? extends Exception> expected) {
 75
 76      ParseS3ErrorFromXmlContent function = Guice.createInjector(new SaxParserModule(), new AbstractModule() {
 77
 78         @Override
 79         protected void configure() {
 80            bind(RequestSigner.class).toInstance(createMock(RequestSigner.class));
 81            bindConstant().annotatedWith(Names.named(PROPERTY_HEADER_TAG)).to(S3Headers.DEFAULT_AMAZON_HEADERTAG);
 82            bindConstant().annotatedWith(Names.named(PROPERTY_S3_SERVICE_PATH)).to(SERVICE_PATH);
 83            bindConstant().annotatedWith(Names.named(PROPERTY_S3_VIRTUAL_HOST_BUCKETS)).to(virtualHost);
 84         }
 85
 86      }).getInstance(ParseS3ErrorFromXmlContent.class);
 87
 88      HttpCommand command = createMock(HttpCommand.class);
 89      HttpRequest request = new HttpRequest(method, uri);
 90      HttpResponse response = new HttpResponse(statusCode, message, Payloads.newInputStreamPayload(Strings2
 91               .toInputStream(content)));
 92      response.getPayload().getContentMetadata().setContentType("application/xml");
 93
 94      expect(command.getCurrentRequest()).andReturn(request).atLeastOnce();
 95      command.setException(classEq(expected));
 96
 97      replay(command);
 98
 99      function.handleError(command, response);
100
101      verify(command);
102   }
103
104   public static Exception classEq(final Class<? extends Exception> in) {
105      reportMatcher(new IArgumentMatcher() {
106
107         @Override
108         public void appendTo(StringBuffer buffer) {
109            buffer.append("classEq(");
110            buffer.append(in);
111            buffer.append(")");
112         }
113
114         @Override
115         public boolean matches(Object arg) {
116            return arg.getClass() == in;
117         }
118
119      });
120      return null;
121   }
122
123}