cleaner error messages in templateBuilder

This commit is contained in:
Adrian Cole 2012-07-02 12:21:13 -07:00
parent c016bcc8fd
commit 547c73ab00
3 changed files with 163 additions and 277 deletions

View File

@ -18,6 +18,7 @@
*/ */
package org.jclouds.ec2.compute.functions; package org.jclouds.ec2.compute.functions;
import static com.google.common.base.Preconditions.checkState;
import static com.google.common.collect.Maps.uniqueIndex; import static com.google.common.collect.Maps.uniqueIndex;
import java.util.Map; import java.util.Map;
@ -45,11 +46,12 @@ public class ImagesToRegionAndIdMap implements Function<Iterable<? extends Image
@Override @Override
public RegionAndName apply(Image from) { 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()); return new RegionAndName(from.getLocation().getId(), from.getProviderId());
} }
}); });
} }
} }

View File

@ -19,6 +19,7 @@
package org.jclouds.compute.domain.internal; package org.jclouds.compute.domain.internal;
import static com.google.common.base.Preconditions.checkNotNull; 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.base.Predicates.and;
import static com.google.common.collect.Iterables.filter; import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.find; import static com.google.common.collect.Iterables.find;
@ -140,6 +141,29 @@ public class TemplateBuilderImpl implements TemplateBuilder {
this.defaultTemplateProvider = defaultTemplateProvider; this.defaultTemplateProvider = defaultTemplateProvider;
} }
static Predicate<Hardware> supportsImagesPredicate(final Iterable<? extends Image> images) {
return new Predicate<Hardware>() {
@Override
public boolean apply(final Hardware hardware) {
return Iterables.any(images, new Predicate<Image>() {
@Override
public boolean apply(Image input) {
return hardware.supportsImage().apply(input);
}
@Override
public String toString() {
return "hardware(" + hardware + ").supportsImage()";
}
});
}
};
}
final Predicate<ComputeMetadata> locationPredicate = new NullEqualToIsParentOrIsGrandparentOfCurrentLocation(new Supplier<Location>(){ final Predicate<ComputeMetadata> locationPredicate = new NullEqualToIsParentOrIsGrandparentOfCurrentLocation(new Supplier<Location>(){
@Override @Override
@ -627,7 +651,9 @@ public class TemplateBuilderImpl implements TemplateBuilder {
options = optionsProvider.get(); options = optionsProvider.get();
logger.debug(">> searching params(%s)", this); logger.debug(">> searching params(%s)", this);
Set<? extends Image> images = getImages(); Set<? extends Image> images = getImages();
checkState(images.size() > 0, "no images present!");
Set<? extends Hardware> hardwaresToSearch = hardwares.get(); Set<? extends Hardware> hardwaresToSearch = hardwares.get();
checkState(hardwaresToSearch.size() > 0, "no hardware profiles present!");
Image image = null; Image image = null;
if (imageId != null) { if (imageId != null) {
@ -708,41 +734,33 @@ public class TemplateBuilderImpl implements TemplateBuilder {
protected Hardware resolveHardware(Set<? extends Hardware> hardwarel, final Iterable<? extends Image> images) { protected Hardware resolveHardware(Set<? extends Hardware> hardwarel, final Iterable<? extends Image> images) {
Ordering<Hardware> hardwareOrdering = hardwareSorter(); Ordering<Hardware> hardwareOrdering = hardwareSorter();
Iterable<? extends Hardware> hardwaresThatAreCompatibleWithOurImages = ImmutableSet.of();
try { Iterable<Predicate<Image>> supportsImagePredicates = Iterables.transform(hardwarel,
hardwaresThatAreCompatibleWithOurImages = filter(hardwarel, new Predicate<Hardware>() { new Function<Hardware, Predicate<Image>>() {
@Override
public boolean apply(final Hardware hardware) {
return Iterables.any(images, new Predicate<Image>() {
@Override @Override
public boolean apply(Image input) { public Predicate<Image> apply(Hardware input) {
return hardware.supportsImage().apply(input); return input.supportsImage();
}
@Override
public String toString() {
return "hardware(" + hardware + ").supportsImage()";
} }
}); });
} Predicate<Image> supportsImagePredicate = Iterables.size(supportsImagePredicates) == 1 ? Iterables
}); .getOnlyElement(supportsImagePredicates) : Predicates.<Image>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()); Iterable<? extends Hardware> hardwareCompatibleWithOurImages = filter(hardwarel, supportsImagesPredicate(images));
throw throwNoSuchElementExceptionAfterLoggingHardwareIds(message, hardwaresThatAreCompatibleWithOurImages);
}
Predicate<Hardware> hardwarePredicate = buildHardwarePredicate(); Predicate<Hardware> hardwarePredicate = buildHardwarePredicate();
Hardware hardware; Hardware hardware;
try { try {
hardware = hardwareOrdering.max(filter(hardwaresThatAreCompatibleWithOurImages, hardwarePredicate)); hardware = hardwareOrdering.max(filter(hardwareCompatibleWithOurImages, hardwarePredicate));
} catch (NoSuchElementException exception) { } catch (NoSuchElementException exception) {
String message = format("no hardware profiles match params: %s", hardwarePredicate); 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()); logger.trace("<< matched hardware(%s)", hardware.getId());
return hardware; return hardware;

View File

@ -58,58 +58,51 @@ import com.google.common.collect.ImmutableSet;
*/ */
@Test(groups = "unit", singleThreaded = true, testName = "TemplateBuilderImplTest") @Test(groups = "unit", singleThreaded = true, testName = "TemplateBuilderImplTest")
public class 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") protected Location region = new LocationBuilder().scope(LocationScope.REGION).id("us-east-1")
.parent(provider).build(); .description("us-east-1").parent(provider).build();
protected Location region2 = new LocationBuilder().scope(LocationScope.REGION).id("us-east-2").description("us-east-2") protected Location region2 = new LocationBuilder().scope(LocationScope.REGION).id("us-east-2")
.parent(provider).build(); .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") @SuppressWarnings("unchecked")
public void testLocationPredicateWhenComputeMetadataIsNotLocationBound() { public void testLocationPredicateWhenComputeMetadataIsNotLocationBound() {
Image image = createMock(Image.class);
OperatingSystem os = createMock(OperatingSystem.class);
Hardware hardware = new HardwareBuilder().id("hardwareId").build(); Hardware hardware = new HardwareBuilder().id("hardwareId").build();
Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of(region)); .<Location> of(region));
Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.<Image> of( Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.<Image> of(image));
image));
Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet
.<Hardware> of(hardware)); .<Hardware> of(hardware));
Provider<TemplateOptions> optionsProvider = createMock(Provider.class); Provider<TemplateOptions> optionsProvider = createMock(Provider.class);
Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class); Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class);
TemplateBuilder defaultTemplate = createMock(TemplateBuilder.class); TemplateBuilder defaultTemplate = createMock(TemplateBuilder.class);
expect(image.getLocation()).andReturn(region).anyTimes();
expect(image.getProviderId()).andReturn("imageId").anyTimes();
replay(image); replay(defaultTemplate, optionsProvider, templateBuilderProvider);
replay(os);
replay(defaultTemplate);
replay(optionsProvider);
replay(templateBuilderProvider);
TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region, TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region,
optionsProvider, templateBuilderProvider); optionsProvider, templateBuilderProvider);
assert template.locationPredicate.apply(hardware); assert template.locationPredicate.apply(hardware);
verify(image); verify(defaultTemplate, optionsProvider, templateBuilderProvider);
verify(os);
verify(defaultTemplate);
verify(optionsProvider);
verify(templateBuilderProvider);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Test @Test
public void testResolveImages() { 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(); Hardware hardware = new HardwareBuilder().id("hardwareId").build();
@ -123,56 +116,19 @@ public class TemplateBuilderImplTest {
Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class); Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class);
TemplateBuilder defaultTemplate = createMock(TemplateBuilder.class); TemplateBuilder defaultTemplate = createMock(TemplateBuilder.class);
expect(image.getName()).andReturn("imageName"); replay(defaultTemplate, optionsProvider, templateBuilderProvider);
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);
TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region, TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region,
optionsProvider, templateBuilderProvider); optionsProvider, templateBuilderProvider);
assertEquals(template.resolveImage(hardware, images.get()), image2); assertEquals(template.resolveImage(hardware, images.get()), image2);
verify(image); verify(defaultTemplate, optionsProvider, templateBuilderProvider);
verify(image2);
verify(os);
verify(os2);
verify(defaultTemplate);
verify(optionsProvider);
verify(templateBuilderProvider);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Test @Test
public void testArchWins() { 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(); Hardware hardware = new HardwareBuilder().id("hardwareId").build();
@ -188,45 +144,19 @@ public class TemplateBuilderImplTest {
expect(optionsProvider.get()).andReturn(new TemplateOptions()); expect(optionsProvider.get()).andReturn(new TemplateOptions());
replay(defaultTemplate, optionsProvider, templateBuilderProvider);
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);
TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region, TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region,
optionsProvider, templateBuilderProvider); optionsProvider, templateBuilderProvider);
assertEquals(template.smallest().osArchMatches("X86_32").build().getImage(), image); assertEquals(template.smallest().osArchMatches("X86_32").build().getImage(), image);
verify(image); verify(defaultTemplate, optionsProvider, templateBuilderProvider);
verify(image2);
verify(os);
verify(os2);
verify(defaultTemplate);
verify(optionsProvider);
verify(templateBuilderProvider);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Test @Test
public void testHardwareWithImageIdPredicateOnlyAcceptsImage() { 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")) Hardware hardware = new HardwareBuilder().id("hardwareId").supportsImage(ImagePredicates.idEquals("us-east-1/imageId"))
.build(); .build();
@ -242,25 +172,7 @@ public class TemplateBuilderImplTest {
TemplateBuilder defaultTemplate = createMock(TemplateBuilder.class); TemplateBuilder defaultTemplate = createMock(TemplateBuilder.class);
expect(optionsProvider.get()).andReturn(new TemplateOptions()); 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(defaultTemplate);
replay(optionsProvider); replay(optionsProvider);
replay(templateBuilderProvider); replay(templateBuilderProvider);
@ -270,8 +182,6 @@ public class TemplateBuilderImplTest {
template.imageId("us-east-1/imageId").build(); template.imageId("us-east-1/imageId").build();
verify(image);
verify(os);
verify(defaultTemplate); verify(defaultTemplate);
verify(optionsProvider); verify(optionsProvider);
verify(templateBuilderProvider); verify(templateBuilderProvider);
@ -280,8 +190,6 @@ public class TemplateBuilderImplTest {
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Test @Test
public void testHardwareWithImageIdPredicateOnlyAcceptsImageWhenLocationNull() { 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")) Hardware hardware = new HardwareBuilder().id("hardwareId").supportsImage(ImagePredicates.idEquals("us-east-1/imageId"))
.build(); .build();
@ -297,37 +205,21 @@ public class TemplateBuilderImplTest {
TemplateBuilder defaultTemplate = createMock(TemplateBuilder.class); TemplateBuilder defaultTemplate = createMock(TemplateBuilder.class);
expect(optionsProvider.get()).andReturn(new TemplateOptions()); 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(); replay(defaultTemplate, optionsProvider, templateBuilderProvider);
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);
TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region, TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region,
optionsProvider, templateBuilderProvider); optionsProvider, templateBuilderProvider);
template.imageId("us-east-1/imageId").build(); template.imageId("us-east-1/imageId").build();
verify(image, os, defaultTemplate, optionsProvider, templateBuilderProvider); verify(defaultTemplate, optionsProvider, templateBuilderProvider);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Test @Test
public void testHardwareWithImageIdPredicateOnlyDoesntImage() { public void testHardwareWithImageIdPredicateOnlyDoesntImage() {
Image image = createMock(Image.class);
OperatingSystem os = createMock(OperatingSystem.class);
Hardware hardware = new HardwareBuilder().id("hardwareId").supportsImage(ImagePredicates.idEquals("differentImageId")) Hardware hardware = new HardwareBuilder().id("hardwareId").supportsImage(ImagePredicates.idEquals("differentImageId"))
.build(); .build();
@ -343,27 +235,8 @@ public class TemplateBuilderImplTest {
TemplateBuilder defaultTemplate = createMock(TemplateBuilder.class); TemplateBuilder defaultTemplate = createMock(TemplateBuilder.class);
expect(optionsProvider.get()).andReturn(new TemplateOptions()); 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();
replay(defaultTemplate, optionsProvider, templateBuilderProvider);
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);
TemplateBuilderImpl template = createTemplateBuilder(image, locations, images, hardwares, region, TemplateBuilderImpl template = createTemplateBuilder(image, locations, images, hardwares, region,
optionsProvider, templateBuilderProvider); optionsProvider, templateBuilderProvider);
@ -371,15 +244,9 @@ public class TemplateBuilderImplTest {
template.imageId("us-east-1/imageId").build(); template.imageId("us-east-1/imageId").build();
assert false; assert false;
} catch (NoSuchElementException e) { } catch (NoSuchElementException e) {
// make sure big data is not in the exception message // make sure message is succinct
assertEquals( assertEquals(e.getMessage(), "no hardware profiles support images matching params: idEquals(differentImageId)");
e.getMessage(), verify(defaultTemplate, optionsProvider, templateBuilderProvider);
"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);
} }
} }
@ -468,80 +335,47 @@ public class TemplateBuilderImplTest {
public void testSuppliedImageLocationWiderThanDefault() { public void testSuppliedImageLocationWiderThanDefault() {
TemplateOptions from = provideTemplateOptions(); TemplateOptions from = provideTemplateOptions();
Image image = createMock(Image.class); Hardware hardware = new HardwareBuilder().id("hardwareId").supportsImage(ImagePredicates.idEquals(image.getId())).build();
Hardware hardware = new HardwareBuilder().id("hardwareId").supportsImage(ImagePredicates.idEquals("us-east-1/foo")).build();
Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of(region)); .<Location> of(provider, region));
Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet
.<Image> of(image)); .<Image> of(image));
Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet
.<Hardware> of(hardware)); .<Hardware> of(hardware));
Location imageLocation = createMock(Location.class);
OperatingSystem os = createMock(OperatingSystem.class);
Provider<TemplateOptions> optionsProvider = createMock(Provider.class); Provider<TemplateOptions> optionsProvider = createMock(Provider.class);
Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class); Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class);
TemplateOptions defaultOptions = createMock(TemplateOptions.class); TemplateOptions defaultOptions = createMock(TemplateOptions.class);
expect(optionsProvider.get()).andReturn(from).atLeastOnce(); expect(optionsProvider.get()).andReturn(from).atLeastOnce();
replay(defaultOptions, optionsProvider, templateBuilderProvider);
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);
TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region, TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region,
optionsProvider, templateBuilderProvider); 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(defaultOptions, optionsProvider, templateBuilderProvider);
verify(imageLocation);
verify(image);
verify(os);
verify(optionsProvider);
verify(templateBuilderProvider);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Test @Test
public void testSuppliedLocationWithNoOptions() { public void testSuppliedLocationWithNoOptions() {
Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of()); .<Location> of(region));
Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.<Image> of()); Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.<Image> of());
Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet
.<Hardware> of()); .<Hardware> of());
Location defaultLocation = createMock(Location.class);
Provider<TemplateOptions> optionsProvider = createMock(Provider.class); Provider<TemplateOptions> optionsProvider = createMock(Provider.class);
Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class); Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class);
TemplateOptions defaultOptions = createMock(TemplateOptions.class); TemplateOptions defaultOptions = createMock(TemplateOptions.class);
replay(defaultOptions); replay(defaultOptions);
replay(defaultLocation);
replay(optionsProvider); replay(optionsProvider);
replay(templateBuilderProvider); replay(templateBuilderProvider);
TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, defaultLocation, TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region,
optionsProvider, templateBuilderProvider); optionsProvider, templateBuilderProvider);
try { try {
@ -552,7 +386,6 @@ public class TemplateBuilderImplTest {
} }
verify(defaultOptions); verify(defaultOptions);
verify(defaultLocation);
verify(optionsProvider); verify(optionsProvider);
verify(templateBuilderProvider); verify(templateBuilderProvider);
} }
@ -563,21 +396,19 @@ public class TemplateBuilderImplTest {
TemplateOptions from = provideTemplateOptions(); TemplateOptions from = provideTemplateOptions();
Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of()); .<Location> of(region));
Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.<Image> of()); Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.<Image> of());
Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet
.<Hardware> of()); .<Hardware> of());
Location defaultLocation = createMock(Location.class);
Provider<TemplateOptions> optionsProvider = createMock(Provider.class); Provider<TemplateOptions> optionsProvider = createMock(Provider.class);
Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class); Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class);
expect(optionsProvider.get()).andReturn(from).atLeastOnce(); expect(optionsProvider.get()).andReturn(from).atLeastOnce();
replay(defaultLocation);
replay(optionsProvider); replay(optionsProvider);
replay(templateBuilderProvider); replay(templateBuilderProvider);
TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, defaultLocation, TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region,
optionsProvider, templateBuilderProvider); optionsProvider, templateBuilderProvider);
try { try {
@ -587,83 +418,124 @@ public class TemplateBuilderImplTest {
} }
verify(defaultLocation);
verify(optionsProvider); verify(optionsProvider);
verify(templateBuilderProvider); verify(templateBuilderProvider);
} }
@SuppressWarnings("unchecked")
public void testImagesMustBePresentWhenQuerying() {
Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of(region));
Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.<Image> of());
Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet
.<Hardware> of(createMock(Hardware.class)));
Provider<TemplateOptions> optionsProvider = createMock(Provider.class);
Provider<TemplateBuilder> 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<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of(region));
Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.<Image> of(image));
Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet
.<Hardware> of());
Provider<TemplateOptions> optionsProvider = createMock(Provider.class);
Provider<TemplateBuilder> 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") @SuppressWarnings("unchecked")
@Test @Test
public void testDefaultLocationWithNoOptionsNoSuchElement() { public void testDefaultLocationWithNoOptionsNoSuchElement() {
Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of()); .<Location> of(region));
Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.<Image> of()); Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.<Image> of(image));
Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet
.<Hardware> of()); .<Hardware> of(createMock(Hardware.class)));
Location defaultLocation = createMock(Location.class);
Provider<TemplateOptions> optionsProvider = createMock(Provider.class); Provider<TemplateOptions> optionsProvider = createMock(Provider.class);
Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class); Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class);
TemplateOptions defaultOptions = createMock(TemplateOptions.class); TemplateOptions defaultOptions = createMock(TemplateOptions.class);
expect(optionsProvider.get()).andReturn(defaultOptions); expect(optionsProvider.get()).andReturn(defaultOptions);
replay(defaultOptions); replay(defaultOptions, optionsProvider, templateBuilderProvider);
replay(defaultLocation);
replay(optionsProvider);
replay(templateBuilderProvider);
TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, defaultLocation, TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region,
optionsProvider, templateBuilderProvider); optionsProvider, templateBuilderProvider);
try { try {
template.imageId("region/imageId").build(); template.imageId("region/imageId2").build();
assert false; assert false;
} catch (NoSuchElementException e) { } catch (NoSuchElementException e) {
// make sure big data is not in the exception message // 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(defaultOptions, optionsProvider, templateBuilderProvider);
verify(defaultLocation);
verify(optionsProvider);
verify(templateBuilderProvider);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")
@Test @Test
public void testDefaultLocationWithUnmatchedPredicateExceptionMessageAndLocationNotCalled() { public void testDefaultLocationWithUnmatchedPredicateExceptionMessageAndLocationNotCalled() {
Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of()); .<Location> of(region));
Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.<Image> of()); Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.<Image> of(image));
Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet
.<Hardware> of()); .<Hardware> of(createMock(Hardware.class)));
Location defaultLocation = createMock(Location.class);
Provider<TemplateOptions> optionsProvider = createMock(Provider.class); Provider<TemplateOptions> optionsProvider = createMock(Provider.class);
Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class); Provider<TemplateBuilder> templateBuilderProvider = createMock(Provider.class);
TemplateOptions defaultOptions = createMock(TemplateOptions.class); TemplateOptions defaultOptions = createMock(TemplateOptions.class);
expect(optionsProvider.get()).andReturn(defaultOptions); expect(optionsProvider.get()).andReturn(defaultOptions);
replay(defaultOptions); replay(defaultOptions, optionsProvider, templateBuilderProvider);
replay(defaultLocation);
replay(optionsProvider);
replay(templateBuilderProvider);
TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, defaultLocation, TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, region,
optionsProvider, templateBuilderProvider); optionsProvider, templateBuilderProvider);
try { try {
template.imageDescriptionMatches("description").build(); template.imageDescriptionMatches("notDescription").build();
assert false; assert false;
} catch (NoSuchElementException e) { } catch (NoSuchElementException e) {
// make sure big data is not in the exception message // 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(defaultOptions, optionsProvider, templateBuilderProvider);
verify(defaultLocation);
verify(optionsProvider);
verify(templateBuilderProvider);
} }
protected TemplateOptions provideTemplateOptions() { protected TemplateOptions provideTemplateOptions() {
@ -675,9 +547,9 @@ public class TemplateBuilderImplTest {
public void testDefaultLocationWithOptions() { public void testDefaultLocationWithOptions() {
Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet Supplier<Set<? extends Location>> locations = Suppliers.<Set<? extends Location>> ofInstance(ImmutableSet
.<Location> of()); .<Location> of());
Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.<Image> of()); Supplier<Set<? extends Image>> images = Suppliers.<Set<? extends Image>> ofInstance(ImmutableSet.<Image> of(image));
Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet Supplier<Set<? extends Hardware>> hardwares = Suppliers.<Set<? extends Hardware>> ofInstance(ImmutableSet
.<Hardware> of()); .<Hardware> of(createMock(Hardware.class)));
Location defaultLocation = createMock(Location.class); Location defaultLocation = createMock(Location.class);
Provider<TemplateOptions> optionsProvider = createMock(Provider.class); Provider<TemplateOptions> optionsProvider = createMock(Provider.class);
TemplateOptions from = provideTemplateOptions(); TemplateOptions from = provideTemplateOptions();
@ -685,11 +557,7 @@ public class TemplateBuilderImplTest {
expect(optionsProvider.get()).andReturn(from); expect(optionsProvider.get()).andReturn(from);
expect(from.getInboundPorts()).andReturn(new int[] { 22 }); replay(defaultLocation, optionsProvider, templateBuilderProvider);
replay(defaultLocation);
replay(optionsProvider);
replay(templateBuilderProvider);
TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, defaultLocation, TemplateBuilderImpl template = createTemplateBuilder(null, locations, images, hardwares, defaultLocation,
optionsProvider, templateBuilderProvider); optionsProvider, templateBuilderProvider);
@ -701,9 +569,7 @@ public class TemplateBuilderImplTest {
} }
verify(defaultLocation); verify(defaultLocation, optionsProvider, templateBuilderProvider);
// verify(optionsProvider);
verify(templateBuilderProvider);
} }
@SuppressWarnings("unchecked") @SuppressWarnings("unchecked")