OrganizationalAccountAvailableResourcesProvider implements AvailableResourcesProvider { @VisibleForTesting List<Resource> getUsedResourcesBySuborganizations(String parentQualifiedName) throws NotFoundException, ServerException { ResourceManager resourceManager = resourceManagerProvider.get(); List<Resource> usedResources = new ArrayList<>(); for (Organization suborganization : Pages.iterate( (maxItems, skipCount) -> organizationManager.getSuborganizations( parentQualifiedName, maxItems, skipCount))) { usedResources.addAll(resourceManager.getUsedResources(suborganization.getId())); } return usedResources; } @Inject  OrganizationalAccountAvailableResourcesProvider(
      Provider<ResourceManager> resourceManagerProvider,
      ResourceAggregator resourceAggregator,
      OrganizationManager organizationManager); @Override List<? extends Resource> getAvailableResources(String accountId);  }
@Test public void shouldCalculateUsedResourceBySuborganizations() throws Exception { doReturn(new Page<>(singletonList(suborganization), 0, 1, 2)) .doReturn(new Page<>(singletonList(subsuborganization), 1, 1, 2)) .when(organizationManager) .getSuborganizations(anyString(), anyInt(), anyLong()); ResourceImpl usedBySuborgResource = new ResourceImpl("test", 1500, "unit"); doReturn(singletonList(usedBySuborgResource)).when(resourceManager).getUsedResources(SUBORG_ID); ResourceImpl usedBySubsuborgResource = new ResourceImpl("test", 2000, "unit"); doReturn(singletonList(usedBySubsuborgResource)) .when(resourceManager) .getUsedResources(SUBSUBORG_ID); List<? extends Resource> usedResources = availableResourcesProvider.getUsedResourcesBySuborganizations(ROOT_ORG_NAME); assertEquals(usedResources.size(), 2); assertTrue(usedResources.contains(usedBySuborgResource)); assertTrue(usedResources.contains(usedBySubsuborgResource)); verify(organizationManager, times(2)) .getSuborganizations(eq(ROOT_ORG_NAME), anyInt(), anyLong()); verify(resourceManager).getUsedResources(SUBORG_ID); verify(resourceManager).getUsedResources(SUBSUBORG_ID); }