ImagesBulkImportServiceImpl implements ImportService { @Override public BulkImportResult doImport(final JobContext context) { final JobStatusListener statusListener = context.getListener(); final Set<String> importedFiles = context.getAttribute(JobContextKeys.IMPORT_FILE_SET); final String fileName = context.getAttribute(JobContextKeys.IMPORT_FILE); final String imageVaultRootDirectory = context.getAttribute(JobContextKeys.IMAGE_VAULT_PATH); final CsvImportDescriptor importDescriptor = context.getAttribute(JobContextKeys.IMPORT_DESCRIPTOR); final String imageImportDescriptorName = context.getAttribute(JobContextKeys.IMPORT_DESCRIPTOR_NAME); final String regExp = importDescriptor.getImportFileDescriptor().getFileNameMask(); statusListener.notifyMessage( "start images import with {} path using {} and file mask {}", importDescriptor.getImportDirectory(), imageImportDescriptorName, regExp ); File[] files = ImportFileUtils.getFilesToImport(importDescriptor, fileName); if (files != null) { statusListener.notifyMessage("found {} images to import", files.length); int count = 0; int total = files.length; for (File file : files) { doImport(file, importDescriptor, statusListener, importedFiles, imageVaultRootDirectory); statusListener.notifyPing("Processed {} of {} images", ++count, total); } } return BulkImportResult.OK; } ImagesBulkImportServiceImpl(final FederationFacade federationFacade,
                                       final ImageService imageService,
                                       final ImageImportDomainObjectStrategy[] strategies); @Override BulkImportResult doImport(final JobContext context);  }
