From 29fbfb36b7220030be257b304a3723490a89c0fc Mon Sep 17 00:00:00 2001 From: danikov Date: Fri, 10 Feb 2012 17:38:14 +0000 Subject: [PATCH] improve expect test --- .../features/NetworkClientExpectTest.java | 196 +++++++++++++----- .../src/test/resources/network/error400.xml | 2 + .../resources/network/error403-catalog.xml | 2 + .../test/resources/network/error403-fake.xml | 2 + 4 files changed, 146 insertions(+), 56 deletions(-) create mode 100644 labs/vcloud-director/src/test/resources/network/error400.xml create mode 100644 labs/vcloud-director/src/test/resources/network/error403-catalog.xml create mode 100644 labs/vcloud-director/src/test/resources/network/error403-fake.xml diff --git a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/NetworkClientExpectTest.java b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/NetworkClientExpectTest.java index 7d80c72009..46e887a551 100644 --- a/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/NetworkClientExpectTest.java +++ b/labs/vcloud-director/src/test/java/org/jclouds/vcloud/director/v1_5/features/NetworkClientExpectTest.java @@ -18,12 +18,15 @@ */ package org.jclouds.vcloud.director.v1_5.features; -import static org.testng.Assert.*; +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.fail; import java.net.URI; import org.jclouds.vcloud.director.v1_5.VCloudDirectorClient; +import org.jclouds.vcloud.director.v1_5.VCloudDirectorException; import org.jclouds.vcloud.director.v1_5.VCloudDirectorMediaType; +import org.jclouds.vcloud.director.v1_5.domain.Error; import org.jclouds.vcloud.director.v1_5.domain.IpAddresses; import org.jclouds.vcloud.director.v1_5.domain.IpRange; import org.jclouds.vcloud.director.v1_5.domain.IpRanges; @@ -48,60 +51,102 @@ public class NetworkClientExpectTest extends BaseVCloudDirectorRestClientExpectT @Test public void testWhenResponseIs2xxLoginReturnsValidNetwork() { - URI networkRef = URI.create("https://vcloudbeta.bluelock.com/api/network/55a677cf-ab3f-48ae-b880-fab90421980c"); + URI networkUri = URI.create(endpoint + "/network/55a677cf-ab3f-48ae-b880-fab90421980c"); VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse, - getStandardRequest("GET", networkRef), + getStandardRequest("GET", "/network/55a677cf-ab3f-48ae-b880-fab90421980c"), getStandardPayloadResponse("/network/network.xml", VCloudDirectorMediaType.ORG_NETWORK)); - OrgNetwork expected = OrgNetwork - .builder() - .name("internet01-Jclouds") - .id("urn:vcloud:network:55a677cf-ab3f-48ae-b880-fab90421980c") - .type(VCloudDirectorMediaType.ORG_NETWORK) - .href(URI.create("https://vcloudbeta.bluelock.com/api/network/55a677cf-ab3f-48ae-b880-fab90421980c")) - .link(Link.builder() - .rel("up") - .type("application/vnd.vmware.vcloud.org+xml") - .href(URI.create("https://vcloudbeta.bluelock.com/api/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0")) - .build()) - .link(Link.builder() - .rel("down") - .type("application/vnd.vmware.vcloud.metadata+xml") - .href(URI.create("https://vcloudbeta.bluelock.com/api/network/55a677cf-ab3f-48ae-b880-fab90421980c/metadata")) - .build()) - .description("") - .configuration(NetworkConfiguration.builder() - .ipScope(IpScope.builder() - .isInherited(true) - .gateway("173.240.107.49") - .netmask("255.255.255.240") - .dns1("173.240.111.52") - .dns2("173.240.111.53") - .ipRanges(IpRanges.builder() - .ipRange(IpRange.builder() - .startAddress("173.240.107.50") - .endAddress("173.240.107.62") - .build()) - .build()) - .build()) - .fenceMode("bridged") - .retainNetInfoAcrossDeployments(false) - .syslogServerSettings(SyslogServerSettings.builder().build()) - .build()) - .allowedExternalIpAddresses(IpAddresses.builder().build()) - .build(); + OrgNetwork expected = orgNetwork(); + + Reference networkRef = Reference.builder().href(networkUri).build(); assertEquals(client.getNetworkClient().getNetwork(networkRef), expected); } + + @Test + public void testWhenResponseIs400ForInvalidNetworkId() { + URI networkUri = URI.create(endpoint + "/network/NOTAUUID"); + + VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse, + getStandardRequest("GET", "/network/NOTAUUID"), + getStandardPayloadResponse(400, "/network/error400.xml", VCloudDirectorMediaType.ERROR)); + + Error expected = Error.builder() + .message("validation error : EntityRef has incorrect type, expected type is com.vmware.vcloud.entity.network.") + .majorErrorCode(400) + .minorErrorCode("BAD_REQUEST") + .build(); + + Reference networkRef = Reference.builder().href(networkUri).build(); + try { + client.getNetworkClient().getNetwork(networkRef); + fail("Should give HTTP 400 error"); + } catch (VCloudDirectorException vde) { + assertEquals(vde.getError(), expected); + } catch (Exception e) { + fail("Should have thrown a VCloudDirectorException"); + } + } + + @Test + public void testWhenResponseIs403ForCatalogIdUsedAsNetworkId() { + URI networkUri = URI.create(endpoint + "/network/9e08c2f6-077a-42ce-bece-d5332e2ebb5c"); + + VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse, + getStandardRequest("GET", "/network/9e08c2f6-077a-42ce-bece-d5332e2ebb5c"), + getStandardPayloadResponse(403, "/network/error403-catalog.xml", VCloudDirectorMediaType.ERROR)); + + Error expected = Error.builder() + .message("This operation is denied.") + .majorErrorCode(403) + .minorErrorCode("ACCESS_TO_RESOURCE_IS_FORBIDDEN") + .build(); + + Reference networkRef = Reference.builder().href(networkUri).build(); + + try { + client.getNetworkClient().getNetwork(networkRef); + fail("Should give HTTP 403 error"); + } catch (VCloudDirectorException vde) { + assertEquals(vde.getError(), expected); + } catch (Exception e) { + fail("Should have thrown a VCloudDirectorException"); + } + } + + @Test + public void testWhenResponseIs403ForFakeNetworkId() { + URI networkUri = URI.create(endpoint + "/network/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"); + + VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse, + getStandardRequest("GET", "/network/aaaaaaaa-bbbb-cccc-dddd-eeeeeeeeeeee"), + getStandardPayloadResponse(403, "/network/error403-fake.xml", VCloudDirectorMediaType.ERROR)); + + Error expected = Error.builder() + .message("This operation is denied.") + .majorErrorCode(403) + .minorErrorCode("ACCESS_TO_RESOURCE_IS_FORBIDDEN") + .build(); + + Reference networkRef = Reference.builder().href(networkUri).build(); + + try { + client.getNetworkClient().getNetwork(networkRef); + fail("Should give HTTP 403 error"); + } catch (VCloudDirectorException vde) { + assertEquals(vde.getError(), expected); + } catch (Exception e) { + fail("Should have thrown a VCloudDirectorException"); + } + } @Test - public void testWhenResponseIs2xxLoginReturnsValidMetadata() { - URI orgUri = URI.create(endpoint + "/network/55a677cf-ab3f-48ae-b880-fab90421980c"); - URI metaUri = URI.create(orgUri.toASCIIString() + "/metadata/"); + public void testWhenResponseIs2xxLoginReturnsValidMetadataList() { + URI networkUri = URI.create(endpoint + "/network/55a677cf-ab3f-48ae-b880-fab90421980c"); VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse, - getStandardRequest("GET", metaUri), + getStandardRequest("GET", "/network/55a677cf-ab3f-48ae-b880-fab90421980c/metadata"), getStandardPayloadResponse("/network/metadata.xml", VCloudDirectorMediaType.METADATA)); Metadata expected = Metadata.builder() @@ -114,25 +159,64 @@ public class NetworkClientExpectTest extends BaseVCloudDirectorRestClientExpectT .build()) .build(); - // TODO change network client to use ReferenceType params - Reference orgRef = Reference.builder().href(orgUri).build(); - - assertEquals(client.getNetworkClient().getMetadata(orgUri), expected); + Reference networkRef = Reference.builder().href(networkUri).build(); + + assertEquals(client.getNetworkClient().getMetadata(networkRef), expected); } @Test(enabled=false) // No metadata in exemplar xml... - public void testWhenResponseIs2xxLoginReturnsValidMetadataEntry() { - URI metadataUri = URI.create(endpoint + "/network/55a677cf-ab3f-48ae-b880-fab90421980c/metadata/KEY"); + public void testWhenResponseIs2xxLoginReturnsValidMetadata() { + URI networkUri = URI.create("https://vcloudbeta.bluelock.com/api/network/55a677cf-ab3f-48ae-b880-fab90421980c"); VCloudDirectorClient client = requestsSendResponses(loginRequest, sessionResponse, - getStandardRequest("GET", metadataUri), - getStandardPayloadResponse("/network/metadata.xml", VCloudDirectorMediaType.METADATA_ENTRY)); + getStandardRequest("GET", "/network/55a677cf-ab3f-48ae-b880-fab90421980c/metadata/KEY"), + getStandardPayloadResponse("/network/metadataEntry.xml", VCloudDirectorMediaType.METADATA_ENTRY)); - MetadataEntry expected = MetadataEntry.builder().build(); + MetadataEntry expected = MetadataEntry.builder() + .key("KEY") + .build(); - // TODO change network client to use ReferenceType params - Reference orgRef = Reference.builder().href(metadataUri).build(); + Reference networkRef = Reference.builder().href(networkUri).build(); - assertEquals(client.getNetworkClient().getMetadataEntry(metadataUri), expected); + assertEquals(client.getOrgClient().getMetadataEntry(networkRef, "KEY"), expected); + } + + public static OrgNetwork orgNetwork() { + return OrgNetwork.builder() + .name("internet01-Jclouds") + .id("urn:vcloud:network:55a677cf-ab3f-48ae-b880-fab90421980c") + .type(VCloudDirectorMediaType.ORG_NETWORK) + .href(URI.create("https://vcloudbeta.bluelock.com/api/network/55a677cf-ab3f-48ae-b880-fab90421980c")) + .link(Link.builder() + .rel("up") + .type("application/vnd.vmware.vcloud.org+xml") + .href(URI.create("https://vcloudbeta.bluelock.com/api/org/6f312e42-cd2b-488d-a2bb-97519cd57ed0")) + .build()) + .link(Link.builder() + .rel("down") + .type("application/vnd.vmware.vcloud.metadata+xml") + .href(URI.create("https://vcloudbeta.bluelock.com/api/network/55a677cf-ab3f-48ae-b880-fab90421980c/metadata")) + .build()) + .description("") + .configuration(NetworkConfiguration.builder() + .ipScope(IpScope.builder() + .isInherited(true) + .gateway("173.240.107.49") + .netmask("255.255.255.240") + .dns1("173.240.111.52") + .dns2("173.240.111.53") + .ipRanges(IpRanges.builder() + .ipRange(IpRange.builder() + .startAddress("173.240.107.50") + .endAddress("173.240.107.62") + .build()) + .build()) + .build()) + .fenceMode("bridged") + .retainNetInfoAcrossDeployments(false) + .syslogServerSettings(SyslogServerSettings.builder().build()) + .build()) + .allowedExternalIpAddresses(IpAddresses.builder().build()) + .build(); } } diff --git a/labs/vcloud-director/src/test/resources/network/error400.xml b/labs/vcloud-director/src/test/resources/network/error400.xml new file mode 100644 index 0000000000..4013d6ff57 --- /dev/null +++ b/labs/vcloud-director/src/test/resources/network/error400.xml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/labs/vcloud-director/src/test/resources/network/error403-catalog.xml b/labs/vcloud-director/src/test/resources/network/error403-catalog.xml new file mode 100644 index 0000000000..38c90857ee --- /dev/null +++ b/labs/vcloud-director/src/test/resources/network/error403-catalog.xml @@ -0,0 +1,2 @@ + + \ No newline at end of file diff --git a/labs/vcloud-director/src/test/resources/network/error403-fake.xml b/labs/vcloud-director/src/test/resources/network/error403-fake.xml new file mode 100644 index 0000000000..38c90857ee --- /dev/null +++ b/labs/vcloud-director/src/test/resources/network/error403-fake.xml @@ -0,0 +1,2 @@ + + \ No newline at end of file