Changed expectations for parser tests

This commit is contained in:
vicglarson 2011-04-13 18:58:03 +04:00 committed by Dmitri Babaev
parent 5e2bae24e8
commit 8d2d3e8088
12 changed files with 351 additions and 289 deletions

View File

@ -45,6 +45,7 @@ import static org.testng.Assert.assertEquals;
public class ParseAddressesFromJsonResponseTest {
Injector i = Guice.createInjector(new GsonModule());
@Test
public void testApplyInputStreamDetails() throws UnknownHostException {
InputStream is = getClass().getResourceAsStream("/test_list_addresses.json");

View File

@ -43,7 +43,9 @@ import static org.testng.Assert.assertEquals;
*/
@Test(groups = "unit")
public class ParseFlavorFromJsonResponseTest {
public void test() throws IOException {
@Test
public void testParseFlavorFromJsonResponseTest() throws IOException {
Flavor response = parseFlavor();
String json = new Gson().toJson(response);
@ -52,6 +54,11 @@ public class ParseFlavorFromJsonResponseTest {
ParseFlavorFromJsonResponseTest.class.getResourceAsStream("/test_get_flavor_details.json"))
.replace("\n", "").replace("\t", "").replace("\r", "").replace(" ", "");
assertEquals(response.getId(), 1);
assertEquals(response.getName(), "256 MB Server");
assertEquals(response.getDisk().intValue(), 10);
assertEquals(response.getRam().intValue(), 256);
assertEquals(json, expectedJson);
}

View File

@ -18,64 +18,65 @@
*/
package org.jclouds.openstack.nova.functions;
import static org.testng.Assert.assertEquals;
import java.io.InputStream;
import java.net.UnknownHostException;
import java.util.List;
import org.jclouds.openstack.nova.domain.Flavor;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.functions.UnwrapOnlyJsonValue;
import org.jclouds.io.Payloads;
import org.jclouds.json.config.GsonModule;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableList;
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.json.config.GsonModule;
import org.jclouds.openstack.nova.domain.Flavor;
import org.testng.annotations.Test;
import java.io.InputStream;
import java.net.UnknownHostException;
import java.util.List;
import static org.testng.Assert.assertEquals;
/**
* Tests behavior of {@code ParseFlavorListFromJsonResponse}
*
*
* @author Adrian Cole
*/
@Test(groups = "unit")
public class ParseFlavorListFromJsonResponseTest {
Injector i = Guice.createInjector(new GsonModule());
Injector i = Guice.createInjector(new GsonModule());
public void testApplyInputStream() {
InputStream is = getClass().getResourceAsStream("/test_list_flavors.json");
@Test
public void testApplyInputStream() {
InputStream is = getClass().getResourceAsStream("/test_list_flavors.json");
List<Flavor> expects = ImmutableList.of(new Flavor(1, "256 MB Server"), new Flavor(2, "512 MB Server"));
List<Flavor> expects = ImmutableList.of(new Flavor(1, "256 MB Server"), new Flavor(2, "512 MB Server"));
UnwrapOnlyJsonValue<List<Flavor>> parser = i.getInstance(Key
.get(new TypeLiteral<UnwrapOnlyJsonValue<List<Flavor>>>() {
}));
List<Flavor> response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
assertEquals(response, expects);
}
UnwrapOnlyJsonValue<List<Flavor>> parser = i.getInstance(Key
.get(new TypeLiteral<UnwrapOnlyJsonValue<List<Flavor>>>() {
}));
List<Flavor> response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
assertEquals(response, expects);
}
public void testApplyInputStreamDetails() throws UnknownHostException {
InputStream is = getClass().getResourceAsStream("/test_list_flavors_detail.json");
@Test
public void testApplyInputStreamDetails() throws UnknownHostException {
InputStream is = getClass().getResourceAsStream("/test_list_flavors_detail.json");
UnwrapOnlyJsonValue<List<Flavor>> parser = i.getInstance(Key
.get(new TypeLiteral<UnwrapOnlyJsonValue<List<Flavor>>>() {
}));
List<Flavor> response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
assertEquals(response.get(0).getId(), 1);
assertEquals(response.get(0).getName(), "256 MB Server");
assertEquals(response.get(0).getDisk(), new Integer(10));
assertEquals(response.get(0).getRam(), new Integer(256));
UnwrapOnlyJsonValue<List<Flavor>> parser = i.getInstance(Key
.get(new TypeLiteral<UnwrapOnlyJsonValue<List<Flavor>>>() {
}));
List<Flavor> response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
assertEquals(response.get(0).getId(), 1);
assertEquals(response.get(0).getName(), "256 MB Server");
assertEquals(response.get(0).getDisk(), new Integer(10));
assertEquals(response.get(0).getRam(), new Integer(256));
assertEquals(response.get(1).getId(), 2);
assertEquals(response.get(1).getName(), "512 MB Server");
assertEquals(response.get(1).getDisk(), new Integer(20));
assertEquals(response.get(1).getRam(), new Integer(512));
assertEquals(response.get(1).getId(), 2);
assertEquals(response.get(1).getName(), "512 MB Server");
assertEquals(response.get(1).getDisk(), new Integer(20));
assertEquals(response.get(1).getRam(), new Integer(512));
}
}
}

View File

@ -18,13 +18,7 @@
*/
package org.jclouds.openstack.nova.functions;
import static org.testng.Assert.assertEquals;
import java.io.InputStream;
import java.net.UnknownHostException;
import org.jclouds.openstack.nova.domain.Image;
import org.jclouds.openstack.nova.domain.ImageStatus;
import com.google.inject.*;
import org.jclouds.date.DateService;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.functions.UnwrapOnlyJsonValue;
@ -32,61 +26,66 @@ import org.jclouds.io.Payloads;
import org.jclouds.json.config.GsonModule;
import org.jclouds.json.config.GsonModule.DateAdapter;
import org.jclouds.json.config.GsonModule.Iso8601DateAdapter;
import org.jclouds.openstack.nova.domain.Image;
import org.jclouds.openstack.nova.domain.ImageStatus;
import org.testng.annotations.Test;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.TypeLiteral;
import java.io.InputStream;
import java.net.UnknownHostException;
import static org.testng.Assert.assertEquals;
/**
* Tests behavior of {@code ParseImageFromJsonResponse}
*
*
* @author Adrian Cole
*/
@Test(groups = "unit")
public class ParseImageFromJsonResponseTest {
Injector i = Guice.createInjector(new AbstractModule() {
Injector i = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(DateAdapter.class).to(Iso8601DateAdapter.class);
}
}, new GsonModule());
DateService dateService = i.getInstance(DateService.class);
public void testApplyInputStreamDetails() throws UnknownHostException {
Image response = parseImage();
assertEquals(response.getId(), 2);
assertEquals(response.getName(), "CentOS 5.2");
assertEquals(response.getCreated(), dateService.iso8601SecondsDateParse("2010-08-10T12:00:00Z"));
assertEquals(response.getProgress(), new Integer(80));
assertEquals(response.getServerId(), new Integer(12));
assertEquals(response.getStatus(), ImageStatus.SAVING);
assertEquals(response.getUpdated(), dateService.iso8601SecondsDateParse(("2010-10-10T12:00:00Z")));
}
public static Image parseImage() {
Injector i = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
@Override
protected void configure() {
bind(DateAdapter.class).to(Iso8601DateAdapter.class);
}
}
}, new GsonModule());
}, new GsonModule());
InputStream is = ParseImageFromJsonResponseTest.class.getResourceAsStream("/test_get_image_details.json");
DateService dateService = i.getInstance(DateService.class);
UnwrapOnlyJsonValue<Image> parser = i.getInstance(Key.get(new TypeLiteral<UnwrapOnlyJsonValue<Image>>() {
}));
Image response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
return response;
}
@Test
public void testApplyInputStreamDetails() throws UnknownHostException {
Image response = parseImage();
assertEquals(response.getId(), 2);
assertEquals(response.getName(), "CentOS 5.2");
assertEquals(response.getServerId(), new Integer(12));
assertEquals(response.getCreated(), dateService.iso8601SecondsDateParse("2010-08-10T12:00:00Z"));
assertEquals(response.getProgress(), new Integer(80));
assertEquals(response.getStatus(), ImageStatus.SAVING);
assertEquals(response.getUpdated(), dateService.iso8601SecondsDateParse(("2010-10-10T12:00:00Z")));
assertEquals(response.getServerId(), "http://servers.api.openstack.org/v1.1/1234/servers/12", "Change serverId to serverRefs");
assertEquals(response.getMetadata().get("ImageVersion"), "1.5");
assertEquals(response.getMetadata().get("ImageType"), "Gold");
assertEquals(response.getMetadata().size(), 2);
}
public static Image parseImage() {
Injector i = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(DateAdapter.class).to(Iso8601DateAdapter.class);
}
}, new GsonModule());
InputStream is = ParseImageFromJsonResponseTest.class.getResourceAsStream("/test_get_image_details.json");
UnwrapOnlyJsonValue<Image> parser = i.getInstance(Key.get(new TypeLiteral<UnwrapOnlyJsonValue<Image>>() {
}));
Image response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
return response;
}
}

