@SuppressWarnings("unchecked") @Test public void testApply() throws ExecutionException { EC2ImageParser parser = createMock(EC2ImageParser.class); EC2Client caller = createMock(EC2Client.class); AMIClient client = createMock(AMIClient.class); org.jclouds.ec2.domain.Image ec2Image = createMock(org.jclouds.ec2.domain.Image.class); Image image = createNiceMock(Image.class); Set<? extends org.jclouds.ec2.domain.Image> images = ImmutableSet.<org.jclouds.ec2.domain.Image> of(ec2Image); expect(caller.getAMIServices()).andReturn(client).atLeastOnce(); expect(client.describeImagesInRegion("region", imageIds("ami"))).andReturn(Set.class.cast(images)); expect(parser.apply(ec2Image)).andReturn(image); replay(caller); replay(image); replay(parser); replay(client); RegionAndIdToImage function = new RegionAndIdToImage(parser, caller); assertEquals(function.load(new RegionAndName("region", "ami")), image); verify(caller); verify(image); verify(image); verify(client); }
assertEquals(function.load(new RegionAndName("region", "ami")), null);
@SuppressWarnings("unchecked") @Test public void testApply() throws ExecutionException { EC2ImageParser parser = createMock(EC2ImageParser.class); EC2Api caller = createMock(EC2Api.class); AMIApi client = createMock(AMIApi.class); org.jclouds.ec2.domain.Image ec2Image = createMock(org.jclouds.ec2.domain.Image.class); Image image = createNiceMock(Image.class); Set<? extends org.jclouds.ec2.domain.Image> images = ImmutableSet.<org.jclouds.ec2.domain.Image> of(ec2Image); expect(caller.getAMIApi()).andReturn((Optional) Optional.of(client)).atLeastOnce(); expect(client.describeImagesInRegion("region", imageIds("ami"))).andReturn(Set.class.cast(images)); expect(parser.apply(ec2Image)).andReturn(image); replay(caller); replay(image); replay(parser); replay(client); RegionAndIdToImage function = new RegionAndIdToImage(parser, caller); assertEquals(function.load(new RegionAndName("region", "ami")), image); verify(caller); verify(image); verify(image); verify(client); }
@SuppressWarnings("unchecked") @Test(expectedExceptions = ExecutionException.class) public void testApplyNotFoundMakesExecutionException() throws ExecutionException { EC2ImageParser parser = createMock(EC2ImageParser.class); EC2Client caller = createMock(EC2Client.class); AMIClient client = createMock(AMIClient.class); org.jclouds.ec2.domain.Image ec2Image = createMock(org.jclouds.ec2.domain.Image.class); Image image = createNiceMock(Image.class); Set<? extends org.jclouds.ec2.domain.Image> images = ImmutableSet.<org.jclouds.ec2.domain.Image> of(ec2Image); expect(caller.getAMIServices()).andReturn(client).atLeastOnce(); expect(client.describeImagesInRegion("region", imageIds("ami"))).andReturn(Set.class.cast(images)); expect(parser.apply(ec2Image)).andThrow(new ResourceNotFoundException()); replay(caller); replay(image); replay(parser); replay(client); RegionAndIdToImage function = new RegionAndIdToImage(parser, caller); assertEquals(function.load(new RegionAndName("region", "ami")), null); verify(caller); verify(image); verify(parser); verify(client); }
@SuppressWarnings("unchecked") @Test(expectedExceptions = ExecutionException.class) public void testApplyNotFoundMakesExecutionException() throws ExecutionException { EC2ImageParser parser = createMock(EC2ImageParser.class); EC2Api caller = createMock(EC2Api.class); AMIApi client = createMock(AMIApi.class); org.jclouds.ec2.domain.Image ec2Image = createMock(org.jclouds.ec2.domain.Image.class); Image image = createNiceMock(Image.class); Set<? extends org.jclouds.ec2.domain.Image> images = ImmutableSet.<org.jclouds.ec2.domain.Image> of(ec2Image); expect(caller.getAMIApi()).andReturn((Optional) Optional.of(client)).atLeastOnce(); expect(client.describeImagesInRegion("region", imageIds("ami"))).andReturn(Set.class.cast(images)); expect(parser.apply(ec2Image)).andThrow(new ResourceNotFoundException()); replay(caller); replay(image); replay(parser); replay(client); RegionAndIdToImage function = new RegionAndIdToImage(parser, caller); assertEquals(function.load(new RegionAndName("region", "ami")), null); verify(caller); verify(image); verify(parser); verify(client); }
assertEquals(function.load(new RegionAndName("region", "ami")), null);
@Test public void testCacheLoaderDoesNotReloadAfterAuthorizationException() throws Exception { EC2ComputeServiceContextModule module = new EC2ComputeServiceContextModule() { public Supplier<CacheLoader<RegionAndName, Image>> provideRegionAndNameToImageSupplierCacheLoader(RegionAndIdToImage delegate) { return super.provideRegionAndNameToImageSupplierCacheLoader(delegate); } }; RegionAndName regionAndName = new RegionAndName("myregion", "myname"); AuthorizationException authException = new AuthorizationException(); RegionAndIdToImage mockRegionAndIdToImage = createMock(RegionAndIdToImage.class); expect(mockRegionAndIdToImage.load(regionAndName)).andThrow(authException).once(); replay(mockRegionAndIdToImage); CacheLoader<RegionAndName, Image> cacheLoader = module.provideRegionAndNameToImageSupplierCacheLoader(mockRegionAndIdToImage).get(); for (int i = 0; i < 2; i++) { try { Image image = cacheLoader.load(regionAndName); fail("Expected Authorization exception, but got " + image); } catch (AuthorizationException e) { // success } } } }
@Test public void testCacheLoaderDoesNotReloadAfterAuthorizationException() throws Exception { EC2ComputeServiceContextModule module = new EC2ComputeServiceContextModule() { public Supplier<CacheLoader<RegionAndName, Image>> provideRegionAndNameToImageSupplierCacheLoader(RegionAndIdToImage delegate) { return super.provideRegionAndNameToImageSupplierCacheLoader(delegate); } }; RegionAndName regionAndName = new RegionAndName("myregion", "myname"); AuthorizationException authException = new AuthorizationException(); RegionAndIdToImage mockRegionAndIdToImage = createMock(RegionAndIdToImage.class); expect(mockRegionAndIdToImage.load(regionAndName)).andThrow(authException).once(); replay(mockRegionAndIdToImage); CacheLoader<RegionAndName, Image> cacheLoader = module.provideRegionAndNameToImageSupplierCacheLoader(mockRegionAndIdToImage).get(); for (int i = 0; i < 2; i++) { try { Image image = cacheLoader.load(regionAndName); fail("Expected Authorization exception, but got " + image); } catch (AuthorizationException e) { // success } } } }
@Test public void testCacheLoaderDoesNotReloadAfterAuthorizationException() throws Exception { AWSEC2ComputeServiceContextModule module = new AWSEC2ComputeServiceContextModule() { public Supplier<CacheLoader<RegionAndName, Image>> provideRegionAndNameToImageSupplierCacheLoader(RegionAndIdToImage delegate) { return super.provideRegionAndNameToImageSupplierCacheLoader(delegate); } }; RegionAndName regionAndName = new RegionAndName("myregion", "myname"); AuthorizationException authException = new AuthorizationException(); RegionAndIdToImage mockRegionAndIdToImage = createMock(RegionAndIdToImage.class); expect(mockRegionAndIdToImage.load(regionAndName)).andThrow(authException).once(); replay(mockRegionAndIdToImage); CacheLoader<RegionAndName, Image> cacheLoader = module.provideRegionAndNameToImageSupplierCacheLoader(mockRegionAndIdToImage).get(); for (int i = 0; i < 2; i++) { try { Image image = cacheLoader.load(regionAndName); fail("Expected Authorization exception, but got " + image); } catch (AuthorizationException e) { // success } } } }