Merge pull request #523 from grkvlt/error

Issue 830: Use ResourceNotFoundException
This commit is contained in:
Adrian Cole 2012-03-27 22:47:41 -07:00
commit 018702a4c1
9 changed files with 74 additions and 78 deletions

View File

@ -21,6 +21,12 @@ package org.jclouds.vcloud.director.v1_5;
import org.jclouds.vcloud.director.v1_5.domain.Error;
import org.jclouds.vcloud.director.v1_5.domain.Task;
import com.google.common.base.CaseFormat;
import com.google.common.base.Function;
import com.google.common.base.Joiner;
import com.google.common.base.Splitter;
import com.google.common.collect.Iterables;
/**
* @author grkvlt@apache.org
*/
@ -29,19 +35,19 @@ public class VCloudDirectorException extends RuntimeException {
/** The serialVersionUID. */
private static final long serialVersionUID = -5292516858598372960L;
private static final String MSG_FMT = "%s: %s";
private static final String MSG_FMT = "%s (%d) %s: %s";
private final Error error;
private final Task task;
public VCloudDirectorException(Error error) {
super(String.format(MSG_FMT, "Error", error.getMessage()));
super(message(error, "Error"));
this.error = error;
this.task = null;
}
public VCloudDirectorException(Task task) {
super(String.format(MSG_FMT, "Task error", task.getError().getMessage()));
super(message(task.getError(), "Task error"));
this.error = task.getError();
this.task = task;
}
@ -65,4 +71,14 @@ public class VCloudDirectorException extends RuntimeException {
public Task getTask() {
return task;
}
private static String message(Error error, String from) {
Iterable<String> words = Iterables.transform(Splitter.on('_').split(error.getMinorErrorCode()), new Function<String, String>() {
@Override
public String apply(String input) {
return CaseFormat.UPPER_UNDERSCORE.to(CaseFormat.UPPER_CAMEL, input);
}
});
return String.format(MSG_FMT, Joiner.on(' ').join(words), error.getMajorErrorCode(), from, error.getMessage());
}
}

View File

@ -203,7 +203,7 @@ public class Error {
/**
* Specific API error code.
* <p/>
*
* For example - can indicate that vApp power on failed by some reason.
*/
public String getMinorErrorCode() {

View File

@ -26,6 +26,7 @@ import javax.xml.bind.JAXB;
import org.jclouds.http.HttpResponseException;
import org.jclouds.io.InputSuppliers;
import org.jclouds.rest.ResourceNotFoundException;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorException;
import org.jclouds.vcloud.director.v1_5.domain.Error;
@ -49,6 +50,9 @@ public class ThrowVCloudErrorOn4xx implements Function<Exception, Object> {
if (exception != null && exception.getResponse() != null && exception.getResponse().getStatusCode() >= 400 && exception.getResponse().getStatusCode() < 500) {
try {
Error error = JAXB.unmarshal(InputSuppliers.of(exception.getContent()).getInput(), Error.class);
if (exception.getResponse().getStatusCode() == 403 && error.getMinorErrorCode().equals("ACCESS_TO_RESOURCE_IS_FORBIDDEN")) {
throw new ResourceNotFoundException(error.getMessage());
}
throw new VCloudDirectorException(error);
} catch (IOException e) {
Throwables.propagate(e);

View File

@ -23,6 +23,7 @@ import static org.testng.Assert.fail;
import java.net.URI;
import org.jclouds.rest.ResourceNotFoundException;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorClient;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorException;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType;
@ -165,19 +166,15 @@ public class MediaClientExpectTest extends BaseVCloudDirectorRestClientExpectTes
.xmlFilePayload("/media/error403-catalog.xml", VCloudDirectorMediaType.ERROR)
.httpResponseBuilder().statusCode(403).build());
Error expected = Error.builder()
.message("No access to entity \"(com.vmware.vcloud.entity.media:e9cd3387-ac57-4d27-a481-9bee75e0690f)\".")
.majorErrorCode(403)
.minorErrorCode("ACCESS_TO_RESOURCE_IS_FORBIDDEN")
.build();
String message = "No access to entity \"(com.vmware.vcloud.entity.media:e9cd3387-ac57-4d27-a481-9bee75e0690f)\".";
try {
client.getMediaClient().getMedia(mediaUri);
fail("Should give HTTP 403 error");
} catch (VCloudDirectorException vde) {
assertEquals(vde.getError(), expected);
} catch (ResourceNotFoundException rnfe) {
assertEquals(rnfe.getMessage(), message);
} catch (Exception e) {
fail("Should have thrown a VCloudDirectorException");
fail("Should have thrown a ResourceNotFoundException");
}
}
@ -194,19 +191,15 @@ public class MediaClientExpectTest extends BaseVCloudDirectorRestClientExpectTes
.xmlFilePayload("/media/error403-fake.xml", VCloudDirectorMediaType.ERROR)
.httpResponseBuilder().statusCode(403).build());
Error expected = Error.builder()
.message("No access to entity \"(com.vmware.vcloud.entity.media:aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee)\".")
.majorErrorCode(403)
.minorErrorCode("ACCESS_TO_RESOURCE_IS_FORBIDDEN")
.build();
String message = "No access to entity \"(com.vmware.vcloud.entity.media:aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee)\".";
try {
client.getMediaClient().getMedia(mediaUri);
fail("Should give HTTP 403 error");
} catch (VCloudDirectorException vde) {
assertEquals(vde.getError(), expected);
} catch (ResourceNotFoundException rnfe) {
assertEquals(rnfe.getMessage(), message);
} catch (Exception e) {
fail("Should have thrown a VCloudDirectorException");
fail("Should have thrown a ResourceNotFoundException");
}
}

View File

@ -23,6 +23,7 @@ import static org.testng.Assert.fail;
import java.net.URI;
import org.jclouds.rest.ResourceNotFoundException;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorClient;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorException;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType;
@ -113,19 +114,15 @@ public class NetworkClientExpectTest extends BaseVCloudDirectorRestClientExpectT
.xmlFilePayload("/network/error403-catalog.xml", VCloudDirectorMediaType.ERROR)
.httpResponseBuilder().statusCode(403).build());
Error expected = Error.builder()
.message("This operation is denied.")
.majorErrorCode(403)
.minorErrorCode("ACCESS_TO_RESOURCE_IS_FORBIDDEN")
.build();
String message = "This operation is denied.";
try {
client.getNetworkClient().getNetwork(networkUri);
fail("Should give HTTP 403 error");
} catch (VCloudDirectorException vde) {
assertEquals(vde.getError(), expected);
} catch (ResourceNotFoundException rnfe) {
assertEquals(rnfe.getMessage(), message);
} catch (Exception e) {
fail("Should have thrown a VCloudDirectorException");
fail("Should have thrown a ResourceNotFoundException");
}
}
@ -142,19 +139,15 @@ public class NetworkClientExpectTest extends BaseVCloudDirectorRestClientExpectT
.xmlFilePayload("/network/error403-fake.xml", VCloudDirectorMediaType.ERROR)
.httpResponseBuilder().statusCode(403).build());
Error expected = Error.builder()
.message("This operation is denied.")
.majorErrorCode(403)
.minorErrorCode("ACCESS_TO_RESOURCE_IS_FORBIDDEN")
.build();
String message = "This operation is denied.";
try {
client.getNetworkClient().getNetwork(networkUri);
fail("Should give HTTP 403 error");
} catch (VCloudDirectorException vde) {
assertEquals(vde.getError(), expected);
} catch (ResourceNotFoundException rnfe) {
assertEquals(rnfe.getMessage(), message);
} catch (Exception e) {
fail("Should have thrown a VCloudDirectorException");
fail("Should have thrown a ResourceNotFoundException");
}
}