View File

@ -18,14 +18,8 @@
*/
package org.jclouds.openstack.nova.functions;
import static org.testng.Assert.assertEquals;
import java.io.InputStream;
import java.net.UnknownHostException;
import java.util.List;
import org.jclouds.openstack.nova.domain.Image;
import org.jclouds.openstack.nova.domain.ImageStatus;
import com.google.common.collect.ImmutableList;
import com.google.inject.*;
import org.jclouds.date.DateService;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.functions.UnwrapOnlyJsonValue;
@ -33,69 +27,80 @@ import org.jclouds.io.Payloads;
import org.jclouds.json.config.GsonModule;
import org.jclouds.json.config.GsonModule.DateAdapter;
import org.jclouds.json.config.GsonModule.Iso8601DateAdapter;
import org.jclouds.openstack.nova.domain.Image;
import org.jclouds.openstack.nova.domain.ImageStatus;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableList;
import com.google.inject.AbstractModule;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.TypeLiteral;
import java.io.InputStream;
import java.net.UnknownHostException;
import java.util.List;
import static org.testng.Assert.assertEquals;
/**
* Tests behavior of {@code ParseImageListFromJsonResponse}
*
*
* @author Adrian Cole
*/
@Test(groups = "unit")
public class ParseImageListFromJsonResponseTest {
Injector i = Guice.createInjector(new AbstractModule() {
Injector i = Guice.createInjector(new AbstractModule() {
@Override
protected void configure() {
bind(DateAdapter.class).to(Iso8601DateAdapter.class);
}
@Override
protected void configure() {
bind(DateAdapter.class).to(Iso8601DateAdapter.class);
}
},new GsonModule());
DateService dateService = i.getInstance(DateService.class);
}, new GsonModule());
DateService dateService = i.getInstance(DateService.class);
public void testApplyInputStream() {
InputStream is = getClass().getResourceAsStream("/test_list_images.json");
@Test
public void testApplyInputStream() {
InputStream is = getClass().getResourceAsStream("/test_list_images.json");
List<Image> expects = ImmutableList.of(new Image(2, "CentOS 5.2"), new Image(743, "My Server Backup"));
List<Image> expects = ImmutableList.of(new Image(1, "CentOS 5.2"), new Image(743, "My Server Backup"));
UnwrapOnlyJsonValue<List<Image>> parser = i.getInstance(Key
.get(new TypeLiteral<UnwrapOnlyJsonValue<List<Image>>>() {
}));
List<Image> response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
UnwrapOnlyJsonValue<List<Image>> parser = i.getInstance(Key
.get(new TypeLiteral<UnwrapOnlyJsonValue<List<Image>>>() {
}));
List<Image> response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
assertEquals(response, expects);
}
assertEquals(response, expects);
}
public void testApplyInputStreamDetails() throws UnknownHostException {
InputStream is = getClass().getResourceAsStream("/test_list_images_detail.json");
@Test
public void testApplyInputStreamDetails() throws UnknownHostException {
InputStream is = getClass().getResourceAsStream("/test_list_images_detail.json");
UnwrapOnlyJsonValue<List<Image>> parser = i.getInstance(Key
.get(new TypeLiteral<UnwrapOnlyJsonValue<List<Image>>>() {
}));
List<Image> response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
UnwrapOnlyJsonValue<List<Image>> parser = i.getInstance(Key
.get(new TypeLiteral<UnwrapOnlyJsonValue<List<Image>>>() {
}));
List<Image> response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
assertEquals(response.get(0).getId(), 2);
assertEquals(response.get(0).getName(), "CentOS 5.2");
assertEquals(response.get(0).getCreated(), dateService.iso8601SecondsDateParse("2010-08-10T12:00:00Z"));
assertEquals(response.get(0).getProgress(), null);
assertEquals(response.get(0).getServerId(), null);
assertEquals(response.get(0).getStatus(), ImageStatus.ACTIVE);
assertEquals(response.get(0).getUpdated(), dateService.iso8601SecondsDateParse("2010-10-10T12:00:00Z"));
assertEquals(response.get(0).getId(), 1);
assertEquals(response.get(0).getName(), "CentOS 5.2");
assertEquals(response.get(0).getCreated(), dateService.iso8601SecondsDateParse("2010-08-10T12:00:00Z"));
assertEquals(response.get(0).getProgress(), null);
assertEquals(response.get(0).getServerId(), null);
assertEquals(response.get(0).getStatus(), ImageStatus.ACTIVE);
assertEquals(response.get(0).getUpdated(), dateService.iso8601SecondsDateParse("2010-10-10T12:00:00Z"));
assertEquals(response.get(0).getMetadata().get("ImageType"), "Gold");
assertEquals(response.get(0).getMetadata().get("ImageVersion"), "1.5");
assertEquals(response.get(0).getMetadata().size(), 2);
assertEquals(response.get(0).getServerId(), null, "Change serverId to serverRef");
assertEquals(response.get(1).getId(), 743);
assertEquals(response.get(1).getName(), "My Server Backup");
assertEquals(response.get(1).getCreated(), dateService.iso8601SecondsDateParse("2009-07-07T09:56:16-05:00"));
;
assertEquals(response.get(1).getProgress(), new Integer(80));
assertEquals(response.get(1).getServerId(), new Integer(12));
assertEquals(response.get(1).getStatus(), ImageStatus.SAVING);
assertEquals(response.get(1).getUpdated(), dateService.iso8601SecondsDateParse("2010-10-10T12:00:00Z"));
}
assertEquals(response.get(1).getId(), 743);
assertEquals(response.get(1).getName(), "My Server Backup");
assertEquals(response.get(1).getCreated(), dateService.iso8601SecondsDateParse("2009-07-07T09:56:16Z"));
assertEquals(response.get(1).getProgress(), new Integer(80));
assertEquals(response.get(1).getServerId(), new Integer(12));
assertEquals(response.get(1).getStatus(), ImageStatus.SAVING);
assertEquals(response.get(1).getUpdated(), dateService.iso8601SecondsDateParse("2010-10-10T12:00:00Z"));
assertEquals(response.get(1).getServerId(), "http://servers.api.openstack.org/v1.1/1234/servers/12", "Change serverId to serverRef");
//short form of reference
assertEquals(response.get(2).getServerId().intValue(), 12, "Change serverId to serverRef");
}
}

