unit test fixes

This commit is contained in:
Dmitri Babaev 2011-04-18 21:37:01 +04:00 committed by Dmitri Babaev
parent 7f02824928
commit 06b99c1cdc
14 changed files with 239 additions and 185 deletions

View File

@ -28,9 +28,6 @@ import org.jclouds.domain.Credentials;
import com.google.common.base.Function; import com.google.common.base.Function;
import java.util.List;
import java.util.Map;
/** /**
* *
* @author Adrian Cole * @author Adrian Cole

View File

@ -18,39 +18,32 @@
*/ */
package org.jclouds.openstack.nova.compute.functions; package org.jclouds.openstack.nova.compute.functions;
import static com.google.common.base.Preconditions.checkNotNull; import com.google.common.base.Function;
import static org.jclouds.compute.util.ComputeServiceUtils.parseGroupFromName; import com.google.common.base.Predicate;
import com.google.common.base.Supplier;
import java.util.Map; import com.google.common.collect.Iterables;
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 org.jclouds.collect.Memoized; import org.jclouds.collect.Memoized;
import org.jclouds.compute.domain.Hardware; import org.jclouds.compute.domain.*;
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.reference.ComputeServiceConstants; import org.jclouds.compute.reference.ComputeServiceConstants;
import org.jclouds.domain.Credentials; import org.jclouds.domain.Credentials;
import org.jclouds.domain.Location; import org.jclouds.domain.Location;
import org.jclouds.domain.LocationBuilder; import org.jclouds.domain.LocationBuilder;
import org.jclouds.domain.LocationScope; import org.jclouds.domain.LocationScope;
import org.jclouds.logging.Logger; 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 javax.annotation.Resource;
import com.google.common.base.Predicate; import javax.inject.Inject;
import com.google.common.base.Supplier; import javax.inject.Named;
import com.google.common.collect.Iterables; 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 * @author Adrian Cole
@ -113,8 +106,11 @@ public class ServerToNodeMetadata implements Function<Server, NodeMetadata> {
from.getHostId()).parent(location.get()).build()); from.getHostId()).parent(location.get()).build());
builder.userMetadata(from.getMetadata()); builder.userMetadata(from.getMetadata());
builder.group(parseGroupFromName(from.getName())); builder.group(parseGroupFromName(from.getName()));
builder.imageId(from.getImageRef() + ""); Image image = parseImage(from);
builder.operatingSystem(parseOperatingSystem(from)); if (image != null) {
builder.imageId(image.getId());
builder.operatingSystem(image.getOperatingSystem());
}
builder.hardware(parseHardware(from)); builder.hardware(parseHardware(from));
builder.state(serverToNodeState.get(from.getStatus())); builder.state(serverToNodeState.get(from.getStatus()));
builder.publicAddresses(Iterables.transform(from.getAddresses().getPublicAddresses(), Address.newAddress2StringFunction())); builder.publicAddresses(Iterables.transform(from.getAddresses().getPublicAddresses(), Address.newAddress2StringFunction()));
@ -133,9 +129,9 @@ public class ServerToNodeMetadata implements Function<Server, NodeMetadata> {
return null; return null;
} }
protected OperatingSystem parseOperatingSystem(Server from) { protected Image parseImage(Server from) {
try { try {
return Iterables.find(images.get(), new FindImageForServer(from)).getOperatingSystem(); return Iterables.find(images.get(), new FindImageForServer(from));
} catch (NoSuchElementException e) { } catch (NoSuchElementException e) {
logger.warn("could not find a matching image for server %s in location %s", from, location); logger.warn("could not find a matching image for server %s in location %s", from, location);
} }

View File

@ -14,6 +14,7 @@ public class Address {
private int version; private int version;
//for de-serialization //for de-serialization
@SuppressWarnings("unused")
private Address() { private Address() {
} }

View File

@ -1,12 +1,13 @@
package org.jclouds.openstack.nova.domain; package org.jclouds.openstack.nova.domain;
import com.google.common.collect.Lists;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.util.List; import java.util.List;
import java.util.Map; import java.util.Map;
import com.google.common.base.Functions;
import com.google.common.collect.Lists;
/** /**
* @author Dmitri Babaev * @author Dmitri Babaev
*/ */
@ -17,6 +18,11 @@ public class Resource {
public URI getURI() { public URI getURI() {
for (Map<String, String> linkProperties : links) { for (Map<String, String> linkProperties : links) {
try { 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")); return new URI(linkProperties.get("href"));
} catch (URISyntaxException e) { } catch (URISyntaxException e) {
throw new RuntimeException(e); throw new RuntimeException(e);

View File

@ -28,6 +28,8 @@ import org.jclouds.openstack.nova.domain.Flavor;
import org.jclouds.openstack.nova.functions.ParseFlavorFromJsonResponseTest; import org.jclouds.openstack.nova.functions.ParseFlavorFromJsonResponseTest;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException; import java.net.UnknownHostException;
import static org.testng.Assert.assertEquals; 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(); Location provider = new LocationBuilder().scope(LocationScope.ZONE).id("dallas").description("description").build();
@Test @Test
public void test() throws UnknownHostException { public void test() throws UnknownHostException, URISyntaxException {
assertEquals(convertFlavor(), new HardwareBuilder().ids("1").name("256 MB Server").processors( Hardware flavor = convertFlavor();
ImmutableList.of(new Processor(1.0, 1.0))).ram(256).volumes( Hardware tempFlavor = new HardwareBuilder().ids("1").name("256 MB Server")
ImmutableList.of(new VolumeBuilder().type(Volume.Type.LOCAL).size(10.0f).durable(true).bootDevice(true) .processors(ImmutableList.of(new Processor(1.0, 1.0)))
.build())).build()); .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() { public static Hardware convertFlavor() {

View File

@ -18,8 +18,12 @@
*/ */
package org.jclouds.openstack.nova.compute.functions; package org.jclouds.openstack.nova.compute.functions;
import com.google.inject.AbstractModule; import static org.testng.Assert.assertEquals;
import com.google.inject.Guice;
import java.net.URI;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import org.jclouds.compute.config.BaseComputeServiceContextModule; import org.jclouds.compute.config.BaseComputeServiceContextModule;
import org.jclouds.compute.domain.Image; import org.jclouds.compute.domain.Image;
import org.jclouds.compute.domain.ImageBuilder; 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.compute.reference.ComputeServiceConstants;
import org.jclouds.domain.Credentials; import org.jclouds.domain.Credentials;
import org.jclouds.json.Json; import org.jclouds.json.Json;
import org.jclouds.json.config.GsonModule;
import org.jclouds.openstack.nova.functions.ParseImageFromJsonResponseTest; import org.jclouds.openstack.nova.functions.ParseImageFromJsonResponseTest;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import java.net.UnknownHostException; import com.google.inject.Guice;
import static org.testng.Assert.assertEquals;
/** /**
* @author Adrian Cole * @author Adrian Cole
@ -42,27 +45,23 @@ import static org.testng.Assert.assertEquals;
public class NovaImageToImageTest { public class NovaImageToImageTest {
@Test @Test
public void testApplyWhereImageNotFound() throws UnknownHostException { public void testApplyWhereImageNotFound() throws UnknownHostException, URISyntaxException {
assertEquals( Image image = new ImageBuilder()
convertImage(),
new ImageBuilder()
.name("CentOS 5.2") .name("CentOS 5.2")
.operatingSystem( .operatingSystem(
new OperatingSystem.Builder().family(OsFamily.CENTOS).version("5.2").description("CentOS 5.2").is64Bit(true) 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)) .build()).description("CentOS 5.2").defaultCredentials(new Credentials("root", null))
.ids("2").version("1286712000000").build()); .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() { public static Image convertImage() {
org.jclouds.openstack.nova.domain.Image image = ParseImageFromJsonResponseTest.parseImage(); org.jclouds.openstack.nova.domain.Image image = ParseImageFromJsonResponseTest.parseImage();
NovaImageToImage parser = new NovaImageToImage(new NovaImageToOperatingSystem(new BaseComputeServiceContextModule() { NovaImageToImage parser = new NovaImageToImage(new NovaImageToOperatingSystem(new BaseComputeServiceContextModule() {
}.provideOsVersionMap(new ComputeServiceConstants.ReferenceData(), Guice.createInjector(new AbstractModule() { }.provideOsVersionMap(new ComputeServiceConstants.ReferenceData(), Guice.createInjector(new GsonModule())
@Override
protected void configure() {
//To change body of implemented methods use File | Settings | File Templates.
}
})
.getInstance(Json.class)))); .getInstance(Json.class))));
return parser.apply(image); return parser.apply(image);

View File

@ -18,11 +18,25 @@
*/ */
package org.jclouds.openstack.nova.compute.functions; package org.jclouds.openstack.nova.compute.functions;
import com.google.common.base.Suppliers; import static org.testng.Assert.assertEquals;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap; import java.net.URI;
import com.google.common.collect.ImmutableSet; import java.net.URISyntaxException;
import org.jclouds.compute.domain.*; 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.Credentials;
import org.jclouds.domain.Location; import org.jclouds.domain.Location;
import org.jclouds.domain.LocationBuilder; 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.jclouds.openstack.nova.functions.ParseServerFromJsonResponseTest;
import org.testng.annotations.Test; import org.testng.annotations.Test;
import java.net.UnknownHostException; import com.google.common.base.Suppliers;
import java.util.Map; import com.google.common.collect.ImmutableList;
import java.util.Set; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.ImmutableSet;
import static org.testng.Assert.assertEquals;
/** /**
* @author Adrian Cole * @author Adrian Cole
@ -47,7 +60,7 @@ public class ServerToNodeMetadataTest {
Location provider = new LocationBuilder().scope(LocationScope.ZONE).id("dallas").description("description").build(); Location provider = new LocationBuilder().scope(LocationScope.ZONE).id("dallas").description("description").build();
@Test @Test
public void testApplyWhereImageAndHardwareNotFoundButCredentialsFound() throws UnknownHostException, NoSuchMethodException, ClassNotFoundException { public void testApplyWhereImageAndHardwareNotFoundButCredentialsFound() throws UnknownHostException, NoSuchMethodException, ClassNotFoundException, URISyntaxException {
Credentials creds = new Credentials("root", "abdce"); Credentials creds = new Credentials("root", "abdce");
Map<ServerStatus, NodeState> serverStateToNodeState = NovaComputeServiceDependenciesModule.serverToNodeState; Map<ServerStatus, NodeState> serverStateToNodeState = NovaComputeServiceDependenciesModule.serverToNodeState;
@ -61,16 +74,13 @@ public class ServerToNodeMetadataTest {
NodeMetadata metadata = parser.apply(server); NodeMetadata metadata = parser.apply(server);
assertEquals(metadata, new NodeMetadataBuilder().state(NodeState.PENDING).publicAddresses( NodeMetadata constructedMetadata = newNodeMetadataBuilder()
ImmutableSet.of("67.23.10.132", "67.23.10.131")).privateAddresses(ImmutableSet.of("10.176.42.16")) .credentials(creds).build();
.imageId("2").id("1234").providerId("1234").name("sample-server").credentials(creds).location( assertEquals(metadata, constructedMetadata);
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());
} }
@Test @Test
public void testApplyWhereImageAndHardwareNotFound() throws UnknownHostException, NoSuchMethodException, ClassNotFoundException { public void testApplyWhereImageAndHardwareNotFound() throws UnknownHostException, NoSuchMethodException, ClassNotFoundException, URISyntaxException {
Map<ServerStatus, NodeState> serverStateToNodeState = NovaComputeServiceDependenciesModule.serverToNodeState; Map<ServerStatus, NodeState> serverStateToNodeState = NovaComputeServiceDependenciesModule.serverToNodeState;
Set<org.jclouds.compute.domain.Image> images = ImmutableSet.of(); Set<org.jclouds.compute.domain.Image> images = ImmutableSet.of();
Set<org.jclouds.compute.domain.Hardware> hardwares = ImmutableSet.of(); Set<org.jclouds.compute.domain.Hardware> hardwares = ImmutableSet.of();
@ -82,17 +92,31 @@ public class ServerToNodeMetadataTest {
NodeMetadata metadata = parser.apply(server); NodeMetadata metadata = parser.apply(server);
assertEquals(metadata, new NodeMetadataBuilder().state(NodeState.PENDING).publicAddresses( NodeMetadata constructedMetadata = newNodeMetadataBuilder().build();
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( assertEquals(metadata, constructedMetadata);
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());
} }
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 @Test
public void testApplyWhereImageFoundAndHardwareNotFound() throws UnknownHostException, NoSuchMethodException, ClassNotFoundException { public void testApplyWhereImageFoundAndHardwareNotFound() throws UnknownHostException, NoSuchMethodException, ClassNotFoundException, URISyntaxException {
Map<ServerStatus, NodeState> serverStateToNodeState = NovaComputeServiceDependenciesModule.serverToNodeState; Map<ServerStatus, NodeState> serverStateToNodeState = NovaComputeServiceDependenciesModule.serverToNodeState;
org.jclouds.compute.domain.Image jcImage = NovaImageToImageTest.convertImage(); org.jclouds.compute.domain.Image jcImage = NovaImageToImageTest.convertImage();
Set<org.jclouds.compute.domain.Image> images = ImmutableSet.of(jcImage); Set<org.jclouds.compute.domain.Image> images = ImmutableSet.of(jcImage);
@ -105,19 +129,21 @@ public class ServerToNodeMetadataTest {
NodeMetadata metadata = parser.apply(server); NodeMetadata metadata = parser.apply(server);
assertEquals(metadata, new NodeMetadataBuilder().state(NodeState.PENDING).publicAddresses( NodeMetadata constructedMetadata = newNodeMetadataBuilder()
ImmutableSet.of("67.23.10.132", "67.23.10.131")).privateAddresses(ImmutableSet.of("10.176.42.16")) .imageId("1")
.imageId("2").operatingSystem( .operatingSystem(new OperatingSystem.Builder()
new OperatingSystemBuilder().family(OsFamily.CENTOS).description("CentOS 5.2").version("5.2") .family(OsFamily.CENTOS)
.is64Bit(true).build()).id("1234").providerId("1234").name("sample-server").location( .description("CentOS 5.2")
new LocationBuilder().scope(LocationScope.HOST).id("e4d909c290d0fb1ca068ffaddf22cbd0") .version("5.2")
.description("e4d909c290d0fb1ca068ffaddf22cbd0").parent(provider).build()) .is64Bit(true).build())
.userMetadata(ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1")).build()); .build();
assertEquals(metadata, constructedMetadata);
} }
@Test @Test
public void testApplyWhereImageAndHardwareFound() throws UnknownHostException, NoSuchMethodException, ClassNotFoundException { public void testApplyWhereImageAndHardwareFound() throws UnknownHostException, NoSuchMethodException, ClassNotFoundException, URISyntaxException {
Map<ServerStatus, NodeState> serverStateToNodeState = NovaComputeServiceDependenciesModule.serverToNodeState; Map<ServerStatus, NodeState> serverStateToNodeState = NovaComputeServiceDependenciesModule.serverToNodeState;
Set<org.jclouds.compute.domain.Image> images = ImmutableSet.of(NovaImageToImageTest.convertImage()); Set<org.jclouds.compute.domain.Image> images = ImmutableSet.of(NovaImageToImageTest.convertImage());
Set<org.jclouds.compute.domain.Hardware> hardwares = ImmutableSet.of(FlavorToHardwareTest.convertFlavor()); Set<org.jclouds.compute.domain.Hardware> hardwares = ImmutableSet.of(FlavorToHardwareTest.convertFlavor());
@ -129,17 +155,27 @@ public class ServerToNodeMetadataTest {
NodeMetadata metadata = parser.apply(server); NodeMetadata metadata = parser.apply(server);
assertEquals(metadata, new NodeMetadataBuilder().state(NodeState.PENDING).publicAddresses( NodeMetadata constructedMetadata = newNodeMetadataBuilder()
ImmutableSet.of("67.23.10.132", "67.23.10.131")).privateAddresses(ImmutableSet.of("10.176.42.16")) .imageId("1")
.imageId("2").hardware( .operatingSystem(new OperatingSystem.Builder()
new HardwareBuilder().ids("1").name("256 MB Server").processors( .family(OsFamily.CENTOS)
ImmutableList.of(new Processor(1.0, 1.0))).ram(256).volumes( .description("CentOS 5.2")
ImmutableList.of(new VolumeBuilder().type(Volume.Type.LOCAL).size(10.0f).durable(true) .version("5.2")
.bootDevice(true).build())).build()).operatingSystem( .is64Bit(true).build())
new OperatingSystemBuilder().family(OsFamily.CENTOS).description("CentOS 5.2").version("5.2") .hardware(new HardwareBuilder()
.is64Bit(true).build()).id("1234").providerId("1234").name("sample-server").location( .ids("1")
new LocationBuilder().scope(LocationScope.HOST).id("e4d909c290d0fb1ca068ffaddf22cbd0") .name("256 MB Server")
.description("e4d909c290d0fb1ca068ffaddf22cbd0").parent(provider).build()) .processors(ImmutableList.of(new Processor(1.0, 1.0)))
.userMetadata(ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1")).build()); .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, constructedMetadata);
} }
} }

View File

@ -18,7 +18,9 @@
*/ */
package org.jclouds.openstack.nova.functions; 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.Guice;
import com.google.inject.Injector; import com.google.inject.Injector;
import com.google.inject.Key; import com.google.inject.Key;
@ -33,8 +35,9 @@ import org.testng.annotations.Test;
import java.io.InputStream; import java.io.InputStream;
import java.net.UnknownHostException; 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} * Tests behavior of {@code ParseAddressesFromJsonResponse}
@ -53,13 +56,15 @@ public class ParseAddressesFromJsonResponseTest {
})); }));
Addresses response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is))); Addresses response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
ImmutableSet<Address> publicAddresses = ImmutableSet.of(new Address("67.23.10.132", 4) List<Address> publicAddresses = ImmutableList.copyOf(
, new Address("::babe:67.23.10.132", 6), new Address("67.23.10.131", 4), new Address("::babe:4317:0A83", 6)); Iterables.transform(ImmutableList.of("67.23.10.132", "::babe:67.23.10.132", "67.23.10.131", "::babe:4317:0A83"),
Address.newString2AddressFunction()));
ImmutableSet<Address> privateAddresses = ImmutableSet.of(new Address("10.176.42.16", 4), List<Address> privateAddresses = ImmutableList.copyOf(
new Address("::babe:10.176.42.16", 6)); Iterables.transform(ImmutableList.of("10.176.42.16", "::babe:10.176.42.16"),
Address.newString2AddressFunction()));
assertEqualsNoOrder(response.getPublicAddresses().toArray(), publicAddresses.toArray()); assertTrue(response.getPublicAddresses().equals(Sets.newHashSet(publicAddresses)));
assertEqualsNoOrder(response.getPrivateAddresses().toArray(), privateAddresses.toArray()); assertTrue(response.getPrivateAddresses().equals(Sets.newHashSet(privateAddresses)));
} }
} }

