ProductManager { public Product createProduct(ProductDTO dto, Owner owner) { if (dto == null) { throw new IllegalArgumentException("dto is null"); } if (owner == null) { throw new IllegalArgumentException("owner is null"); } if (dto.getId() == null || dto.getName() == null) { throw new IllegalArgumentException("dto is incomplete"); } if (this.ownerProductCurator.productExists(owner, dto.getId())) { throw new IllegalStateException("product has already been created: " + dto.getId()); } Product entity = new Product(dto.getId(), dto.getName()); this.applyProductChanges(entity, dto, owner); log.debug("Creating new product for org: {}, {}", entity, owner); List<Product> alternateVersions = this.ownerProductCurator.getProductsByVersions( owner, Collections.singletonMap(entity.getId(), entity.getEntityVersion())) .list(); for (Product alt : alternateVersions) { if (alt.equals(entity)) { this.ownerProductCurator.mapProductToOwner(alt, owner); return alt; } } entity = this.productCurator.create(entity); this.ownerProductCurator.mapProductToOwner(entity, owner); return entity; } @Inject  ProductManager(EntitlementCertificateGenerator entitlementCertGenerator,
        OwnerContentCurator ownerContentCurator, OwnerProductCurator ownerProductCurator,
        ProductCurator productCurator); Product createProduct(ProductDTO dto, Owner owner); @Transactional Product updateProduct(ProductDTO update, Owner owner, boolean regenerateEntitlementCerts); @Transactional @Traceable ImportResult<Product> importProducts(@TraceableParam("owner") Owner owner,
        Map<String, ? extends ProductInfo> productData, Map<String, Content> importedContent); void removeProduct(Owner owner, Product entity); @Transactional void removeAllProducts(Owner owner); void removeProducts(Owner owner, Collection<Product> products); void removeProductsByUuids(Owner owner, Collection<String> productUuids); static boolean isChangedBy(Product entity, ProductDTO dto); static boolean isChangedBy(Product entity, ProductInfo update);  }
@Test public void testCreateProduct() { Owner owner = this.createOwner("test-owner", "Test Owner"); ProductDTO dto = TestUtil.createProductDTO("p1", "prod1"); assertNull(this.ownerProductCurator.getProductById(owner, "p1")); Product output = this.productManager.createProduct(dto, owner); assertEquals(output, this.ownerProductCurator.getProductById(owner, "p1")); }
@Test public void testCreateProductThatAlreadyExists() { Owner owner = this.createOwner("test-owner", "Test Owner"); ProductDTO dto = TestUtil.createProductDTO("p1", "prod1"); Product output = this.productManager.createProduct(dto, owner); assertNotNull(output); assertEquals(output, this.ownerProductCurator.getProductById(owner, dto.getId())); assertThrows(IllegalStateException.class, () -> this.productManager.createProduct(dto, owner)); }
@Test public void testCreateProductMergeWithExisting() { Owner owner1 = this.createOwner("test-owner-1", "Test Owner 1"); Owner owner2 = this.createOwner("test-owner-2", "Test Owner 2"); Product product1 = TestUtil.createProduct("p1", "prod1"); Product product2 = this.createProduct("p1", "prod1", owner2); ProductDTO pdto = this.modelTranslator.translate(product1, ProductDTO.class); Product output = this.productManager.createProduct(pdto, owner1); assertEquals(output.getUuid(), product2.getUuid()); assertEquals(output, product2); assertTrue(this.ownerProductCurator.isProductMappedToOwner(output, owner1)); assertTrue(this.ownerProductCurator.isProductMappedToOwner(output, owner2)); }
@Test public void testCreateProductWithBranding() { Owner owner = this.createOwner("test-owner", "Test Owner"); ProductDTO dto = TestUtil.createProductDTO("p1", "prod1"); BrandingDTO brandingDTO = new BrandingDTO(); brandingDTO.setProductId("eng_prod_id"); brandingDTO.setName("brand_name"); brandingDTO.setType("OS"); dto.addBranding(brandingDTO); assertNull(this.ownerProductCurator.getProductById(owner, "p1")); Product output = this.productManager.createProduct(dto, owner); assertEquals(output, this.ownerProductCurator.getProductById(owner, "p1")); assertEquals(1, this.ownerProductCurator.getProductById(owner, "p1").getBranding().size()); }