ResourceProvisionerSimple extends ResourceProvisionerAbstract { @Override public boolean allocateResourceForVm(final Vm vm, final long newTotalVmResourceCapacity) { Objects.requireNonNull(vm); if (!isSuitableForVm(vm, newTotalVmResourceCapacity)) { return false; } final long prevVmResourceAllocation = vm.getResource(getResourceClass()).getAllocatedResource(); if (getResourceAllocationMap().containsKey(vm)) { deallocateResourceForVm(vm); } if(!getResource().isSubClassOf(Pe.class) && !vm.getResource(getResourceClass()).setCapacity(newTotalVmResourceCapacity)){ return false; } getResource().allocateResource(newTotalVmResourceCapacity); getResourceAllocationMap().put(vm, newTotalVmResourceCapacity); vm.getResource(getResourceClass()).setAllocatedResource(prevVmResourceAllocation); return true; } ResourceProvisionerSimple(); protected  ResourceProvisionerSimple(final ResourceManageable resource); @Override boolean allocateResourceForVm(final Vm vm, final long newTotalVmResourceCapacity); @Override boolean allocateResourceForVm(final Vm vm, final double newTotalVmResource); @Override boolean deallocateResourceForVm(final Vm vm); @Override boolean isSuitableForVm(final Vm vm, final long newVmTotalAllocatedResource);  }
@Test public void testGetTotalAllocatedResource() { final ResourceProvisioner provisioner = createSimpleProvisioner(); assertEquals(provisioner.getCapacity(), provisioner.getAvailableResource()); assertEquals(0, provisioner.getTotalAllocatedResource()); final VmSimple vm = createVm(1, CAPACITY); final long allocatedResource = HALF_CAPACITY; provisioner.allocateResourceForVm(vm, allocatedResource); assertEquals(allocatedResource, provisioner.getTotalAllocatedResource()); }
@Test public void testAllocateResourceForVm() { final VmSimple vm0 = createVm(0, HALF_CAPACITY); final VmSimple vm1 = createVm(1, CAPACITY); assertTrue(provisioner.isSuitableForVm(vm0, HALF_CAPACITY)); assertTrue(provisioner.allocateResourceForVm(vm0, HALF_CAPACITY)); assertEquals(HALF_CAPACITY, provisioner.getAvailableResource()); assertFalse(provisioner.isSuitableForVm(vm1, CAPACITY)); assertFalse(provisioner.allocateResourceForVm(vm1, CAPACITY)); assertEquals(HALF_CAPACITY, provisioner.getAvailableResource()); assertTrue(provisioner.isSuitableForVm(vm1, QUARTER_OF_CAPACITY)); assertTrue(provisioner.allocateResourceForVm(vm1, QUARTER_OF_CAPACITY)); assertEquals(QUARTER_OF_CAPACITY, provisioner.getAvailableResource()); assertTrue(provisioner.isSuitableForVm(vm1, HALF_CAPACITY)); assertTrue(provisioner.allocateResourceForVm(vm1, HALF_CAPACITY)); assertEquals(0, provisioner.getAvailableResource()); }