AnnotationOptionsResolver implements OptionsResolver { @Override public Options resolve(final String key, final JoinPoint joinPoint) throws OptionsException { final RateLimited rateLimited = findAnnotation(joinPoint, RateLimited.class); if (rateLimited == null) { throw new AmbiguousOptionsException("Unable to locate @RateLimited annotation"); } if (!rateLimited.enabled()) { return InternalOptions.disabled(key); } if (rateLimited.maxRequests() == -1 || rateLimited.interval().interval() == -1) { throw new IllegalConfigurationException("Annotation configuration is enabled, maxRequests and interval must be greater then 0"); } final InternalOptions options = InternalOptions.enabled(key, rateLimited.maxRequests(), InternalOptions.intervalOf(rateLimited.interval().interval(), rateLimited.interval().unit())); final RateLimitedRetry retry = findAnnotation(joinPoint, RateLimitedRetry.class); if (retry != null) { if (retry.retryCount() < 1 && retry.interval().interval() < 1) { throw new IllegalConfigurationException("Retry configuration is not valid"); } else { options.enableRetry(retry.retryCount(), InternalOptions.intervalOf(retry.interval().interval(), retry.interval().unit())); } } return options; }  @Override boolean isDynamic(); @Override Options resolve(final String key, final JoinPoint joinPoint); @Override boolean supports(final String key);  }
@Test(expected = IllegalConfigurationException.class) public void shouldFailForIllegalConfiguration() { initMocks("invalidInterval"); new AnnotationOptionsResolver().resolve("test", joinPoint); }
@Test public void shouldResolveCorrect() { initMocks("correct"); assertThat(new AnnotationOptionsResolver().resolve("test", joinPoint)).isEqualTo(InternalOptions.enabled("test", 50, InternalOptions.intervalOf(10L, TimeUnit.HOURS))); }
@Test public void shouldResolveCorrectWithRetry() { initMocks("correctWithRetry"); assertThat(new AnnotationOptionsResolver().resolve("test", joinPoint)).isEqualTo(InternalOptions .enabled("test", 5, InternalOptions.intervalOf(30L, TimeUnit.SECONDS)) .enableRetry(4, InternalOptions.intervalOf(100L, TimeUnit.MILLISECONDS))); }
@Test public void shouldResolveDisabled() { initMocks("disabled"); assertThat(new AnnotationOptionsResolver().resolve("test", joinPoint)).isEqualTo(InternalOptions.disabled("test")); }