ProductItemsToHardware implements Function<Iterable<ProductItem>, Hardware> { @Override public Hardware apply(Iterable<ProductItem> items) { ProductItem coresItem = getOnlyElement(filter(items, matches(cpuDescriptionRegex))); ProductItem ramItem = getOnlyElement(filter(items, categoryCode(RAM_CATEGORY))); ProductItem volumeItem = get(filter(items, categoryCode(FIRST_GUEST_DISK)), 0); String hardwareId = hardwareId().apply(ImmutableList.of(coresItem, ramItem, volumeItem)); double cores = ProductItems.capacity().apply(coresItem).doubleValue(); Matcher cpuMatcher = cpuDescriptionRegex.matcher(coresItem.getDescription()); double coreSpeed = (cpuMatcher.matches()) ? Double.parseDouble(cpuMatcher.group(cpuMatcher.groupCount())) : DEFAULT_CORE_SPEED; int ram = ProductItems.capacity().apply(ramItem).intValue(); return new HardwareBuilder().ids(hardwareId).processors(ImmutableList.of(new Processor(cores, coreSpeed))).ram( ram) .hypervisor("XenServer") .volumes( Iterables.transform(filter(items, categoryCodeMatches(diskCategoryRegex)), new Function<ProductItem, Volume>() { @Override public Volume apply(ProductItem item) { float volumeSize = ProductItems.capacity().apply(item); return new VolumeImpl( item.getId() + "", item.getDescription().indexOf(STORAGE_AREA_NETWORK) != -1 ? Volume.Type.SAN : Volume.Type.LOCAL, volumeSize, null, categoryCode(FIRST_GUEST_DISK).apply(item), false); } })).build(); } @Inject  ProductItemsToHardware();  ProductItemsToHardware(Pattern cpuDescriptionRegex, Pattern diskCategoryRegex); @Override Hardware apply(Iterable<ProductItem> items); static Function<List<ProductItem>, String> hardwareId();  }
@Test public void testHardware() { Hardware hardware = toHardware.apply(ImmutableSet.of(cpuItem, ramItem, volumeItem)); assertEquals("123,456,789", hardware.getId()); List<? extends Processor> processors = hardware.getProcessors(); assertEquals(1, processors.size()); assertEquals(2.0, processors.get(0).getCores()); assertEquals(2, hardware.getRam()); List<? extends Volume> volumes = hardware.getVolumes(); assertEquals(1, volumes.size()); Volume volume = volumes.get(0); assertEquals(100F, volume.getSize()); assertEquals(Volume.Type.SAN, volume.getType()); assertEquals(true, volume.isBootDevice()); }
@Test public void testHardwareWithPrivateCore() { cpuItem = cpuItem.toBuilder() .description("Private 2 x 2.0 GHz Cores") .build(); Hardware hardware = toHardware.apply(ImmutableSet.of(cpuItem, ramItem, volumeItem)); assertEquals("123,456,789", hardware.getId()); List<? extends Processor> processors = hardware.getProcessors(); assertEquals(1, processors.size()); assertEquals(2.0, processors.get(0).getCores()); assertEquals(2, hardware.getRam()); List<? extends Volume> volumes = hardware.getVolumes(); assertEquals(1, volumes.size()); assertEquals(100F, volumes.get(0).getSize()); }
@Test public void testHardwareWithTwoDisks() { ProductItem localVolumeItem = ProductItem.builder().id(4).description("25 GB").capacity(25F).prices( ProductItemPrice.builder().id(987).build()).categories( ProductItemCategory.builder().categoryCode("guest_disk1").build()).build(); Hardware hardware = toHardware.apply(ImmutableSet.of(cpuItem, ramItem, volumeItem,localVolumeItem)); List<? extends Volume> volumes = hardware.getVolumes(); assertEquals(2, volumes.size()); Volume volume = volumes.get(0); assertEquals(100F, volume.getSize()); assertEquals(Volume.Type.SAN, volume.getType()); assertEquals(true, volume.isBootDevice()); Volume volume1 = volumes.get(1); assertEquals(25F, volume1.getSize()); assertEquals(Volume.Type.LOCAL, volume1.getType()); assertEquals(false, volume1.isBootDevice()); }