GetRegionForBucket implements Function<String, Optional<String>> { @Override public Optional<String> apply(String bucket) { try { return bucketToRegion.get(bucket); } catch (ExecutionException e) { logger.debug("error looking up region for bucket %s: %s", bucket, e); } catch (UncheckedExecutionException e) { logger.debug("error looking up region for bucket %s: %s", bucket, e); } catch (InvalidCacheLoadException e) { logger.trace("bucket %s not found: %s", bucket, e); } return Optional.absent(); } @Inject  GetRegionForBucket(@Bucket LoadingCache<String, Optional<String>> bucketToRegion); @Override Optional<String> apply(String bucket);  }
@SuppressWarnings("unchecked") @Test void test() throws Exception { LoadingCache<String, Optional<String>> bucketToRegionCache = createMock(LoadingCache.class); expect(bucketToRegionCache.get("bucket")).andReturn(Optional.of("us-east-1")); replay(bucketToRegionCache); GetRegionForBucket fn = new GetRegionForBucket(bucketToRegionCache); assertEquals(fn.apply("bucket"), Optional.of("us-east-1")); verify(bucketToRegionCache); }
@SuppressWarnings("unchecked") @Test(dataProvider = "exceptions") void testGracefulOnException(Exception exception) throws Exception { LoadingCache<String, Optional<String>> bucketToRegionCache = createMock(LoadingCache.class); expect(bucketToRegionCache.get("bucket")).andThrow(exception); replay(bucketToRegionCache); GetRegionForBucket fn = new GetRegionForBucket(bucketToRegionCache); assertEquals(fn.apply("bucket"), Optional.absent()); verify(bucketToRegionCache); }