DelegatingOptionsResolver implements OptionsResolver { @Override public Options resolve(final String key, final JoinPoint joinPoint) throws OptionsException { final List<OptionsResolver> selected = new ArrayList<>(); for (final OptionsResolver resolver : resolvers) { if (resolver.supports(key)) { selected.add(resolver); } } if (!CollectionUtils.isEmpty(selected) && selected.size() > 1) { throw new AmbiguousOptionsException("Expected unique resolver, found " + selected.size()); } if (CollectionUtils.isEmpty(selected)) { if (treatMissingAsDisabled) { return new DisabledOptions(key); } else { throw new AmbiguousOptionsException("No option resolver found, and treatMissingAsDisabled is false"); } } return selected.get(0).resolve(key, joinPoint); } DelegatingOptionsResolver(final Collection<OptionsResolver> resolvers, final boolean treatMissingAsDisabled);  DelegatingOptionsResolver(final Collection<OptionsResolver> resolvers); @Override boolean isDynamic(); @Override Options resolve(final String key, final JoinPoint joinPoint); @Override boolean supports(final String key);  }
@Test public void shouldDisableMissing() { final List<OptionsResolver> objects = Collections.emptyList(); assertThat(new DelegatingOptionsResolver(objects, true) .resolve("test", mock(JoinPoint.class)).enabled()) .isFalse(); }
@Test public void shouldResolve() { final Options resolve = new DelegatingOptionsResolver(Arrays.asList(new OptionsResolver() { @Override public boolean isDynamic() { return false; } @Override public Options resolve(final String key, final JoinPoint joinPoint) throws OptionsException { return null; } @Override public boolean supports(final String key) { return false; } }, new OptionsResolver() { @Override public boolean isDynamic() { return false; } @Override public Options resolve(final String key, final JoinPoint joinPoint) throws OptionsException { return new Options() { @Override public boolean blocked() { return false; } @Override public boolean enabled() { return true; } @Override public OptionsInterval interval() { return null; } @Override public Long maxRequests() { return 15L; } @Override public String resolvedKey() { return null; } @Override public OptionsRetry retry() { return null; } @Override public boolean retryEnabled() { return false; } }; } @Override public boolean supports(final String key) { return true; } })) .resolve("test", mock(JoinPoint.class)); assertThat(resolve).isNotNull(); assertThat(resolve.enabled()).isTrue(); assertThat(resolve.maxRequests()).isEqualTo(15L); }
@Test(expected = AmbiguousOptionsException.class) public void shouldThrowExceptionIfMoreThenOne() { new DelegatingOptionsResolver(Arrays.asList(new OptionsResolver() { @Override public boolean isDynamic() { return false; } @Override public Options resolve(final String key, final JoinPoint joinPoint) throws OptionsException { return null; } @Override public boolean supports(final String key) { return true; } }, new OptionsResolver() { @Override public boolean isDynamic() { return false; } @Override public Options resolve(final String key, final JoinPoint joinPoint) throws OptionsException { return null; } @Override public boolean supports(final String key) { return true; } })) .resolve("test", mock(JoinPoint.class)); }
@Test(expected = AmbiguousOptionsException.class) public void shouldThrowExceptionOnMissing() { final List<OptionsResolver> objects = Collections.emptyList(); new DelegatingOptionsResolver(objects) .resolve("test", mock(JoinPoint.class)); }