workaround for error handling on deltacloud

This commit is contained in:
Adrian Cole 2011-04-01 14:03:22 -07:00
parent eb4a8393fb
commit 12d927f40d
4 changed files with 38 additions and 34 deletions

View File

@ -50,10 +50,8 @@
</repositories>
<properties>
<!-- when instances are hung, open a ticket and add here -->
<jclouds.compute.blacklist-nodes>trmkrun-ccc,test.trmk-924</jclouds.compute.blacklist-nodes>
<test.deltacloud.endpoint>http://localhost:3001/api</test.deltacloud.endpoint>
<test.deltacloud.apiversion>0.1.2</test.deltacloud.apiversion>
<test.deltacloud.apiversion>0.3.0</test.deltacloud.apiversion>
<test.deltacloud.identity>mockuser</test.deltacloud.identity>
<test.deltacloud.credential>mockpassword</test.deltacloud.credential>
</properties>
@ -127,10 +125,6 @@
<name>test.deltacloud.credential</name>
<value>${test.deltacloud.credential}</value>
</property>
<property>
<name>jclouds.compute.blacklist-nodes</name>
<value>${jclouds.compute.blacklist-nodes}</value>
</property>
</systemProperties>
</configuration>
</execution>

View File

@ -34,7 +34,7 @@ public class DeltacloudPropertiesBuilder extends PropertiesBuilder {
@Override
protected Properties defaultProperties() {
Properties properties = super.defaultProperties();
properties.setProperty(PROPERTY_API_VERSION, "0.1.2");
properties.setProperty(PROPERTY_API_VERSION, "0.3.0");
return properties;
}

View File

@ -50,28 +50,32 @@ public class DeltacloudErrorHandler implements HttpErrorHandler {
// it is important to always read fully and close streams
String message = parseMessage(response);
Exception exception = message != null ? new HttpResponseException(command, response, message)
: new HttpResponseException(command, response);
: new HttpResponseException(command, response);
try {
message = message != null ? message : String.format("%s -> %s", command.getCurrentRequest().getRequestLine(),
response.getStatusLine());
switch (response.getStatusCode()) {
case 400:
exception = new IllegalArgumentException(message, exception);
break;
case 401:
exception = new AuthorizationException(message, exception);
break;
case 404:
if (!command.getCurrentRequest().getMethod().equals("DELETE")) {
exception = new ResourceNotFoundException(message, exception);
response.getStatusLine());
if (message.indexOf("ItemNotFound") != -1) {
exception = new ResourceNotFoundException(message, exception);
} else {
switch (response.getStatusCode()) {
case 400:
exception = new IllegalArgumentException(message, exception);
break;
case 401:
exception = new AuthorizationException(message, exception);
break;
case 404:
if (!command.getCurrentRequest().getMethod().equals("DELETE")) {
exception = new ResourceNotFoundException(message, exception);
}
break;
case 405:
exception = new IllegalArgumentException(message, exception);
break;
case 409:
exception = new IllegalStateException(message, exception);
break;
}
break;
case 405:
exception = new IllegalArgumentException(message, exception);
break;
case 409:
exception = new IllegalStateException(message, exception);
break;
}
} finally {
if (response.getPayload() != null)

View File

@ -49,25 +49,31 @@ public class DeltacloudErrorHandlerTest {
@Test
public void test400MakesIllegalArgumentException() {
assertCodeMakes("GET", URI.create("https://deltacloud.com/foo"), 400, "", "Bad Request",
IllegalArgumentException.class);
IllegalArgumentException.class);
}
@Test
public void test401MakesAuthorizationException() {
assertCodeMakes("GET", URI.create("https://deltacloud.com/foo"), 401, "", "Unauthorized",
AuthorizationException.class);
AuthorizationException.class);
}
@Test
public void test404MakesResourceNotFoundException() {
assertCodeMakes("GET", URI.create("https://deltacloud.com/foo"), 404, "", "Not Found",
ResourceNotFoundException.class);
ResourceNotFoundException.class);
}
@Test
public void testItemNotFoundMakesResourceNotFoundException() {
assertCodeMakes("GET", URI.create("https://deltacloud.com/foo"), 500, "", "ItemNotFound",
ResourceNotFoundException.class);
}
@Test
public void test405MakesIllegalArgumentException() {
assertCodeMakes("GET", URI.create("https://deltacloud.com/foo"), 405, "", "Method Not Allowed",
IllegalArgumentException.class);
IllegalArgumentException.class);
}
@Test
@ -76,19 +82,19 @@ public class DeltacloudErrorHandlerTest {
}
private void assertCodeMakes(String method, URI uri, int statusCode, String message, String content,
Class<? extends Exception> expected) {
Class<? extends Exception> expected) {
assertCodeMakes(method, uri, statusCode, message, "text/xml", content, expected);
}
private void assertCodeMakes(String method, URI uri, int statusCode, String message, String contentType,
String content, Class<? extends Exception> expected) {
String content, Class<? extends Exception> expected) {
DeltacloudErrorHandler function = Guice.createInjector().getInstance(DeltacloudErrorHandler.class);
HttpCommand command = createMock(HttpCommand.class);
HttpRequest request = new HttpRequest(method, uri);
HttpResponse response = new HttpResponse(statusCode, message, Payloads.newInputStreamPayload(Strings2
.toInputStream(content)));
.toInputStream(content)));
response.getPayload().getContentMetadata().setContentType(contentType);
expect(command.getCurrentRequest()).andReturn(request).atLeastOnce();