FreeResourcesProvider implements ResourcesProvider { @Override public List<ProvidedResources> getResources(String accountId) throws ServerException, NotFoundException { Map<String, ResourceImpl> freeResources = new HashMap<>(); String limitId = null; try { FreeResourcesLimit resourcesLimit = freeResourcesLimitManager.get(accountId); for (Resource resource : resourcesLimit.getResources()) { freeResources.put(resource.getType(), new ResourceImpl(resource)); } limitId = resourcesLimit.getAccountId(); } catch (NotFoundException ignored) { } for (ResourceImpl resource : getDefaultResources(accountId)) { freeResources.putIfAbsent(resource.getType(), resource); } if (!freeResources.isEmpty()) { return singletonList( new ProvidedResourcesImpl( FREE_RESOURCES_PROVIDER, limitId, accountId, -1L, -1L, freeResources.values())); } else { return emptyList(); } } @Inject  FreeResourcesProvider(
      FreeResourcesLimitManager freeResourcesLimitManager,
      AccountManager accountManager,
      Set<DefaultResourcesProvider> defaultResourcesProviders); @Override List<ProvidedResources> getResources(String accountId); static final String FREE_RESOURCES_PROVIDER; }
@Test public void shouldProvideDefaultResourcesIfThereAreProviderForThisAccountType() throws Exception { when(accountManager.getById(any())).thenReturn(account); when(freeResourcesLimitManager.get(any())).thenThrow(new NotFoundException("not found")); List<ProvidedResources> result = provider.getResources("user123"); assertEquals(result.size(), 1); ProvidedResources providedResources = result.get(0); assertEquals( providedResources, new ProvidedResourcesImpl( FreeResourcesProvider.FREE_RESOURCES_PROVIDER, null, "user123", -1L, -1L, singletonList(new ResourceImpl(TEST_RESOURCE_TYPE, 1020, TEST_RESOURCE_UNIT)))); verify(freeResourcesLimitManager).get("user123"); }
@Test public void shouldRewriteDefaultResourcesWithFreeResourcesLimitIfItExists() throws Exception { when(accountManager.getById(any())).thenReturn(account); when(freeResourcesLimitManager.get(any())) .thenReturn( new FreeResourcesLimitImpl( "user123", singletonList(new ResourceImpl(TEST_RESOURCE_TYPE, 12345, TEST_RESOURCE_UNIT)))); List<ProvidedResources> result = provider.getResources("user123"); assertEquals(result.size(), 1); ProvidedResources providedResources = result.get(0); assertEquals( providedResources, new ProvidedResourcesImpl( FreeResourcesProvider.FREE_RESOURCES_PROVIDER, "user123", "user123", -1L, -1L, singletonList(new ResourceImpl(TEST_RESOURCE_TYPE, 12345, TEST_RESOURCE_UNIT)))); verify(freeResourcesLimitManager).get("user123"); }
@Test public void shouldNotProvideDefaultResourcesForAccountThatDoesNotHaveDefaultResourcesProvider() throws Exception { when(account.getType()).thenReturn("anotherTestType"); when(accountManager.getById(any())).thenReturn(account); when(freeResourcesLimitManager.get(any())).thenThrow(new NotFoundException("not found")); doThrow(new NotFoundException("not found")).when(freeResourcesLimitManager).get(any()); List<ProvidedResources> result = provider.getResources("account123"); assertTrue(result.isEmpty()); }
@Test public void shouldNotProvideDefaultResourcesForAccountIfDefaultResourcesProviderProvidesEmptyList() throws Exception { when(accountManager.getById(any())).thenReturn(account); when(defaultResourcesProvider.getResources(any())).thenReturn(emptyList()); doThrow(new NotFoundException("not found")).when(freeResourcesLimitManager).get(any()); List<ProvidedResources> result = provider.getResources("user123"); assertTrue(result.isEmpty()); }
@Test public void shouldProvideResourcesFromFreeResourcesLimitIfItExists() throws Exception { when(account.getType()).thenReturn("anotherTestType"); when(accountManager.getById(any())).thenReturn(account); when(freeResourcesLimitManager.get(any())) .thenReturn( new FreeResourcesLimitImpl( "account123", singletonList(new ResourceImpl(TEST_RESOURCE_TYPE, 12345, TEST_RESOURCE_UNIT)))); List<ProvidedResources> result = provider.getResources("account123"); assertEquals(result.size(), 1); ProvidedResources providedResources = result.get(0); assertEquals( providedResources, new ProvidedResourcesImpl( FreeResourcesProvider.FREE_RESOURCES_PROVIDER, "account123", "account123", -1L, -1L, singletonList(new ResourceImpl(TEST_RESOURCE_TYPE, 12345, TEST_RESOURCE_UNIT)))); verify(freeResourcesLimitManager).get("account123"); }