Merge pull request #210 from richardcloudsoft/master

Fix bug in parsing VirtualMachine.cpuUsed
This commit is contained in:
Adrian Cole 2011-12-05 10:18:47 -08:00
commit c7a240d887
2 changed files with 59 additions and 4 deletions

View File

@ -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<VirtualMachine> {
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<VirtualMachine> {
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<VirtualMachine> {
@SerializedName("securitygroup")
private Set<SecurityGroup> securityGroups = ImmutableSet.<SecurityGroup> 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<VirtualMachine> {
Set<SecurityGroup> securityGroups, long serviceOfferingId, String serviceOfferingName, State state,
String templateDisplayText, long templateId, String templateName, long zoneId, String zoneName, Set<NIC> 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;
@ -478,7 +481,7 @@ public class VirtualMachine implements Comparable<VirtualMachine> {
* @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;
}
/**

View File

@ -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);
}
}