View File

@ -23,6 +23,7 @@ import static org.testng.Assert.fail;
import java.net.URI;
import org.jclouds.rest.ResourceNotFoundException;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorClient;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorException;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType;
@ -127,19 +128,15 @@ public class OrgClientExpectTest extends BaseVCloudDirectorRestClientExpectTest
getStandardRequest("GET", "/org/9e08c2f6-077a-42ce-bece-d5332e2ebb5c"),
getStandardPayloadResponse(403, "/org/error403-catalog.xml", VCloudDirectorMediaType.ERROR));
Error expected = Error.builder()
.message("No access to entity \"com.vmware.vcloud.entity.org:9e08c2f6-077a-42ce-bece-d5332e2ebb5c\".")
.majorErrorCode(403)
.minorErrorCode("ACCESS_TO_RESOURCE_IS_FORBIDDEN")
.build();
String message = "No access to entity \"com.vmware.vcloud.entity.org:9e08c2f6-077a-42ce-bece-d5332e2ebb5c\".";
try {
client.getOrgClient().getOrg(orgUri);
fail("Should give HTTP 403 error");
} catch (VCloudDirectorException vde) {
assertEquals(vde.getError(), expected);
} catch (ResourceNotFoundException rnfe) {
assertEquals(rnfe.getMessage(), message);
} catch (Exception e) {
fail("Should have thrown a VCloudDirectorException");
fail("Should have thrown a ResourceNotFoundException");
}
}
@ -151,19 +148,15 @@ public class OrgClientExpectTest extends BaseVCloudDirectorRestClientExpectTest
getStandardRequest("GET", "/org/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"),
getStandardPayloadResponse(403, "/org/error403-fake.xml", VCloudDirectorMediaType.ERROR));
Error expected = Error.builder()
.message("No access to entity \"com.vmware.vcloud.entity.org:aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee\".")
.majorErrorCode(403)
.minorErrorCode("ACCESS_TO_RESOURCE_IS_FORBIDDEN")
.build();
String message = "No access to entity \"com.vmware.vcloud.entity.org:aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee\".";
try {
client.getOrgClient().getOrg(orgUri);
fail("Should give HTTP 403 error");
} catch (VCloudDirectorException vde) {
assertEquals(vde.getError(), expected);
} catch (ResourceNotFoundException rnfe) {
assertEquals(rnfe.getMessage(), message);
} catch (Exception e) {
fail("Should have thrown a VCloudDirectorException");
fail("Should have thrown a ResourceNotFoundException");
}
}

