HostRequestsQuotaService implements QuotaService { @Override public boolean isQuotaExceeded(String key) { if (enabled) { long count = requestRegistry.tick(key); return !requestsPolicy.isValid(count); } return false; } HostRequestsQuotaService(HostRequestIntervalRegistry requestRegistry, HostRequestLimitPolicy requestsPolicy,
                                    IntervalRegistryCleaner registryCleaner, IntervalRegistryLogger registryLogger,
                                    @Value("${quota.host.enabled}") boolean enabled); @PostConstruct void init(); @PreDestroy void close(); @Override boolean isQuotaExceeded(String key);  }
@Test public void quotaExceededIfRequestCountBiggerThanAllowed() { when(requestRegistry.tick("key")).thenReturn(10L); when(requestsPolicy.isValid(10L)).thenReturn(false); assertTrue(quotaService.isQuotaExceeded("key")); verify(requestRegistry).tick("key"); verify(requestsPolicy).isValid(10L); verifyNoMoreInteractions(requestRegistry, requestsPolicy); }
@Test public void quotaNotExceededIfRequestCountLessThanAllowed() { when(requestRegistry.tick("key")).thenReturn(10L); when(requestsPolicy.isValid(10L)).thenReturn(true); assertFalse(quotaService.isQuotaExceeded("key")); verify(requestRegistry).tick("key"); verify(requestsPolicy).isValid(10L); verifyNoMoreInteractions(requestRegistry, requestsPolicy); }
@Test public void serviceCanBeDisabled() { quotaService = new HostRequestsQuotaService(requestRegistry, requestsPolicy, registryCleaner, registryLogger, false); assertFalse(quotaService.isQuotaExceeded("key")); verifyNoMoreInteractions(requestRegistry, requestsPolicy); }