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