View File

@ -26,6 +26,7 @@ import com.google.inject.TypeLiteral;
import org.jclouds.http.HttpResponse; import org.jclouds.http.HttpResponse;
import org.jclouds.http.functions.UnwrapOnlyJsonValue; import org.jclouds.http.functions.UnwrapOnlyJsonValue;
import org.jclouds.io.Payloads; import org.jclouds.io.Payloads;
import org.jclouds.json.config.GsonModule;
import org.jclouds.openstack.nova.domain.Address; import org.jclouds.openstack.nova.domain.Address;
import org.testng.annotations.Test; import org.testng.annotations.Test;
@ -43,7 +44,7 @@ import static org.testng.Assert.assertEquals;
@Test(groups = "unit") @Test(groups = "unit")
public class ParseInetAddressListFromJsonResponseTest { public class ParseInetAddressListFromJsonResponseTest {
Injector i = Guice.createInjector(new ParserModule()); Injector i = Guice.createInjector(new GsonModule());
@Test @Test
public void testPublic() throws UnknownHostException { public void testPublic() throws UnknownHostException {

View File

@ -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);
}
}

View File

@ -18,21 +18,7 @@
*/ */
package org.jclouds.openstack.nova.functions; package org.jclouds.openstack.nova.functions;
import com.google.common.collect.ImmutableList; import static org.testng.Assert.assertEquals;
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 java.io.InputStream; import java.io.InputStream;
import java.net.UnknownHostException; import java.net.UnknownHostException;
@ -43,7 +29,23 @@ import java.util.List;
import java.util.Locale; import java.util.Locale;
import java.util.SimpleTimeZone; 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} * Tests behavior of {@code ParseServerFromJsonResponse}
@ -72,9 +74,12 @@ public class ParseServerFromJsonResponseTest {
assertEquals(response.getUpdated(), assertEquals(response.getUpdated(),
dateFormat.parse("2010-10-10T12:00:00Z")); dateFormat.parse("2010-10-10T12:00:00Z"));
List<Address> publicAddresses = ImmutableList.copyOf(Iterables.transform(
List<Address> 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())); ImmutableList.of("67.23.10.132", "::babe:67.23.10.132", "67.23.10.131", "::babe:4317:0A83"),
List<Address> privateAddresses = ImmutableList.copyOf(Iterables.transform(ImmutableList.of("10.176.42.16", "::babe:10.176.42.16"), Address.newString2AddressFunction())); Address.newString2AddressFunction()));
List<Address> privateAddresses = ImmutableList.copyOf(Iterables.transform(
ImmutableList.of("10.176.42.16", "::babe:10.176.42.16"),
Address.newString2AddressFunction()));
Addresses addresses1 = new Addresses(new HashSet<Address>(publicAddresses), new HashSet<Address>(privateAddresses)); Addresses addresses1 = new Addresses(new HashSet<Address>(publicAddresses), new HashSet<Address>(privateAddresses));
assertEquals(response.getAddresses(), addresses1); assertEquals(response.getAddresses(), addresses1);
assertEquals(response.getMetadata(), ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1")); 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 { 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"); InputStream is = ParseServerFromJsonResponseTest.class.getResourceAsStream("/test_get_server_detail.json");
UnwrapOnlyJsonValue<Server> parser = i.getInstance(Key.get(new TypeLiteral<UnwrapOnlyJsonValue<Server>>() { UnwrapOnlyJsonValue<Server> parser = i.getInstance(Key.get(new TypeLiteral<UnwrapOnlyJsonValue<Server>>() {}));
}));
//Function<HttpResponse, ?> parser = i.getInstance(getParserOrThrowException(NovaClient.class.getMethod("getServer", int.class)));
return (Server) parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is))); return (Server) parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
} }

