PageRenderTime 32ms CodeModel.GetById 19ms app.highlight 11ms RepoModel.GetById 0ms app.codeStats 0ms

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

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