Search in sources :

Example 1 with NoEnoughResourcesException

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);
}
Also used : NoEnoughResourcesException(org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException) ResourceImpl(org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl) Test(org.testng.annotations.Test)

Example 2 with NoEnoughResourcesException

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);
}
Also used : NoEnoughResourcesException(org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException) ResourceImpl(org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl) Environment(org.eclipse.che.api.core.model.workspace.config.Environment) WorkspaceConfig(org.eclipse.che.api.core.model.workspace.WorkspaceConfig) Test(org.testng.annotations.Test)

Example 3 with NoEnoughResourcesException

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));
}
Also used : NoEnoughResourcesException(org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException) ResourceImpl(org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl) Test(org.testng.annotations.Test)

Example 4 with NoEnoughResourcesException

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);
}
Also used : NoEnoughResourcesException(org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException) ResourceImpl(org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl) Test(org.testng.annotations.Test)

Example 5 with NoEnoughResourcesException

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());
}
Also used : NoEnoughResourcesException(org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException) Resource(org.eclipse.che.multiuser.resource.model.Resource) ArrayList(java.util.ArrayList)

Aggregations

NoEnoughResourcesException (org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException)26 ResourceImpl (org.eclipse.che.multiuser.resource.spi.impl.ResourceImpl)20 Test (org.testng.annotations.Test)16 Resource (org.eclipse.che.multiuser.resource.model.Resource)8 ArrayList (java.util.ArrayList)6 VisibleForTesting (com.google.common.annotations.VisibleForTesting)4 Environment (org.eclipse.che.api.core.model.workspace.config.Environment)4 WorkspaceConfig (org.eclipse.che.api.core.model.workspace.WorkspaceConfig)2 ResourceManager (org.eclipse.che.multiuser.resource.api.usage.ResourceManager)2