private void mockProductImport(Owner owner, final Map<String, Product> products) { when(productManager.importProducts(eq(owner), any(Map.class), any(Map.class))) .thenAnswer( new Answer<ImportResult<Product>>() { @Override public ImportResult<Product> answer(InvocationOnMock invocation) throws Throwable { Object[] args = invocation.getArguments(); Map<String, ProductData> productData = (Map<String, ProductData>) args[1]; ImportResult<Product> importResult = new ImportResult<Product>(); Map<String, Product> output = importResult.getCreatedEntities(); if (productData != null) { for (String pid : productData.keySet()) { Product product = products.get(pid); if (product != null) { output.put(product.getId(), product); } } } return importResult; } }); }
private void mockUpdateProduct(final Product product, Owner owner) { when(productManager.updateProduct(any(ProductData.class), eq(owner), anyBoolean())) .thenAnswer( new Answer<Product>() { @Override public Product answer(InvocationOnMock invocation) throws Throwable { Object[] args = invocation.getArguments(); ProductData pdata = (ProductData) args[0]; return (product.getId().equals(pdata.getId())) ? product : null; } }); }
@Test public void testCreatedDevSkuWithNoSla() { Owner owner = TestUtil.createOwner("o"); List<ProductData> devProdDTOs = new ArrayList<ProductData>(); final Product p1 = TestUtil.createProduct("dev-product", "Dev Product"); devProdDTOs.add(p1.toDTO()); Consumer devSystem = TestUtil.createConsumer(owner); devSystem.setFact("dev_sku", p1.getId()); when(productAdapter.getProductsByIds(eq(owner), any(List.class))).thenReturn(devProdDTOs); mockUpdateProduct(p1, owner); this.mockContentImport(owner, Collections.<String, Content>emptyMap()); when(productManager.importProducts(eq(owner), any(Map.class), any(Map.class))) .thenAnswer( new Answer<ImportResult<Product>>() { @Override public ImportResult<Product> answer(InvocationOnMock invocation) throws Throwable { Object[] args = invocation.getArguments(); Map<String, ProductData> productData = (Map<String, ProductData>) args[1]; ImportResult<Product> importResult = new ImportResult<Product>(); Map<String, Product> output = importResult.getCreatedEntities(); // We need to copy the attributes from the product data to the product to // simulate a proper update. for (ProductData pdata : productData.values()) { if (pdata != null) { if (p1.getId().equals(pdata.getId())) { p1.clearAttributes(); if (pdata.getAttributes() != null) { for (ProductAttributeData attrib : pdata.getAttributes()) { p1.setAttribute(attrib.getName(), attrib.getValue()); } } output.put(p1.getId(), p1); } else { Product product = new Product(pdata.getId(), pdata.getName()); // Do we care about this product? Probably not. output.put(product.getId(), product); } } } return importResult; } }); Pool created = entitler.assembleDevPool(devSystem, devSystem.getFact("dev_sku")); assertEquals(entitler.DEFAULT_DEV_SLA, created.getProduct().getAttributeValue("support_level")); }