// Copy existing section and edit fields
VirtualHardwareSection oldSection = vmApi.getVirtualHardwareSection(vmUrn);
Set<? extends ResourceAllocationSettingData> oldItems = oldSection.getItems();
Set<ResourceAllocationSettingData> newItems = Sets.newLinkedHashSet(oldItems);
ResourceAllocationSettingData oldMemory = Iterables.find(oldItems,
new Predicate<ResourceAllocationSettingData>() {
@Override
public boolean apply(ResourceAllocationSettingData rasd) {
return rasd.getResourceType() == ResourceAllocationSettingData.ResourceType.MEMORY;
}
});
ResourceAllocationSettingData newMemory = oldMemory.toBuilder().elementName("1024 MB of memory")
.virtualQuantity(new BigInteger("1024")).build();
newItems.remove(oldMemory);
newItems.add(newMemory);
VirtualHardwareSection newSection = oldSection.toBuilder().items(newItems).build();
// The method under test
Task editVirtualHardwareSection = vmApi.editVirtualHardwareSection(vmUrn, newSection);
assertTrue(retryTaskSuccess.apply(editVirtualHardwareSection),
String.format(TASK_COMPLETE_TIMELY, "editVirtualHardwareSection"));
// Retrieve the modified section
VirtualHardwareSection modifiedSection = vmApi.getVirtualHardwareSection(vmUrn);
// Check the retrieved object is well formed
checkVirtualHardwareSection(modifiedSection);
// Check the modified section fields are set correctly
ResourceAllocationSettingData modifiedMemory = Iterables.find(modifiedSection.getItems(),
new Predicate<ResourceAllocationSettingData>() {
@Override
public boolean apply(ResourceAllocationSettingData rasd) {
return rasd.getResourceType() == ResourceAllocationSettingData.ResourceType.MEMORY;
}
});
assertEquals(modifiedMemory.getVirtualQuantity(), new BigInteger("1024"));
assertEquals(modifiedMemory, newMemory);
assertEquals(modifiedSection, newSection);
}