From 1e017d47e1b14efe22ca1cddda6a322fb32c80ed Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Tue, 25 Jan 2011 23:22:03 -0800 Subject: [PATCH] fixed regression on azureblob --- .../ParseAzureBlobErrorFromXmlContent.java | 42 ++++--- ...ParseAzureBlobErrorFromXmlContentTest.java | 115 ++++++++++++++++++ 2 files changed, 142 insertions(+), 15 deletions(-) create mode 100644 providers/azureblob/src/test/java/org/jclouds/azure/storage/handlers/ParseAzureBlobErrorFromXmlContentTest.java diff --git a/providers/azureblob/src/main/java/org/jclouds/azureblob/handlers/ParseAzureBlobErrorFromXmlContent.java b/providers/azureblob/src/main/java/org/jclouds/azureblob/handlers/ParseAzureBlobErrorFromXmlContent.java index b613b446b5..93985735b2 100644 --- a/providers/azureblob/src/main/java/org/jclouds/azureblob/handlers/ParseAzureBlobErrorFromXmlContent.java +++ b/providers/azureblob/src/main/java/org/jclouds/azureblob/handlers/ParseAzureBlobErrorFromXmlContent.java @@ -19,6 +19,8 @@ package org.jclouds.azureblob.handlers; +import java.util.List; + import javax.inject.Inject; import javax.inject.Singleton; @@ -31,6 +33,10 @@ import org.jclouds.http.HttpCommand; import org.jclouds.http.HttpResponse; import org.jclouds.rest.ResourceNotFoundException; +import com.google.common.base.Joiner; +import com.google.common.base.Splitter; +import com.google.common.collect.Lists; + /** * @author Adrian Cole * @@ -43,22 +49,28 @@ public class ParseAzureBlobErrorFromXmlContent extends ParseAzureStorageErrorFro super(utils); } - protected Exception refineException(HttpCommand command, HttpResponse response, Exception exception, AzureStorageError error, - String message) { + protected Exception refineException(HttpCommand command, HttpResponse response, Exception exception, + AzureStorageError error, String message) { switch (response.getStatusCode()) { - case 404: - if (!command.getCurrentRequest().getMethod().equals("DELETE")) { - exception = new ResourceNotFoundException(message, exception); - String container = command.getCurrentRequest().getEndpoint().getHost(); - String key = command.getCurrentRequest().getEndpoint().getPath(); - if (key == null || key.equals("/")) - exception = new ContainerNotFoundException(container, message); - else - exception = new KeyNotFoundException(container, key, message); - } - return exception; - default: - return super.refineException(command, response, exception, error, message); + case 404: + if (!command.getCurrentRequest().getMethod().equals("DELETE")) { + exception = new ResourceNotFoundException(message, exception); + List parts = Lists.newArrayList(Splitter.on('/').split( + command.getCurrentRequest().getEndpoint().getPath())); + parts.remove(""); + if (parts.size() > 0) { + String container = parts.remove(0); + String query = command.getCurrentRequest().getEndpoint().getQuery(); + if (query != null && query.indexOf("container") != -1) { + exception = new ContainerNotFoundException(container, message); + } else { + exception = new KeyNotFoundException(container, Joiner.on('/').join(parts), message); + } + } + } + return exception; + default: + return super.refineException(command, response, exception, error, message); } } } \ No newline at end of file diff --git a/providers/azureblob/src/test/java/org/jclouds/azure/storage/handlers/ParseAzureBlobErrorFromXmlContentTest.java b/providers/azureblob/src/test/java/org/jclouds/azure/storage/handlers/ParseAzureBlobErrorFromXmlContentTest.java new file mode 100644 index 0000000000..5533045547 --- /dev/null +++ b/providers/azureblob/src/test/java/org/jclouds/azure/storage/handlers/ParseAzureBlobErrorFromXmlContentTest.java @@ -0,0 +1,115 @@ +/** + * + * Copyright (C) 2010 Cloud Conscious, LLC. + * + * ==================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ==================================================================== + */ + +package org.jclouds.azure.storage.handlers; + +import static org.easymock.EasyMock.expect; +import static org.easymock.EasyMock.reportMatcher; +import static org.easymock.classextension.EasyMock.createMock; +import static org.easymock.classextension.EasyMock.replay; +import static org.easymock.classextension.EasyMock.verify; + +import java.net.URI; + +import org.easymock.IArgumentMatcher; +import org.jclouds.azure.storage.filters.SharedKeyLiteAuthentication; +import org.jclouds.azureblob.handlers.ParseAzureBlobErrorFromXmlContent; +import org.jclouds.blobstore.ContainerNotFoundException; +import org.jclouds.blobstore.KeyNotFoundException; +import org.jclouds.http.HttpCommand; +import org.jclouds.http.HttpRequest; +import org.jclouds.http.HttpResponse; +import org.jclouds.http.functions.config.SaxParserModule; +import org.jclouds.io.Payloads; +import org.jclouds.util.Strings2; +import org.testng.annotations.Test; + +import com.google.inject.AbstractModule; +import com.google.inject.Guice; + +/** + * + * @author Adrian Cole + */ +@Test(groups = { "unit" }) +public class ParseAzureBlobErrorFromXmlContentTest { + + @Test + public void test404OnContainerIsContainerNotFound() { + assertCodeMakes("GET", URI + .create("https://jclouds.blob.core.windows.net/adriancole-azureblob-413790770?restype=container"), 404, + "Not Found", "text/html; charset=us-ascii", "Not Found\r\n", + ContainerNotFoundException.class); + } + + @Test + public void test404WithoutContainerIsKeyNotFound() { + assertCodeMakes("GET", URI.create("https://jclouds.blob.core.windows.net/adriancole-blobstore0/apples"), 404, + "Not Found", "text/html; charset=us-ascii", "Not Found\r\n", + KeyNotFoundException.class); + } + + private void assertCodeMakes(String method, URI uri, int statusCode, String message, String contentType, + String content, Class expected) { + + ParseAzureBlobErrorFromXmlContent function = Guice.createInjector(new SaxParserModule(), new AbstractModule() { + + @Override + protected void configure() { + bind(SharedKeyLiteAuthentication.class).toInstance(createMock(SharedKeyLiteAuthentication.class)); + } + + }).getInstance(ParseAzureBlobErrorFromXmlContent.class); + + HttpCommand command = createMock(HttpCommand.class); + HttpRequest request = new HttpRequest(method, uri); + HttpResponse response = new HttpResponse(statusCode, message, Payloads.newInputStreamPayload(Strings2 + .toInputStream(content))); + response.getPayload().getContentMetadata().setContentType(contentType); + + expect(command.getCurrentRequest()).andReturn(request).atLeastOnce(); + command.setException(classEq(expected)); + + replay(command); + + function.handleError(command, response); + + verify(command); + } + + public static Exception classEq(final Class in) { + reportMatcher(new IArgumentMatcher() { + + @Override + public void appendTo(StringBuffer buffer) { + buffer.append("classEq("); + buffer.append(in); + buffer.append(")"); + } + + @Override + public boolean matches(Object arg) { + return arg.getClass() == in; + } + + }); + return null; + } + +} \ No newline at end of file