From 835ea8bb57db3a2148d0d1a6c6eaf05d601062c2 Mon Sep 17 00:00:00 2001 From: Jason King Date: Wed, 23 Nov 2011 16:35:30 +0000 Subject: [PATCH] Issue 695: Service call for HardwareConfiguration+parsing+tests --- .../hardware/HardwareConfiguration.java | 2 + .../features/VirtualMachineAsyncClient.java | 10 ++ .../features/VirtualMachineClient.java | 9 ++ .../VirtualMachineAsyncClientTest.java | 16 +- .../VirtualMachineClientLiveTest.java | 18 ++- .../HardwareConfigurationJAXBParsingTest.java | 142 ++++++++++++++++++ .../test/resources/hardwareConfiguration.xml | 37 +++++ 7 files changed, 230 insertions(+), 4 deletions(-) create mode 100644 sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/xml/HardwareConfigurationJAXBParsingTest.java create mode 100644 sandbox-providers/tmrk-enterprisecloud/src/test/resources/hardwareConfiguration.xml diff --git a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/hardware/HardwareConfiguration.java b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/hardware/HardwareConfiguration.java index 6a00f781a9..c994bef5ad 100644 --- a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/hardware/HardwareConfiguration.java +++ b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/domain/hardware/HardwareConfiguration.java @@ -29,6 +29,7 @@ import org.jclouds.tmrk.enterprisecloud.domain.network.Nics; import org.jclouds.tmrk.enterprisecloud.domain.network.VirtualNic; import javax.xml.bind.annotation.XmlElement; +import javax.xml.bind.annotation.XmlRootElement; import java.net.URI; import java.util.Collections; import java.util.Map; @@ -40,6 +41,7 @@ import static com.google.common.base.Preconditions.checkNotNull; * * @author Jason King */ +@XmlRootElement(name = "HardwareConfiguration") public class HardwareConfiguration extends BaseResource { @SuppressWarnings("unchecked") diff --git a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/VirtualMachineAsyncClient.java b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/VirtualMachineAsyncClient.java index a7e1a19b46..b61fee1695 100644 --- a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/VirtualMachineAsyncClient.java +++ b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/VirtualMachineAsyncClient.java @@ -22,6 +22,7 @@ import com.google.common.util.concurrent.ListenableFuture; import org.jclouds.http.filters.BasicAuthentication; import org.jclouds.rest.annotations.*; import org.jclouds.rest.functions.ReturnNullOnNotFoundOr404; +import org.jclouds.tmrk.enterprisecloud.domain.hardware.HardwareConfiguration; import org.jclouds.tmrk.enterprisecloud.domain.network.AssignedIpAddresses; import org.jclouds.tmrk.enterprisecloud.domain.vm.VirtualMachine; import org.jclouds.tmrk.enterprisecloud.domain.vm.VirtualMachineConfigurationOptions; @@ -81,4 +82,13 @@ public interface VirtualMachineAsyncClient { @JAXBResponseParser @ExceptionParser(ReturnNullOnNotFoundOr404.class) ListenableFuture getConfigurationOptions(@EndpointParam URI uri); + + /** + * @see VirtualMachineClient#getHardwareConfiguration + */ + @GET + @Consumes("application/vnd.tmrk.cloud.virtualMachineHardware") + @JAXBResponseParser + @ExceptionParser(ReturnNullOnNotFoundOr404.class) + ListenableFuture getHardwareConfiguration(@EndpointParam URI uri); } diff --git a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/VirtualMachineClient.java b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/VirtualMachineClient.java index c72f9255ce..a2680fc3ff 100644 --- a/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/VirtualMachineClient.java +++ b/sandbox-providers/tmrk-enterprisecloud/src/main/java/org/jclouds/tmrk/enterprisecloud/features/VirtualMachineClient.java @@ -19,6 +19,7 @@ package org.jclouds.tmrk.enterprisecloud.features; import org.jclouds.concurrent.Timeout; +import org.jclouds.tmrk.enterprisecloud.domain.hardware.HardwareConfiguration; import org.jclouds.tmrk.enterprisecloud.domain.network.AssignedIpAddresses; import org.jclouds.tmrk.enterprisecloud.domain.vm.VirtualMachine; import org.jclouds.tmrk.enterprisecloud.domain.vm.VirtualMachineConfigurationOptions; @@ -71,4 +72,12 @@ public interface VirtualMachineClient { */ VirtualMachineConfigurationOptions getConfigurationOptions(URI uri); + /** + * The Get Virtual Machines Hardware Configuration call returns information + * regarding the hardware configuration of a specified virtual machine in a compute pool. + * @param uri the uri for the hardware configuration e.g. /cloudapi/ecloud/virtualmachines/{id}/hardwareconfiguration + * @return + */ + HardwareConfiguration getHardwareConfiguration(URI uri); + } diff --git a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/VirtualMachineAsyncClientTest.java b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/VirtualMachineAsyncClientTest.java index fe9eda287c..8122981a11 100644 --- a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/VirtualMachineAsyncClientTest.java +++ b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/VirtualMachineAsyncClientTest.java @@ -81,7 +81,7 @@ public class VirtualMachineAsyncClientTest extends BaseTerremarkEnterpriseCloudA checkFilters(httpRequest); } - public void testGetVirtualMachineConfigurationOptions() throws SecurityException, NoSuchMethodException, IOException, URISyntaxException { + public void testGetConfigurationOptions() throws SecurityException, NoSuchMethodException, IOException, URISyntaxException { Method method = VirtualMachineAsyncClient.class.getMethod("getConfigurationOptions", URI.class); HttpRequest httpRequest = processor.createRequest(method,new URI("/cloudapi/ecloud/virtualmachines/5504/configurationoptions")); @@ -95,6 +95,20 @@ public class VirtualMachineAsyncClientTest extends BaseTerremarkEnterpriseCloudA checkFilters(httpRequest); } + public void testGetHardwareConfiguration() throws SecurityException, NoSuchMethodException, IOException, URISyntaxException { + Method method = VirtualMachineAsyncClient.class.getMethod("getHardwareConfiguration", URI.class); + HttpRequest httpRequest = processor.createRequest(method,new URI("/cloudapi/ecloud/virtualmachines/5504/hardwareconfiguration")); + + assertRequestLineEquals(httpRequest, "GET https://services-beta.enterprisecloud.terremark.com/cloudapi/ecloud/virtualmachines/5504/hardwareconfiguration HTTP/1.1"); + assertNonPayloadHeadersEqual(httpRequest, "Accept: application/vnd.tmrk.cloud.virtualMachineHardware\nx-tmrk-version: 2011-07-01\n"); + assertPayloadEquals(httpRequest, null, null, false); + + assertResponseParserClassEquals(method, httpRequest, ParseXMLWithJAXB.class); + assertExceptionParserClassEquals(method, ReturnNullOnNotFoundOr404.class); + + checkFilters(httpRequest); + } + @Override protected TypeLiteral> createTypeLiteral() { return new TypeLiteral>() { diff --git a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/VirtualMachineClientLiveTest.java b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/VirtualMachineClientLiveTest.java index 049f151e73..89cc671f49 100644 --- a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/VirtualMachineClientLiveTest.java +++ b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/features/VirtualMachineClientLiveTest.java @@ -19,6 +19,7 @@ package org.jclouds.tmrk.enterprisecloud.features; import com.google.common.collect.Iterables; +import org.jclouds.tmrk.enterprisecloud.domain.hardware.HardwareConfiguration; import org.jclouds.tmrk.enterprisecloud.domain.network.AssignedIpAddresses; import org.jclouds.tmrk.enterprisecloud.domain.network.DeviceNetwork; import org.jclouds.tmrk.enterprisecloud.domain.vm.VirtualMachine; @@ -85,8 +86,19 @@ public class VirtualMachineClientLiveTest extends BaseTerremarkEnterpriseCloudCl assertNull(assignedIpAddresses); } - public void testGetVirtualMachineConfigurationOptions() throws Exception { - VirtualMachineConfigurationOptions virtualMachineConfigurationOptions = client.getConfigurationOptions(new URI("/cloudapi/ecloud/virtualmachines/5504/configurationoptions")); - assertNotNull(virtualMachineConfigurationOptions); + public void testGetConfigurationOptions() throws Exception { + VirtualMachineConfigurationOptions configurationOptions = client.getConfigurationOptions(new URI("/cloudapi/ecloud/virtualmachines/5504/configurationoptions")); + assertNotNull(configurationOptions); } + + public void testGetHardwareConfiguration() throws Exception { + HardwareConfiguration hardwareConfiguration = client.getHardwareConfiguration(new URI("/cloudapi/ecloud/virtualmachines/5504/hardwareconfiguration")); + assertNotNull(hardwareConfiguration); + } + + public void testGetHardwareConfigurationWhenMissing() throws Exception { + HardwareConfiguration result = client.getHardwareConfiguration(new URI("/cloudapi/ecloud/virtualmachines/-1/hardwareconfiguration")); + assertNull(result); + } + } diff --git a/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/xml/HardwareConfigurationJAXBParsingTest.java b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/xml/HardwareConfigurationJAXBParsingTest.java new file mode 100644 index 0000000000..63bf121c5a --- /dev/null +++ b/sandbox-providers/tmrk-enterprisecloud/src/test/java/org/jclouds/tmrk/enterprisecloud/xml/HardwareConfigurationJAXBParsingTest.java @@ -0,0 +1,142 @@ +/** + * Licensed to jclouds, Inc. (jclouds) under one or more + * contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. jclouds licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.jclouds.tmrk.enterprisecloud.xml; + +import com.google.common.base.Function; +import com.google.common.collect.ImmutableSet; +import com.google.inject.AbstractModule; +import com.google.inject.Module; +import com.google.inject.Provides; +import org.jclouds.crypto.Crypto; +import org.jclouds.http.HttpRequest; +import org.jclouds.http.HttpResponse; +import org.jclouds.http.functions.ParseSax; +import org.jclouds.http.functions.ParseXMLWithJAXB; +import org.jclouds.logging.config.NullLoggingModule; +import org.jclouds.rest.AuthorizationException; +import org.jclouds.rest.BaseRestClientTest; +import org.jclouds.rest.RestContextSpec; +import org.jclouds.rest.internal.RestAnnotationProcessor; +import org.jclouds.tmrk.enterprisecloud.domain.hardware.HardwareConfiguration; +import org.jclouds.tmrk.enterprisecloud.domain.hardware.VirtualDisk; +import org.jclouds.tmrk.enterprisecloud.domain.internal.ResourceCapacity; +import org.jclouds.tmrk.enterprisecloud.domain.network.NetworkReference; +import org.jclouds.tmrk.enterprisecloud.domain.network.VirtualNic; +import org.jclouds.tmrk.enterprisecloud.features.VirtualMachineAsyncClient; +import org.testng.annotations.BeforeClass; +import org.testng.annotations.Test; + +import javax.inject.Named; +import java.io.InputStream; +import java.lang.reflect.Method; +import java.net.URI; +import java.util.Set; + +import static org.jclouds.io.Payloads.newInputStreamPayload; +import static org.jclouds.rest.RestContextFactory.contextSpec; +import static org.jclouds.rest.RestContextFactory.createContextBuilder; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; +import static org.testng.AssertJUnit.assertEquals; +import static org.testng.AssertJUnit.assertNotNull; + +/** + * Tests behavior of JAXB parsing for VirtualMachines + * @author Jason King + * + */ +public class HardwareConfigurationJAXBParsingTest extends BaseRestClientTest { + + @BeforeClass + void setupFactory() { + RestContextSpec contextSpec = contextSpec("test", "http://localhost:9999", "1", "", "userfoo", + "credentialFoo", String.class, Integer.class, + ImmutableSet. of(new MockModule(), new NullLoggingModule(), new AbstractModule() { + + @Override + protected void configure() {} + + @SuppressWarnings("unused") + @Provides + @Named("exception") + Set exception() { + throw new AuthorizationException(); + } + + })); + + injector = createContextBuilder(contextSpec).buildInjector(); + parserFactory = injector.getInstance(ParseSax.Factory.class); + crypto = injector.getInstance(Crypto.class); + } + + @Test(groups = "unit", testName = "HardwareConfigurationJAXBParsingTest") + public void testParse() throws Exception { + + Method method = VirtualMachineAsyncClient.class.getMethod("getHardwareConfiguration", URI.class); + HttpRequest request = factory(VirtualMachineAsyncClient.class).createRequest(method,new URI("/1")); + assertResponseParserClassEquals(method, request, ParseXMLWithJAXB.class); + + Function parser = (Function) RestAnnotationProcessor + .createResponseParser(parserFactory, injector, method, request); + + InputStream is = getClass().getResourceAsStream("/hardwareConfiguration.xml"); + HardwareConfiguration hardwareConfiguration = parser.apply(new HttpResponse(200, "ok", newInputStreamPayload(is))); + + assertHardwareConfiguration(hardwareConfiguration); + } + + private void assertHardwareConfiguration(HardwareConfiguration hardwareConfiguration) throws Exception { + assertEquals(1,hardwareConfiguration.getActions().size()); + assertEquals(1,hardwareConfiguration.getProcessorCount()); + ResourceCapacity memory = ResourceCapacity.builder().value(384).unit("MB").build(); + assertEquals(memory,hardwareConfiguration.getMemory()); + assertDisks(hardwareConfiguration.getVirtualDisks()); + assertNics(hardwareConfiguration.getVirtualNics()); + } + + private void assertDisks(Set disks) { + VirtualDisk disk = VirtualDisk.builder().index(0).name("Hard Disk 1") + .size(ResourceCapacity.builder().value(10).unit("GB").build()) + .build(); + + assertEquals(ImmutableSet.of(disk), disks); + } + + + private void assertNics(Set nics) throws Exception { + + assertEquals(1, nics.size()); + + NetworkReference network = NetworkReference.builder() + .href(new URI("/cloudapi/ecloud/networks/3936")) + .name("10.146.204.64/28") + .type("application/vnd.tmrk.cloud.network") + .networkType(NetworkReference.NetworkType.INTERNAL) + .build(); + + VirtualNic nic = VirtualNic.builder() + .macAddress("00:50:56:b8:00:58") + .name("Network adapter 1") + .network(network) + .unitNumber(7) + .build(); + assertEquals(nic,nics.iterator().next()); + } +} diff --git a/sandbox-providers/tmrk-enterprisecloud/src/test/resources/hardwareConfiguration.xml b/sandbox-providers/tmrk-enterprisecloud/src/test/resources/hardwareConfiguration.xml new file mode 100644 index 0000000000..2eb8996f00 --- /dev/null +++ b/sandbox-providers/tmrk-enterprisecloud/src/test/resources/hardwareConfiguration.xml @@ -0,0 +1,37 @@ + + + + + 1 + + MB + 384 + + + + 0 + + GB + 10 + + Hard Disk 1 + + + + + 7 + Network adapter 1 + 00:50:56:b8:00:58 + + Internal + + + + \ No newline at end of file