FlavorInZoneToHardware implements Function<FlavorInZone, Hardware> { @Override public Hardware apply(FlavorInZone flavorInZone) { Location location = locationIndex.get().get(flavorInZone.getZone()); checkState(location != null, "location %s not in locationIndex: %s", flavorInZone.getZone(), locationIndex.get()); Flavor flavor = flavorInZone.getFlavor(); return new HardwareBuilder().id(flavorInZone.slashEncode()).providerId(flavor.getId()).name(flavor.getName()) .ram(flavor.getRam()).processor(new Processor(flavor.getVcpus(), 1.0)).volume( new VolumeImpl(Float.valueOf(flavor.getDisk()), true, true)).location(location).build(); } @Inject  FlavorInZoneToHardware(Supplier<Map<String, Location>> locationIndex); @Override Hardware apply(FlavorInZone flavorInZone);  }
@Test public void testConversionWhereLocationFound() { UUID id = UUID.randomUUID(); FlavorInZone flavorInZoneToConvert = new FlavorInZone(Flavor.builder().id(id.toString()) .name("Test Flavor " + id).ram(262144).disk(10000).vcpus(16).build(), "az-1.region-a.geo-1"); Hardware converted = new FlavorInZoneToHardware(locationIndex).apply(flavorInZoneToConvert); Flavor flavorToConvert = flavorInZoneToConvert.getFlavor(); assertEquals(converted.getName(), flavorToConvert.getName()); assertEquals(converted.getId(), flavorInZoneToConvert.slashEncode()); assertEquals(converted.getProviderId(), flavorToConvert.getId()); assertEquals(converted.getLocation(), locationIndex.get().get("az-1.region-a.geo-1")); assertEquals(converted.getRam(), flavorToConvert.getRam()); assertNotNull(converted.getProcessors()); assertFalse(converted.getProcessors().isEmpty()); assertEquals(converted.getProcessors().iterator().next().getCores(), (double) flavorToConvert.getVcpus()); assertNotNull(converted.getVolumes()); assertFalse(converted.getVolumes().isEmpty()); assertEquals(converted.getVolumes().iterator().next().getSize(), Float.valueOf(flavorToConvert.getDisk())); }
@Test(expectedExceptions = IllegalStateException.class) public void testConversionWhereLocationNotFound() { UUID id = UUID.randomUUID(); FlavorInZone flavorInZoneToConvert = new FlavorInZone(Flavor.builder().id(id.toString()) .name("Test Flavor " + id).ram(262144).disk(10000).vcpus(16).build(), "South"); new FlavorInZoneToHardware(locationIndex).apply(flavorInZoneToConvert); }