From 547c73ab00b3f058d6391524286dd1b062be4a93 Mon Sep 17 00:00:00 2001 From: Adrian Cole Date: Mon, 2 Jul 2012 12:21:13 -0700 Subject: [PATCH] cleaner error messages in templateBuilder --- .../functions/ImagesToRegionAndIdMap.java | 10 +- .../domain/internal/TemplateBuilderImpl.java | 66 ++-- .../internal/TemplateBuilderImplTest.java | 364 ++++++------------ 3 files changed, 163 insertions(+), 277 deletions(-) diff --git a/apis/ec2/src/main/java/org/jclouds/ec2/compute/functions/ImagesToRegionAndIdMap.java b/apis/ec2/src/main/java/org/jclouds/ec2/compute/functions/ImagesToRegionAndIdMap.java index 012b557220..36f93ef374 100644 --- a/apis/ec2/src/main/java/org/jclouds/ec2/compute/functions/ImagesToRegionAndIdMap.java +++ b/apis/ec2/src/main/java/org/jclouds/ec2/compute/functions/ImagesToRegionAndIdMap.java @@ -18,6 +18,7 @@ */ package org.jclouds.ec2.compute.functions; +import static com.google.common.base.Preconditions.checkState; import static com.google.common.collect.Maps.uniqueIndex; import java.util.Map; @@ -38,18 +39,19 @@ public class ImagesToRegionAndIdMap implements Function imagesToMap(Iterable input) { return new ImagesToRegionAndIdMap().apply(input); } - + @Override public Map apply(Iterable input) { return uniqueIndex(input, new Function() { - + @Override public RegionAndName apply(Image from) { + checkState(from.getLocation() != null, + "in ec2, image locations cannot be null; typically, they are Region-scoped"); return new RegionAndName(from.getLocation().getId(), from.getProviderId()); } - + }); } - } 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 e75fc7d58f..bbf3fa8e02 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 @@ -19,6 +19,7 @@ package org.jclouds.compute.domain.internal; import static com.google.common.base.Preconditions.checkNotNull; +import static com.google.common.base.Preconditions.checkState; import static com.google.common.base.Predicates.and; import static com.google.common.collect.Iterables.filter; import static com.google.common.collect.Iterables.find; @@ -140,6 +141,29 @@ public class TemplateBuilderImpl implements TemplateBuilder { this.defaultTemplateProvider = defaultTemplateProvider; } + static Predicate supportsImagesPredicate(final Iterable images) { + return new Predicate() { + @Override + public boolean apply(final Hardware hardware) { + return Iterables.any(images, new Predicate() { + + @Override + public boolean apply(Image input) { + return hardware.supportsImage().apply(input); + } + + @Override + public String toString() { + return "hardware(" + hardware + ").supportsImage()"; + } + + }); + + } + + }; + } + final Predicate locationPredicate = new NullEqualToIsParentOrIsGrandparentOfCurrentLocation(new Supplier(){ @Override @@ -627,7 +651,9 @@ public class TemplateBuilderImpl implements TemplateBuilder { options = optionsProvider.get(); logger.debug(">> searching params(%s)", this); Set images = getImages(); + checkState(images.size() > 0, "no images present!"); Set hardwaresToSearch = hardwares.get(); + checkState(hardwaresToSearch.size() > 0, "no hardware profiles present!"); Image image = null; if (imageId != null) { @@ -705,44 +731,36 @@ public class TemplateBuilderImpl implements TemplateBuilder { logger.warn(exception, "hardware ids that didn't match: %s", transform(hardwares, hardwareToId)); throw exception; } - + protected Hardware resolveHardware(Set hardwarel, final Iterable images) { Ordering hardwareOrdering = hardwareSorter(); - Iterable hardwaresThatAreCompatibleWithOurImages = ImmutableSet.of(); - try { - hardwaresThatAreCompatibleWithOurImages = filter(hardwarel, new Predicate() { - @Override - public boolean apply(final Hardware hardware) { - return Iterables.any(images, new Predicate() { + + Iterable> supportsImagePredicates = Iterables.transform(hardwarel, + new Function>() { @Override - public boolean apply(Image input) { - return hardware.supportsImage().apply(input); - } - - @Override - public String toString() { - return "hardware(" + hardware + ").supportsImage()"; + public Predicate apply(Hardware input) { + return input.supportsImage(); } }); + + Predicate supportsImagePredicate = Iterables.size(supportsImagePredicates) == 1 ? Iterables + .getOnlyElement(supportsImagePredicates) : Predicates.or(supportsImagePredicates); - } - }); - } catch (NoSuchElementException exception) { + if (!Iterables.any(images, supportsImagePredicate)) { + String message = format("no hardware profiles support images matching params: %s", supportsImagePredicate); + throw throwNoSuchElementExceptionAfterLoggingHardwareIds(message, hardwarel); + } - } - if (size(hardwaresThatAreCompatibleWithOurImages) == 0) { - String message = format("no hardware profiles support images matching params: %s", toString()); - throw throwNoSuchElementExceptionAfterLoggingHardwareIds(message, hardwaresThatAreCompatibleWithOurImages); - } + Iterable hardwareCompatibleWithOurImages = filter(hardwarel, supportsImagesPredicate(images)); Predicate hardwarePredicate = buildHardwarePredicate(); Hardware hardware; try { - hardware = hardwareOrdering.max(filter(hardwaresThatAreCompatibleWithOurImages, hardwarePredicate)); + hardware = hardwareOrdering.max(filter(hardwareCompatibleWithOurImages, hardwarePredicate)); } catch (NoSuchElementException exception) { String message = format("no hardware profiles match params: %s", hardwarePredicate); - throw throwNoSuchElementExceptionAfterLoggingHardwareIds(message, hardwaresThatAreCompatibleWithOurImages); + throw throwNoSuchElementExceptionAfterLoggingHardwareIds(message, hardwareCompatibleWithOurImages); } logger.trace("<< matched hardware(%s)", hardware.getId()); return hardware; 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 1f4bb92bc9..e7fa6984f0 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 @@ -58,58 +58,51 @@ import com.google.common.collect.ImmutableSet; */ @Test(groups = "unit", singleThreaded = true, testName = "TemplateBuilderImplTest") public class TemplateBuilderImplTest { - Location provider = new LocationBuilder().scope(LocationScope.PROVIDER).id("aws-ec2").description("aws-ec2").build(); + protected Location provider = new LocationBuilder().scope(LocationScope.PROVIDER).id("aws-ec2").description("aws-ec2").build(); - protected Location region = new LocationBuilder().scope(LocationScope.REGION).id("us-east-1").description("us-east-1") - .parent(provider).build(); - - protected Location region2 = new LocationBuilder().scope(LocationScope.REGION).id("us-east-2").description("us-east-2") - .parent(provider).build(); + protected Location region = new LocationBuilder().scope(LocationScope.REGION).id("us-east-1") + .description("us-east-1").parent(provider).build(); + + protected Location region2 = new LocationBuilder().scope(LocationScope.REGION).id("us-east-2") + .description("us-east-2").parent(provider).build(); + + protected OperatingSystem os = OperatingSystem.builder().name("osName").version("osVersion") + .description("osDescription").arch("X86_32").build(); + + protected Image image = new ImageBuilder().id("us-east-1/imageId").providerId("imageId").name("imageName") + .description("imageDescription").version("imageVersion").operatingSystem(os).status(Image.Status.AVAILABLE) + .location(region).build(); + + protected Image image2 = ImageBuilder.fromImage(image).operatingSystem(os.toBuilder().arch("X86_64").build()).build(); @SuppressWarnings("unchecked") public void testLocationPredicateWhenComputeMetadataIsNotLocationBound() { - Image image = createMock(Image.class); - OperatingSystem os = createMock(OperatingSystem.class); Hardware hardware = new HardwareBuilder().id("hardwareId").build(); Supplier> locations = Suppliers.> ofInstance(ImmutableSet . of(region)); - Supplier> images = Suppliers.> ofInstance(ImmutableSet. of( - image)); + Supplier> images = Suppliers.> ofInstance(ImmutableSet. of(image)); Supplier> hardwares = Suppliers.> ofInstance(ImmutableSet . of(hardware)); Provider optionsProvider = createMock(Provider.class); Provider templateBuilderProvider = createMock(Provider.class); TemplateBuilder defaultTemplate = createMock(TemplateBuilder.class); - expect(image.getLocation()).andReturn(region).anyTimes(); - expect(image.getProviderId()).andReturn("imageId").anyTimes(); - replay(image); - replay(os); - replay(defaultTemplate); - replay(optionsProvider); - replay(templateBuilderProvider); + replay(defaultTemplate, optionsProvider, templateBuilderProvider); TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region, optionsProvider, templateBuilderProvider); assert template.locationPredicate.apply(hardware); - verify(image); - verify(os); - verify(defaultTemplate); - verify(optionsProvider); - verify(templateBuilderProvider); + verify(defaultTemplate, optionsProvider, templateBuilderProvider); } @SuppressWarnings("unchecked") @Test public void testResolveImages() { - Image image = createMock(Image.class); - OperatingSystem os = createMock(OperatingSystem.class); - Image image2 = createMock(Image.class); - OperatingSystem os2 = createMock(OperatingSystem.class); + Hardware hardware = new HardwareBuilder().id("hardwareId").build(); @@ -123,56 +116,19 @@ public class TemplateBuilderImplTest { Provider templateBuilderProvider = createMock(Provider.class); TemplateBuilder defaultTemplate = createMock(TemplateBuilder.class); - expect(image.getName()).andReturn("imageName"); - expect(image2.getName()).andReturn("imageName"); - expect(image.getDescription()).andReturn("imageDescription"); - expect(image2.getDescription()).andReturn("imageDescription"); - expect(image.getVersion()).andReturn("imageVersion"); - expect(image2.getVersion()).andReturn("imageVersion"); - expect(image.getOperatingSystem()).andReturn(os).atLeastOnce(); - expect(image2.getOperatingSystem()).andReturn(os2).atLeastOnce(); - expect(image.getLocation()).andReturn(region).anyTimes(); - expect(image2.getLocation()).andReturn(region).anyTimes(); - expect(image.getProviderId()).andReturn("imageId").anyTimes(); - expect(image2.getProviderId()).andReturn("imageId2").anyTimes(); - expect(os.getName()).andReturn("osName"); - expect(os2.getName()).andReturn("osName"); - expect(os.getVersion()).andReturn("osVersion"); - expect(os2.getVersion()).andReturn("osVersion"); - expect(os.getDescription()).andReturn("osDescription"); - expect(os2.getDescription()).andReturn("osDescription"); - expect(os.getArch()).andReturn("X86_64").atLeastOnce(); - expect(os2.getArch()).andReturn("X86_64").atLeastOnce(); - - replay(image); - replay(image2); - replay(os); - replay(os2); - replay(defaultTemplate); - replay(optionsProvider); - replay(templateBuilderProvider); + replay(defaultTemplate, optionsProvider, templateBuilderProvider); TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region, optionsProvider, templateBuilderProvider); assertEquals(template.resolveImage(hardware, images.get()), image2); - verify(image); - verify(image2); - verify(os); - verify(os2); - verify(defaultTemplate); - verify(optionsProvider); - verify(templateBuilderProvider); + verify(defaultTemplate, optionsProvider, templateBuilderProvider); } @SuppressWarnings("unchecked") @Test public void testArchWins() { - Image image = createMock(Image.class); - Image image2 = createMock(Image.class); - OperatingSystem os = createMock(OperatingSystem.class); - OperatingSystem os2 = createMock(OperatingSystem.class); Hardware hardware = new HardwareBuilder().id("hardwareId").build(); @@ -188,45 +144,19 @@ public class TemplateBuilderImplTest { expect(optionsProvider.get()).andReturn(new TemplateOptions()); - - expect(image.getLocation()).andReturn(region).atLeastOnce(); - expect(image2.getLocation()).andReturn(region).atLeastOnce(); - expect(image.getOperatingSystem()).andReturn(os).atLeastOnce(); - expect(image2.getOperatingSystem()).andReturn(os2).atLeastOnce(); - expect(image.getId()).andReturn("us-east-1/1").atLeastOnce(); - expect(image.getProviderId()).andReturn("1").anyTimes(); - expect(image2.getProviderId()).andReturn("2").anyTimes(); - - expect(os.getArch()).andReturn("X86_32").atLeastOnce(); - expect(os2.getArch()).andReturn("X86_64").atLeastOnce(); - - replay(image); - replay(image2); - replay(os); - replay(os2); - replay(defaultTemplate); - replay(optionsProvider); - replay(templateBuilderProvider); + replay(defaultTemplate, optionsProvider, templateBuilderProvider); TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region, optionsProvider, templateBuilderProvider); assertEquals(template.smallest().osArchMatches("X86_32").build().getImage(), image); - verify(image); - verify(image2); - verify(os); - verify(os2); - verify(defaultTemplate); - verify(optionsProvider); - verify(templateBuilderProvider); + verify(defaultTemplate, optionsProvider, templateBuilderProvider); } @SuppressWarnings("unchecked") @Test public void testHardwareWithImageIdPredicateOnlyAcceptsImage() { - Image image = createMock(Image.class); - OperatingSystem os = createMock(OperatingSystem.class); Hardware hardware = new HardwareBuilder().id("hardwareId").supportsImage(ImagePredicates.idEquals("us-east-1/imageId")) .build(); @@ -242,25 +172,7 @@ public class TemplateBuilderImplTest { TemplateBuilder defaultTemplate = createMock(TemplateBuilder.class); expect(optionsProvider.get()).andReturn(new TemplateOptions()); - expect(image.getId()).andReturn("us-east-1/imageId").atLeastOnce(); - expect(image.getLocation()).andReturn(region).atLeastOnce(); - expect(image.getName()).andReturn(null).atLeastOnce(); - expect(image.getDescription()).andReturn(null).atLeastOnce(); - expect(image.getVersion()).andReturn(null).atLeastOnce(); - expect(image.getOperatingSystem()).andReturn(os).atLeastOnce(); - expect(image.getProviderId()).andReturn("imageId").anyTimes(); - - expect(os.getName()).andReturn(null).atLeastOnce(); - expect(os.getVersion()).andReturn(null).atLeastOnce(); - expect(os.getFamily()).andReturn(null).atLeastOnce(); - expect(os.getDescription()).andReturn(null).atLeastOnce(); - expect(os.getArch()).andReturn(null).atLeastOnce(); - expect(os.is64Bit()).andReturn(false).atLeastOnce(); - - - replay(image); - replay(os); replay(defaultTemplate); replay(optionsProvider); replay(templateBuilderProvider); @@ -270,8 +182,6 @@ public class TemplateBuilderImplTest { template.imageId("us-east-1/imageId").build(); - verify(image); - verify(os); verify(defaultTemplate); verify(optionsProvider); verify(templateBuilderProvider); @@ -280,8 +190,6 @@ public class TemplateBuilderImplTest { @SuppressWarnings("unchecked") @Test public void testHardwareWithImageIdPredicateOnlyAcceptsImageWhenLocationNull() { - Image image = createMock(Image.class); - OperatingSystem os = createMock(OperatingSystem.class); Hardware hardware = new HardwareBuilder().id("hardwareId").supportsImage(ImagePredicates.idEquals("us-east-1/imageId")) .build(); @@ -297,37 +205,21 @@ public class TemplateBuilderImplTest { TemplateBuilder defaultTemplate = createMock(TemplateBuilder.class); expect(optionsProvider.get()).andReturn(new TemplateOptions()); - expect(image.getId()).andReturn("us-east-1/imageId").atLeastOnce(); - expect(image.getLocation()).andReturn(null).atLeastOnce(); - expect(image.getName()).andReturn(null).atLeastOnce(); - expect(image.getDescription()).andReturn(null).atLeastOnce(); - expect(image.getVersion()).andReturn(null).atLeastOnce(); - expect(image.getOperatingSystem()).andReturn(os).atLeastOnce(); - expect(image.getProviderId()).andReturn("imageId").anyTimes(); - - expect(os.getName()).andReturn(null).atLeastOnce(); - expect(os.getVersion()).andReturn(null).atLeastOnce(); - expect(os.getFamily()).andReturn(null).atLeastOnce(); - expect(os.getDescription()).andReturn(null).atLeastOnce(); - expect(os.getArch()).andReturn(null).atLeastOnce(); - expect(os.is64Bit()).andReturn(false).atLeastOnce(); - replay(image, os, defaultTemplate, optionsProvider, templateBuilderProvider); + replay(defaultTemplate, optionsProvider, templateBuilderProvider); TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region, optionsProvider, templateBuilderProvider); template.imageId("us-east-1/imageId").build(); - verify(image, os, defaultTemplate, optionsProvider, templateBuilderProvider); + verify(defaultTemplate, optionsProvider, templateBuilderProvider); } @SuppressWarnings("unchecked") @Test public void testHardwareWithImageIdPredicateOnlyDoesntImage() { - Image image = createMock(Image.class); - OperatingSystem os = createMock(OperatingSystem.class); Hardware hardware = new HardwareBuilder().id("hardwareId").supportsImage(ImagePredicates.idEquals("differentImageId")) .build(); @@ -343,43 +235,18 @@ public class TemplateBuilderImplTest { TemplateBuilder defaultTemplate = createMock(TemplateBuilder.class); expect(optionsProvider.get()).andReturn(new TemplateOptions()); - expect(image.getId()).andReturn("us-east-1/imageId").atLeastOnce(); - expect(image.getLocation()).andReturn(region).atLeastOnce(); - expect(image.getOperatingSystem()).andReturn(os).atLeastOnce(); - expect(image.getName()).andReturn(null).atLeastOnce(); - expect(image.getDescription()).andReturn(null).atLeastOnce(); - expect(image.getVersion()).andReturn(null).atLeastOnce(); - expect(image.getProviderId()).andReturn("imageId").anyTimes(); - - expect(os.getName()).andReturn(null).atLeastOnce(); - expect(os.getVersion()).andReturn(null).atLeastOnce(); - expect(os.getFamily()).andReturn(null).atLeastOnce(); - expect(os.getDescription()).andReturn(null).atLeastOnce(); - expect(os.getArch()).andReturn(null).atLeastOnce(); - expect(os.is64Bit()).andReturn(false).atLeastOnce(); - - replay(image); - replay(os); - replay(defaultTemplate); - replay(optionsProvider); - replay(templateBuilderProvider); - + replay(defaultTemplate, optionsProvider, templateBuilderProvider); + TemplateBuilderImpl template = createTemplateBuilder(image, locations, images, hardwares, region, optionsProvider, templateBuilderProvider); try { template.imageId("us-east-1/imageId").build(); assert false; } catch (NoSuchElementException e) { - // make sure big data is not in the exception message - assertEquals( - e.getMessage(), - "no hardware profiles support images matching params: {imageId=us-east-1/imageId, locationId=us-east-1, os64Bit=false}"); - verify(image); - verify(os); - verify(defaultTemplate); - verify(optionsProvider); - verify(templateBuilderProvider); + // make sure message is succinct + assertEquals(e.getMessage(), "no hardware profiles support images matching params: idEquals(differentImageId)"); + verify(defaultTemplate, optionsProvider, templateBuilderProvider); } } @@ -468,80 +335,47 @@ public class TemplateBuilderImplTest { public void testSuppliedImageLocationWiderThanDefault() { TemplateOptions from = provideTemplateOptions(); - Image image = createMock(Image.class); - - Hardware hardware = new HardwareBuilder().id("hardwareId").supportsImage(ImagePredicates.idEquals("us-east-1/foo")).build(); + Hardware hardware = new HardwareBuilder().id("hardwareId").supportsImage(ImagePredicates.idEquals(image.getId())).build(); Supplier> locations = Suppliers.> ofInstance(ImmutableSet - . of(region)); + . of(provider, region)); Supplier> images = Suppliers.> ofInstance(ImmutableSet . of(image)); Supplier> hardwares = Suppliers.> ofInstance(ImmutableSet . of(hardware)); - Location imageLocation = createMock(Location.class); - OperatingSystem os = createMock(OperatingSystem.class); Provider optionsProvider = createMock(Provider.class); Provider templateBuilderProvider = createMock(Provider.class); TemplateOptions defaultOptions = createMock(TemplateOptions.class); expect(optionsProvider.get()).andReturn(from).atLeastOnce(); - - expect(image.getId()).andReturn("us-east-1/foo").atLeastOnce(); - expect(image.getLocation()).andReturn(region).atLeastOnce(); - expect(image.getOperatingSystem()).andReturn(os).atLeastOnce(); - expect(image.getName()).andReturn(null).atLeastOnce(); - expect(image.getDescription()).andReturn(null).atLeastOnce(); - expect(image.getVersion()).andReturn(null).atLeastOnce(); - expect(image.getProviderId()).andReturn("foo").anyTimes(); - - expect(os.getName()).andReturn(null).atLeastOnce(); - expect(os.getVersion()).andReturn(null).atLeastOnce(); - expect(os.getFamily()).andReturn(null).atLeastOnce(); - expect(os.getDescription()).andReturn(null).atLeastOnce(); - expect(os.getArch()).andReturn(null).atLeastOnce(); - expect(os.is64Bit()).andReturn(false).atLeastOnce(); - - - replay(defaultOptions); - replay(imageLocation); - replay(image); - replay(os); - replay(optionsProvider); - replay(templateBuilderProvider); + replay(defaultOptions, optionsProvider, templateBuilderProvider); TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region, optionsProvider, templateBuilderProvider); - assertEquals(template.imageId("us-east-1/foo").locationId(region.getId()).build().getLocation(), region); + assertEquals(template.imageId(image.getId()).locationId(provider.getId()).build().getLocation(), region); - verify(defaultOptions); - verify(imageLocation); - verify(image); - verify(os); - verify(optionsProvider); - verify(templateBuilderProvider); + verify(defaultOptions, optionsProvider, templateBuilderProvider); } @SuppressWarnings("unchecked") @Test public void testSuppliedLocationWithNoOptions() { Supplier> locations = Suppliers.> ofInstance(ImmutableSet - . of()); + . of(region)); Supplier> images = Suppliers.> ofInstance(ImmutableSet. of()); Supplier> hardwares = Suppliers.> ofInstance(ImmutableSet . of()); - Location defaultLocation = createMock(Location.class); Provider optionsProvider = createMock(Provider.class); Provider templateBuilderProvider = createMock(Provider.class); TemplateOptions defaultOptions = createMock(TemplateOptions.class); replay(defaultOptions); - replay(defaultLocation); replay(optionsProvider); replay(templateBuilderProvider); - TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, defaultLocation, + TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region, optionsProvider, templateBuilderProvider); try { @@ -552,7 +386,6 @@ public class TemplateBuilderImplTest { } verify(defaultOptions); - verify(defaultLocation); verify(optionsProvider); verify(templateBuilderProvider); } @@ -563,21 +396,19 @@ public class TemplateBuilderImplTest { TemplateOptions from = provideTemplateOptions(); Supplier> locations = Suppliers.> ofInstance(ImmutableSet - . of()); + . of(region)); Supplier> images = Suppliers.> ofInstance(ImmutableSet. of()); Supplier> hardwares = Suppliers.> ofInstance(ImmutableSet . of()); - Location defaultLocation = createMock(Location.class); Provider optionsProvider = createMock(Provider.class); Provider templateBuilderProvider = createMock(Provider.class); expect(optionsProvider.get()).andReturn(from).atLeastOnce(); - replay(defaultLocation); replay(optionsProvider); replay(templateBuilderProvider); - TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, defaultLocation, + TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region, optionsProvider, templateBuilderProvider); try { @@ -587,83 +418,124 @@ public class TemplateBuilderImplTest { } - verify(defaultLocation); verify(optionsProvider); verify(templateBuilderProvider); } + @SuppressWarnings("unchecked") + public void testImagesMustBePresentWhenQuerying() { + Supplier> locations = Suppliers.> ofInstance(ImmutableSet + . of(region)); + Supplier> images = Suppliers.> ofInstance(ImmutableSet. of()); + Supplier> hardwares = Suppliers.> ofInstance(ImmutableSet + . of(createMock(Hardware.class))); + Provider optionsProvider = createMock(Provider.class); + Provider templateBuilderProvider = createMock(Provider.class); + TemplateOptions defaultOptions = createMock(TemplateOptions.class); + + expect(optionsProvider.get()).andReturn(defaultOptions); + + replay(defaultOptions, optionsProvider, templateBuilderProvider); + + TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region, + optionsProvider, templateBuilderProvider); + + try { + template.os64Bit(true).build(); + assert false; + } catch (IllegalStateException e) { + assertEquals(e.getMessage(), "no images present!"); + } + + verify(defaultOptions, optionsProvider, templateBuilderProvider); + } + + @SuppressWarnings("unchecked") + public void testHardwareProfilesMustBePresentWhenQuerying() { + Supplier> locations = Suppliers.> ofInstance(ImmutableSet + . of(region)); + Supplier> images = Suppliers.> ofInstance(ImmutableSet. of(image)); + Supplier> hardwares = Suppliers.> ofInstance(ImmutableSet + . of()); + Provider optionsProvider = createMock(Provider.class); + Provider templateBuilderProvider = createMock(Provider.class); + TemplateOptions defaultOptions = createMock(TemplateOptions.class); + + expect(optionsProvider.get()).andReturn(defaultOptions); + + replay(defaultOptions, optionsProvider, templateBuilderProvider); + + TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region, + optionsProvider, templateBuilderProvider); + + try { + template.os64Bit(true).build(); + assert false; + } catch (IllegalStateException e) { + assertEquals(e.getMessage(), "no hardware profiles present!"); + } + + verify(defaultOptions, optionsProvider, templateBuilderProvider); + } + @SuppressWarnings("unchecked") @Test public void testDefaultLocationWithNoOptionsNoSuchElement() { Supplier> locations = Suppliers.> ofInstance(ImmutableSet - . of()); - Supplier> images = Suppliers.> ofInstance(ImmutableSet. of()); + . of(region)); + Supplier> images = Suppliers.> ofInstance(ImmutableSet. of(image)); Supplier> hardwares = Suppliers.> ofInstance(ImmutableSet - . of()); - Location defaultLocation = createMock(Location.class); + . of(createMock(Hardware.class))); Provider optionsProvider = createMock(Provider.class); Provider templateBuilderProvider = createMock(Provider.class); TemplateOptions defaultOptions = createMock(TemplateOptions.class); expect(optionsProvider.get()).andReturn(defaultOptions); - replay(defaultOptions); - replay(defaultLocation); - replay(optionsProvider); - replay(templateBuilderProvider); + replay(defaultOptions, optionsProvider, templateBuilderProvider); - TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, defaultLocation, + TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region, optionsProvider, templateBuilderProvider); try { - template.imageId("region/imageId").build(); + template.imageId("region/imageId2").build(); assert false; } catch (NoSuchElementException e) { // make sure big data is not in the exception message - assertEquals(e.getMessage(), "imageId(region/imageId) not found"); + assertEquals(e.getMessage(), "imageId(region/imageId2) not found"); } - verify(defaultOptions); - verify(defaultLocation); - verify(optionsProvider); - verify(templateBuilderProvider); + verify(defaultOptions, optionsProvider, templateBuilderProvider); } @SuppressWarnings("unchecked") @Test public void testDefaultLocationWithUnmatchedPredicateExceptionMessageAndLocationNotCalled() { Supplier> locations = Suppliers.> ofInstance(ImmutableSet - . of()); - Supplier> images = Suppliers.> ofInstance(ImmutableSet. of()); + . of(region)); + Supplier> images = Suppliers.> ofInstance(ImmutableSet. of(image)); Supplier> hardwares = Suppliers.> ofInstance(ImmutableSet - . of()); - Location defaultLocation = createMock(Location.class); + . of(createMock(Hardware.class))); Provider optionsProvider = createMock(Provider.class); Provider templateBuilderProvider = createMock(Provider.class); TemplateOptions defaultOptions = createMock(TemplateOptions.class); expect(optionsProvider.get()).andReturn(defaultOptions); - replay(defaultOptions); - replay(defaultLocation); - replay(optionsProvider); - replay(templateBuilderProvider); + replay(defaultOptions, optionsProvider, templateBuilderProvider); - TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, defaultLocation, + TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region, optionsProvider, templateBuilderProvider); try { - template.imageDescriptionMatches("description").build(); + template.imageDescriptionMatches("notDescription").build(); assert false; } catch (NoSuchElementException e) { // make sure big data is not in the exception message - assertEquals(e.getMessage(), "no image matched predicate: And(nullEqualToIsParentOrIsGrandparentOfCurrentLocation(),imageDescription(description))"); + assertEquals(e.getMessage(), "no image matched predicate: And(nullEqualToIsParentOrIsGrandparentOfCurrentLocation(),imageDescription(notDescription))"); } - verify(defaultOptions); - verify(defaultLocation); - verify(optionsProvider); - verify(templateBuilderProvider); + verify(defaultOptions, optionsProvider, templateBuilderProvider); } protected TemplateOptions provideTemplateOptions() { @@ -675,9 +547,9 @@ public class TemplateBuilderImplTest { public void testDefaultLocationWithOptions() { Supplier> locations = Suppliers.> ofInstance(ImmutableSet . of()); - Supplier> images = Suppliers.> ofInstance(ImmutableSet. of()); + Supplier> images = Suppliers.> ofInstance(ImmutableSet. of(image)); Supplier> hardwares = Suppliers.> ofInstance(ImmutableSet - . of()); + . of(createMock(Hardware.class))); Location defaultLocation = createMock(Location.class); Provider optionsProvider = createMock(Provider.class); TemplateOptions from = provideTemplateOptions(); @@ -685,11 +557,7 @@ public class TemplateBuilderImplTest { expect(optionsProvider.get()).andReturn(from); - expect(from.getInboundPorts()).andReturn(new int[] { 22 }); - - replay(defaultLocation); - replay(optionsProvider); - replay(templateBuilderProvider); + replay(defaultLocation, optionsProvider, templateBuilderProvider); TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, defaultLocation, optionsProvider, templateBuilderProvider); @@ -701,9 +569,7 @@ public class TemplateBuilderImplTest { } - verify(defaultLocation); - // verify(optionsProvider); - verify(templateBuilderProvider); + verify(defaultLocation, optionsProvider, templateBuilderProvider); } @SuppressWarnings("unchecked")