@Test public void getOwerOfProduct() { User expectedUser = new User(); expectedUser.setUsername("koko"); Product product = dao.read(0L); Assert.assertNotNull(product); User user = dao.getOwnerOfProduct(product); // To be sure that both are equals... expectedUser.setUUID(user.getUUID()); Assert.assertNotNull(user); Assert.assertTrue( user.equals(expectedUser), "User " + user.getUsername() + "#" + user.getUUID() + " not expected (" + expectedUser.getUsername() + "#" + expectedUser.getUUID() + ")."); product = dao.read(6L); user = dao.getOwnerOfProduct(product); Assert.assertNull(user); }
@Test public void getProductByDownloadableFilename() { fr.gael.dhus.database.object.Collection validCollection; fr.gael.dhus.database.object.Collection invalidCollection; Product product; validCollection = new Collection(); validCollection.setUUID("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa3"); validCollection.setName("Japan"); invalidCollection = new Collection(); invalidCollection.setUUID("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa4"); invalidCollection.setName("China"); product = dao.getProductByDownloadableFilename("prod0", validCollection); Assert.assertNotNull(product); Assert.assertEquals(product.getId().intValue(), 0); product = dao.getProductByDownloadableFilename("prod6", validCollection); Assert.assertNull(product); product = dao.getProductByDownloadableFilename("prod0", invalidCollection); Assert.assertNull(product); product = dao.getProductByDownloadableFilename("prod6", null); Assert.assertNotNull(product); Assert.assertEquals(product.getId().intValue(), 6); product = dao.getProductByDownloadableFilename(null, null); Assert.assertNull(product); }
@Override public void create() { String identifier = "test-create-product"; String indexName = "index-name"; String indexCategory = "category"; String indexValue = "test"; MetadataIndex mi = new MetadataIndex(); mi.setName(indexName); mi.setCategory(indexCategory); mi.setQueryable(null); mi.setValue(indexValue); Product product = new Product(); product.setIdentifier(identifier); product.setLocked(false); product.setProcessed(true); product.setIndexes(Arrays.asList(mi)); try { product.setPath(new URL("file:/titi/tata")); } catch (MalformedURLException e) { Assert.fail(e.getMessage(), e); } Product createdProduct = dao.create(product); Assert.assertNotNull(createdProduct); Assert.assertEquals(dao.count(), (howMany() + 1)); Assert.assertEquals(createdProduct.getUuid(), product.getUuid()); List<MetadataIndex> indexes = createdProduct.getIndexes(); Assert.assertEquals(indexes.size(), 1); Assert.assertEquals(indexes.get(0), mi); }
@Override public void delete() { cancelListeners(getHibernateDao()); Long pid = Long.valueOf(6L); Product product = dao.read(pid); Assert.assertNotNull(product); Set<User> authorizedUsers = product.getAuthorizedUsers(); List<MetadataIndex> indexes = product.getIndexes(); Assert.assertNotNull(indexes); Assert.assertFalse(indexes.isEmpty()); Assert.assertFalse(authorizedUsers.isEmpty()); dao.delete(product); getHibernateDao().getSessionFactory().getCurrentSession().flush(); Assert.assertNull(dao.read(pid)); Assert.assertEquals(countElements("METADATA_INDEXES", pid), 0); Assert.assertEquals(countElements("CHECKSUMS", pid), 0); for (User user : authorizedUsers) { Assert.assertNotNull(udao.read(user.getUUID())); } }
@Test(groups = {"non-regression"}) public void testChecksumUpdate() throws MalformedURLException { Product product = new Product(); product.setPath(new URL("file:/product/path")); Download download = new Product.Download(); download.setPath("/no/path/file"); download.setSize(0L); download.setType("application/octet-stream"); download.setChecksums( Maps.newHashMap( ImmutableMap.of( "MD5", "54ABCDEF98765", "SHA-1", "9876FEDCBA1234"))); product.setDownload(download); // First create the defined product: try { product = dao.create(product); } catch (Exception e) { Assert.fail("Creation of product fails", e); } /** Clear/putAll feature testing */ product.getDownload().getChecksums().clear(); product .getDownload() .getChecksums() .putAll( Maps.newHashMap( ImmutableMap.of( "SHA-256", "4554ABCDEF98765", "SHA-512", "ABDEFFE9876FEDCBA1234"))); try { dao.update(product); } catch (Exception e) { Assert.fail("Modifying checksums with map clear/put fails", e); } /** Set feature testing */ product .getDownload() .setChecksums( Maps.newHashMap( ImmutableMap.of( "MD5", "54ABCDEF98765", "SHA-1", "9876FEDCBA1234"))); try { dao.update(product); } catch (Exception e) { Assert.fail("Modifying checksums with \"set\" fails", e); } /** Remove residuals for this test */ cancelListeners(getHibernateDao()); dao.delete(product); }
@Test public void testDeleteProductsByYandexId() { // Given assertThat(productDao.findByYandexId("10495462").size()).isEqualTo(2); // When productDao.deleteProductsByYandexId("10495462"); // Then assertThat(productDao.findByYandexId("10495462").size()).isEqualTo(0); }
@Test public void getProductByIngestionDate() { Date date = new Date(); Iterator<Product> products = dao.getProductsByIngestionDate(date); Assert.assertEquals(getIteratorSize(products), 4); Calendar calendar = Calendar.getInstance(); calendar.set(2014, Calendar.JUNE, 07, 0, 0, 0); date = calendar.getTime(); products = dao.getProductsByIngestionDate(date); Assert.assertEquals(getIteratorSize(products), 1); }
@Override public void first() { String hql = "FROM Product WHERE processed IS FALSE ORDER BY id DESC"; Product product = dao.first(hql); Assert.assertNotNull(product); Assert.assertEquals(product.getId().intValue(), 4); }
@Test public void exists() { boolean bool; String path; try { path = "file:/home/lambert/test/prod0"; bool = dao.exists(new URL(path)); Assert.assertTrue(bool, "product with path '" + path + "' should be" + "exists"); path = "file:/home/lambert/test/product999"; bool = dao.exists(new URL(path)); Assert.assertFalse(bool, "product with path '" + path + "' should be npt exists"); } catch (MalformedURLException e) { Assert.fail("Error: malformed URL", e); } }
@Test public void testSaveProduct() { List<Product> products = productDao.getProductList(); Product p = products.get(0); Double price = p.getPrice(); p.setPrice(200.12); productDao.saveProduct(p); List<Product> updatedProducts = productDao.getProductList(); Product p2 = updatedProducts.get(0); assertEquals(p2.getPrice(), 200.12, 0); p2.setPrice(price); productDao.saveProduct(p2); }
@Override public void read() { Product p = dao.read(6L); Assert.assertNotNull(p); Assert.assertEquals(p.getIdentifier(), "prod6"); Download dl = p.getDownload(); Map<String, String> checksums = dl.getChecksums(); Assert.assertEquals(checksums.get("MD5"), "abc"); }
@Test public void testFindByYandexId() { // When List<Product> products = productDao.findByYandexId("10495459"); // Then Product expectedProduct = new Product(); expectedProduct.setId(200L); Product expectedProduct1 = new Product(); expectedProduct1.setId(201L); assertThat(products).isEqualTo(Arrays.asList(expectedProduct, expectedProduct1)); }
@Test public void getNoCollectionProducts() { String uid = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0"; long expectedPid1 = 6; long expectedPid2 = 7; List<Product> products = dao.getNoCollectionProducts(emulateUser(uid, "koko")); Assert.assertEquals(products.size(), 2); Assert.assertEquals(products.get(0).getId().intValue(), expectedPid1); Assert.assertEquals(products.get(1).getId().intValue(), expectedPid2); }
@Test public void testFindByVendor() { // When List<Product> products = productDao.findByVendor(201L); // Then Product expectedProduct = new Product(); expectedProduct.setId(209L); Product expectedProduct1 = new Product(); expectedProduct1.setId(210L); assertThat(products).isEqualTo(Arrays.asList(expectedProduct, expectedProduct1)); }
@Test public void testGetOrderByProperties() { // When Iterable<String> properties = productDao.getOrderByProperties(); // Then Iterator<String> iterator = properties.iterator(); assertThat(iterator.next()).isEqualTo("vendor.id"); assertThat(iterator.next()).isEqualTo("type.id"); assertThat(iterator.next()).isEqualTo("name"); assertThat(iterator.next()).isEqualTo("color.id"); }
@Test public void getAuthorizedProduct() { String uid; List<Long> expected; List<Long> products; uid = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0"; expected = Arrays.asList(0L, 5L, 6L, 7L); products = dao.getAuthorizedProducts(uid); Assert.assertEquals(products.size(), expected.size()); Assert.assertTrue(products.containsAll(expected)); }
@Test public void getProductByPath() { URL valid = null; URL invalid = null; try { valid = new URL("file:/home/lambert/test/prod5"); invalid = new URL("file:/home/lambert/test/prod512"); } catch (MalformedURLException e) { Assert.fail("Malformed URL !", e); } Product product = dao.getProductByPath(valid); Assert.assertNotNull(product); Assert.assertEquals(product.getId().intValue(), 5); product = dao.getProductByPath(invalid); Assert.assertNull(product); product = dao.getProductByPath(null); Assert.assertNull(product); }
@Test public void testFindColors() { // When List<Color> products = productDao.findColors("10495461"); // Then Color expectedColor = new Color(); expectedColor.setId(200L); Color expectedColor1 = new Color(); expectedColor1.setId(201L); assertThat(products).isEqualTo(Arrays.asList(expectedColor, expectedColor1)); }
@Test public void getProductByUuid() { String valid = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa6"; String invalid = "bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb6"; User user = new User(); user.setUUID("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0"); user.setUsername("koko"); Product product = dao.getProductByUuid(valid); Assert.assertNotNull(product); Assert.assertEquals(product.getId().intValue(), 6); product = dao.getProductByUuid(invalid); Assert.assertNull(product); product = dao.getProductByUuid(valid); Assert.assertNotNull(product); Assert.assertEquals(product.getId().intValue(), 6); product = dao.getProductByUuid(null); Assert.assertNull(product); }
private int countElements(final String table, final Long pid) { return dao.getHibernateTemplate() .execute( new HibernateCallback<Integer>() { @Override public Integer doInHibernate(Session session) throws HibernateException, SQLException { String sql = "SELECT count(*) FROM " + table + " WHERE PRODUCT_ID = ?"; Query query = session.createSQLQuery(sql).setLong(0, pid); return ((BigInteger) query.uniqueResult()).intValue(); } }); }
@Override public void update() { String productIdentifier = "test-prod-7"; String indexName = "updatable"; Long pid = Long.valueOf(7); Product product = dao.read(pid); List<MetadataIndex> indexes = product.getIndexes(); product.setIdentifier(productIdentifier); for (MetadataIndex mi : indexes) { mi.setName(indexName); } dao.setIndexes(pid, indexes); dao.update(product); product = dao.read(pid); indexes = product.getIndexes(); Assert.assertNotNull(product); Assert.assertEquals(product.getIdentifier(), productIdentifier); for (MetadataIndex mi : indexes) { Assert.assertEquals(mi.getName(), indexName); } }
@Test public void testBuy() throws InsufficientProductsException { int availableQuantity = 30; System.out.println("Stubbing getAvailableProducts(product) to return " + availableQuantity); when(productDao.getAvailableProducts(product)).thenReturn(availableQuantity); System.out.println("Calling ProductService.buy(product," + purchaseQuantity + ")"); productService.buy(product, purchaseQuantity); System.out.println("Verifying ProductDao(product, " + purchaseQuantity + ") is called"); verify(productDao).orderProduct(product, purchaseQuantity); System.out.println("Verifying getAvailableProducts(product) is called at least once"); verify(productDao, atLeastOnce()).getAvailableProducts(product); System.out.println( "Verifying order of method calls on ProductDao: First call getAvailableProducts() followed by orderProduct()"); InOrder order = inOrder(productDao); order.verify(productDao).getAvailableProducts(product); order.verify(productDao).orderProduct(product, purchaseQuantity); }
@Test(expected = InsufficientProductsException.class) public void purchaseWithInsufficientAvailableQuantity() throws InsufficientProductsException { int availableQuantity = 3; System.out.println("Stubbing getAvailableProducts(product) to return " + availableQuantity); when(productDao.getAvailableProducts(product)).thenReturn(availableQuantity); try { System.out.println( "productService.buy(product" + purchaseQuantity + ") should throw InsufficientProductsException"); productService.buy(product, purchaseQuantity); } catch (InsufficientProductsException e) { System.out.println("InsufficientProductsException has been thrown"); verify(productDao, times(0)).orderProduct(product, purchaseQuantity); System.out.println("Verified orderProduct(product, " + purchaseQuantity + ") is not called"); throw e; } }
@Test public void testFindActiveByVendorOrderByVendorAndName() { // When List<Product> products = productDao.findActiveByVendorOrderByVendorAndName(200, Arrays.asList(200L, 201L)); // Then Product expectedProduct = new Product(); expectedProduct.setId(206L); Product expectedProduct1 = new Product(); expectedProduct1.setId(205L); Product expectedProduct2 = new Product(); expectedProduct2.setId(204L); Product expectedProduct3 = new Product(); expectedProduct3.setId(203L); assertThat(products) .isEqualTo( Arrays.asList(expectedProduct, expectedProduct1, expectedProduct2, expectedProduct3)); }
@Test public void isAuthorized() { Assert.assertTrue(dao.isAuthorized("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0", 0)); Assert.assertTrue(dao.isAuthorized("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1", 0)); Assert.assertTrue(dao.isAuthorized("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa2", 0)); Assert.assertTrue(dao.isAuthorized("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa3", 0)); Assert.assertTrue(dao.isAuthorized("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa0", 1)); Assert.assertTrue(dao.isAuthorized("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa2", 1)); Assert.assertTrue(dao.isAuthorized("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa3", 1)); // Unknown user Assert.assertFalse(dao.isAuthorized("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1000", 1)); // Unknown product Assert.assertFalse(dao.isAuthorized("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa1", 1000)); }
@Override public void scroll() { String hql = "WHERE processed IS TRUE"; Iterator<Product> it = dao.scroll(hql, -1, -1).iterator(); Assert.assertTrue(CheckIterator.checkElementNumber(it, 4)); }
/** * Convenient call for {@link AbstractDao#update(Object)}. Entity must attached to an entity * context. */ public void update() { if (myDao == null) { throw new DaoException("Entity is detached from DAO context"); } myDao.update(this); }
@Test public void count() { Assert.assertEquals(dao.count(null, "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa3"), 1); }
/** * Convenient call for {@link AbstractDao#refresh(Object)}. Entity must attached to an entity * context. */ public void refresh() { if (myDao == null) { throw new DaoException("Entity is detached from DAO context"); } myDao.refresh(this); }
@Transactional(readOnly = true) public Page findPage(ProductQuery query) { return productDao.findPage(query); }