View File

@ -25,6 +25,7 @@ import java.net.URI;
import org.jclouds.http.HttpRequest;
import org.jclouds.http.HttpResponse;
import org.jclouds.rest.ResourceNotFoundException;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorClient;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorException;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType;
@ -162,20 +163,16 @@ public class TaskClientExpectTest extends BaseVCloudDirectorRestClientExpectTest
VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse, taskRequest, taskResponse, orgRequest, orgResponse);
Error expected = Error.builder()
.message("No access to entity \"com.vmware.vcloud.entity.org:aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee\".")
.majorErrorCode(403)
.minorErrorCode("ACCESS_TO_RESOURCE_IS_FORBIDDEN")
.build();
String message = "No access to entity \"com.vmware.vcloud.entity.org:aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee\".";
try {
client.getTaskClient().getTaskList(URI.create("https://vcloudbeta.bluelock.com/api/org/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"));
fail("Should give HTTP 403 error");
} catch (VCloudDirectorException vde) {
assertEquals(vde.getError(), expected);
} catch (Exception e) {
fail("Should have thrown a VCloudDirectorException");
}
} catch (ResourceNotFoundException rnfe) {
assertEquals(rnfe.getMessage(), message);
} catch (Exception e) {
fail("Should have thrown a ResourceNotFoundException");
}
}
@Test

View File