View File

@ -18,53 +18,54 @@
*/
package org.jclouds.openstack.nova.functions;
import static org.testng.Assert.assertEquals;
import java.io.InputStream;
import java.net.UnknownHostException;
import java.util.List;
import com.google.common.collect.ImmutableList;
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.json.config.GsonModule;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableList;
import com.google.inject.Guice;
import com.google.inject.Injector;
import com.google.inject.Key;
import com.google.inject.TypeLiteral;
import java.io.InputStream;
import java.net.UnknownHostException;
import java.util.List;
import static org.testng.Assert.assertEquals;
/**
* Tests behavior of {@code ParseInetAddressListFromJsonResponse}
*
*
* @author Adrian Cole
*/
@Test(groups = "unit")
public class ParseInetAddressListFromJsonResponseTest {
Injector i = Guice.createInjector(new GsonModule());
Injector i = Guice.createInjector(new GsonModule());
public void testPublic() throws UnknownHostException {
InputStream is = getClass().getResourceAsStream("/test_list_addresses_public.json");
@Test
public void testPublic() throws UnknownHostException {
InputStream is = getClass().getResourceAsStream("/test_list_addresses_public.json");
UnwrapOnlyJsonValue<List<String>> parser = i.getInstance(Key
.get(new TypeLiteral<UnwrapOnlyJsonValue<List<String>>>() {
}));
List<String> response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
UnwrapOnlyJsonValue<List<String>> parser = i.getInstance(Key
.get(new TypeLiteral<UnwrapOnlyJsonValue<List<String>>>() {
}));
List<String> response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
assertEquals(response, ImmutableList.of("67.23.10.132", "67.23.10.131"));
}
assertEquals(response, ImmutableList.of("67.23.10.132", "::babe:67.23.10.132", "67.23.10.131", "::babe:4317:0A83"));
}
public void testPrivate() throws UnknownHostException {
InputStream is = getClass().getResourceAsStream("/test_list_addresses_private.json");
@Test
public void testPrivate() throws UnknownHostException {
InputStream is = getClass().getResourceAsStream("/test_list_addresses_private.json");
UnwrapOnlyJsonValue<List<String>> parser = i.getInstance(Key
.get(new TypeLiteral<UnwrapOnlyJsonValue<List<String>>>() {
}));
List<String> response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
UnwrapOnlyJsonValue<List<String>> parser = i.getInstance(Key
.get(new TypeLiteral<UnwrapOnlyJsonValue<List<String>>>() {
}));
List<String> response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
assertEquals(response, ImmutableList.of("10.176.42.16"));
}
assertEquals(response, ImmutableList.of("67.23.10.132", "::babe:67.23.10.132", "67.23.10.131", "::babe:4317:0A83"));
}
}

