PageRenderTime 21ms CodeModel.GetById 30ms RepoModel.GetById 0ms app.codeStats 0ms

/common/trmk/src/test/java/org/jclouds/trmk/vcloud_0_8/xml/VAppHandlerTest.java

https://github.com/edwardt/jclouds
Java | 196 lines | 135 code | 36 blank | 25 comment | 0 complexity | 74011157de801377b29e6fa3f23ac27d 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.trmk.vcloud_0_8.xml;
  20. import static com.google.common.collect.Iterables.find;
  21. import static org.testng.Assert.assertEquals;
  22. import java.io.InputStream;
  23. import java.net.URI;
  24. import java.net.UnknownHostException;
  25. import java.util.List;
  26. import java.util.Properties;
  27. import org.jclouds.Constants;
  28. import org.jclouds.cim.CIMPredicates;
  29. import org.jclouds.cim.ResourceAllocationSettingData;
  30. import org.jclouds.cim.VirtualSystemSettingData;
  31. import org.jclouds.cim.ResourceAllocationSettingData.ResourceType;
  32. import org.jclouds.http.functions.BaseHandlerTest;
  33. import org.jclouds.http.functions.ParseSax;
  34. import org.jclouds.http.functions.config.SaxParserModule;
  35. import org.jclouds.trmk.vcloud_0_8.TerremarkVCloudMediaType;
  36. import org.jclouds.trmk.vcloud_0_8.domain.Status;
  37. import org.jclouds.trmk.vcloud_0_8.domain.VApp;
  38. import org.jclouds.trmk.vcloud_0_8.domain.internal.ReferenceTypeImpl;
  39. import org.jclouds.trmk.vcloud_0_8.xml.VAppHandler;
  40. import org.testng.annotations.BeforeTest;
  41. import org.testng.annotations.Test;
  42. import com.google.common.collect.ImmutableList;
  43. import com.google.common.collect.ImmutableSet;
  44. import com.google.common.collect.Iterables;
  45. import com.google.common.collect.Lists;
  46. import com.google.inject.AbstractModule;
  47. import com.google.inject.Guice;
  48. import com.google.inject.name.Names;
  49. /**
  50. * Tests behavior of {@code VAppHandler}
  51. *
  52. * @author Adrian Cole
  53. */
  54. // NOTE:without testName, this will not call @Before* and fail w/NPE during surefire
  55. @Test(groups = "unit", testName = "VAppHandlerTest")
  56. public class VAppHandlerTest extends BaseHandlerTest {
  57. @BeforeTest
  58. @Override
  59. protected void setUpInjector() {
  60. injector = Guice.createInjector(new SaxParserModule(), new AbstractModule() {
  61. @Override
  62. protected void configure() {
  63. Properties toBind = new Properties();
  64. toBind.setProperty(Constants.PROPERTY_API_VERSION, "0.8");
  65. Names.bindProperties(binder(), toBind);
  66. }
  67. });
  68. factory = injector.getInstance(ParseSax.Factory.class);
  69. }
  70. public void testApplyInputStream() {
  71. InputStream is = getClass().getResourceAsStream("/launched_vapp.xml");
  72. VApp result = (VApp) factory.create(
  73. injector.getInstance(VAppHandler.class)).parse(is);
  74. assertEquals(result.getName(), "adriantest");
  75. assertEquals(result.getStatus(), Status.UNRESOLVED);
  76. assertEquals(result.getSize().longValue(), 4l);
  77. assertEquals(result.getHref(), URI.create("https://services.vcloudexpress.terremark.com/api/v0.8/vapp/13775"));
  78. assertEquals(result.getVDC(), new ReferenceTypeImpl(null, "application/vnd.vmware.vcloud.vdc+xml", URI
  79. .create("https://services.vcloudexpress.terremark.com/api/v0.8/vdc/32")));
  80. }
  81. public void testGetVApp() throws UnknownHostException {
  82. InputStream is = getClass().getResourceAsStream("/get_vapp.xml");
  83. VApp result = (VApp) factory.create(
  84. injector.getInstance(VAppHandler.class)).parse(is);
  85. assertEquals(result.getName(), "centos-53");
  86. assertEquals(result.getStatus(), Status.OFF);
  87. assertEquals(result.getSize().longValue(), 10485760);
  88. assertEquals(result.getOperatingSystemDescription(), "Red Hat Enterprise Linux 5 (64-bit)");
  89. assertEquals(result.getHref(), URI.create("https://services.vcloudexpress.terremark.com/api/v0.8/vapp/16238"));
  90. assertEquals(result.getVDC(), new ReferenceTypeImpl(null, TerremarkVCloudMediaType.VDC_XML, URI
  91. .create("https://services.vcloudexpress.terremark.com/api/v0.8/vdc/32")));
  92. VirtualSystemSettingData system = VirtualSystemSettingData.builder().instanceID("0").elementName(
  93. "Virtual Hardware Family").virtualSystemIdentifier("centos-53").virtualSystemType("vmx-07").build();
  94. assertEquals(result.getSystem().toString(), system.toString());
  95. assertEquals(result.getNetworkToAddresses().get("Internal"), ImmutableList.<String> of("10.114.34.132"));
  96. ResourceAllocationSettingData cpu = ResourceAllocationSettingData.builder().instanceID("1").elementName(
  97. "1 virtual CPU(s)").description("Number of Virtual CPUs").resourceType(ResourceType.PROCESSOR)
  98. .virtualQuantity(2l).virtualQuantityUnits("hertz * 10^6").build();
  99. ResourceAllocationSettingData controller = ResourceAllocationSettingData.builder().instanceID("3").elementName(
  100. "SCSI Controller 0").description("SCSI Controller").resourceType(ResourceType.PARALLEL_SCSI_HBA)
  101. .resourceSubType("lsilogic").address("0").virtualQuantity(1l).build();
  102. ResourceAllocationSettingData memory = ResourceAllocationSettingData.builder().instanceID("2").elementName(
  103. "512MB of memory").description("Memory Size").resourceType(ResourceType.MEMORY).virtualQuantity(512l)
  104. .virtualQuantityUnits("byte * 2^20").build();
  105. ResourceAllocationSettingData disk = ResourceAllocationSettingData.builder().instanceID("9").elementName(
  106. "Hard Disk 1").resourceType(ResourceType.DISK_DRIVE).hostResource("10485760").addressOnParent("0")
  107. .virtualQuantity(10485760l).virtualQuantityUnits("byte * 2^20").build();
  108. assertEquals(result.getResourceAllocations(), ImmutableSet.of(cpu, memory, controller, disk));
  109. assertEquals(find(result.getResourceAllocations(), CIMPredicates.resourceTypeIn(ResourceType.PROCESSOR))
  110. .getVirtualQuantity().intValue(), 1);
  111. find(result.getResourceAllocations(), CIMPredicates.resourceTypeIn(ResourceType.PARALLEL_SCSI_HBA));
  112. assertEquals(find(result.getResourceAllocations(), CIMPredicates.resourceTypeIn(ResourceType.MEMORY))
  113. .getVirtualQuantity().intValue(), 512);
  114. assertEquals(find(result.getResourceAllocations(), CIMPredicates.resourceTypeIn(ResourceType.DISK_DRIVE))
  115. .getVirtualQuantity().longValue(), 10485760);
  116. assertEquals(result.getSize().longValue(), find(result.getResourceAllocations(),
  117. CIMPredicates.resourceTypeIn(ResourceType.DISK_DRIVE)).getVirtualQuantity().longValue());
  118. }
  119. public void testGetVApp2disks() throws UnknownHostException {
  120. InputStream is = getClass().getResourceAsStream("/get_vapp2disks.xml");
  121. VApp vApp = factory.create(injector.getInstance(VAppHandler.class)).parse(is);
  122. assertEquals(vApp.getName(), "eduardo");
  123. assertEquals(vApp.getStatus(), Status.OFF);
  124. assertEquals(vApp.getSize().longValue(), 30408704);
  125. assertEquals(vApp.getOperatingSystemDescription(), "Ubuntu Linux (32-bit)");
  126. assertEquals(vApp.getHref(), URI.create("https://services.vcloudexpress.terremark.com/api/v0.8/vapp/15639"));
  127. assertEquals(vApp.getVDC(), new ReferenceTypeImpl(null, TerremarkVCloudMediaType.VDC_XML, URI
  128. .create("https://services.vcloudexpress.terremark.com/api/v0.8/vdc/32")));
  129. VirtualSystemSettingData system = VirtualSystemSettingData.builder().instanceID("0").elementName(
  130. "Virtual Hardware Family").virtualSystemIdentifier("eduardo").virtualSystemType("vmx-07").build();
  131. assertEquals(vApp.getSystem().toString(), system.toString());
  132. assertEquals(vApp.getNetworkToAddresses().get("Internal"), ImmutableList.of("10.114.34.131"));
  133. ResourceAllocationSettingData cpu = ResourceAllocationSettingData.builder().instanceID("1").elementName(
  134. "2 virtual CPU(s)").description("Number of Virtual CPUs").resourceType(ResourceType.PROCESSOR)
  135. .virtualQuantity(2l).virtualQuantityUnits("hertz * 10^6").build();
  136. ResourceAllocationSettingData controller = ResourceAllocationSettingData.builder().instanceID("3").elementName(
  137. "SCSI Controller 0").description("SCSI Controller").resourceType(ResourceType.PARALLEL_SCSI_HBA)
  138. .resourceSubType("lsilogic").address("0").virtualQuantity(1l).build();
  139. ResourceAllocationSettingData memory = ResourceAllocationSettingData.builder().instanceID("2").elementName(
  140. "1024MB of memory").description("Memory Size").resourceType(ResourceType.MEMORY).virtualQuantity(1024l)
  141. .virtualQuantityUnits("byte * 2^20").build();
  142. ResourceAllocationSettingData disk = ResourceAllocationSettingData.builder().instanceID("9").elementName(
  143. "Hard Disk 1").resourceType(ResourceType.DISK_DRIVE).hostResource("4194304").addressOnParent("0")
  144. .virtualQuantity(4194304l).virtualQuantityUnits("byte * 2^20").build();
  145. ResourceAllocationSettingData disk2 = ResourceAllocationSettingData.builder().instanceID("9").elementName(
  146. "Hard Disk 2").resourceType(ResourceType.DISK_DRIVE).hostResource("26214400").addressOnParent("1")
  147. .virtualQuantity(26214400l).virtualQuantityUnits("byte * 2^20").build();
  148. assertEquals(vApp.getResourceAllocations(), ImmutableSet.of(cpu, memory, controller, disk, disk2));
  149. assertEquals(find(vApp.getResourceAllocations(), CIMPredicates.resourceTypeIn(ResourceType.PROCESSOR))
  150. .getVirtualQuantity().intValue(), 2);
  151. find(vApp.getResourceAllocations(), CIMPredicates.resourceTypeIn(ResourceType.PARALLEL_SCSI_HBA));
  152. assertEquals(find(vApp.getResourceAllocations(), CIMPredicates.resourceTypeIn(ResourceType.MEMORY))
  153. .getVirtualQuantity().longValue(), 1024);
  154. // extract the disks on the vApp sorted by addressOnParent
  155. List<ResourceAllocationSettingData> disks = Lists.newArrayList(Iterables.filter(vApp.getResourceAllocations(),
  156. CIMPredicates.resourceTypeIn(ResourceType.DISK_DRIVE)));
  157. assertEquals(disks.get(0).getVirtualQuantity().longValue(), 4194304);
  158. assertEquals(disks.get(1).getVirtualQuantity().longValue(), 26214400);
  159. }
  160. }