diff --git a/apis/ec2/src/test/java/org/jclouds/ec2/compute/internal/EC2TemplateBuilderImplTest.java b/apis/ec2/src/test/java/org/jclouds/ec2/compute/internal/EC2TemplateBuilderImplTest.java index 7e9cb8f065..0b12bb0779 100644 --- a/apis/ec2/src/test/java/org/jclouds/ec2/compute/internal/EC2TemplateBuilderImplTest.java +++ b/apis/ec2/src/test/java/org/jclouds/ec2/compute/internal/EC2TemplateBuilderImplTest.java @@ -96,7 +96,6 @@ public class EC2TemplateBuilderImplTest extends TemplateBuilderImplTest { return "us-east-1/"+uniqueLabel; } - @Override @Test public void testHardwareWithImageIdPredicateOnlyAcceptsImageWhenLocationNull() { diff --git a/compute/src/main/java/org/jclouds/compute/domain/TemplateBuilder.java b/compute/src/main/java/org/jclouds/compute/domain/TemplateBuilder.java index c4fdb46216..155dbdc262 100644 --- a/compute/src/main/java/org/jclouds/compute/domain/TemplateBuilder.java +++ b/compute/src/main/java/org/jclouds/compute/domain/TemplateBuilder.java @@ -177,7 +177,7 @@ public interface TemplateBuilder { * If no function is supplied, jclouds will select one according to an internal strategy. * This strategy may change from version to version. */ - TemplateBuilder imageChooser(Function,Image> imageChooser); + TemplateBuilder imageChooser(Function, Image> imageChooser); /** * Configure this template to require the minimum cores below diff --git a/compute/src/main/java/org/jclouds/compute/domain/internal/TemplateBuilderImpl.java b/compute/src/main/java/org/jclouds/compute/domain/internal/TemplateBuilderImpl.java index 8b60aec2f0..426511fcf0 100644 --- a/compute/src/main/java/org/jclouds/compute/domain/internal/TemplateBuilderImpl.java +++ b/compute/src/main/java/org/jclouds/compute/domain/internal/TemplateBuilderImpl.java @@ -975,7 +975,7 @@ public class TemplateBuilderImpl implements TemplateBuilder { * {@inheritDoc} */ @Override - public TemplateBuilderImpl imageChooser(Function,Image> imageChooser) { + public TemplateBuilderImpl imageChooser(Function, Image> imageChooser) { this.imageChooser = imageChooser; return this; } diff --git a/compute/src/test/java/org/jclouds/compute/domain/internal/TemplateBuilderImplTest.java b/compute/src/test/java/org/jclouds/compute/domain/internal/TemplateBuilderImplTest.java index 4661c4bde7..8419a85b74 100644 --- a/compute/src/test/java/org/jclouds/compute/domain/internal/TemplateBuilderImplTest.java +++ b/compute/src/test/java/org/jclouds/compute/domain/internal/TemplateBuilderImplTest.java @@ -146,9 +146,16 @@ public class TemplateBuilderImplTest { verify(defaultTemplate, optionsProvider, templateBuilderProvider); } + /** + * Resolves images using a default TemplateBuilder, optionally applying a customizer to that + * TemplateBuilder where the customizer may e.g. apply an imageChooser + */ @SuppressWarnings("unchecked") - protected void doTestResolveImages(Supplier> images, Image expectedBest, - Function builderCustomisation) { + protected void doTestResolveImagesWithTemplateBuilderCustomizer( + Supplier> inputImages, + Function templateBuilderPerTestCustomizer, + Image expectedBest) { + Hardware hardware = new HardwareBuilder().id("hardwareId").build(); Supplier> locations = Suppliers.> ofInstance(ImmutableSet @@ -161,30 +168,40 @@ public class TemplateBuilderImplTest { replay(defaultTemplate, optionsProvider, templateBuilderProvider); - TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region, + TemplateBuilderImpl template = createTemplateBuilder(null, locations, inputImages, hardwares, region, optionsProvider, templateBuilderProvider); - template = builderCustomisation.apply(template); + template = templateBuilderPerTestCustomizer.apply(template); - assertEquals(template.resolveImage(hardware, images.get()), expectedBest); + assertEquals(template.resolveImage(hardware, inputImages.get()), expectedBest); verify(defaultTemplate, optionsProvider, templateBuilderProvider); } + protected void doTestResolveImages(Supplier> inputImages, Image expectedBest) { + doTestResolveImagesWithTemplateBuilderCustomizer( + inputImages, + Functions.identity(), + expectedBest); + } + public void testResolveImagesSimple() { - doTestResolveImages(Suppliers.> ofInstance(ImmutableSet. of( - image, image64bit)), - image64bit, Functions.identity()); + doTestResolveImages( + Suppliers.> ofInstance(ImmutableSet. of(image, image64bit)), + image64bit); } public void testResolveImagesPrefersNull() { // preferring null has been the default behaviour; not sure if this is ideal // (would make more sense to prefer nonNull) but don't change behaviour by default - doTestResolveImages(Suppliers.> ofInstance(ImmutableSet. of( - image, imageArchNull, image64bit)), - imageArchNull, Functions.identity()); + doTestResolveImages( + Suppliers.> ofInstance(ImmutableSet. of(image, imageArchNull, image64bit)), + imageArchNull); } public void testResolveImagesCustomSorterPreferringNonNull() { + // preferring null has been the default behaviour; + // see comments in TemplateBuilderImpl.DEFAULT_IMAGE_ORDERING + final Ordering sorterPreferringNonNullArch = new Ordering() { @Override public int compare(Image left, Image right) { @@ -198,67 +215,68 @@ public class TemplateBuilderImplTest { assertTrue(TemplateBuilderImpl.DEFAULT_IMAGE_ORDERING.compare(image64bit, imageArchNull) < 0, "wrong default image ordering"); assertTrue(sorterPreferringNonNullArch.compare(image64bit, imageArchNull) > 0, "wrong custom image ordering"); - // preferring null has been the default behaviour; - // see comments in TemplateBuilderImpl.DEFAULT_IMAGE_ORDERING - doTestResolveImages(Suppliers.> ofInstance(ImmutableSet. of( - image, imageArchNull, image64bit)), - image64bit, new Function() { - @Override - public TemplateBuilderImpl apply(TemplateBuilderImpl input) { - return input.imageChooser(input.imageChooserFromOrdering(sorterPreferringNonNullArch)); - } - }); + Function tbCustomiser = new Function() { + @Override + public TemplateBuilderImpl apply(TemplateBuilderImpl input) { + return input.imageChooser(input.imageChooserFromOrdering(sorterPreferringNonNullArch)); + } + }; + + doTestResolveImagesWithTemplateBuilderCustomizer( + Suppliers.> ofInstance(ImmutableSet. of(image, imageArchNull, image64bit)), + tbCustomiser, image64bit); } public void testResolveImagesPrefersImageBecauseNameIsLastAlphabetically() { // preferring that which comes later alphabetically is the default behaviour; // see comments in TemplateBuilderImpl.DEFAULT_IMAGE_ORDERING - doTestResolveImages(Suppliers.> ofInstance(ImmutableSet. of( - imageNameAlt, image)), - image, Functions.identity()); + doTestResolveImages( + Suppliers.> ofInstance(ImmutableSet. of(imageNameAlt, image)), + image); } public void testResolveImagesCustomSorterPreferringAltImage() { - doTestResolveImages(Suppliers.> ofInstance(ImmutableSet. of( - imageNameAlt, image, imageArchNull, image64bit)), - imageNameAlt, new Function() { - @Override - public TemplateBuilderImpl apply(TemplateBuilderImpl input) { - return input.imageChooser(input.imageChooserFromOrdering(new Ordering() { - private int score(Image img) { - if (img.getName().contains("alternate")) return 10; - return 0; - } - @Override - public int compare(Image left, Image right) { - return score(left) - score(right); - } - })); - } - }); + Function tbSortWithAlternate = new Function() { + @Override + public TemplateBuilderImpl apply(TemplateBuilderImpl input) { + return input.imageChooser(input.imageChooserFromOrdering(new Ordering() { + private int score(Image img) { + if (img.getName().contains("alternate")) return 10; + return 0; + } + @Override + public int compare(Image left, Image right) { + return score(left) - score(right); + } + })); + } + }; + doTestResolveImagesWithTemplateBuilderCustomizer( + Suppliers.> ofInstance(ImmutableSet. of(imageNameAlt, image, imageArchNull, image64bit)), + tbSortWithAlternate, imageNameAlt); } public void testResolveImagesCustomChooserPreferringAltImage() { - doTestResolveImages(Suppliers.> ofInstance(ImmutableSet. of( - imageNameAlt, image, imageArchNull, image64bit)), - imageNameAlt, new Function() { - @Override - public TemplateBuilderImpl apply(TemplateBuilderImpl input) { - return input.imageChooser(new Function, Image>() { - @Override - public Image apply(Iterable input) { - return Iterables.find(input, new Predicate() { - @Override - public boolean apply(Image input) { - return input.getName()!=null && input.getName().contains("alternate"); - } - }); - } - }); - } - }); - } - + Function tbChooseAlternate = new Function() { + @Override + public TemplateBuilderImpl apply(TemplateBuilderImpl input) { + return input.imageChooser(new Function, Image>() { + @Override + public Image apply(Iterable input) { + return Iterables.find(input, new Predicate() { + @Override + public boolean apply(Image input) { + return input.getName() != null && input.getName().contains("alternate"); + } + }); + } + }); + } + }; + doTestResolveImagesWithTemplateBuilderCustomizer( + Suppliers.> ofInstance(ImmutableSet. of(imageNameAlt, image, imageArchNull, image64bit)), + tbChooseAlternate, imageNameAlt); + } @SuppressWarnings("unchecked") @Test