@Test public void testDoImportSkuOnly() throws Exception { getTx().execute(new TransactionCallbackWithoutResult() { @Override public void doInTransactionWithoutResult(TransactionStatus status) { try { ProductService productService = createContext().getBean("productService", ProductService.class); Product product = productService.getProductById(10000L, true); assertNotNull(product); assertNull(product.getAttributeByCode("IMAGE2")); final XStreamProvider<ImportDescriptor> xml = createContext().getBean("importCsvDescriptorXStreamProvider", XStreamProvider.class); final ImportDescriptor descriptor = xml.fromXML(new FileInputStream(new File("src/test/resources/import/img/productimages.xml"))); descriptor.setImportDirectory(new File("src/test/resources/import/img/").getAbsolutePath()); final JobContext context = mockery.mock(JobContext.class, "context"); final JobStatusListener listener = mockery.mock(JobStatusListener.class, "listener"); final Set<String> imported = new HashSet<>(); mockery.checking(new Expectations() {{ allowing(context).getListener(); will(returnValue(listener)); allowing(context).getAttribute(JobContextKeys.IMPORT_FILE_SET); will(returnValue(imported)); allowing(context).getAttribute(JobContextKeys.IMPORT_FILE); will(returnValue(new File("src/test/resources/import/img/im-image-file_SOBOT-BEER_c.jpeg").getAbsolutePath())); allowing(context).getAttribute(JobContextKeys.IMAGE_VAULT_PATH); will(returnValue("file:" + File.separator + File.separator + "target" + File.separator)); allowing(context).getAttribute(JobContextKeys.IMPORT_DESCRIPTOR_NAME); will(returnValue("productimages.xml")); allowing(context).getAttribute(JobContextKeys.IMPORT_DESCRIPTOR); will(returnValue(descriptor)); allowing(listener).notifyMessage(with(any(String.class)), with(any(Object[].class))); allowing(listener).notifyPing("Processed {} of {} images", 1, 1); allowing(listener).count(with(any(String.class))); }}); ImportService service = (ImportService) createContext().getBean("imagesBulkImportService"); service.doImport(context); clearCache(); product = productService.getProductById(10000L, true); assertNotNull(product); final GenericDAO<AttrValueEntityProduct, Long> attrValueEntityProductDao = createContext().getBean("attrValueEntityProductDao", GenericDAO.class); assertNull("SOBOT-BEER is sku specific image", product.getAttributeByCode("IMAGE2")); boolean skuFound = false; for (ProductSku productSku : product.getSku()) { if (productSku.getCode().equals("SOBOT-BEER")) { skuFound = true; assertNotNull(productSku.getAttributeByCode("IMAGE2")); assertEquals("im-image-file_SOBOT-BEER_c.jpeg", productSku.getAttributeByCode("IMAGE2").getVal()); attrValueEntityProductDao.delete(productSku.getAttributeByCode("IMAGE2")); } } assertTrue("SKU not found", skuFound); status.setRollbackOnly(); mockery.assertIsSatisfied(); } catch (Exception exp) { fail(exp.getMessage()); } } }); }
@Test public void testDoImportProductAndSku() throws Exception { getTx().execute(new TransactionCallbackWithoutResult() { @Override public void doInTransactionWithoutResult(TransactionStatus status) { try { ProductService productService = createContext().getBean("productService", ProductService.class); Product product = productService.getProductById(10000L, true); assertNotNull(product); assertNull(product.getAttributeByCode("IMAGE2")); final XStreamProvider<ImportDescriptor> xml = createContext().getBean("importCsvDescriptorXStreamProvider", XStreamProvider.class); final ImportDescriptor descriptor = xml.fromXML(new FileInputStream(new File("src/test/resources/import/img/productimages.xml"))); descriptor.setImportDirectory(new File("src/test/resources/import/img/").getAbsolutePath()); final JobContext context = mockery.mock(JobContext.class, "context"); final JobStatusListener listener = mockery.mock(JobStatusListener.class, "listener"); final Set<String> imported = new HashSet<>(); mockery.checking(new Expectations() {{ allowing(context).getListener(); will(returnValue(listener)); allowing(context).getAttribute(JobContextKeys.IMPORT_FILE_SET); will(returnValue(imported)); allowing(context).getAttribute(JobContextKeys.IMPORT_FILE); will(returnValue(new File("src/test/resources/import/img/im-image-file_BENDER_c.jpeg").getAbsolutePath())); allowing(context).getAttribute(JobContextKeys.IMAGE_VAULT_PATH); will(returnValue("file:" + File.separator + File.separator + "target" + File.separator)); allowing(context).getAttribute(JobContextKeys.IMPORT_DESCRIPTOR_NAME); will(returnValue("productimages.xml")); allowing(context).getAttribute(JobContextKeys.IMPORT_DESCRIPTOR); will(returnValue(descriptor)); allowing(listener).notifyMessage(with(any(String.class)), with(any(Object[].class))); allowing(listener).notifyPing("Processed {} of {} images", 1, 1); allowing(listener).count(with(any(String.class))); }}); ImportService service = (ImportService) createContext().getBean("imagesBulkImportService"); service.doImport(context); clearCache(); product = productService.getProductById(9999L, true); assertNotNull(product); final GenericDAO<AttrValueEntityProduct, Long> attrValueEntityProductDao = createContext().getBean("attrValueEntityProductDao", GenericDAO.class); assertNotNull("BENDER is single sku", product.getAttributeByCode("IMAGE2")); assertEquals("im-image-file_BENDER_c.jpeg", product.getAttributeByCode("IMAGE2").getVal()); boolean skuFound = false; for (ProductSku productSku : product.getSku()) { if (productSku.getCode().equals("BENDER")) { skuFound = true; assertNotNull(productSku.getAttributeByCode("IMAGE2")); assertEquals("im-image-file_BENDER_c.jpeg", productSku.getAttributeByCode("IMAGE2").getVal()); attrValueEntityProductDao.delete(productSku.getAttributeByCode("IMAGE2")); } } assertTrue("SKU not found", skuFound); status.setRollbackOnly(); mockery.assertIsSatisfied(); } catch (Exception exp) { fail(exp.getMessage()); } } }); }
@Test public void testDoImportProductAndSkuCodeOnly() throws Exception { getTx().execute(new TransactionCallbackWithoutResult() { @Override public void doInTransactionWithoutResult(TransactionStatus status) { try { ProductService productService = createContext().getBean("productService", ProductService.class); Product product = productService.getProductById(10000L, true); assertNotNull(product); assertFalse("BENDER.jpeg".equals(product.getAttributeValueByCode("IMAGE0"))); final XStreamProvider<ImportDescriptor> xml = createContext().getBean("importCsvDescriptorXStreamProvider", XStreamProvider.class); final ImportDescriptor descriptor = xml.fromXML(new FileInputStream(new File("src/test/resources/import/img/productimages_simple.xml"))); descriptor.setImportDirectory(new File("src/test/resources/import/img/").getAbsolutePath()); final JobContext context = mockery.mock(JobContext.class, "context"); final JobStatusListener listener = mockery.mock(JobStatusListener.class, "listener"); final Set<String> imported = new HashSet<>(); mockery.checking(new Expectations() {{ allowing(context).getListener(); will(returnValue(listener)); allowing(context).getAttribute(JobContextKeys.IMPORT_FILE_SET); will(returnValue(imported)); allowing(context).getAttribute(JobContextKeys.IMPORT_FILE); will(returnValue(new File("src/test/resources/import/img/BENDER.jpeg").getAbsolutePath())); allowing(context).getAttribute(JobContextKeys.IMAGE_VAULT_PATH); will(returnValue("file:" + File.separator + File.separator + "target" + File.separator)); allowing(context).getAttribute(JobContextKeys.IMPORT_DESCRIPTOR_NAME); will(returnValue("productimages_simple.xml")); allowing(context).getAttribute(JobContextKeys.IMPORT_DESCRIPTOR); will(returnValue(descriptor)); allowing(listener).notifyMessage(with(any(String.class)), with(any(Object[].class))); allowing(listener).notifyPing("Processed {} of {} images", 1, 1); allowing(listener).count(with(any(String.class))); }}); ImportService service = (ImportService) createContext().getBean("imagesBulkImportService"); service.doImport(context); clearCache(); product = productService.getProductById(9999L, true); assertNotNull(product); final GenericDAO<AttrValueEntityProduct, Long> attrValueEntityProductDao = createContext().getBean("attrValueEntityProductDao", GenericDAO.class); assertNotNull("BENDER is single sku", product.getAttributeByCode("IMAGE0")); assertEquals("BENDER.jpeg", product.getAttributeByCode("IMAGE0").getVal()); boolean skuFound = false; for (ProductSku productSku : product.getSku()) { if (productSku.getCode().equals("BENDER")) { skuFound = true; assertNotNull(productSku.getAttributeByCode("IMAGE0")); assertEquals("BENDER.jpeg", productSku.getAttributeByCode("IMAGE0").getVal()); attrValueEntityProductDao.delete(productSku.getAttributeByCode("IMAGE0")); } } assertTrue("SKU not found", skuFound); status.setRollbackOnly(); mockery.assertIsSatisfied(); } catch (Exception exp) { fail(exp.getMessage()); } } }); }