From 06b99c1cdc2af6a7fbe900968798b57f6b11a1d0 Mon Sep 17 00:00:00 2001 From: Dmitri Babaev <-> Date: Mon, 18 Apr 2011 21:37:01 +0400 Subject: [PATCH] unit test fixes --- .../compute/functions/NovaImageToImage.java | 3 - .../functions/ServerToNodeMetadata.java | 62 ++++----- .../openstack/nova/domain/Address.java | 1 + .../openstack/nova/domain/Resource.java | 10 +- .../functions/FlavorToHardwareTest.java | 17 ++- .../functions/NovaImageToImageTest.java | 39 +++--- .../functions/ServerToNodeMetadataTest.java | 128 +++++++++++------- .../ParseAddressesFromJsonResponseTest.java | 21 +-- ...seInetAddressListFromJsonResponseTest.java | 3 +- .../openstack/nova/functions/ParseModule.java | 19 --- .../ParseServerFromJsonResponseTest.java | 57 ++++---- .../ParseServerListFromJsonResponseTest.java | 58 +++++--- .../resources/test_get_image_details.json | 2 +- .../resources/test_get_server_detail.json | 4 +- 14 files changed, 239 insertions(+), 185 deletions(-) delete mode 100644 sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/functions/ParseModule.java diff --git a/sandbox-apis/nova/src/main/java/org/jclouds/openstack/nova/compute/functions/NovaImageToImage.java b/sandbox-apis/nova/src/main/java/org/jclouds/openstack/nova/compute/functions/NovaImageToImage.java index 6a880dd9fc..e606c5b13a 100644 --- a/sandbox-apis/nova/src/main/java/org/jclouds/openstack/nova/compute/functions/NovaImageToImage.java +++ b/sandbox-apis/nova/src/main/java/org/jclouds/openstack/nova/compute/functions/NovaImageToImage.java @@ -28,9 +28,6 @@ import org.jclouds.domain.Credentials; import com.google.common.base.Function; -import java.util.List; -import java.util.Map; - /** * * @author Adrian Cole diff --git a/sandbox-apis/nova/src/main/java/org/jclouds/openstack/nova/compute/functions/ServerToNodeMetadata.java b/sandbox-apis/nova/src/main/java/org/jclouds/openstack/nova/compute/functions/ServerToNodeMetadata.java index 2e131ad622..715a2521a0 100644 --- a/sandbox-apis/nova/src/main/java/org/jclouds/openstack/nova/compute/functions/ServerToNodeMetadata.java +++ b/sandbox-apis/nova/src/main/java/org/jclouds/openstack/nova/compute/functions/ServerToNodeMetadata.java @@ -18,39 +18,32 @@ */ package org.jclouds.openstack.nova.compute.functions; -import static com.google.common.base.Preconditions.checkNotNull; -import static org.jclouds.compute.util.ComputeServiceUtils.parseGroupFromName; - -import java.util.Map; -import java.util.NoSuchElementException; -import java.util.Set; - -import javax.annotation.Resource; -import javax.inject.Inject; -import javax.inject.Named; -import javax.inject.Singleton; - -import org.jclouds.openstack.nova.domain.Address; -import org.jclouds.openstack.nova.domain.Server; -import org.jclouds.openstack.nova.domain.ServerStatus; +import com.google.common.base.Function; +import com.google.common.base.Predicate; +import com.google.common.base.Supplier; +import com.google.common.collect.Iterables; import org.jclouds.collect.Memoized; -import org.jclouds.compute.domain.Hardware; -import org.jclouds.compute.domain.Image; -import org.jclouds.compute.domain.NodeMetadata; -import org.jclouds.compute.domain.NodeMetadataBuilder; -import org.jclouds.compute.domain.NodeState; -import org.jclouds.compute.domain.OperatingSystem; +import org.jclouds.compute.domain.*; import org.jclouds.compute.reference.ComputeServiceConstants; import org.jclouds.domain.Credentials; import org.jclouds.domain.Location; import org.jclouds.domain.LocationBuilder; import org.jclouds.domain.LocationScope; import org.jclouds.logging.Logger; +import org.jclouds.openstack.nova.domain.Address; +import org.jclouds.openstack.nova.domain.Server; +import org.jclouds.openstack.nova.domain.ServerStatus; -import com.google.common.base.Function; -import com.google.common.base.Predicate; -import com.google.common.base.Supplier; -import com.google.common.collect.Iterables; +import javax.annotation.Resource; +import javax.inject.Inject; +import javax.inject.Named; +import javax.inject.Singleton; +import java.util.Map; +import java.util.NoSuchElementException; +import java.util.Set; + +import static com.google.common.base.Preconditions.checkNotNull; +import static org.jclouds.compute.util.ComputeServiceUtils.parseGroupFromName; /** * @author Adrian Cole @@ -95,8 +88,8 @@ public class ServerToNodeMetadata implements Function { @Inject ServerToNodeMetadata(Map serverStateToNodeState, Map credentialStore, - @Memoized Supplier> images, Supplier location, - @Memoized Supplier> hardwares) { + @Memoized Supplier> images, Supplier location, + @Memoized Supplier> hardwares) { this.serverToNodeState = checkNotNull(serverStateToNodeState, "serverStateToNodeState"); this.credentialStore = checkNotNull(credentialStore, "credentialStore"); this.images = checkNotNull(images, "images"); @@ -110,11 +103,14 @@ public class ServerToNodeMetadata implements Function { builder.ids(from.getId() + ""); builder.name(from.getName()); builder.location(new LocationBuilder().scope(LocationScope.HOST).id(from.getHostId()).description( - from.getHostId()).parent(location.get()).build()); + from.getHostId()).parent(location.get()).build()); builder.userMetadata(from.getMetadata()); builder.group(parseGroupFromName(from.getName())); - builder.imageId(from.getImageRef() + ""); - builder.operatingSystem(parseOperatingSystem(from)); + Image image = parseImage(from); + if (image != null) { + builder.imageId(image.getId()); + builder.operatingSystem(image.getOperatingSystem()); + } builder.hardware(parseHardware(from)); builder.state(serverToNodeState.get(from.getStatus())); builder.publicAddresses(Iterables.transform(from.getAddresses().getPublicAddresses(), Address.newAddress2StringFunction())); @@ -132,10 +128,10 @@ public class ServerToNodeMetadata implements Function { } return null; } - - protected OperatingSystem parseOperatingSystem(Server from) { + + protected Image parseImage(Server from) { try { - return Iterables.find(images.get(), new FindImageForServer(from)).getOperatingSystem(); + return Iterables.find(images.get(), new FindImageForServer(from)); } catch (NoSuchElementException e) { logger.warn("could not find a matching image for server %s in location %s", from, location); } diff --git a/sandbox-apis/nova/src/main/java/org/jclouds/openstack/nova/domain/Address.java b/sandbox-apis/nova/src/main/java/org/jclouds/openstack/nova/domain/Address.java index 8ef964acb9..96a8538d99 100644 --- a/sandbox-apis/nova/src/main/java/org/jclouds/openstack/nova/domain/Address.java +++ b/sandbox-apis/nova/src/main/java/org/jclouds/openstack/nova/domain/Address.java @@ -14,6 +14,7 @@ public class Address { private int version; //for de-serialization + @SuppressWarnings("unused") private Address() { } diff --git a/sandbox-apis/nova/src/main/java/org/jclouds/openstack/nova/domain/Resource.java b/sandbox-apis/nova/src/main/java/org/jclouds/openstack/nova/domain/Resource.java index b97f29f2f4..6e6f9f241e 100644 --- a/sandbox-apis/nova/src/main/java/org/jclouds/openstack/nova/domain/Resource.java +++ b/sandbox-apis/nova/src/main/java/org/jclouds/openstack/nova/domain/Resource.java @@ -1,12 +1,13 @@ package org.jclouds.openstack.nova.domain; -import com.google.common.collect.Lists; - import java.net.URI; import java.net.URISyntaxException; import java.util.List; import java.util.Map; +import com.google.common.base.Functions; +import com.google.common.collect.Lists; + /** * @author Dmitri Babaev */ @@ -17,6 +18,11 @@ public class Resource { public URI getURI() { for (Map linkProperties : links) { try { + if (!Functions.forMap(linkProperties, "").apply("rel").equals("bookmark")) + continue; + if (!Functions.forMap(linkProperties, "").apply("type").contains("json")) + continue; + return new URI(linkProperties.get("href")); } catch (URISyntaxException e) { throw new RuntimeException(e); diff --git a/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/compute/functions/FlavorToHardwareTest.java b/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/compute/functions/FlavorToHardwareTest.java index fdab38a748..414f5ba06e 100644 --- a/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/compute/functions/FlavorToHardwareTest.java +++ b/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/compute/functions/FlavorToHardwareTest.java @@ -28,6 +28,8 @@ import org.jclouds.openstack.nova.domain.Flavor; import org.jclouds.openstack.nova.functions.ParseFlavorFromJsonResponseTest; import org.testng.annotations.Test; +import java.net.URI; +import java.net.URISyntaxException; import java.net.UnknownHostException; import static org.testng.Assert.assertEquals; @@ -40,11 +42,16 @@ public class FlavorToHardwareTest { Location provider = new LocationBuilder().scope(LocationScope.ZONE).id("dallas").description("description").build(); @Test - public void test() throws UnknownHostException { - assertEquals(convertFlavor(), new HardwareBuilder().ids("1").name("256 MB Server").processors( - ImmutableList.of(new Processor(1.0, 1.0))).ram(256).volumes( - ImmutableList.of(new VolumeBuilder().type(Volume.Type.LOCAL).size(10.0f).durable(true).bootDevice(true) - .build())).build()); + public void test() throws UnknownHostException, URISyntaxException { + Hardware flavor = convertFlavor(); + Hardware tempFlavor = new HardwareBuilder().ids("1").name("256 MB Server") + .processors(ImmutableList.of(new Processor(1.0, 1.0))) + .ram(256) + .volumes(ImmutableList.of( + new VolumeBuilder().type(Volume.Type.LOCAL).size(10.0f).durable(true).bootDevice(true).build())) + .uri(new URI("http://servers.api.openstack.org/1234/flavors/1")) + .build(); + assertEquals(flavor, tempFlavor); } public static Hardware convertFlavor() { diff --git a/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/compute/functions/NovaImageToImageTest.java b/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/compute/functions/NovaImageToImageTest.java index d5dc9151ea..c207d53357 100644 --- a/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/compute/functions/NovaImageToImageTest.java +++ b/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/compute/functions/NovaImageToImageTest.java @@ -18,8 +18,12 @@ */ package org.jclouds.openstack.nova.compute.functions; -import com.google.inject.AbstractModule; -import com.google.inject.Guice; +import static org.testng.Assert.assertEquals; + +import java.net.URI; +import java.net.URISyntaxException; +import java.net.UnknownHostException; + import org.jclouds.compute.config.BaseComputeServiceContextModule; import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.ImageBuilder; @@ -28,12 +32,11 @@ import org.jclouds.compute.domain.OsFamily; import org.jclouds.compute.reference.ComputeServiceConstants; import org.jclouds.domain.Credentials; import org.jclouds.json.Json; +import org.jclouds.json.config.GsonModule; import org.jclouds.openstack.nova.functions.ParseImageFromJsonResponseTest; import org.testng.annotations.Test; -import java.net.UnknownHostException; - -import static org.testng.Assert.assertEquals; +import com.google.inject.Guice; /** * @author Adrian Cole @@ -42,27 +45,23 @@ import static org.testng.Assert.assertEquals; public class NovaImageToImageTest { @Test - public void testApplyWhereImageNotFound() throws UnknownHostException { - assertEquals( - convertImage(), - new ImageBuilder() - .name("CentOS 5.2") - .operatingSystem( - new OperatingSystem.Builder().family(OsFamily.CENTOS).version("5.2").description("CentOS 5.2").is64Bit(true) - .build()).description("CentOS 5.2").defaultCredentials(new Credentials("root", null)) - .ids("2").version("1286712000000").build()); + public void testApplyWhereImageNotFound() throws UnknownHostException, URISyntaxException { + Image image = new ImageBuilder() + .name("CentOS 5.2") + .operatingSystem( + new OperatingSystem.Builder().family(OsFamily.CENTOS).version("5.2").description("CentOS 5.2").is64Bit(true) + .build()).description("CentOS 5.2").defaultCredentials(new Credentials("root", null)) + .ids("1").version("1286712000000").uri(new URI("http://servers.api.openstack.org/1234/images/1")).build(); + Image parsedImage = convertImage(); + + assertEquals(parsedImage, image); } public static Image convertImage() { org.jclouds.openstack.nova.domain.Image image = ParseImageFromJsonResponseTest.parseImage(); NovaImageToImage parser = new NovaImageToImage(new NovaImageToOperatingSystem(new BaseComputeServiceContextModule() { - }.provideOsVersionMap(new ComputeServiceConstants.ReferenceData(), Guice.createInjector(new AbstractModule() { - @Override - protected void configure() { - //To change body of implemented methods use File | Settings | File Templates. - } - }) + }.provideOsVersionMap(new ComputeServiceConstants.ReferenceData(), Guice.createInjector(new GsonModule()) .getInstance(Json.class)))); return parser.apply(image); diff --git a/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/compute/functions/ServerToNodeMetadataTest.java b/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/compute/functions/ServerToNodeMetadataTest.java index 674f75b38e..cdc5a13838 100644 --- a/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/compute/functions/ServerToNodeMetadataTest.java +++ b/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/compute/functions/ServerToNodeMetadataTest.java @@ -18,11 +18,25 @@ */ package org.jclouds.openstack.nova.compute.functions; -import com.google.common.base.Suppliers; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.ImmutableSet; -import org.jclouds.compute.domain.*; +import static org.testng.Assert.assertEquals; + +import java.net.URI; +import java.net.URISyntaxException; +import java.net.UnknownHostException; +import java.util.Map; +import java.util.Set; + +import org.jclouds.compute.domain.Hardware; +import org.jclouds.compute.domain.HardwareBuilder; +import org.jclouds.compute.domain.Image; +import org.jclouds.compute.domain.NodeMetadata; +import org.jclouds.compute.domain.NodeMetadataBuilder; +import org.jclouds.compute.domain.NodeState; +import org.jclouds.compute.domain.OperatingSystem; +import org.jclouds.compute.domain.OsFamily; +import org.jclouds.compute.domain.Processor; +import org.jclouds.compute.domain.Volume; +import org.jclouds.compute.domain.VolumeBuilder; import org.jclouds.domain.Credentials; import org.jclouds.domain.Location; import org.jclouds.domain.LocationBuilder; @@ -33,11 +47,10 @@ import org.jclouds.openstack.nova.domain.ServerStatus; import org.jclouds.openstack.nova.functions.ParseServerFromJsonResponseTest; import org.testng.annotations.Test; -import java.net.UnknownHostException; -import java.util.Map; -import java.util.Set; - -import static org.testng.Assert.assertEquals; +import com.google.common.base.Suppliers; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableSet; /** * @author Adrian Cole @@ -47,7 +60,7 @@ public class ServerToNodeMetadataTest { Location provider = new LocationBuilder().scope(LocationScope.ZONE).id("dallas").description("description").build(); @Test - public void testApplyWhereImageAndHardwareNotFoundButCredentialsFound() throws UnknownHostException, NoSuchMethodException, ClassNotFoundException { + public void testApplyWhereImageAndHardwareNotFoundButCredentialsFound() throws UnknownHostException, NoSuchMethodException, ClassNotFoundException, URISyntaxException { Credentials creds = new Credentials("root", "abdce"); Map serverStateToNodeState = NovaComputeServiceDependenciesModule.serverToNodeState; @@ -61,16 +74,13 @@ public class ServerToNodeMetadataTest { NodeMetadata metadata = parser.apply(server); - assertEquals(metadata, new NodeMetadataBuilder().state(NodeState.PENDING).publicAddresses( - ImmutableSet.of("67.23.10.132", "67.23.10.131")).privateAddresses(ImmutableSet.of("10.176.42.16")) - .imageId("2").id("1234").providerId("1234").name("sample-server").credentials(creds).location( - new LocationBuilder().scope(LocationScope.HOST).id("e4d909c290d0fb1ca068ffaddf22cbd0") - .description("e4d909c290d0fb1ca068ffaddf22cbd0").parent(provider).build()) - .userMetadata(ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1")).build()); + NodeMetadata constructedMetadata = newNodeMetadataBuilder() + .credentials(creds).build(); + assertEquals(metadata, constructedMetadata); } @Test - public void testApplyWhereImageAndHardwareNotFound() throws UnknownHostException, NoSuchMethodException, ClassNotFoundException { + public void testApplyWhereImageAndHardwareNotFound() throws UnknownHostException, NoSuchMethodException, ClassNotFoundException, URISyntaxException { Map serverStateToNodeState = NovaComputeServiceDependenciesModule.serverToNodeState; Set images = ImmutableSet.of(); Set hardwares = ImmutableSet.of(); @@ -82,17 +92,31 @@ public class ServerToNodeMetadataTest { NodeMetadata metadata = parser.apply(server); - assertEquals(metadata, new NodeMetadataBuilder().state(NodeState.PENDING).publicAddresses( - ImmutableSet.of("67.23.10.132", "67.23.10.131")).privateAddresses(ImmutableSet.of("10.176.42.16")) - .imageId("2").id("1234").providerId("1234").name("sample-server").location( - new LocationBuilder().scope(LocationScope.HOST).id("e4d909c290d0fb1ca068ffaddf22cbd0") - .description("e4d909c290d0fb1ca068ffaddf22cbd0").parent(provider).build()) - .userMetadata(ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1")).build()); + NodeMetadata constructedMetadata = newNodeMetadataBuilder().build(); + + assertEquals(metadata, constructedMetadata); } + private NodeMetadataBuilder newNodeMetadataBuilder() throws URISyntaxException { + return new NodeMetadataBuilder() + .state(NodeState.PENDING) + .publicAddresses(ImmutableSet.of("67.23.10.132", "::babe:67.23.10.132", "67.23.10.131", "::babe:4317:0A83")) + .privateAddresses(ImmutableSet.of("10.176.42.16", "::babe:10.176.42.16")) + .id("1234") + .providerId("1234") + .name("sample-server") + .location(new LocationBuilder() + .scope(LocationScope.HOST) + .id("e4d909c290d0fb1ca068ffaddf22cbd0") + .description("e4d909c290d0fb1ca068ffaddf22cbd0") + .parent(provider).build()) + .userMetadata(ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1")) + .uri(new URI("http://servers.api.openstack.org/1234/servers/1234")); + } + @Test - public void testApplyWhereImageFoundAndHardwareNotFound() throws UnknownHostException, NoSuchMethodException, ClassNotFoundException { + public void testApplyWhereImageFoundAndHardwareNotFound() throws UnknownHostException, NoSuchMethodException, ClassNotFoundException, URISyntaxException { Map serverStateToNodeState = NovaComputeServiceDependenciesModule.serverToNodeState; org.jclouds.compute.domain.Image jcImage = NovaImageToImageTest.convertImage(); Set images = ImmutableSet.of(jcImage); @@ -105,19 +129,21 @@ public class ServerToNodeMetadataTest { NodeMetadata metadata = parser.apply(server); - assertEquals(metadata, new NodeMetadataBuilder().state(NodeState.PENDING).publicAddresses( - ImmutableSet.of("67.23.10.132", "67.23.10.131")).privateAddresses(ImmutableSet.of("10.176.42.16")) - .imageId("2").operatingSystem( - new OperatingSystemBuilder().family(OsFamily.CENTOS).description("CentOS 5.2").version("5.2") - .is64Bit(true).build()).id("1234").providerId("1234").name("sample-server").location( - new LocationBuilder().scope(LocationScope.HOST).id("e4d909c290d0fb1ca068ffaddf22cbd0") - .description("e4d909c290d0fb1ca068ffaddf22cbd0").parent(provider).build()) - .userMetadata(ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1")).build()); + NodeMetadata constructedMetadata = newNodeMetadataBuilder() + .imageId("1") + .operatingSystem(new OperatingSystem.Builder() + .family(OsFamily.CENTOS) + .description("CentOS 5.2") + .version("5.2") + .is64Bit(true).build()) + .build(); + + assertEquals(metadata, constructedMetadata); } @Test - public void testApplyWhereImageAndHardwareFound() throws UnknownHostException, NoSuchMethodException, ClassNotFoundException { + public void testApplyWhereImageAndHardwareFound() throws UnknownHostException, NoSuchMethodException, ClassNotFoundException, URISyntaxException { Map serverStateToNodeState = NovaComputeServiceDependenciesModule.serverToNodeState; Set images = ImmutableSet.of(NovaImageToImageTest.convertImage()); Set hardwares = ImmutableSet.of(FlavorToHardwareTest.convertFlavor()); @@ -128,18 +154,28 @@ public class ServerToNodeMetadataTest { .ofInstance(provider), Suppliers.>ofInstance(hardwares)); NodeMetadata metadata = parser.apply(server); + + NodeMetadata constructedMetadata = newNodeMetadataBuilder() + .imageId("1") + .operatingSystem(new OperatingSystem.Builder() + .family(OsFamily.CENTOS) + .description("CentOS 5.2") + .version("5.2") + .is64Bit(true).build()) + .hardware(new HardwareBuilder() + .ids("1") + .name("256 MB Server") + .processors(ImmutableList.of(new Processor(1.0, 1.0))) + .ram(256) + .volumes(ImmutableList.of(new VolumeBuilder() + .type(Volume.Type.LOCAL) + .size(10.0f) + .durable(true) + .bootDevice(true).build())) + .uri(new URI("http://servers.api.openstack.org/1234/flavors/1")) + .build()) + .build(); - assertEquals(metadata, new NodeMetadataBuilder().state(NodeState.PENDING).publicAddresses( - ImmutableSet.of("67.23.10.132", "67.23.10.131")).privateAddresses(ImmutableSet.of("10.176.42.16")) - .imageId("2").hardware( - new HardwareBuilder().ids("1").name("256 MB Server").processors( - ImmutableList.of(new Processor(1.0, 1.0))).ram(256).volumes( - ImmutableList.of(new VolumeBuilder().type(Volume.Type.LOCAL).size(10.0f).durable(true) - .bootDevice(true).build())).build()).operatingSystem( - new OperatingSystemBuilder().family(OsFamily.CENTOS).description("CentOS 5.2").version("5.2") - .is64Bit(true).build()).id("1234").providerId("1234").name("sample-server").location( - new LocationBuilder().scope(LocationScope.HOST).id("e4d909c290d0fb1ca068ffaddf22cbd0") - .description("e4d909c290d0fb1ca068ffaddf22cbd0").parent(provider).build()) - .userMetadata(ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1")).build()); + assertEquals(metadata, constructedMetadata); } } diff --git a/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/functions/ParseAddressesFromJsonResponseTest.java b/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/functions/ParseAddressesFromJsonResponseTest.java index de27b9931c..a945d1522a 100644 --- a/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/functions/ParseAddressesFromJsonResponseTest.java +++ b/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/functions/ParseAddressesFromJsonResponseTest.java @@ -18,7 +18,9 @@ */ package org.jclouds.openstack.nova.functions; -import com.google.common.collect.ImmutableSet; +import com.google.common.collect.ImmutableList; +import com.google.common.collect.Iterables; +import com.google.common.collect.Sets; import com.google.inject.Guice; import com.google.inject.Injector; import com.google.inject.Key; @@ -33,8 +35,9 @@ import org.testng.annotations.Test; import java.io.InputStream; import java.net.UnknownHostException; +import java.util.List; -import static org.testng.Assert.assertEqualsNoOrder; +import static org.testng.Assert.assertTrue; /** * Tests behavior of {@code ParseAddressesFromJsonResponse} @@ -53,13 +56,15 @@ public class ParseAddressesFromJsonResponseTest { })); Addresses response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is))); - ImmutableSet
publicAddresses = ImmutableSet.of(new Address("67.23.10.132", 4) - , new Address("::babe:67.23.10.132", 6), new Address("67.23.10.131", 4), new Address("::babe:4317:0A83", 6)); + List
publicAddresses = ImmutableList.copyOf( + Iterables.transform(ImmutableList.of("67.23.10.132", "::babe:67.23.10.132", "67.23.10.131", "::babe:4317:0A83"), + Address.newString2AddressFunction())); - ImmutableSet
privateAddresses = ImmutableSet.of(new Address("10.176.42.16", 4), - new Address("::babe:10.176.42.16", 6)); + List
privateAddresses = ImmutableList.copyOf( + Iterables.transform(ImmutableList.of("10.176.42.16", "::babe:10.176.42.16"), + Address.newString2AddressFunction())); - assertEqualsNoOrder(response.getPublicAddresses().toArray(), publicAddresses.toArray()); - assertEqualsNoOrder(response.getPrivateAddresses().toArray(), privateAddresses.toArray()); + assertTrue(response.getPublicAddresses().equals(Sets.newHashSet(publicAddresses))); + assertTrue(response.getPrivateAddresses().equals(Sets.newHashSet(privateAddresses))); } } diff --git a/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/functions/ParseInetAddressListFromJsonResponseTest.java b/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/functions/ParseInetAddressListFromJsonResponseTest.java index e34ffaf23b..dbdc14c41f 100644 --- a/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/functions/ParseInetAddressListFromJsonResponseTest.java +++ b/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/functions/ParseInetAddressListFromJsonResponseTest.java @@ -26,6 +26,7 @@ import com.google.inject.TypeLiteral; import org.jclouds.http.HttpResponse; import org.jclouds.http.functions.UnwrapOnlyJsonValue; import org.jclouds.io.Payloads; +import org.jclouds.json.config.GsonModule; import org.jclouds.openstack.nova.domain.Address; import org.testng.annotations.Test; @@ -43,7 +44,7 @@ import static org.testng.Assert.assertEquals; @Test(groups = "unit") public class ParseInetAddressListFromJsonResponseTest { - Injector i = Guice.createInjector(new ParserModule()); + Injector i = Guice.createInjector(new GsonModule()); @Test public void testPublic() throws UnknownHostException { diff --git a/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/functions/ParseModule.java b/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/functions/ParseModule.java deleted file mode 100644 index 8392b741c6..0000000000 --- a/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/functions/ParseModule.java +++ /dev/null @@ -1,19 +0,0 @@ -package org.jclouds.openstack.nova.functions; - -import org.jclouds.json.config.GsonModule; - -/** - * Created by IntelliJ IDEA. - * User: VGalkin - * Date: 4/16/11 - * Time: 7:47 AM - * To change this template use File | Settings | File Templates. - */ -class ParserModule extends GsonModule { - - @Override - protected void configure() { - super.configure(); - bind(DateAdapter.class).to(Iso8601DateAdapter.class); - } -} diff --git a/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/functions/ParseServerFromJsonResponseTest.java b/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/functions/ParseServerFromJsonResponseTest.java index 70e12db6e0..d574659d8c 100644 --- a/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/functions/ParseServerFromJsonResponseTest.java +++ b/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/functions/ParseServerFromJsonResponseTest.java @@ -18,21 +18,7 @@ */ package org.jclouds.openstack.nova.functions; -import com.google.common.collect.ImmutableList; -import com.google.common.collect.ImmutableMap; -import com.google.common.collect.Iterables; -import com.google.inject.Guice; -import com.google.inject.Injector; -import com.google.inject.Key; -import com.google.inject.TypeLiteral; -import org.jclouds.http.HttpResponse; -import org.jclouds.http.functions.UnwrapOnlyJsonValue; -import org.jclouds.io.Payloads; -import org.jclouds.openstack.nova.domain.Address; -import org.jclouds.openstack.nova.domain.Addresses; -import org.jclouds.openstack.nova.domain.Server; -import org.jclouds.openstack.nova.domain.ServerStatus; -import org.testng.annotations.Test; +import static org.testng.Assert.assertEquals; import java.io.InputStream; import java.net.UnknownHostException; @@ -43,7 +29,23 @@ import java.util.List; import java.util.Locale; import java.util.SimpleTimeZone; -import static org.testng.Assert.assertEquals; +import org.jclouds.http.HttpResponse; +import org.jclouds.http.functions.UnwrapOnlyJsonValue; +import org.jclouds.io.Payloads; +import org.jclouds.json.config.GsonModule; +import org.jclouds.openstack.nova.domain.Address; +import org.jclouds.openstack.nova.domain.Addresses; +import org.jclouds.openstack.nova.domain.Server; +import org.jclouds.openstack.nova.domain.ServerStatus; +import org.testng.annotations.Test; + +import com.google.common.collect.ImmutableList; +import com.google.common.collect.ImmutableMap; +import com.google.common.collect.Iterables; +import com.google.inject.Guice; +import com.google.inject.Injector; +import com.google.inject.Key; +import com.google.inject.TypeLiteral; /** * Tests behavior of {@code ParseServerFromJsonResponse} @@ -72,9 +74,12 @@ public class ParseServerFromJsonResponseTest { assertEquals(response.getUpdated(), dateFormat.parse("2010-10-10T12:00:00Z")); - - List
publicAddresses = ImmutableList.copyOf(Iterables.transform(ImmutableList.of("67.23.10.132", "::babe:67.23.10.132", "67.23.10.131", "::babe:4317:0A83"), Address.newString2AddressFunction())); - List
privateAddresses = ImmutableList.copyOf(Iterables.transform(ImmutableList.of("10.176.42.16", "::babe:10.176.42.16"), Address.newString2AddressFunction())); + List
publicAddresses = ImmutableList.copyOf(Iterables.transform( + ImmutableList.of("67.23.10.132", "::babe:67.23.10.132", "67.23.10.131", "::babe:4317:0A83"), + Address.newString2AddressFunction())); + List
privateAddresses = ImmutableList.copyOf(Iterables.transform( + ImmutableList.of("10.176.42.16", "::babe:10.176.42.16"), + Address.newString2AddressFunction())); Addresses addresses1 = new Addresses(new HashSet
(publicAddresses), new HashSet
(privateAddresses)); assertEquals(response.getAddresses(), addresses1); assertEquals(response.getMetadata(), ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1")); @@ -84,14 +89,18 @@ public class ParseServerFromJsonResponseTest { public static Server parseServer() throws NoSuchMethodException, ClassNotFoundException { - Injector i = Guice.createInjector(new ParserModule()); + Injector i = Guice.createInjector(new GsonModule() { + @Override + protected void configure() { + super.configure(); + bind(DateAdapter.class).to(Iso8601DateAdapter.class); + } + }); InputStream is = ParseServerFromJsonResponseTest.class.getResourceAsStream("/test_get_server_detail.json"); - UnwrapOnlyJsonValue parser = i.getInstance(Key.get(new TypeLiteral>() { - })); - - //Function parser = i.getInstance(getParserOrThrowException(NovaClient.class.getMethod("getServer", int.class))); + UnwrapOnlyJsonValue parser = i.getInstance(Key.get(new TypeLiteral>() {})); + return (Server) parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is))); } diff --git a/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/functions/ParseServerListFromJsonResponseTest.java b/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/functions/ParseServerListFromJsonResponseTest.java index 0c61c1deb7..5d55e9c9c6 100644 --- a/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/functions/ParseServerListFromJsonResponseTest.java +++ b/sandbox-apis/nova/src/test/java/org/jclouds/openstack/nova/functions/ParseServerListFromJsonResponseTest.java @@ -18,6 +18,23 @@ */ package org.jclouds.openstack.nova.functions; +import static org.testng.Assert.assertEquals; + +import java.io.InputStream; +import java.net.UnknownHostException; +import java.util.HashSet; +import java.util.List; + +import org.jclouds.http.HttpResponse; +import org.jclouds.http.functions.UnwrapOnlyJsonValue; +import org.jclouds.io.Payloads; +import org.jclouds.json.config.GsonModule; +import org.jclouds.openstack.nova.domain.Address; +import org.jclouds.openstack.nova.domain.Addresses; +import org.jclouds.openstack.nova.domain.Server; +import org.jclouds.openstack.nova.domain.ServerStatus; +import org.testng.annotations.Test; + import com.google.common.collect.ImmutableList; import com.google.common.collect.ImmutableMap; import com.google.common.collect.Iterables; @@ -25,21 +42,6 @@ import com.google.inject.Guice; import com.google.inject.Injector; import com.google.inject.Key; import com.google.inject.TypeLiteral; -import org.jclouds.http.HttpResponse; -import org.jclouds.http.functions.UnwrapOnlyJsonValue; -import org.jclouds.io.Payloads; -import org.jclouds.openstack.nova.domain.Address; -import org.jclouds.openstack.nova.domain.Addresses; -import org.jclouds.openstack.nova.domain.Server; -import org.jclouds.openstack.nova.domain.ServerStatus; -import org.testng.annotations.Test; - -import java.io.InputStream; -import java.net.UnknownHostException; -import java.util.HashSet; -import java.util.List; - -import static org.testng.Assert.assertEquals; /** * Tests behavior of {@code ParseServerListFromJsonResponse} @@ -49,7 +51,13 @@ import static org.testng.Assert.assertEquals; @Test(groups = "unit") public class ParseServerListFromJsonResponseTest { - Injector i = Guice.createInjector(new ParserModule()); + Injector i = Guice.createInjector(new GsonModule() { + @Override + protected void configure() { + super.configure(); + bind(DateAdapter.class).to(Iso8601DateAdapter.class); + } + }); @Test public void testApplyInputStream() { @@ -82,8 +90,12 @@ public class ParseServerListFromJsonResponseTest { assertEquals(response.get(0).getStatus(), ServerStatus.BUILD); assertEquals(response.get(0).getProgress(), new Integer(60)); - List
publicAddresses = ImmutableList.copyOf(Iterables.transform(ImmutableList.of("67.23.10.132", "::babe:67.23.10.132", "67.23.10.131", "::babe:4317:0A83"), Address.newString2AddressFunction())); - List
privateAddresses = ImmutableList.copyOf(Iterables.transform(ImmutableList.of("10.176.42.16", "::babe:10.176.42.16"), Address.newString2AddressFunction())); + List
publicAddresses = ImmutableList.copyOf(Iterables.transform( + ImmutableList.of("67.23.10.132", "::babe:67.23.10.132", "67.23.10.131", "::babe:4317:0A83"), + Address.newString2AddressFunction())); + List
privateAddresses = ImmutableList.copyOf(Iterables.transform( + ImmutableList.of("10.176.42.16", "::babe:10.176.42.16"), + Address.newString2AddressFunction())); Addresses addresses1 = new Addresses(new HashSet
(publicAddresses), new HashSet
(privateAddresses)); assertEquals(response.get(0).getAddresses(), addresses1); @@ -97,13 +109,17 @@ public class ParseServerListFromJsonResponseTest { assertEquals(response.get(1).getStatus(), ServerStatus.ACTIVE); assertEquals(response.get(1).getProgress(), null); - List
publicAddresses2 = ImmutableList.of(new Address("67.23.10.133", 4), new Address("::babe:67.23.10.133", 6)); - List
privateAddresses2 = ImmutableList.of(new Address("10.176.42.17", 4), new Address("::babe:10.176.42.17", 6)); + List
publicAddresses2 = ImmutableList.copyOf(Iterables.transform( + ImmutableList.of("67.23.10.133", "::babe:67.23.10.133"), + Address.newString2AddressFunction())); + List
privateAddresses2 = ImmutableList.copyOf(Iterables.transform( + ImmutableList.of("10.176.42.17", "::babe:10.176.42.17"), + Address.newString2AddressFunction())); Addresses addresses2 = new Addresses(new HashSet
(publicAddresses2), new HashSet
(privateAddresses2)); assertEquals(response.get(1).getAddresses(), addresses2); assertEquals(response.get(1).getMetadata(), ImmutableMap.of("Server Label", "DB 1")); - assertEquals(response.get(1).getURI(), "http://servers.api.openstack.org/1234/servers/56789"); + assertEquals(response.get(1).getURI().toString(), "http://servers.api.openstack.org/1234/servers/56789"); } diff --git a/sandbox-apis/nova/src/test/resources/test_get_image_details.json b/sandbox-apis/nova/src/test/resources/test_get_image_details.json index 3e3b93fb02..f9aa2f5719 100644 --- a/sandbox-apis/nova/src/test/resources/test_get_image_details.json +++ b/sandbox-apis/nova/src/test/resources/test_get_image_details.json @@ -24,7 +24,7 @@ { "rel" : "bookmark", "type" : "application/vnd.openstack.computev1.1+json", - "href" : "http://servers.api.openstack.org/1234/images/1" + "href" : "https://servers.api.rackspacecloud.com/v1.1/1234/images/1" } ] } diff --git a/sandbox-apis/nova/src/test/resources/test_get_server_detail.json b/sandbox-apis/nova/src/test/resources/test_get_server_detail.json index 0cc9cd3fca..6a7bd39af3 100644 --- a/sandbox-apis/nova/src/test/resources/test_get_server_detail.json +++ b/sandbox-apis/nova/src/test/resources/test_get_server_detail.json @@ -2,8 +2,8 @@ "server" : { "id" : 1234, "name" : "sample-server", - "imageRef" : "https://servers.api.rackspacecloud.com/v1.1/32278/images/1234", - "flavorRef" : "https://servers.api.rackspacecloud.com/v1.1/32278/flavors/1", + "imageRef" : "https://servers.api.rackspacecloud.com/v1.1/1234/images/1", + "flavorRef" : "http://servers.api.openstack.org/1234/flavors/1", "updated" : "2010-10-10T12:00:00Z", "created" : "2010-08-10T12:00:00Z", "hostId" : "e4d909c290d0fb1ca068ffaddf22cbd0",