CachingDiscoveryProvider implements DiscoveryProvider { @Override public DirectoryList getDirectory(final String root) throws InternalServerErrorException { try { return directoryByRoot.get(root, new Callable<DirectoryList>() { @Override public DirectoryList call() throws Exception { return delegate.getDirectory(root); } }); } catch (ExecutionException | UncheckedExecutionException e) { if (e.getCause() instanceof InternalServerErrorException) { throw (InternalServerErrorException) e.getCause(); } else { logger.atSevere().withCause(e.getCause()).log("Could not generate or cache directory"); throw new InternalServerErrorException("Internal Server Error", e.getCause()); } } } CachingDiscoveryProvider(DiscoveryProvider delegate);  CachingDiscoveryProvider(
      DiscoveryProvider delegate, long cacheExpiry, TimeUnit cacheExpiryUnit); @Override RestDescription getRestDocument(final String root, final String name, final String version); @Override RpcDescription getRpcDocument(final String root, final String name, final String version); @Override DirectoryList getDirectory(final String root);  }
@Test public void getDirectory() throws Exception { CachingDiscoveryProvider provider = createNonExpiringProvider(); setupNormalMockDelegate(); assertThat(provider.getDirectory(ROOT)).isEqualTo(DIRECTORY); assertThat(provider.getDirectory(ROOT)).isEqualTo(DIRECTORY); verify(delegate, times(1)).getDirectory(ROOT); }
@Test public void getDirectory_internalServerError() throws Exception { CachingDiscoveryProvider provider = createNonExpiringProvider(); when(delegate.getDirectory(ROOT)).thenThrow(new InternalServerErrorException("")); try { provider.getDirectory(ROOT); fail("expected InternalServerErrorException"); } catch (InternalServerErrorException e) { } }
@Test public void getDirectory_runtimeException() throws Exception { CachingDiscoveryProvider provider = createNonExpiringProvider(); when(delegate.getDirectory(ROOT)).thenThrow(new RuntimeException("")); try { provider.getDirectory(ROOT); fail("expected InternalServerErrorException"); } catch (InternalServerErrorException e) { } }