fixed regression on azureblob

This commit is contained in:
Adrian Cole 2011-01-25 23:22:03 -08:00
parent ec589e5180
commit 1e017d47e1
2 changed files with 142 additions and 15 deletions

View File

@ -19,6 +19,8 @@
package org.jclouds.azureblob.handlers; package org.jclouds.azureblob.handlers;
import java.util.List;
import javax.inject.Inject; import javax.inject.Inject;
import javax.inject.Singleton; import javax.inject.Singleton;
@ -31,6 +33,10 @@ import org.jclouds.http.HttpCommand;
import org.jclouds.http.HttpResponse; import org.jclouds.http.HttpResponse;
import org.jclouds.rest.ResourceNotFoundException; 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 * @author Adrian Cole
* *
@ -43,22 +49,28 @@ public class ParseAzureBlobErrorFromXmlContent extends ParseAzureStorageErrorFro
super(utils); super(utils);
} }
protected Exception refineException(HttpCommand command, HttpResponse response, Exception exception, AzureStorageError error, protected Exception refineException(HttpCommand command, HttpResponse response, Exception exception,
String message) { AzureStorageError error, String message) {
switch (response.getStatusCode()) { switch (response.getStatusCode()) {
case 404: case 404:
if (!command.getCurrentRequest().getMethod().equals("DELETE")) { if (!command.getCurrentRequest().getMethod().equals("DELETE")) {
exception = new ResourceNotFoundException(message, exception); exception = new ResourceNotFoundException(message, exception);
String container = command.getCurrentRequest().getEndpoint().getHost(); List<String> parts = Lists.newArrayList(Splitter.on('/').split(
String key = command.getCurrentRequest().getEndpoint().getPath(); command.getCurrentRequest().getEndpoint().getPath()));
if (key == null || key.equals("/")) parts.remove("");
exception = new ContainerNotFoundException(container, message); if (parts.size() > 0) {
else String container = parts.remove(0);
exception = new KeyNotFoundException(container, key, message); String query = command.getCurrentRequest().getEndpoint().getQuery();
} if (query != null && query.indexOf("container") != -1) {
return exception; exception = new ContainerNotFoundException(container, message);
default: } else {
return super.refineException(command, response, exception, error, message); exception = new KeyNotFoundException(container, Joiner.on('/').join(parts), message);
}
}
}
return exception;
default:
return super.refineException(command, response, exception, error, message);
} }
} }
} }

View File

@ -0,0 +1,115 @@
/**
*
* Copyright (C) 2010 Cloud Conscious, LLC. <info@cloudconscious.com>
*
* ====================================================================
* 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", "<HTML><HEAD><TITLE>Not Found</TITLE>\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", "<HTML><HEAD><TITLE>Not Found</TITLE>\r\n",
KeyNotFoundException.class);
}
private void assertCodeMakes(String method, URI uri, int statusCode, String message, String contentType,
String content, Class<? extends Exception> 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<? extends Exception> 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;
}
}