View File

@ -18,65 +18,68 @@
*/
package org.jclouds.openstack.nova.functions;
import static org.testng.Assert.assertEquals;
import java.io.InputStream;
import java.net.UnknownHostException;
import java.util.List;
import org.jclouds.openstack.nova.domain.Addresses;
import org.jclouds.openstack.nova.domain.Server;
import org.jclouds.openstack.nova.domain.ServerStatus;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.functions.UnwrapOnlyJsonValue;
import org.jclouds.io.Payloads;
import org.jclouds.json.config.GsonModule;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
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.json.config.GsonModule;
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 ParseServerFromJsonResponse}
*
*
* @author Adrian Cole
*/
@Test(groups = "unit")
public class ParseServerFromJsonResponseTest {
public void testApplyInputStreamDetails() throws UnknownHostException {
Server response = parseServer();
@Test
public void testApplyInputStreamDetails() throws UnknownHostException {
Server response = parseServer();
assertEquals(response.getId(), 1234);
assertEquals(response.getName(), "sample-server");
assertEquals(response.getImageId(), new Integer(2));
assertEquals(response.getFlavorId(), new Integer(1));
assertEquals(response.getHostId(), "e4d909c290d0fb1ca068ffaddf22cbd0");
assertEquals(response.getStatus(), ServerStatus.BUILD);
assertEquals(response.getProgress(), new Integer(60));
List<String> publicAddresses = Lists.newArrayList("67.23.10.132", "67.23.10.131");
List<String> privateAddresses = Lists.newArrayList("10.176.42.16");
Addresses addresses1 = new Addresses();
addresses1.getPrivateAddresses().addAll(privateAddresses);
addresses1.getPublicAddresses().addAll(publicAddresses);
assertEquals(response.getAddresses(), addresses1);
assertEquals(response.getMetadata(), ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1"));
assertEquals(response.getId(), 1234);
assertEquals(response.getName(), "sample-server");
assertEquals(response.getImageId().intValue(), 1234);
assertEquals(response.getFlavorId().intValue(), 1);
assertEquals(response.getImageId(), "https://servers.api.rackspacecloud.com/v1.1/32278/images/1234");
assertEquals(response.getFlavorId(), "https://servers.api.rackspacecloud.com/v1.1/32278/flavors/1");
assertEquals(response.getHostId(), "e4d909c290d0fb1ca068ffaddf22cbd0");
assertEquals(true, false, "Uncomment next line");
//assertEquals(response.getAffinityId(), "fc88bcf8394db9c8d0564e08ca6a9724188a84d1");
assertEquals(response.getStatus(), ServerStatus.BUILD);
assertEquals(response.getProgress(), new Integer(60));
}
List<String> publicAddresses = ImmutableList.of("67.23.10.132", "::babe:67.23.10.132", "67.23.10.131", "::babe:4317:0A83");
List<String> privateAddresses = ImmutableList.of("10.176.42.16", "::babe:10.176.42.16");
Addresses addresses1 = new Addresses(new HashSet<String>(publicAddresses), new HashSet<String>(privateAddresses));
assertEquals(response.getAddresses(), addresses1);
assertEquals(response.getMetadata(), ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1"));
}
public static Server parseServer() {
Injector i = Guice.createInjector(new GsonModule());
public static Server parseServer() {
Injector i = Guice.createInjector(new GsonModule());
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>>() {
}));
Server response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
return response;
}
UnwrapOnlyJsonValue<Server> parser = i.getInstance(Key.get(new TypeLiteral<UnwrapOnlyJsonValue<Server>>() {
}));
Server response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
return response;
}
}

