Search in sources :

Example 21 with NoEnoughResourcesException

use of org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException in project che-server by eclipse-che.

the class OrganizationalAccountAvailableResourcesProvider method getAvailableOrganizationResources.

/**
 * Returns total resources minus resources which are already used by organization or by any of its
 * suborganizations.
 *
 * @param organization organization id to calculate its available resources
 * @return resources which are available for usage by specified organization
 * @throws NotFoundException when organization with specified id doesn't exist
 * @throws ServerException when any other exception occurs on calculation of available resources
 */
@VisibleForTesting
List<? extends Resource> getAvailableOrganizationResources(Organization organization) throws NotFoundException, ServerException {
    final ResourceManager resourceManager = resourceManagerProvider.get();
    final List<? extends Resource> total = resourceManager.getTotalResources(organization.getId());
    final List<Resource> unavailable = new ArrayList<>(resourceManager.getUsedResources(organization.getId()));
    unavailable.addAll(getUsedResourcesBySuborganizations(organization.getQualifiedName()));
    try {
        return resourceAggregator.deduct(total, unavailable);
    } catch (NoEnoughResourcesException e) {
        LOG.warn("Organization with id {} uses more resources {} than it has {}.", organization.getId(), format(unavailable), format(total));
        return resourceAggregator.excess(total, unavailable);
    }
}
Also used : NoEnoughResourcesException(org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException) Resource(org.eclipse.che.multiuser.resource.model.Resource) ArrayList(java.util.ArrayList) ResourceManager(org.eclipse.che.multiuser.resource.api.usage.ResourceManager) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 22 with NoEnoughResourcesException

use of org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException in project devspaces-images by redhat-developer.

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 23 with NoEnoughResourcesException

use of org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException in project devspaces-images by redhat-developer.

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 24 with NoEnoughResourcesException

use of org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException in project devspaces-images by redhat-developer.

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 25 with NoEnoughResourcesException

use of org.eclipse.che.multiuser.resource.api.exception.NoEnoughResourcesException in project devspaces-images by redhat-developer.

the class OrganizationResourcesDistributorTest method shouldResourceAvailabilityCappingResourcesWhenResourceCapIsGreaterThanUsedOne.

@Test(expectedExceptions = ConflictException.class, expectedExceptionsMessageRegExp = "Resources are currently in use. Denied.")
public void shouldResourceAvailabilityCappingResourcesWhenResourceCapIsGreaterThanUsedOne() throws Exception {
    // given
    doCallRealMethod().when(manager).checkResourcesAvailability(anyString(), any());
    doReturn("Denied.").when(manager).getMessage(anyString());
    ResourceImpl used = createTestResource(1000);
    doReturn(singletonList(used)).when(resourceManager).getUsedResources(any());
    ResourceImpl toCap = createTestResource(700);
    doThrow(new NoEnoughResourcesException(emptyList(), emptyList(), singletonList(toCap))).when(resourceAggregator).deduct((Resource) any(), any());
    // when
    manager.checkResourcesAvailability(ORG_ID, singletonList(toCap));
    // then
    verify(resourceManager).getUsedResources(ORG_ID);
    verify(resourceAggregator).deduct(toCap, used);
}
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)

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