PageRenderTime 54ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/apis/nova/src/test/java/org/jclouds/openstack/nova/functions/ParseServerFromJsonResponseTest.java

https://github.com/ddurnev/jclouds
Java | 107 lines | 70 code | 14 blank | 23 comment | 0 complexity | 1429c81e6aae94f3d7765e82b40184ab MD5 | raw file
  1. /**
  2. * Licensed to jclouds, Inc. (jclouds) under one or more
  3. * contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. jclouds licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing,
  13. * software distributed under the License is distributed on an
  14. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  15. * KIND, either express or implied. See the License for the
  16. * specific language governing permissions and limitations
  17. * under the License.
  18. */
  19. package org.jclouds.openstack.nova.functions;
  20. import com.google.common.collect.ImmutableList;
  21. import com.google.common.collect.ImmutableMap;
  22. import com.google.common.collect.Iterables;
  23. import com.google.inject.Guice;
  24. import com.google.inject.Injector;
  25. import com.google.inject.Key;
  26. import com.google.inject.TypeLiteral;
  27. import org.jclouds.http.HttpResponse;
  28. import org.jclouds.http.functions.UnwrapOnlyJsonValue;
  29. import org.jclouds.io.Payloads;
  30. import org.jclouds.json.config.GsonModule;
  31. import org.jclouds.openstack.nova.domain.Address;
  32. import org.jclouds.openstack.nova.domain.Addresses;
  33. import org.jclouds.openstack.nova.domain.Server;
  34. import org.jclouds.openstack.nova.domain.ServerStatus;
  35. import org.testng.annotations.Test;
  36. import java.io.InputStream;
  37. import java.net.UnknownHostException;
  38. import java.text.ParseException;
  39. import java.text.SimpleDateFormat;
  40. import java.util.HashSet;
  41. import java.util.List;
  42. import java.util.Locale;
  43. import java.util.SimpleTimeZone;
  44. import static org.testng.Assert.assertEquals;
  45. /**
  46. * Tests behavior of {@code ParseServerFromJsonResponse}
  47. *
  48. * @author Adrian Cole
  49. */
  50. @Test(groups = "unit")
  51. public class ParseServerFromJsonResponseTest {
  52. @Test
  53. public void testApplyInputStreamDetails() throws UnknownHostException, NoSuchMethodException, ClassNotFoundException, ParseException {
  54. Server response = parseServer();
  55. assertEquals(response.getId(), 1234);
  56. assertEquals(response.getName(), "sample-server");
  57. assertEquals(response.getImageRef(), "https://servers.api.rackspacecloud.com/v1.1/1234/images/1");
  58. assertEquals(response.getFlavorRef(), "http://servers.api.openstack.org/1234/flavors/1");
  59. assertEquals(response.getHostId(), "e4d909c290d0fb1ca068ffaddf22cbd0");
  60. assertEquals(response.getStatus(), ServerStatus.BUILD);
  61. assertEquals(response.getProgress(), new Integer(60));
  62. SimpleDateFormat dateFormat = new SimpleDateFormat(
  63. "yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);
  64. dateFormat.setTimeZone(new SimpleTimeZone(0, "GMT"));
  65. assertEquals(response.getCreated(),
  66. dateFormat.parse("2010-08-10T12:00:00Z"));
  67. assertEquals(response.getUpdated(),
  68. dateFormat.parse("2010-10-10T12:00:00Z"));
  69. List<Address> publicAddresses = ImmutableList.copyOf(Iterables.transform(
  70. ImmutableList.of("67.23.10.132", "::babe:67.23.10.132", "67.23.10.131", "::babe:4317:0A83"),
  71. Address.newString2AddressFunction()));
  72. List<Address> privateAddresses = ImmutableList.copyOf(Iterables.transform(
  73. ImmutableList.of("10.176.42.16", "::babe:10.176.42.16"),
  74. Address.newString2AddressFunction()));
  75. Addresses addresses1 = new Addresses(new HashSet<Address>(publicAddresses), new HashSet<Address>(privateAddresses));
  76. assertEquals(response.getAddresses(), addresses1);
  77. assertEquals(response.getMetadata(), ImmutableMap.of("Server Label", "Web Head 1", "Image Version", "2.1"));
  78. assertEquals(response.getAddresses(), addresses1);
  79. }
  80. public static Server parseServer() throws NoSuchMethodException, ClassNotFoundException {
  81. Injector i = Guice.createInjector(new GsonModule() {
  82. @Override
  83. protected void configure() {
  84. super.configure();
  85. bind(DateAdapter.class).to(Iso8601DateAdapter.class);
  86. }
  87. });
  88. InputStream is = ParseServerFromJsonResponseTest.class.getResourceAsStream("/test_get_server_detail.json");
  89. UnwrapOnlyJsonValue<Server> parser = i.getInstance(Key.get(new TypeLiteral<UnwrapOnlyJsonValue<Server>>() {
  90. }));
  91. return parser.apply(new HttpResponse(200, "ok", Payloads.newInputStreamPayload(is)));
  92. }
  93. }