View File

@ -18,89 +18,97 @@
*/
package org.jclouds.openstack.nova.functions;
import static org.testng.Assert.assertEquals;
import java.io.InputStream;
import java.net.UnknownHostException;
import java.util.List;
import org.jclouds.openstack.nova.domain.Addresses;
import org.jclouds.openstack.nova.domain.Server;
import org.jclouds.openstack.nova.domain.ServerStatus;
import org.jclouds.http.HttpResponse;
import org.jclouds.http.functions.UnwrapOnlyJsonValue;
import org.jclouds.io.Payloads;
import org.jclouds.json.config.GsonModule;
import org.testng.annotations.Test;
import com.google.common.collect.ImmutableList;
import com.google.common.collect.ImmutableMap;
import com.google.common.collect.Lists;
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.json.config.GsonModule;
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}
*
*
* @author Adrian Cole
*/
@Test(groups = "unit")
public class ParseServerListFromJsonResponseTest {
Injector i = Guice.createInjector(new GsonModule());
Injector i = Guice.createInjector(new GsonModule());
public void testApplyInputStream() {
InputStream is = getClass().getResourceAsStream("/test_list_servers.json");
@Test
public void testApplyInputStream() {
InputStream is = getClass().getResourceAsStream("/test_list_servers.json");
List<Server> expects = ImmutableList.of(new Server(1234, "sample-server"), new Server(5678, "sample-server2"));
List<Server> expects = ImmutableList.of(new Server(1234, "sample-server"), new Server(5678, "sample-server2"));
UnwrapOnlyJsonValue<List<Server>> parser = i.getInstance(Key
.get(new TypeLiteral<UnwrapOnlyJsonValue<List<Server>>>() {
}));
List<Server> response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
UnwrapOnlyJsonValue<List<Server>> parser = i.getInstance(Key
.get(new TypeLiteral<UnwrapOnlyJsonValue<List<Server>>>() {
}));
List<Server> response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
assertEquals(response, expects);
}
assertEquals(response, expects);
}
public void testApplyInputStreamDetails() throws UnknownHostException {
InputStream is = getClass().getResourceAsStream("/test_list_servers_detail.json");
@Test
public void testApplyInputStreamDetails() throws UnknownHostException {
InputStream is = getClass().getResourceAsStream("/test_list_servers_detail.json");
UnwrapOnlyJsonValue<List<Server>> parser = i.getInstance(Key
.get(new TypeLiteral<UnwrapOnlyJsonValue<List<Server>>>() {
}));
List<Server> response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
UnwrapOnlyJsonValue<List<Server>> parser = i.getInstance(Key
.get(new TypeLiteral<UnwrapOnlyJsonValue<List<Server>>>() {
}));
List<Server> response = parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
assertEquals(response.get(0).getId(), 1234);
assertEquals(response.get(0).getName(), "sample-server");
assertEquals(response.get(0).getImageId(), new Integer(2));
assertEquals(response.get(0).getFlavorId(), new Integer(1));
assertEquals(response.get(0).getHostId(), "e4d909c290d0fb1ca068ffaddf22cbd0");
assertEquals(response.get(0).getStatus(), ServerStatus.BUILD);
assertEquals(response.get(0).getProgress(), new Integer(60));
List<String> publicAddresses = Lists.newArrayList("67.23.10.132", "67.23.10.131");
List<String> privateAddresses = Lists.newArrayList("10.176.42.16");
Addresses addresses1 = new Addresses();
addresses1.getPrivateAddresses().addAll(privateAddresses);
addresses1.getPublicAddresses().addAll(publicAddresses);
assertEquals(response.get(0).getAddresses(), addresses1);
assertEquals(response.get(0).getMetadata(), ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1"));
assertEquals(response.get(1).getId(), 5678);
assertEquals(response.get(1).getName(), "sample-server2");
assertEquals(response.get(1).getImageId(), new Integer(2));
assertEquals(response.get(1).getFlavorId(), new Integer(1));
assertEquals(response.get(1).getHostId(), "9e107d9d372bb6826bd81d3542a419d6");
assertEquals(response.get(1).getStatus(), ServerStatus.ACTIVE);
assertEquals(response.get(1).getProgress(), null);
List<String> publicAddresses2 = Lists.newArrayList("67.23.10.133");
List<String> privateAddresses2 = Lists.newArrayList("10.176.42.17");
Addresses addresses2 = new Addresses();
addresses2.getPrivateAddresses().addAll(privateAddresses2);
addresses2.getPublicAddresses().addAll(publicAddresses2);
assertEquals(response.get(1).getAddresses(), addresses2);
assertEquals(response.get(1).getMetadata(), ImmutableMap.of("Server Label", "DB 1"));
assertEquals(response.get(0).getId(), 1234);
assertEquals(response.get(0).getName(), "sample-server");
assertEquals(response.get(0).getImageId().intValue(), 1234);
assertEquals(response.get(0).getFlavorId().intValue(), 1);
assertEquals(response.get(0).getImageId(), "https://servers.api.rackspacecloud.com/v1.1/32278/images/1234");
assertEquals(response.get(0).getFlavorId(), "https://servers.api.rackspacecloud.com/v1.1/32278/flavors/1");
assertEquals(true, false, "Uncomment next line");
//assertEquals(response.getAffinityId(), "fc88bcf8394db9c8d0564e08ca6a9724188a84d1");
assertEquals(response.get(0).getHostId(), "e4d909c290d0fb1ca068ffaddf22cbd0");
assertEquals(response.get(0).getStatus(), ServerStatus.BUILD);
assertEquals(response.get(0).getProgress(), new Integer(60));
}
List<String> publicAddresses = ImmutableList.of("67.23.10.132", "::babe:67.23.10.132", "67.23.10.131", "::babe:4317:0A83");
List<String> privateAddresses = ImmutableList.of("10.176.42.16", "::babe:10.176.42.16");
Addresses addresses1 = new Addresses(new HashSet<String>(publicAddresses), new HashSet<String>(privateAddresses));
assertEquals(response.get(0).getAddresses(), addresses1);
assertEquals(response.get(0).getMetadata(), ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1"));
assertEquals(response.get(1).getId(), 5678);
assertEquals(response.get(1).getName(), "sample-server2");
assertEquals(response.get(0).getImageId(), "https://servers.api.rackspacecloud.com/v1.1/32278/images/1");
assertEquals(response.get(0).getFlavorId(), "https://servers.api.rackspacecloud.com/v1.1/32278/flavors/1");
assertEquals(true, false, "Uncomment next line");
//assertEquals(response.getAffinityId(), "b414fa41cb37b97dcb58d6c76112af1258e9eae2");
assertEquals(response.get(1).getHostId(), "9e107d9d372bb6826bd81d3542a419d6");
assertEquals(response.get(1).getStatus(), ServerStatus.ACTIVE);
assertEquals(response.get(1).getProgress(), null);
List<String> publicAddresses2 = ImmutableList.of("67.23.10.133", "::babe:67.23.10.133");
List<String> privateAddresses2 = ImmutableList.of("10.176.42.17", "::babe:10.176.42.17");
Addresses addresses2 = new Addresses(new HashSet<String>(publicAddresses2), new HashSet<String>(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");
}
}

View File

@ -1,8 +1,11 @@
{
"private" : [
"10.176.42.16"
]
{
"network" {
"id" : "private",
"values" : [
{"version" : 4, "addr" : "67.23.10.132"},
{"version" : 6, "addr" : "::babe:67.23.10.132"},
{"version" : 4, "addr" : "67.23.10.131"},
{"version" : 6, "addr" : "::babe:4317:0A83"}
]
}
}

View File

@ -1,7 +1,11 @@
{
"public" : [
"67.23.10.132",
"67.23.10.131"
{
"network" {
"id" : "public",
"values" : [
{"version" : 4, "addr" : "67.23.10.132"},
{"version" : 6, "addr" : "::babe:67.23.10.132"},
{"version" : 4, "addr" : "67.23.10.131"},
{"version" : 6, "addr" : "::babe:4317:0A83"}
]
}
}

View File

@ -35,7 +35,7 @@
"name" : "My Server Backup",
"serverRef" : "http://servers.api.openstack.org/v1.1/1234/servers/12",
"updated" : "2010-10-10T12:00:00Z",
"created" : "2010-08-10T12:00:00Z",
"created" : "2009-07-07T09:56:16Z",
"status" : "SAVING",
"progress" : 80,
"links": [
@ -54,6 +54,36 @@
"href" : "http://servers.api.openstack.org/1234/images/743"
}
]
},
{
"id" : 5,
"name" : "CentOS 5.2",
"serverRef" : 12,
"updated" : "2010-10-10T12:00:00Z",
"created" : "2010-08-10T12:00:00Z",
"status" : "ACTIVE",
"metadata" : {
"values" : {
"ImageType" : "Gold",
"ImageVersion" : "1.5"
}
},
"links": [
{
"rel" : "self",
"href" : "http://servers.api.openstack.org/v1.1/1234/images/1"
},
{
"rel" : "bookmark",
"type" : "application/vnd.openstack.compute-v1.1+xml",
"href" : "http://servers.api.openstack.org/1234/images/1"
},
{
"rel" : "bookmark",
"type" : "application/vnd.openstack.compute-v1.1+json",
"href" : "http://servers.api.openstack.org/1234/images/1"
}
]
}
]
}

View File

@ -101,7 +101,7 @@
{
"rel" : "bookmark",
"type" : "application/vnd.openstack.compute-v1.1+json",
"href" : "http://servers.api.openstack.org/1234/servers/5678"
"href" : "http://servers.api.openstack.org/1234/servers/56789"
}
]
}