use of org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException in project che-server by eclipse-che.
the class DefaultAvailableResourcesProviderTest method shouldReturnExcessiveResourcesWhenNotOneResourceIsUsedButNotPresentInTotal.
@Test
public void shouldReturnExcessiveResourcesWhenNotOneResourceIsUsedButNotPresentInTotal() throws Exception {
// given
List<ResourceImpl> totalResources = singletonList(new ResourceImpl("test", 5000, "unit"));
doReturn(totalResources).when(resourceManager).getTotalResources(anyString());
List<ResourceImpl> usedResources = Arrays.asList(new ResourceImpl("test", 2000, "unit"), new ResourceImpl("test2", 5, "unit"));
doReturn(usedResources).when(resourceManager).getUsedResources(anyString());
doThrow(new NoEnoughResourcesException(emptyList(), emptyList(), emptyList())).when(resourceAggregator).deduct(anyList(), anyList());
ResourceImpl excessiveResource = new ResourceImpl("test", 3000, "unit");
doReturn(singletonList(excessiveResource)).when(resourceAggregator).excess(anyList(), anyList());
// when
List<? extends Resource> availableResources = defaultAvailableResourcesProvider.getAvailableResources("account123");
// then
assertEquals(availableResources.size(), 1);
assertEquals(availableResources.get(0), excessiveResource);
verify(resourceManager).getTotalResources("account123");
verify(resourceManager).getUsedResources("account123");
verify(resourceAggregator).deduct(totalResources, usedResources);
verify(resourceAggregator).excess(totalResources, usedResources);
}
use of org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException in project che-server by eclipse-che.
the class LimitsCheckingWorkspaceManagerTest method shouldThrowLimitExceedExceptionIfAccountDoesNotHaveEnoughAvailableRamResource.
@Test(expectedExceptions = LimitExceededException.class, expectedExceptionsMessageRegExp = "Workspace namespace/workspace.. needs 3000MB to start\\. " + "Your account has 200MB available and 100MB in use\\. " + "The workspace can't be start. Stop other workspaces or grant more resources\\.")
public void shouldThrowLimitExceedExceptionIfAccountDoesNotHaveEnoughAvailableRamResource() throws Exception {
doThrow(new NoEnoughResourcesException(singletonList(new ResourceImpl(RamResourceType.ID, 200L, RamResourceType.UNIT)), singletonList(new ResourceImpl(RamResourceType.ID, 3000L, RamResourceType.UNIT)), emptyList())).when(resourceManager).checkResourcesAvailability(any(), any());
doReturn(singletonList(new ResourceImpl(RamResourceType.ID, 100L, RamResourceType.UNIT))).when(resourceManager).getUsedResources(any());
// given
LimitsCheckingWorkspaceManager manager = managerBuilder().setResourceManager(resourceManager).setEnvironmentRamCalculator(environmentRamCalculator).build();
when(environmentRamCalculator.calculate(any(Environment.class))).thenReturn(3000L);
WorkspaceConfig config = createConfig("3gb");
// when
manager.checkRamResourcesAvailability(ACCOUNT_ID, NAMESPACE, config, null);
}
use of org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException in project che-server by eclipse-che.
the class ResourceAggregatorTest method shouldThrowConflictExceptionWhenTotalResourcesDoNotHaveEnoughAmountToDeduct.
@Test(expectedExceptions = NoEnoughResourcesException.class)
public void shouldThrowConflictExceptionWhenTotalResourcesDoNotHaveEnoughAmountToDeduct() throws Exception {
// given
final ResourceImpl aResource = new ResourceImpl(A_RESOURCE_TYPE, 111, "unit");
final ResourceImpl anotherAResource = new ResourceImpl(A_RESOURCE_TYPE, 333, "unit");
when(aResourceType.deduct(any(), any())).thenThrow(new NoEnoughResourcesException(singletonList(aResource), singletonList(anotherAResource), singletonList(new ResourceImpl(A_RESOURCE_TYPE, 222, "unit"))));
// when
resourceAggregator.deduct(singletonList(aResource), singletonList(anotherAResource));
}
use of org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException in project che-server by eclipse-che.
the class ResourceAggregatorTest method shouldNotReturnResourceAsExcessiveWhenToCompareResourceIsGreaterThanSource.
@Test
public void shouldNotReturnResourceAsExcessiveWhenToCompareResourceIsGreaterThanSource() throws Exception {
// given
final ResourceImpl sourceAResource = new ResourceImpl(A_RESOURCE_TYPE, 5, "unit");
final ResourceImpl toCompareAResource = new ResourceImpl(A_RESOURCE_TYPE, 10, "unit");
doThrow(new NoEnoughResourcesException(emptyList(), emptyList(), emptyList())).when(aResourceType).deduct(any(), any());
// when
List<? extends Resource> excess = resourceAggregator.excess(singletonList(sourceAResource), singletonList(toCompareAResource));
// then
assertTrue(excess.isEmpty());
verify(aResourceType).deduct(sourceAResource, toCompareAResource);
}
use of org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException in project che-server by eclipse-che.
the class ResourceAggregator method excess.
/**
* Returns list which contains resources from specified {@code sourceResources} which have
* excessive amount in compare to specified {@code resourcesToCompare}.
*
* <p>Example :
*
* <pre>
* | \ | Source | To compare| Result |
* |:------------|:----------|:----------|:---------|
* | Resource1 | 5 | 3 | 2 |
* | ----------- | --------- | --------- | -------- |
* | Resource2 | - | 9 | - |
* | ----------- | --------- | --------- | -------- |
* | Resource3 | 1 | - | 1 |
* | ----------- | --------- | --------- | -------- |
* </pre>
*
* @param sourceResources the source resources
* @param resourcesToCompare the resources which should be compared to {@code sourceResources}
* @throws IllegalArgumentException when {@code sourceResources} or {@code resourcesToCompare}
* contain resource with not supported type
*/
public List<? extends Resource> excess(List<? extends Resource> sourceResources, List<? extends Resource> resourcesToCompare) {
checkSupporting(sourceResources);
checkSupporting(resourcesToCompare);
final Map<String, Resource> result = sourceResources.stream().collect(Collectors.toMap(Resource::getType, Function.identity()));
for (Resource toCompare : resourcesToCompare) {
String resourceType = toCompare.getType();
final Resource sourceResource = result.get(resourceType);
if (sourceResource != null) {
if (sourceResource.getAmount() == toCompare.getAmount()) {
// source resource doesn't have excessive amount
result.remove(resourceType);
continue;
}
try {
Resource excess = deduct(sourceResource, toCompare);
if (excess.getAmount() == 0) {
// source resource doesn't have excessive amount
result.remove(resourceType);
} else {
result.put(resourceType, excess);
}
} catch (NoEnoughResourcesException e) {
// source resource doesn't have excessive amount
result.remove(resourceType);
}
}
}
return new ArrayList<>(result.values());
}
Aggregations