use of org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl in project che-server by eclipse-che.
the class AbstractExhaustibleResource method deduct.
@Override
public Resource deduct(Resource total, Resource deduction) throws NoEnoughResourcesException {
checkResource(total);
checkResource(deduction);
if (total.getAmount() == -1) {
return total;
}
if (deduction.getAmount() == -1) {
throw new NoEnoughResourcesException(total, deduction, deduction);
}
final long resultAmount = total.getAmount() - deduction.getAmount();
if (resultAmount < 0) {
throw new NoEnoughResourcesException(total, deduction, new ResourceImpl(getId(), -resultAmount, getDefaultUnit()));
}
return new ResourceImpl(getId(), resultAmount, getDefaultUnit());
}
use of org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl in project che-server by eclipse-che.
the class ResourceManagerTest method testReturnsResourceDetailsForGivenAccount.
@Test
public void testReturnsResourceDetailsForGivenAccount() throws Exception {
final ResourceImpl testResource = new ResourceImpl("RAM", 1000, "mb");
final ResourceImpl reducedResource = new ResourceImpl("timeout", 2000, "m");
final ProvidedResourcesImpl providedResource = new ProvidedResourcesImpl("test", null, ACCOUNT_ID, 123L, 321L, singletonList(testResource));
when(resourcesProvider.getResources(eq(ACCOUNT_ID))).thenReturn(singletonList(providedResource));
when(resourceAggregator.aggregateByType(any())).thenReturn(ImmutableMap.of(reducedResource.getType(), reducedResource));
final ResourcesDetails resourcesDetails = resourceManager.getResourceDetails(ACCOUNT_ID);
verify(resourcesProvider).getResources(eq(ACCOUNT_ID));
verify(resourceAggregator).aggregateByType(eq(singletonList(testResource)));
assertEquals(resourcesDetails.getAccountId(), ACCOUNT_ID);
assertEquals(resourcesDetails.getProvidedResources().size(), 1);
assertEquals(resourcesDetails.getProvidedResources().get(0), providedResource);
assertEquals(resourcesDetails.getTotalResources().size(), 1);
assertEquals(resourcesDetails.getTotalResources().get(0), reducedResource);
}
use of org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl in project che-server by eclipse-che.
the class ResourceManagerTest method testGetsAvailableResources.
@Test
public void testGetsAvailableResources() throws Exception {
final List<ResourceImpl> availableResources = ImmutableList.of(new ResourceImpl("RAM", 2048, "mb"));
doReturn(availableResources).when(accountTypeToAvailableResourcesProvider).getAvailableResources(ACCOUNT_ID);
List<? extends Resource> actual = resourceManager.getAvailableResources(ACCOUNT_ID);
assertEquals(actual.size(), availableResources.size());
assertTrue(actual.containsAll(availableResources));
}
use of org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl in project che-server by eclipse-che.
the class OrganizationResourcesDistributorTest method shouldResourceAvailabilityCappingResourcesWhenResourceCapIsLessThanUsedOne.
@Test
public void shouldResourceAvailabilityCappingResourcesWhenResourceCapIsLessThanUsedOne() throws Exception {
// given
doCallRealMethod().when(manager).checkResourcesAvailability(anyString(), any());
ResourceImpl used = createTestResource(500);
doReturn(singletonList(used)).when(resourceManager).getUsedResources(any());
ResourceImpl toCap = createTestResource(700);
doReturn(createTestResource(200)).when(resourceAggregator).deduct((Resource) any(), any());
// when
manager.checkResourcesAvailability(ORG_ID, singletonList(toCap));
// then
verify(resourceManager).getUsedResources(ORG_ID);
verify(resourceAggregator).deduct(toCap, used);
}
use of org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl in project che-server by eclipse-che.
the class OrganizationResourcesDistributorTest method shouldRemoveResourceFromListWhenItsAmountEqualsToMinusOne.
@Test
public void shouldRemoveResourceFromListWhenItsAmountEqualsToMinusOne() throws Exception {
ResourceImpl toCap = new ResourceImpl("test1", 1000, "init");
ResourceImpl toReset = new ResourceImpl("test2", -1, "init");
List<ResourceImpl> resourcesToCap = asList(toCap, toReset);
// when
manager.capResources(ORG_ID, resourcesToCap);
// then
verify(manager).checkResourcesAvailability(ORG_ID, singletonList(toCap));
verify(distributedResourcesDao).store(new OrganizationDistributedResourcesImpl(ORG_ID, singletonList(toCap)));
verify(resourcesLocks).lock(ORG_ID);
verify(lock).close();
}
Aggregations