DefaultAvailableResourcesProvider implements AvailableResourcesProvider { @Override public List<? extends Resource> getAvailableResources(String accountId) throws NotFoundException, ServerException { ResourceManager resourceManager = resourceManagerProvider.get(); List<? extends Resource> totalResources = null; List<Resource> usedResources = null; try { totalResources = resourceManager.getTotalResources(accountId); usedResources = new ArrayList<>(resourceManager.getUsedResources(accountId)); return resourceAggregator.deduct(totalResources, usedResources); } catch (NoEnoughResourcesException e) { LOG.warn( "Account with id {} uses more resources {} than he has {}.", accountId, format(usedResources), format(totalResources)); return resourceAggregator.excess(totalResources, usedResources); } } @Inject  DefaultAvailableResourcesProvider(
      Provider<ResourceManager> resourceManagerProvider, ResourceAggregator resourceAggregator); @Override List<? extends Resource> getAvailableResources(String accountId);  }
@Test public void shouldReturnAvailableResourcesWhenNotAllTotalResourcesAreUsed() throws Exception { List<ResourceImpl> totalResources = singletonList(new ResourceImpl("test", 5000, "unit")); doReturn(totalResources).when(resourceManager).getTotalResources(anyString()); List<ResourceImpl> usedResources = singletonList(new ResourceImpl("test", 2000, "unit")); doReturn(usedResources).when(resourceManager).getUsedResources(anyString()); ResourceImpl availableResource = new ResourceImpl("test", 3000, "unit"); doReturn(singletonList(availableResource)) .when(resourceAggregator) .deduct(anyList(), anyList()); List<? extends Resource> availableResources = defaultAvailableResourcesProvider.getAvailableResources("account123"); assertEquals(availableResources.size(), 1); assertEquals(availableResources.get(0), availableResource); verify(resourceManager).getTotalResources("account123"); verify(resourceManager).getUsedResources("account123"); verify(resourceAggregator).deduct(totalResources, usedResources); verify(resourceAggregator, never()).excess(anyList(), anyList()); }
@Test public void shouldReturnExcessiveResourcesWhenNotOneResourceIsUsedButNotPresentInTotal() throws Exception { 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()); List<? extends Resource> availableResources = defaultAvailableResourcesProvider.getAvailableResources("account123"); 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); }