refactored error on current transaction

This commit is contained in:
Adrian Cole 2012-01-10 16:44:06 -08:00
parent ba0725286a
commit 018771d3a4
5 changed files with 121 additions and 15 deletions

View File

@ -209,7 +209,11 @@ public abstract class BaseRestClientExpectTest<S> {
* @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);
}
/**
@ -227,7 +231,12 @@ public abstract class BaseRestClientExpectTest<S> {
*/
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,7 +258,12 @@ public abstract class BaseRestClientExpectTest<S> {
*/
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);
}
/**
@ -261,7 +275,11 @@ public abstract class BaseRestClientExpectTest<S> {
* @return a client configured with this behavior
*/
public S requestsSendResponses(Map<HttpRequest, HttpResponse> requestToResponse) {
return createClient(Functions.forMap(requestToResponse));
return requestsSendResponses(requestToResponse, createModule());
}
public S requestsSendResponses(Map<HttpRequest, HttpResponse> requestToResponse, Module module) {
return createClient(Functions.forMap(requestToResponse), module);
}
public String renderRequest(HttpRequest request) {

View File

@ -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<Boolean> 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<ProductOrder> getOrderTemplate(@PathParam("id") long id);
}

View File

@ -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 {

View File

@ -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<SoftLayerClient> {
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.<String, String> 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);
}
}

View File

@ -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;
@ -57,6 +57,17 @@ public class SoftLayerErrorHandlerTest {
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() {
assertCodeMakes("GET", URI.create("https://api.softlayer.com/foo"), 401, "", "Unauthorized",