ProductItemToImage implements Function<ProductItem, Image> { @Override public Image apply(ProductItem productItem) { checkNotNull(productItem, "productItem"); String description = checkNotNull(productItem.getDescription(), "productItem.description"); OsFamily osFamily = osFamily().apply(description); if (osFamily == OsFamily.UNRECOGNIZED) { logger.debug("Cannot determine os family for item: %s", productItem); } Integer bits = osBits().apply(description); if (bits == null) { logger.debug("Cannot determine os bits for item: %s", productItem); } String osVersion = osVersion().apply(description); if (osVersion == null) { logger.debug("Cannot determine os version for item: %s", productItem); } OperatingSystem os = OperatingSystem.builder() .description(description) .family(osFamily) .version(osVersion) .is64Bit(Objects.equal(bits, 64)) .build(); return new ImageBuilder() .ids(imageId().apply(productItem)) .description(description) .operatingSystem(os) .status(Image.Status.AVAILABLE) .build(); }  @Override Image apply(ProductItem productItem); static Function<String, OsFamily> osFamily(); static Function<String, String> osVersion(); static Function<String, Integer> osBits(); static Function<ProductItem, String> imageId();  }
@Test public void testConversion() { for( String description : operatingSystems ) { ProductItem item = ProductItem.builder() .description(description) .prices(ProductItemPrice.builder().id(1234).build()) .build(); Image i = new ProductItemToImage().apply(item); OperatingSystem os = i.getOperatingSystem(); assertNotNull(os); assertNotNull(os.getFamily()); assertFalse(os.getFamily().equals(OsFamily.UNRECOGNIZED)); assertNotNull(os.getVersion()); } }
@Test public void testUbuntu() { ProductItem item = ProductItem.builder() .description("Ubuntu Linux 10.04 LTS Lucid Lynx - Minimal Install (64 bit)") .prices(ProductItemPrice.builder().id(1234).build()) .build(); Image i = new ProductItemToImage().apply(item); OperatingSystem os = i.getOperatingSystem(); assertNotNull(os); assertEquals(OsFamily.UBUNTU, os.getFamily()); assertEquals("10.04",os.getVersion()); assertTrue(os.is64Bit()); }
@Test public void testUbuntuNoBitCount() { ProductItem item = ProductItem.builder() .description("Ubuntu Linux 10.04 LTS Lucid Lynx - Minimal Install") .prices(ProductItemPrice.builder().id(1234).build()) .build(); Image i = new ProductItemToImage().apply(item); OperatingSystem os = i.getOperatingSystem(); assertNotNull(os); assertEquals(OsFamily.UBUNTU, os.getFamily()); assertEquals("10.04",os.getVersion()); assertFalse(os.is64Bit()); }
@Test public void testCompletelyUnknown() { ProductItem item = ProductItem.builder() .description("This fails to match anything!!!") .prices(ProductItemPrice.builder().id(1234).build()) .build(); Image i = new ProductItemToImage().apply(item); OperatingSystem os = i.getOperatingSystem(); assertNotNull(os); assertEquals(OsFamily.UNRECOGNIZED, os.getFamily()); assertNull(os.getVersion()); assertFalse(os.is64Bit()); }
@Test public void test64BitUnknown() { ProductItem item = ProductItem.builder() .description("This only has the bit-count (64 bit)") .prices(ProductItemPrice.builder().id(1234).build()) .build(); Image i = new ProductItemToImage().apply(item); OperatingSystem os = i.getOperatingSystem(); assertNotNull(os); assertEquals(OsFamily.UNRECOGNIZED, os.getFamily()); assertNull(os.getVersion()); assertTrue(os.is64Bit()); }
@Test(expectedExceptions = NullPointerException.class) public void testNull() { new ProductItemToImage().apply(null); }
@Test(expectedExceptions = NullPointerException.class) public void testNoDescription() { ProductItem item = ProductItem.builder() .prices(ProductItemPrice.builder().id(1234).build()) .build(); new ProductItemToImage().apply(item); }