/apis/openstack-nova/src/test/java/org/jclouds/openstack/nova/v2_0/config/ImageAdapterTest.java

http://github.com/jclouds/jclouds · Java · 93 lines · 56 code · 17 blank · 20 comment · 0 complexity · 200cc5851cdf1be85fe8a653242a9d9e MD5 · raw file

  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.jclouds.openstack.nova.v2_0.config;
  18. import static com.google.common.collect.Iterables.getOnlyElement;
  19. import static org.testng.Assert.assertEquals;
  20. import static org.testng.Assert.assertNotNull;
  21. import java.io.IOException;
  22. import org.jclouds.json.config.GsonModule;
  23. import org.jclouds.openstack.nova.v2_0.domain.Image;
  24. import org.testng.annotations.BeforeTest;
  25. import org.testng.annotations.Test;
  26. import com.google.common.base.Charsets;
  27. import com.google.common.io.Resources;
  28. import com.google.gson.Gson;
  29. import com.google.inject.Guice;
  30. import com.google.inject.Injector;
  31. @Test(groups = "unit")
  32. public class ImageAdapterTest {
  33. private Gson gson;
  34. @BeforeTest
  35. public void setup() {
  36. Injector injector = Guice.createInjector(new GsonModule(), new NovaParserModule());
  37. gson = injector.getInstance(Gson.class);
  38. }
  39. public void testDeserializeWithBlockDeviceMappingAndMetadata() throws Exception {
  40. ImageContainer container = gson.fromJson(stringFromResource("image_details_with_block_device_mapping.json"), ImageContainer.class);
  41. // Note that the block device mapping keys are removed from the metadata by the adapter.
  42. assertNotNull(container.image.getMetadata());
  43. assertEquals(container.image.getMetadata().size(), 2);
  44. assertEquals("Gold", container.image.getMetadata().get("ImageType"));
  45. assertEquals("1.5", container.image.getMetadata().get("ImageVersion"));
  46. assertNotNull(container.image.getBlockDeviceMapping());
  47. assertEquals(container.image.getBlockDeviceMapping().size(), 1);
  48. assertEquals(Integer.valueOf(2), getOnlyElement(container.image.getBlockDeviceMapping()).getBootIndex());
  49. assertEquals("snapshot", getOnlyElement(container.image.getBlockDeviceMapping()).getSourceType());
  50. }
  51. public void testDeserializeWithoutBlockDeviceMapping() throws Exception {
  52. ImageContainer container = gson.fromJson(stringFromResource("image_details.json"), ImageContainer.class);
  53. assertNotNull(container.image.getMetadata());
  54. assertEquals(container.image.getMetadata().size(), 2);
  55. assertEquals("Gold", container.image.getMetadata().get("ImageType"));
  56. assertEquals("1.5", container.image.getMetadata().get("ImageVersion"));
  57. assertNotNull(container.image.getBlockDeviceMapping());
  58. assertEquals(0, container.image.getBlockDeviceMapping().size());
  59. }
  60. public void testDeserializeWithoutBlockDeviceMappingOrMetadata() throws Exception {
  61. ImageContainer container = gson.fromJson(stringFromResource("image_details_without_metadata.json"), ImageContainer.class);
  62. assertNotNull(container.image.getMetadata());
  63. assertEquals(container.image.getMetadata().size(), 0);
  64. assertNotNull(container.image.getBlockDeviceMapping());
  65. assertEquals(0, container.image.getBlockDeviceMapping().size());
  66. }
  67. private String stringFromResource(String resource) throws IOException {
  68. return Resources.toString(Resources.getResource(resource), Charsets.UTF_8);
  69. }
  70. // Note that the ImageApi methods use the "@SelectJson" annotation to unwrap the object inside the "image" key
  71. // We use this container to deserialize the Image object to simulate that behavior and use a *real* json
  72. // in the tests.
  73. public static class ImageContainer {
  74. public Image image;
  75. }
  76. }