diff --git a/aws/core/src/main/java/org/jclouds/aws/ec2/compute/functions/RegionAndIdToImage.java b/aws/core/src/main/java/org/jclouds/aws/ec2/compute/functions/RegionAndIdToImage.java index 1414db2ae9..310b30f9ca 100644 --- a/aws/core/src/main/java/org/jclouds/aws/ec2/compute/functions/RegionAndIdToImage.java +++ b/aws/core/src/main/java/org/jclouds/aws/ec2/compute/functions/RegionAndIdToImage.java @@ -29,7 +29,6 @@ import org.jclouds.aws.ec2.EC2Client; import org.jclouds.aws.ec2.compute.domain.RegionAndName; import org.jclouds.compute.domain.Image; import org.jclouds.logging.Logger; -import org.jclouds.rest.ResourceNotFoundException; import com.google.common.base.Function; import com.google.common.collect.Iterables; @@ -55,11 +54,10 @@ public final class RegionAndIdToImage implements Function public Image apply(RegionAndName key) { try { org.jclouds.aws.ec2.domain.Image image = Iterables.getOnlyElement(sync.getAMIServices() - .describeImagesInRegion(key.getRegion(), imageIds(key.getName()))); + .describeImagesInRegion(key.getRegion(), imageIds(key.getName()))); return parser.apply(image); - } catch (ResourceNotFoundException e) { - logger.warn(e, "no image found for %s/%s: %s", key.getRegion(), key.getName(), e - .getMessage()); + } catch (Exception e) { + logger.warn(e, "could not find image %s/%s: %s", key.getRegion(), key.getName(), e.getMessage()); return null; } } diff --git a/aws/core/src/main/java/org/jclouds/aws/ec2/options/internal/BaseEC2RequestOptions.java b/aws/core/src/main/java/org/jclouds/aws/ec2/options/internal/BaseEC2RequestOptions.java index d0a93240c0..2b22898d13 100644 --- a/aws/core/src/main/java/org/jclouds/aws/ec2/options/internal/BaseEC2RequestOptions.java +++ b/aws/core/src/main/java/org/jclouds/aws/ec2/options/internal/BaseEC2RequestOptions.java @@ -35,10 +35,14 @@ import com.google.common.collect.Sets; */ public class BaseEC2RequestOptions extends BaseHttpRequestOptions { + @Override + public String toString() { + return "[formParameters=" + formParameters + "]"; + } + protected void indexFormValuesWithPrefix(String prefix, String... values) { for (int i = 0; i < values.length; i++) { - formParameters.put(prefix + "." + (i + 1), checkNotNull(values[i], prefix.toLowerCase() - + "s[" + i + "]")); + formParameters.put(prefix + "." + (i + 1), checkNotNull(values[i], prefix.toLowerCase() + "s[" + i + "]")); } } diff --git a/aws/core/src/test/java/org/jclouds/aws/ec2/compute/functions/RegionAndIdToImageTest.java b/aws/core/src/test/java/org/jclouds/aws/ec2/compute/functions/RegionAndIdToImageTest.java index 1af311ef28..6b048fb308 100644 --- a/aws/core/src/test/java/org/jclouds/aws/ec2/compute/functions/RegionAndIdToImageTest.java +++ b/aws/core/src/test/java/org/jclouds/aws/ec2/compute/functions/RegionAndIdToImageTest.java @@ -27,6 +27,7 @@ import static org.easymock.classextension.EasyMock.verify; import static org.jclouds.aws.ec2.options.DescribeImagesOptions.Builder.imageIds; import static org.testng.Assert.assertEquals; +import java.util.NoSuchElementException; import java.util.Set; import org.jclouds.aws.ec2.EC2Client; @@ -44,7 +45,7 @@ import com.google.common.collect.ImmutableSet; @Test(groups = "unit", testName = "ec2.RegionAndIdToImageTest") public class RegionAndIdToImageTest { - @SuppressWarnings({ "unchecked"}) + @SuppressWarnings({ "unchecked" }) @Test public void testApply() { @@ -54,10 +55,10 @@ public class RegionAndIdToImageTest { org.jclouds.aws.ec2.domain.Image ec2Image = createMock(org.jclouds.aws.ec2.domain.Image.class); Image image = createNiceMock(Image.class); Set images = ImmutableSet - . of(ec2Image); + . of(ec2Image); expect(caller.getAMIServices()).andReturn(client).atLeastOnce(); - expect(client.describeImagesInRegion("region", imageIds("ami"))).andReturn((Set)images); + expect(client.describeImagesInRegion("region", imageIds("ami"))).andReturn((Set) images); expect(parser.apply(ec2Image)).andReturn(image); replay(caller); @@ -76,7 +77,7 @@ public class RegionAndIdToImageTest { } - @SuppressWarnings({ "unchecked"}) + @SuppressWarnings({ "unchecked" }) @Test public void testApplyNotFound() { @@ -86,10 +87,10 @@ public class RegionAndIdToImageTest { org.jclouds.aws.ec2.domain.Image ec2Image = createMock(org.jclouds.aws.ec2.domain.Image.class); Image image = createNiceMock(Image.class); Set images = ImmutableSet - . of(ec2Image); + . of(ec2Image); expect(caller.getAMIServices()).andReturn(client).atLeastOnce(); - expect(client.describeImagesInRegion("region", imageIds("ami"))).andReturn((Set)images); + expect(client.describeImagesInRegion("region", imageIds("ami"))).andReturn((Set) images); expect(parser.apply(ec2Image)).andThrow(new ResourceNotFoundException()); replay(caller); @@ -107,4 +108,36 @@ public class RegionAndIdToImageTest { verify(client); } + + @SuppressWarnings({ "unchecked" }) + @Test + public void testApplyNoSuchElementException() { + + ImageParser parser = createMock(ImageParser.class); + EC2Client caller = createMock(EC2Client.class); + AMIClient client = createMock(AMIClient.class); + org.jclouds.aws.ec2.domain.Image ec2Image = createMock(org.jclouds.aws.ec2.domain.Image.class); + Image image = createNiceMock(Image.class); + Set images = ImmutableSet + . of(ec2Image); + + expect(caller.getAMIServices()).andReturn(client).atLeastOnce(); + expect(client.describeImagesInRegion("region", imageIds("ami"))).andReturn((Set) images); + expect(parser.apply(ec2Image)).andThrow(new NoSuchElementException()); + + replay(caller); + replay(image); + replay(parser); + replay(client); + + RegionAndIdToImage function = new RegionAndIdToImage(parser, caller); + + assertEquals(function.apply(new RegionAndName("region", "ami")), null); + + verify(caller); + verify(image); + verify(parser); + verify(client); + + } }