ProductCurator extends AbstractHibernateCurator<Product> { @Transactional public Product create(Product entity) { log.debug("Persisting new product entity: {}", entity); this.validateProductReferences(entity); Product newProduct = super.create(entity, false); for (ProductContent productContent : entity.getProductContent()) { if (productContent.getId() == null) { this.currentSession().save(productContent); } } return newProduct; } @Inject  ProductCurator(Configuration config,
        AttributeValidator attributeValidator); Product getByName(Owner owner, String name); CandlepinQuery<Product> listAllByUuids(Collection<? extends Serializable> uuids); Set<Product> getPoolDerivedProvidedProductsCached(Pool pool); Set<Product> getPoolDerivedProvidedProductsCached(String poolId); Set<Product> getPoolProvidedProductsCached(Pool pool); Set<Product> getPoolProvidedProductsCached(String poolId); Set<String> getPoolProvidedProductUuids(String poolId); Set<String> getDerivedPoolProvidedProductUuids(String poolId); Set<Product> getProductsByUuidCached(Set<String> productUuids); Map<String, Product> getHydratedProductsByUuid(Set<String> uuids); int hydratePoolProvidedProducts(Iterable<Pool> pools); @Transactional Product create(Product entity); @Transactional Product merge(Product entity); @Override @Transactional void delete(Product entity); boolean productHasSubscriptions(Owner owner, Product product); CandlepinQuery<Product> getProductsByContent(Owner owner, Collection<String> contentIds); @SuppressWarnings("unchecked") CandlepinQuery<Product> getProductsByContent(Owner owner, Collection<String> contentIds,
        Collection<String> productsToOmit); @SuppressWarnings("unchecked") CandlepinQuery<Product> getProductsByContentUuids(Collection<String> contentUuids); @SuppressWarnings("unchecked") CandlepinQuery<Product> getProductsByContentUuids(Owner owner, Collection<String> contentUuids);  }
