SuborganizationResourcesProvider implements ResourcesProvider { @Override public List<ProvidedResources> getResources(String accountId) throws NotFoundException, ServerException { final Account account = accountManager.getById(accountId); String parent; if (!OrganizationImpl.ORGANIZATIONAL_ACCOUNT.equals(account.getType()) || (parent = organizationManager.getById(accountId).getParent()) == null) { return emptyList(); } List<? extends Resource> parentTotalResources = resourceManagerProvider.get().getTotalResources(parent); if (!parentTotalResources.isEmpty()) { try { List<? extends Resource> resourcesCaps = distributorProvider.get().getResourcesCaps(accountId); return singletonList( new ProvidedResourcesImpl( PARENT_RESOURCES_PROVIDER, null, accountId, -1L, -1L, cap(parentTotalResources, resourcesCaps))); } catch (ConflictException e) { throw new ServerException(e.getLocalizedMessage()); } } return emptyList(); } @Inject  SuborganizationResourcesProvider(
      AccountManager accountManager,
      OrganizationManager organizationManager,
      Provider<OrganizationResourcesDistributor> distributorProvider,
      Provider<ResourceManager> resourceManagerProvider); @Override List<ProvidedResources> getResources(String accountId); static final String PARENT_RESOURCES_PROVIDER; }
@Test public void shouldNotProvideResourcesForNonOrganizationalAccounts() throws Exception { when(account.getType()).thenReturn("test"); final List<ProvidedResources> providedResources = suborganizationResourcesProvider.getResources("account123"); assertTrue(providedResources.isEmpty()); verify(accountManager).getById("account123"); }
@Test public void shouldNotProvideResourcesForRootOrganizationalAccount() throws Exception { when(account.getType()).thenReturn(OrganizationImpl.ORGANIZATIONAL_ACCOUNT); when(organization.getParent()).thenReturn(null); final List<ProvidedResources> providedResources = suborganizationResourcesProvider.getResources("organization123"); assertTrue(providedResources.isEmpty()); verify(accountManager).getById("organization123"); verify(organizationManager).getById("organization123"); }
@Test public void shouldProvideResourcesForSuborganizationalAccount() throws Exception { when(account.getType()).thenReturn(OrganizationImpl.ORGANIZATIONAL_ACCOUNT); when(organization.getParent()).thenReturn("parentOrg"); final ResourceImpl parentNotCapedResource = new ResourceImpl("test", 1234, "unit"); final ResourceImpl parentCapedResource = new ResourceImpl("caped", 20, "unit"); final ResourceImpl parentUnlimitedCapedResource = new ResourceImpl("unlimited", -1, "unit"); doReturn(asList(parentNotCapedResource, parentCapedResource, parentUnlimitedCapedResource)) .when(resourceManager) .getTotalResources(anyString()); final ResourceImpl capedResourceCap = new ResourceImpl("caped", 10, "unit"); final ResourceImpl unlimitedCapedResourceCap = new ResourceImpl("unlimited", 40, "unit"); doReturn(asList(capedResourceCap, unlimitedCapedResourceCap)) .when(resourcesDistributor) .getResourcesCaps(any()); final List<ProvidedResources> providedResources = suborganizationResourcesProvider.getResources("organization123"); assertEquals(providedResources.size(), 1); assertEquals( providedResources.get(0), new ProvidedResourcesImpl( SuborganizationResourcesProvider.PARENT_RESOURCES_PROVIDER, null, "organization123", -1L, -1L, asList(parentNotCapedResource, capedResourceCap, unlimitedCapedResourceCap))); verify(accountManager).getById("organization123"); verify(organizationManager).getById("organization123"); verify(resourcesDistributor).getResourcesCaps("organization123"); verify(resourceManager).getTotalResources("parentOrg"); }
@Test public void shouldNotProvideResourcesForOrganizationalAccountIfItDoesNotHaveDistributedResources() throws Exception { when(account.getType()).thenReturn(OrganizationImpl.ORGANIZATIONAL_ACCOUNT); when(organization.getParent()).thenReturn("parentOrg"); doReturn(emptyList()).when(resourcesDistributor).getResourcesCaps(any()); doReturn(emptyList()).when(resourceManager).getAvailableResources(anyString()); final List<ProvidedResources> providedResources = suborganizationResourcesProvider.getResources("organization123"); assertTrue(providedResources.isEmpty()); verify(accountManager).getById("organization123"); verify(organizationManager).getById("organization123"); verify(resourcesDistributor, never()).getResourcesCaps("organization123"); verify(resourceManager).getTotalResources("parentOrg"); }