From 018771d3a44ab29db3b4f131b3429d1a65724af3 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Tue, 10 Jan 2012 16:44:06 -0800 Subject: [PATCH] refactored error on current transaction --- .../rest/BaseRestClientExpectTest.java | 30 +++++++-- .../features/VirtualGuestAsyncClient.java | 19 ++++-- .../handlers/SoftLayerErrorHandler.java | 8 ++- .../VirtualGuestClientExpectTest.java | 66 +++++++++++++++++++ .../SoftLayerErrorHandlerTest.java | 13 +++- 5 files changed, 121 insertions(+), 15 deletions(-) create mode 100644 providers/softlayer/src/test/java/org/jclouds/softlayer/features/VirtualGuestClientExpectTest.java rename providers/softlayer/src/test/java/org/jclouds/softlayer/{ => handlers}/SoftLayerErrorHandlerTest.java (90%) diff --git a/core/src/test/java/org/jclouds/rest/BaseRestClientExpectTest.java b/core/src/test/java/org/jclouds/rest/BaseRestClientExpectTest.java index ea337dba49..30124db3fe 100644 --- a/core/src/test/java/org/jclouds/rest/BaseRestClientExpectTest.java +++ b/core/src/test/java/org/jclouds/rest/BaseRestClientExpectTest.java @@ -209,9 +209,13 @@ public abstract class BaseRestClientExpectTest { * @return a client configured with this behavior */ public S requestSendsResponse(HttpRequest request, HttpResponse response) { - return requestsSendResponses(ImmutableMap.of(request, response)); + return requestSendsResponse(request, response, createModule()); } - + + public S requestSendsResponse(HttpRequest request, HttpResponse response, Module module) { + return requestsSendResponses(ImmutableMap.of(request, response), module); + } + /** * creates a client for a mock server which only responds to two types of requests * @@ -227,7 +231,12 @@ public abstract class BaseRestClientExpectTest { */ public S requestsSendResponses(HttpRequest requestA, HttpResponse responseA, HttpRequest requestB, HttpResponse responseB) { - return requestsSendResponses(ImmutableMap.of(requestA, responseA, requestB, responseB)); + return requestsSendResponses(requestA, responseA, requestB, responseB, createModule()); + } + + public S requestsSendResponses(HttpRequest requestA, HttpResponse responseA, HttpRequest requestB, + HttpResponse responseB, Module module) { + return requestsSendResponses(ImmutableMap.of(requestA, responseA, requestB, responseB), module); } /** @@ -249,9 +258,14 @@ public abstract class BaseRestClientExpectTest { */ public S requestsSendResponses(HttpRequest requestA, HttpResponse responseA, HttpRequest requestB, HttpResponse responseB, HttpRequest requestC, HttpResponse responseC) { - return requestsSendResponses(ImmutableMap.of(requestA, responseA, requestB, responseB, requestC, responseC)); + return requestsSendResponses(requestA, responseA, requestB, responseB, requestC, responseC, createModule()); } - + + public S requestsSendResponses(HttpRequest requestA, HttpResponse responseA, HttpRequest requestB, + HttpResponse responseB, HttpRequest requestC, HttpResponse responseC, Module module) { + return requestsSendResponses(ImmutableMap.of(requestA, responseA, requestB, responseB, requestC, responseC), module); + } + /** * creates a client for a mock server which returns responses for requests based on the supplied * Map parameter. @@ -261,7 +275,11 @@ public abstract class BaseRestClientExpectTest { * @return a client configured with this behavior */ public S requestsSendResponses(Map requestToResponse) { - return createClient(Functions.forMap(requestToResponse)); + return requestsSendResponses(requestToResponse, createModule()); + } + + public S requestsSendResponses(Map requestToResponse, Module module) { + return createClient(Functions.forMap(requestToResponse), module); } public String renderRequest(HttpRequest request) { diff --git a/providers/softlayer/src/main/java/org/jclouds/softlayer/features/VirtualGuestAsyncClient.java b/providers/softlayer/src/main/java/org/jclouds/softlayer/features/VirtualGuestAsyncClient.java index 3befe69947..8ad1d7da26 100644 --- a/providers/softlayer/src/main/java/org/jclouds/softlayer/features/VirtualGuestAsyncClient.java +++ b/providers/softlayer/src/main/java/org/jclouds/softlayer/features/VirtualGuestAsyncClient.java @@ -18,13 +18,22 @@ */ package org.jclouds.softlayer.features; -import com.google.common.util.concurrent.ListenableFuture; +import java.util.Set; + +import javax.ws.rs.Consumes; +import javax.ws.rs.GET; +import javax.ws.rs.POST; +import javax.ws.rs.Path; +import javax.ws.rs.PathParam; +import javax.ws.rs.core.MediaType; + import org.jclouds.http.filters.BasicAuthentication; import org.jclouds.rest.annotations.BinderParam; import org.jclouds.rest.annotations.ExceptionParser; import org.jclouds.rest.annotations.QueryParams; import org.jclouds.rest.annotations.RequestFilters; import org.jclouds.rest.functions.ReturnEmptySetOnNotFoundOr404; +import org.jclouds.rest.functions.ReturnFalseOnNotFoundOr404; import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404; import org.jclouds.rest.functions.ReturnVoidOnNotFoundOr404; import org.jclouds.softlayer.binders.ProductOrderToJson; @@ -32,9 +41,7 @@ import org.jclouds.softlayer.domain.ProductOrder; import org.jclouds.softlayer.domain.ProductOrderReceipt; import org.jclouds.softlayer.domain.VirtualGuest; -import javax.ws.rs.*; -import javax.ws.rs.core.MediaType; -import java.util.Set; +import com.google.common.util.concurrent.ListenableFuture; /** * Provides asynchronous access to VirtualGuest via their REST API. @@ -121,7 +128,7 @@ public interface VirtualGuestAsyncClient { @GET @Path("/SoftLayer_Billing_Item/{id}/cancelService.json") @Consumes(MediaType.APPLICATION_JSON) - @ExceptionParser(ReturnVoidOnNotFoundOr404.class) + @ExceptionParser(ReturnFalseOnNotFoundOr404.class) ListenableFuture cancelService(@PathParam("id") long id); /** @@ -141,7 +148,7 @@ public interface VirtualGuestAsyncClient { @GET @Path("SoftLayer_Virtual_Guest/{id}/getOrderTemplate/MONTHLY.json") @Consumes(MediaType.APPLICATION_JSON) - @ExceptionParser(ReturnVoidOnNotFoundOr404.class) + @ExceptionParser(ReturnNullOnNotFoundOr404.class) ListenableFuture getOrderTemplate(@PathParam("id") long id); } diff --git a/providers/softlayer/src/main/java/org/jclouds/softlayer/handlers/SoftLayerErrorHandler.java b/providers/softlayer/src/main/java/org/jclouds/softlayer/handlers/SoftLayerErrorHandler.java index 8eb9887a69..d67d41aedc 100644 --- a/providers/softlayer/src/main/java/org/jclouds/softlayer/handlers/SoftLayerErrorHandler.java +++ b/providers/softlayer/src/main/java/org/jclouds/softlayer/handlers/SoftLayerErrorHandler.java @@ -61,8 +61,12 @@ public class SoftLayerErrorHandler implements HttpErrorHandler { } break; case 500: - if (message != null && message.indexOf("Unable to determine package for") != -1) { - exception = new ResourceNotFoundException(message, exception); + if (message != null ){ + if (message.indexOf("Unable to determine package for") != -1) { + exception = new ResourceNotFoundException(message, exception); + } else if (message.indexOf("currently an active transaction") != -1) { + exception = new IllegalStateException(message, exception); + } } } } finally { diff --git a/providers/softlayer/src/test/java/org/jclouds/softlayer/features/VirtualGuestClientExpectTest.java b/providers/softlayer/src/test/java/org/jclouds/softlayer/features/VirtualGuestClientExpectTest.java new file mode 100644 index 0000000000..d4c8124891 --- /dev/null +++ b/providers/softlayer/src/test/java/org/jclouds/softlayer/features/VirtualGuestClientExpectTest.java @@ -0,0 +1,66 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you 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.softlayer.features; + +import java.net.URI; + +import org.jclouds.http.HttpRequest; +import org.jclouds.http.HttpResponse; +import org.jclouds.rest.BaseRestClientExpectTest; +import org.jclouds.softlayer.SoftLayerClient; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableMultimap; + +/** + * + * @author Adrian Cole + */ +@Test(groups = "unit", testName = "SoftLayerClientExpectTest") +public class VirtualGuestClientExpectTest extends BaseRestClientExpectTest { + + + public VirtualGuestClientExpectTest() { + provider = "softlayer"; + } + + public void testCancelGuestReturnsTrueOn200AndFalseOn404() { + + HttpRequest cancelGuest11 = HttpRequest.builder().method("GET") + .endpoint(URI.create("https://api.softlayer.com/rest/v3/SoftLayer_Billing_Item/11/cancelService.json")) + .headers( + ImmutableMultimap. builder() + .put("Authorization", "Basic aWRlbnRpdHk6Y3JlZGVudGlhbA==") + .put("Accept", "application/json").build()).build(); + + HttpResponse found = HttpResponse.builder().statusCode(200).build(); + + SoftLayerClient clientWhenServiceExists = requestSendsResponse(cancelGuest11, found); + + assert clientWhenServiceExists.getVirtualGuestClient().cancelService(11l); + + + HttpResponse notFound = HttpResponse.builder().statusCode(404).build(); + + SoftLayerClient clientWhenServiceDoesntExist = requestSendsResponse(cancelGuest11, notFound); + + assert !clientWhenServiceDoesntExist.getVirtualGuestClient().cancelService(11l); + + } +} \ No newline at end of file diff --git a/providers/softlayer/src/test/java/org/jclouds/softlayer/SoftLayerErrorHandlerTest.java b/providers/softlayer/src/test/java/org/jclouds/softlayer/handlers/SoftLayerErrorHandlerTest.java similarity index 90% rename from providers/softlayer/src/test/java/org/jclouds/softlayer/SoftLayerErrorHandlerTest.java rename to providers/softlayer/src/test/java/org/jclouds/softlayer/handlers/SoftLayerErrorHandlerTest.java index c30b7b798b..1a083a51bf 100644 --- a/providers/softlayer/src/test/java/org/jclouds/softlayer/SoftLayerErrorHandlerTest.java +++ b/providers/softlayer/src/test/java/org/jclouds/softlayer/handlers/SoftLayerErrorHandlerTest.java @@ -16,7 +16,7 @@ * specific language governing permissions and limitations * under the License. */ -package org.jclouds.softlayer; +package org.jclouds.softlayer.handlers; import static org.easymock.EasyMock.expect; import static org.easymock.EasyMock.reportMatcher; @@ -56,6 +56,17 @@ public class SoftLayerErrorHandlerTest { "{\"error\":\"Unable to determine package for 'node2102835255.me.org'.\"}", ResourceNotFoundException.class); } + + @Test + public void test500MakesIllegalStateExceptionOnActiveTransaction() { + assertCodeMakes( + "GET", + URI.create("https://api.softlayer.com/rest/v3/SoftLayer_Billing_Item/8676376/cancelService.json"), + 500, + "", + "{\"error\":\"There is currently an active transaction.\"}", + IllegalStateException.class); + } @Test public void test401MakesAuthorizationException() {