@Test @SuppressWarnings("unchecked") public void normalCreate() { Product prod = new Product("cptest-label", "My Product"); productCurator.create(prod); List<Product> results = this.getEntityManager().createQuery("select p from Product as p") .getResultList(); assertEquals(5, results.size()); }
@Test public void nameRequired() { Product prod = new Product("someproductlabel", null); assertThrows(PersistenceException.class, () -> productCurator.create(prod, true)); }
@Test public void labelRequired() { Product prod = new Product(null, "My Product Name"); assertThrows(ConstraintViolationException.class, () -> productCurator.create(prod, true)); }
@Test public void nameNonUnique() { Product prod = new Product("label1", "name"); productCurator.create(prod); Product prod2 = new Product("label2", "name"); productCurator.create(prod2); assertEquals(prod.getName(), prod2.getName()); assertNotEquals(prod.getUuid(), prod2.getUuid()); }
@Test public void testWithSimpleJsonAttribute() throws Exception { Map<String, String> data = new HashMap<>(); data.put("a", "1"); data.put("b", "2"); ObjectMapper mapper = new ObjectMapper(); String jsonData = mapper.writeValueAsString(data); Product prod = new Product("cptest-label", "My Product"); prod.setAttribute("content_sets", jsonData); productCurator.create(prod); Product lookedUp = productCurator.get(prod.getUuid()); assertEquals(jsonData, lookedUp.getAttributeValue("content_sets")); data = mapper.readValue(lookedUp.getAttributeValue("content_sets"), new TypeReference<Map<String, String>>(){}); assertEquals("1", data.get("a")); assertEquals("2", data.get("b")); }
@Test public void testJsonListOfHashes() throws Exception { List<Map<String, String>> data = new LinkedList<>(); Map<String, String> contentSet1 = new HashMap<>(); contentSet1.put("name", "cs1"); contentSet1.put("url", "url"); Map<String, String> contentSet2 = new HashMap<>(); contentSet2.put("name", "cs2"); contentSet2.put("url", "url2"); data.add(contentSet1); data.add(contentSet2); ObjectMapper mapper = new ObjectMapper(); String jsonData = mapper.writeValueAsString(data); Product prod = TestUtil.createProduct("cptest-label", "My Product"); prod.setAttribute("content_sets", jsonData); productCurator.create(prod); Product lookedUp = productCurator.get(prod.getUuid()); assertEquals(jsonData, lookedUp.getAttributeValue("content_sets")); data = mapper.readValue(lookedUp.getAttributeValue("content_sets"), new TypeReference<List<Map<String, String>>>(){}); Map<String, String> cs1 = data.get(0); assertEquals("cs1", cs1.get("name")); Map<String, String> cs2 = data.get(1); assertEquals("cs2", cs2.get("name")); }
@Test public void testCreationDate() { Product prod = TestUtil.createProduct("test-label", "test-product-name"); productCurator.create(prod); assertNotNull(prod.getCreated()); }
@Test public void testInitialUpdate() { Product prod = TestUtil.createProduct("test-label", "test-product-name"); productCurator.create(prod); assertNotNull(prod.getUpdated()); }
@Test public void testDependentProducts() { Product prod = new Product("test-label", "test-product-name"); HashSet<String> dependentProductIds = new HashSet<>(); dependentProductIds.add("ProductX"); prod.setDependentProductIds(dependentProductIds); productCurator.create(prod); Product lookedUp = productCurator.get(prod.getUuid()); assertThat(lookedUp.getDependentProductIds(), hasItem("ProductX")); }
@Test public void testProductFullConstructor() { Product prod = new Product("cp_test-label", "variant", "version", "arch", "", "SVC"); productCurator.create(prod); productCurator.get(prod.getUuid()); }
@Test public void testProductAttributeValidationSuccessCreate() { Product original = createTestProduct(); original.setAttribute("product.count", "1"); original.setAttribute("product.pos_count", "5"); original.setAttribute("product.long_multiplier", (new Long(Integer.MAX_VALUE * 1000)).toString()); original.setAttribute("product.long_pos_count", "23"); original.setAttribute("product.bool_val_str", "true"); original.setAttribute("product.bool_val_num", "0"); productCurator.create(original); assertNotNull(original.getUuid()); }
@Test public void testProductAttributeCreationFailBadInt() { Product original = createTestProduct(); original.setAttribute("product.count", "1.0"); assertThrows(PropertyValidationException.class, () -> productCurator.create(original)); }
@Test public void testProductAttributeCreationSuccessZeroInt() { Product original = createTestProduct(); original.setAttribute("product.pos_count", "0"); productCurator.create(original); }
@Test public void testProductAttributeCreationFailBadPosInt() { Product original = createTestProduct(); original.setAttribute("product.pos_count", "-5"); assertThrows(PropertyValidationException.class, () -> productCurator.create(original)); }
@Test public void testProductAttributeCreationFailBadLong() { Product original = createTestProduct(); original.setAttribute("product.long_multiplier", "ZZ"); assertThrows(PropertyValidationException.class, () -> productCurator.create(original)); }
@Test public void testProductAttributeCreationFailBadPosLong() { Product original = createTestProduct(); original.setAttribute("product.long_pos_count", "-1"); assertThrows(PropertyValidationException.class, () -> productCurator.create(original)); }
@Test public void testProductAttributeCreationFailBadStringBool() { Product original = createTestProduct(); original.setAttribute("product.bool_val_str", "yes"); assertThrows(PropertyValidationException.class, () -> productCurator.create(original)); }
@Test public void testProductAttributeCreationFailNumberBool() { Product original = createTestProduct(); original.setAttribute("product.bool_val_num", "2"); assertThrows(PropertyValidationException.class, () -> productCurator.create(original)); }
@Test public void testSubstringConfigList() { Product original = createTestProduct(); original.setAttribute("product.pos", "-5"); productCurator.create(original); }
@Test public void testCannotCreateProductWithBrandingWithNullId() { Product marketingProduct = createTestProduct(); marketingProduct.addBranding( new Branding(marketingProduct, null, "Brand No 1", "OS")); IllegalStateException ise = assertThrows(IllegalStateException.class, () -> productCurator.create(marketingProduct)); assertEquals(ise.getMessage(), "Product contains a Branding with a null product id, name or type.", "The exception should have a different message."); }
@Test public void testCannotCreateProductWithBrandingWithNullType() { Product marketingProduct = createTestProduct(); marketingProduct.addBranding( new Branding(marketingProduct, "eng_prod_id_1", "Brand No 1", null)); IllegalStateException ise = assertThrows(IllegalStateException.class, () -> productCurator.create(marketingProduct)); assertEquals(ise.getMessage(), "Product contains a Branding with a null product id, name or type.", "The exception should have a different message."); }
@Test public void testCannotCreateProductWithBrandingWithNullName() { Product marketingProduct = createTestProduct(); marketingProduct.addBranding( new Branding(marketingProduct, "eng_prod_id_1", null, "OS")); IllegalStateException ise = assertThrows(IllegalStateException.class, () -> productCurator.create(marketingProduct)); assertEquals(ise.getMessage(), "Product contains a Branding with a null product id, name or type.", "The exception should have a different message."); }