@ -38,6 +38,7 @@ import java.text.SimpleDateFormat;
import java.util.Locale;
import java.util.TimeZone;
import org.jclouds.rest.ResourceNotFoundException;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorException;
import org.jclouds.vcloud.director.v1_5.domain.CustomizationSection;
import org.jclouds.vcloud.director.v1_5.domain.FirewallRule;
@ -119,7 +120,7 @@ public class VAppTemplateClientExpectTest extends BaseVCloudDirectorRestClientEx
client.getVAppTemplate(uri);
}
@Test(expectedExceptions = VCloudDirectorException.class)
@Test(expectedExceptions = ResourceNotFoundException.class)
public void testErrorEditVAppTemplate() {
final String templateId = "/vAppTemplate/vappTemplate-ef4415e6-d413-4cbb-9262-f9bbec5f2ea9";
URI uri = URI.create(endpoint + templateId);
@ -157,7 +158,7 @@ public class VAppTemplateClientExpectTest extends BaseVCloudDirectorRestClientEx
assertNotNull(task);
}
@Test(expectedExceptions = VCloudDirectorException.class)
@Test(expectedExceptions = ResourceNotFoundException.class)
public void testConsolidateMissingVAppTemplate() {
final String templateId = "/vAppTemplate/vappTemplate-ef4415e6-d413-4cbb-9262-f9bbec5f2ea9";
URI uri = URI.create(endpoint + templateId);
@ -208,7 +209,7 @@ public class VAppTemplateClientExpectTest extends BaseVCloudDirectorRestClientEx
assertNotNull(task);
}
@Test(expectedExceptions = VCloudDirectorException.class)
@Test(expectedExceptions = ResourceNotFoundException.class)
public void testEnableDownloadMissingVAppTemplate() {
final String templateId = "/vAppTemplate/vappTemplate-ef4415e6-d413-4cbb-9262-f9bbec5f2ea9";
URI uri = URI.create(endpoint + templateId);
@ -274,7 +275,7 @@ public class VAppTemplateClientExpectTest extends BaseVCloudDirectorRestClientEx
assertNotNull(task);
}
@Test(expectedExceptions = VCloudDirectorException.class)
@Test(expectedExceptions = ResourceNotFoundException.class)
public void testErrorGetCustomizationSection() {
final String templateId = "/vAppTemplate/vappTemplate-ef4415e6-d413-4cbb-9262-f9bbec5f2ea9";
URI uri = URI.create(endpoint + templateId);
@ -286,7 +287,7 @@ public class VAppTemplateClientExpectTest extends BaseVCloudDirectorRestClientEx
client.getVAppTemplateCustomizationSection(uri);
}
@Test(expectedExceptions = VCloudDirectorException.class)
@Test(expectedExceptions = ResourceNotFoundException.class)
public void testErrorEditCustomizationSection() {
final String templateId = "/vAppTemplate/vappTemplate-ef4415e6-d413-4cbb-9262-f9bbec5f2ea9";
URI uri = URI.create(endpoint + templateId);
@ -362,7 +363,7 @@ public class VAppTemplateClientExpectTest extends BaseVCloudDirectorRestClientEx
assertNotNull(task);
}
@Test(expectedExceptions = VCloudDirectorException.class)
@Test(expectedExceptions = ResourceNotFoundException.class)
public void testErrorGetLeaseSettingsSection() {
final String templateId = "/vAppTemplate/vappTemplate-ef4415e6-d413-4cbb-9262-f9bbec5f2ea9";
URI uri = URI.create(endpoint + templateId);
@ -374,7 +375,7 @@ public class VAppTemplateClientExpectTest extends BaseVCloudDirectorRestClientEx
client.getVappTemplateLeaseSettingsSection(uri);
}
@Test(expectedExceptions = VCloudDirectorException.class)
@Test(expectedExceptions = ResourceNotFoundException.class)
public void testErrorEditLeaseSettingsSection() throws ParseException {
final String templateId = "/vAppTemplate/vappTemplate-ef4415e6-d413-4cbb-9262-f9bbec5f2ea9";
URI uri = URI.create(endpoint + templateId);
@ -455,7 +456,7 @@ public class VAppTemplateClientExpectTest extends BaseVCloudDirectorRestClientEx
assertNotNull(task);
}
@Test(expectedExceptions = VCloudDirectorException.class)
@Test(expectedExceptions = ResourceNotFoundException.class)
public void testErrorGetMetadataValue() {
final String templateId = "/vAppTemplate/vappTemplate-ef4415e6-d413-4cbb-9262-f9bbec5f2ea9";
URI uri = URI.create(endpoint + templateId);
@ -479,7 +480,7 @@ public class VAppTemplateClientExpectTest extends BaseVCloudDirectorRestClientEx
client.getMetadataClient().setMetadata(uri, "12345", exampleMetadataValue());
}
@Test(expectedExceptions = VCloudDirectorException.class)
@Test(expectedExceptions = ResourceNotFoundException.class)
public void testDeleteMissingMetadataValue() {
final String templateId = "/vAppTemplate/vappTemplate-ef4415e6-d413-4cbb-9262-f9bbec5f2ea9";
URI uri = URI.create(endpoint + templateId);

View File

@ -23,6 +23,7 @@ import static org.testng.Assert.fail;
import java.net.URI;
import org.jclouds.rest.ResourceNotFoundException;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorClient;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorException;
import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType;
@ -119,17 +120,15 @@ public class VdcClientExpectTest extends BaseVCloudDirectorRestClientExpectTest
.xmlFilePayload("/vdc/error403-fake.xml", VCloudDirectorMediaType.ERROR)
.httpResponseBuilder().statusCode(403).build());
Error expected = Error.builder()
.message("No access to entity \"com.vmware.vcloud.entity.vdc:aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee\".")
.majorErrorCode(403)
.minorErrorCode("ACCESS_TO_RESOURCE_IS_FORBIDDEN")
.build();
String message = "No access to entity \"com.vmware.vcloud.entity.vdc:aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee\".";
try {
client.getVdcClient().getVdc(URI.create(endpoint + "/vdc/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"));
fail("Should give HTTP 403 error");
} catch (VCloudDirectorException vde) {
assertEquals(vde.getError(), expected);
} catch (ResourceNotFoundException rnfe) {
assertEquals(rnfe.getMessage(), message);
} catch (Exception e) {
fail("Should have thrown a ResourceNotFoundException");
}
}