From 6d657b1c90d590d38a6110f18f4683592932336d Mon Sep 17 00:00:00 2001 From: Richard Downer Date: Mon, 5 Dec 2011 14:21:17 +0200 Subject: [PATCH 1/2] Fix bug in parsing VirtualMachine.cpuUsed --- .../main/java/org/jclouds/cloudstack/domain/VirtualMachine.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/VirtualMachine.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/VirtualMachine.java index fe8efaadfe..ad46cfa887 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/VirtualMachine.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/VirtualMachine.java @@ -478,7 +478,7 @@ public class VirtualMachine implements Comparable { * @return the amount of the vm's CPU currently used */ public float getCpuUsed() { - return cpuUsed != null ? Float.parseFloat(cpuUsed.substring(9, cpuUsed.length() - 1)) : 0.0f; + return cpuUsed != null ? Float.parseFloat(cpuUsed.substring(0, cpuUsed.length() - 1)) : 0.0f; } /** From 09b6df353a5c1d49040810b2f831f4757222c497 Mon Sep 17 00:00:00 2001 From: Richard Downer Date: Mon, 5 Dec 2011 19:24:49 +0200 Subject: [PATCH 2/2] Further changes to VirtualMachine related to input and parsing of CpuUsed, and add a unit test --- .../cloudstack/domain/VirtualMachine.java | 9 ++-- .../cloudstack/domain/VirtualMachineTest.java | 52 +++++++++++++++++++ 2 files changed, 58 insertions(+), 3 deletions(-) create mode 100644 apis/cloudstack/src/test/java/org/jclouds/cloudstack/domain/VirtualMachineTest.java diff --git a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/VirtualMachine.java b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/VirtualMachine.java index ad46cfa887..17c46e0adf 100644 --- a/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/VirtualMachine.java +++ b/apis/cloudstack/src/main/java/org/jclouds/cloudstack/domain/VirtualMachine.java @@ -26,6 +26,8 @@ import java.util.Set; import javax.annotation.Nullable; import com.google.common.base.CaseFormat; +import com.google.common.base.Preconditions; +import com.google.common.base.Strings; import com.google.common.collect.ImmutableSet; import com.google.common.collect.Iterables; import com.google.gson.annotations.SerializedName; @@ -44,7 +46,7 @@ public class VirtualMachine implements Comparable { private String account; private long cpuCount; private long cpuSpeed; - private Long cpuUsed; + private String cpuUsed; private String displayName; private Date created; private String domain; @@ -102,7 +104,7 @@ public class VirtualMachine implements Comparable { return this; } - public Builder cpuUsed(long cpuUsed) { + public Builder cpuUsed(String cpuUsed) { this.cpuUsed = cpuUsed; return this; } @@ -388,7 +390,7 @@ public class VirtualMachine implements Comparable { @SerializedName("securitygroup") private Set securityGroups = ImmutableSet. of(); - public VirtualMachine(long id, String account, long cpuCount, long cpuSpeed, Long cpuUsed, String displayName, + public VirtualMachine(long id, String account, long cpuCount, long cpuSpeed, String cpuUsed, String displayName, Date created, String domain, long domainId, boolean usesVirtualNetwork, String group, long groupId, long guestOSId, boolean hAEnabled, long hostId, String hostname, String iPAddress, String iSODisplayText, long iSOId, String iSOName, Long jobId, Integer jobStatus, long memory, String name, Long networkKbsRead, @@ -396,6 +398,7 @@ public class VirtualMachine implements Comparable { Set securityGroups, long serviceOfferingId, String serviceOfferingName, State state, String templateDisplayText, long templateId, String templateName, long zoneId, String zoneName, Set nics, String hypervisor) { + Preconditions.checkArgument(Strings.isNullOrEmpty(cpuUsed) || cpuUsed.matches("^[0-9\\.]+%$"), "cpuUsed value should be a decimal number followed by %"); this.id = id; this.account = account; this.cpuCount = cpuCount; diff --git a/apis/cloudstack/src/test/java/org/jclouds/cloudstack/domain/VirtualMachineTest.java b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/domain/VirtualMachineTest.java new file mode 100644 index 0000000000..1004b68714 --- /dev/null +++ b/apis/cloudstack/src/test/java/org/jclouds/cloudstack/domain/VirtualMachineTest.java @@ -0,0 +1,52 @@ +/** + * 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.cloudstack.domain; + +import org.testng.annotations.Test; + +import static org.testng.Assert.assertEquals; +import static org.testng.Assert.assertFalse; +import static org.testng.Assert.assertTrue; + +/** + * Tests for the VirtualMachine class. + * + * @author Richard Downer + */ +@Test(groups = "unit", testName = "VirtualMachineTest") +public class VirtualMachineTest { + + @Test(groups = "unit", enabled = true) + public void testCpuUsed() { + // Class under test should detect if the % is missing off the end + boolean caught = false; + try { VirtualMachine.builder().cpuUsed("23.4").build(); } catch (Exception e) { caught = true; } + assertTrue(caught); + + // If CpuUsed is not specified at all, that's OK + caught = false; + try { VirtualMachine.builder().build(); } catch (Exception e) { caught = true; } + assertFalse(caught); + + // Retrieving CpuUsed should just give us a straightforward float + VirtualMachine vm = VirtualMachine.builder().cpuUsed("23.4%").build(); + assertEquals(vm.getCpuUsed(), 23.4, 0.01); + } + +}