View File

@ -18,6 +18,23 @@
*/ */
package org.jclouds.openstack.nova.functions; 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.ImmutableList;
import com.google.common.collect.ImmutableMap; import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Iterables; import com.google.common.collect.Iterables;
@ -25,21 +42,6 @@ import com.google.inject.Guice;
import com.google.inject.Injector; import com.google.inject.Injector;
import com.google.inject.Key; import com.google.inject.Key;
import com.google.inject.TypeLiteral; 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} * Tests behavior of {@code ParseServerListFromJsonResponse}
@ -49,7 +51,13 @@ import static org.testng.Assert.assertEquals;
@Test(groups = "unit") @Test(groups = "unit")
public class ParseServerListFromJsonResponseTest { 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 @Test
public void testApplyInputStream() { public void testApplyInputStream() {
@ -82,8 +90,12 @@ public class ParseServerListFromJsonResponseTest {
assertEquals(response.get(0).getStatus(), ServerStatus.BUILD); assertEquals(response.get(0).getStatus(), ServerStatus.BUILD);
assertEquals(response.get(0).getProgress(), new Integer(60)); assertEquals(response.get(0).getProgress(), new Integer(60));
List<Address> 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<Address> publicAddresses = ImmutableList.copyOf(Iterables.transform(
List<Address> privateAddresses = ImmutableList.copyOf(Iterables.transform(ImmutableList.of("10.176.42.16", "::babe:10.176.42.16"), Address.newString2AddressFunction())); ImmutableList.of("67.23.10.132", "::babe:67.23.10.132", "67.23.10.131", "::babe:4317:0A83"),
Address.newString2AddressFunction()));
List<Address> privateAddresses = ImmutableList.copyOf(Iterables.transform(
ImmutableList.of("10.176.42.16", "::babe:10.176.42.16"),
Address.newString2AddressFunction()));
Addresses addresses1 = new Addresses(new HashSet<Address>(publicAddresses), new HashSet<Address>(privateAddresses)); Addresses addresses1 = new Addresses(new HashSet<Address>(publicAddresses), new HashSet<Address>(privateAddresses));
assertEquals(response.get(0).getAddresses(), addresses1); assertEquals(response.get(0).getAddresses(), addresses1);
@ -97,13 +109,17 @@ public class ParseServerListFromJsonResponseTest {
assertEquals(response.get(1).getStatus(), ServerStatus.ACTIVE); assertEquals(response.get(1).getStatus(), ServerStatus.ACTIVE);
assertEquals(response.get(1).getProgress(), null); assertEquals(response.get(1).getProgress(), null);
List<Address> publicAddresses2 = ImmutableList.of(new Address("67.23.10.133", 4), new Address("::babe:67.23.10.133", 6)); List<Address> publicAddresses2 = ImmutableList.copyOf(Iterables.transform(
List<Address> privateAddresses2 = ImmutableList.of(new Address("10.176.42.17", 4), new Address("::babe:10.176.42.17", 6)); ImmutableList.of("67.23.10.133", "::babe:67.23.10.133"),
Address.newString2AddressFunction()));
List<Address> privateAddresses2 = ImmutableList.copyOf(Iterables.transform(
ImmutableList.of("10.176.42.17", "::babe:10.176.42.17"),
Address.newString2AddressFunction()));
Addresses addresses2 = new Addresses(new HashSet<Address>(publicAddresses2), new HashSet<Address>(privateAddresses2)); Addresses addresses2 = new Addresses(new HashSet<Address>(publicAddresses2), new HashSet<Address>(privateAddresses2));
assertEquals(response.get(1).getAddresses(), addresses2); assertEquals(response.get(1).getAddresses(), addresses2);
assertEquals(response.get(1).getMetadata(), ImmutableMap.of("Server Label", "DB 1")); 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");
} }

View File

@ -24,7 +24,7 @@
{ {
"rel" : "bookmark", "rel" : "bookmark",
"type" : "application/vnd.openstack.computev1.1+json", "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"
} }
] ]
} }

View File

@ -2,8 +2,8 @@
"server" : { "server" : {
"id" : 1234, "id" : 1234,
"name" : "sample-server", "name" : "sample-server",
"imageRef" : "https://servers.api.rackspacecloud.com/v1.1/32278/images/1234", "imageRef" : "https://servers.api.rackspacecloud.com/v1.1/1234/images/1",
"flavorRef" : "https://servers.api.rackspacecloud.com/v1.1/32278/flavors/1", "flavorRef" : "http://servers.api.openstack.org/1234/flavors/1",
"updated" : "2010-10-10T12:00:00Z", "updated" : "2010-10-10T12:00:00Z",
"created" : "2010-08-10T12:00:00Z", "created" : "2010-08-10T12:00:00Z",
"hostId" : "e4d909c290d0fb1ca068ffaddf22cbd0", "hostId" : "e4d909c290d0fb1ca068ffaddf22cbd0",