package fr.mch.mdo.restaurant.dao.products.hibernate; import java.math.BigDecimal; import java.util.ArrayList; import java.util.HashMap; import java.util.HashSet; import java.util.List; import java.util.Map; import java.util.Set; import junit.framework.Test; import junit.framework.TestSuite; import fr.mch.mdo.restaurant.beans.IMdoBean; import fr.mch.mdo.restaurant.dao.DaoServicesFactory; import fr.mch.mdo.restaurant.dao.IDaoServices; import fr.mch.mdo.restaurant.dao.beans.Category; import fr.mch.mdo.restaurant.dao.beans.Product; import fr.mch.mdo.restaurant.dao.beans.ProductCategory; import fr.mch.mdo.restaurant.dao.beans.ProductLabel; import fr.mch.mdo.restaurant.dao.beans.ProductLanguage; import fr.mch.mdo.restaurant.dao.beans.Restaurant; import fr.mch.mdo.restaurant.dao.beans.ValueAddedTax; import fr.mch.mdo.restaurant.dao.hibernate.DefaultDaoServicesTestCase; import fr.mch.mdo.restaurant.dao.products.IProductsDao; import fr.mch.mdo.restaurant.exception.MdoException; import fr.mch.mdo.test.MdoTestCase; public class DefaultProductsDaoTest extends DefaultDaoServicesTestCase { /** * Create the test case * * @param testName * name of the test case */ public DefaultProductsDaoTest(String testName) { super(testName); } /** * @return the suite of tests being tested */ public static Test suite() { return new TestSuite(DefaultProductsDaoTest.class); } protected IDaoServices getInstance() { return DefaultProductsDao.getInstance(); } protected IMdoBean createNewBean() { // Use the existing data in database String code = "A"; Restaurant restaurant = new Restaurant(); restaurant.setId(1L); // Used for Restaurant.equals restaurant.setReference(DefaultDaoServicesTestCase.RESTAURANT_FIRST_REFERENCE); ValueAddedTax vat = new ValueAddedTax(); vat.setId(1L); BigDecimal price = new BigDecimal(12); Set<ProductCategory> categories = null; Map<Long, String> labels = null; return createNewBean(restaurant, vat, code, price, categories, labels); } protected List<IMdoBean> createListBeans() { List<IMdoBean> list = new ArrayList<IMdoBean>(); // Use the existing data in database String code = "B"; Restaurant restaurant = new Restaurant(); restaurant.setId(1L); // Used for Restaurant.equals restaurant.setReference(DefaultDaoServicesTestCase.RESTAURANT_FIRST_REFERENCE); ValueAddedTax vat = new ValueAddedTax(); vat.setId(1L); BigDecimal price = new BigDecimal(12); Set<ProductCategory> categories = null; Map<Long, String> labels = new HashMap<Long, String>(); Long localeId = 1L; String label = "Boulette de porc à la vapeur"; labels.put(localeId, label); list.add(createNewBean(restaurant, vat, code, price, categories, labels)); return list; } public void testGetInstance() { assertTrue(this.getInstance() instanceof IProductsDao); assertTrue(this.getInstance() instanceof DefaultProductsDao); } @Override public void doFindByUniqueKey() { // 1 restaurant was created at HSQLDB startup Long restaurantId = 1L; // 11 code was created at HSQLDB startup String code = "11"; try { IMdoBean bean = this.getInstance().findByUniqueKey(new Object[] { restaurantId, code }); assertTrue("IMdoBean must be instance of " + Product.class, bean instanceof Product); Product castedBean = (Product) bean; assertEquals("Product code must be equals to unique key", code, castedBean.getCode()); assertNotNull("Product Restaurant must not be null", castedBean.getRestaurant()); assertEquals("Product Restaurant must be equals to unique key", restaurantId, castedBean.getRestaurant().getId()); assertEquals("Product Restaurant Reference must be equals to unique key", DefaultDaoServicesTestCase.RESTAURANT_FIRST_REFERENCE, castedBean.getRestaurant() .getReference()); assertFalse("Product must not be deleted", castedBean.isDeleted()); IProductsDao productsDao = (IProductsDao) this.getInstance(); bean = productsDao.find(DefaultDaoServicesTestCase.RESTAURANT_FIRST_REFERENCE, code); assertTrue("IMdoBean must be instance of " + Product.class, bean instanceof Product); castedBean = (Product) bean; assertEquals("Product code must be equals to unique key", code, castedBean.getCode()); assertNotNull("Product Restaurant must not be null", castedBean.getRestaurant()); assertEquals("Product Restaurant must be equals to unique key", restaurantId, castedBean.getRestaurant().getId()); assertEquals("Product Restaurant Reference must be equals to unique key", DefaultDaoServicesTestCase.RESTAURANT_FIRST_REFERENCE, castedBean.getRestaurant() .getReference()); assertFalse("Product must not be deleted", castedBean.isDeleted()); } catch (Exception e) { fail(MdoTestCase.DEFAULT_FAILED_MESSAGE + ": " + e.getMessage()); } } @Override public void doUpdate() { IMdoBean newBean = null; String code = "C"; // Use the existing data in database Restaurant restaurant = null; try { restaurant = (Restaurant) DaoServicesFactory.getRestaurantsDao().findByPrimaryKey(1L); } catch (MdoException e) { fail("Could not found the restaurant."); } assertNotNull("Restaurant must not be null", restaurant); ValueAddedTax vat = new ValueAddedTax(); try { vat = (ValueAddedTax) DaoServicesFactory.getValueAddedTaxesDao().findByPrimaryKey(1L); } catch (MdoException e) { fail("Could not found the vat."); } assertNotNull("VAT must not be null", vat); BigDecimal price = new BigDecimal(12); Set<ProductCategory> categories = new HashSet<ProductCategory>(); Map<Long, String> labels = new HashMap<Long, String>(); Long localeId = 1L; String label = "Soupe Phnom Penh"; labels.put(localeId, label); localeId = 2L; label = "Soupe Phnom Penh ES"; labels.put(localeId, label); newBean = createNewBean(restaurant, vat, code, price, categories, labels); ProductCategory productCategory = new ProductCategory(); Category category = new Category(); category.setId(1L); productCategory.setCategory(category); BigDecimal quantity = new BigDecimal(1.7); productCategory.setQuantity(quantity); ((Product) newBean).addCategory(productCategory); try { // Create new bean to be updated IMdoBean beanToBeUpdated = this.getInstance().insert(newBean); assertTrue("IMdoBean must be instance of " + Product.class, beanToBeUpdated instanceof Product); Product castedBean = (Product) beanToBeUpdated; assertNotNull("Product code must not be null", castedBean.getCode()); assertEquals("Product name must be equals to the inserted value", code.toString(), castedBean.getCode().toString()); assertNotNull("Product labels must not be null", castedBean.getLabels()); assertEquals("Check Product labels size", labels.size(), castedBean.getLabels().size()); assertNotNull("Product categories must not be null", castedBean.getCategories()); assertEquals("Check Product categories size", categories.size(), castedBean.getCategories().size()); assertFalse("Product must not be deleted", castedBean.isDeleted()); // Update the created bean productCategory = new ProductCategory(); category = new Category(); category.setId(2L); productCategory.setCategory(category); quantity = new BigDecimal(3.57); productCategory.setQuantity(quantity); castedBean.addCategory(productCategory); labels.clear(); localeId = 1L; label = "O2"; labels.put(localeId, label); castedBean.setLabels(labels); castedBean.setDeleted(true); this.getInstance().update(castedBean); // Reload the modified bean Product updatedBean = new Product(); updatedBean.setId(castedBean.getId()); this.getInstance().load(updatedBean); assertNotNull("Product code must not be null", updatedBean.getCode()); assertEquals("Product name must be equals to the updated value", code.toString(), updatedBean.getCode().toString()); assertNotNull("Product labels must not be null", updatedBean.getLabels()); assertEquals("Check Product labels size", labels.size(), updatedBean.getLabels().size()); assertNotNull("Product categories must not be null", castedBean.getCategories()); assertEquals("Check Product categories size", categories.size(), castedBean.getCategories().size()); assertTrue("Product must be deleted", castedBean.isDeleted()); this.getInstance().delete(updatedBean); } catch (Exception e) { fail(MdoTestCase.DEFAULT_FAILED_MESSAGE + ": " + e.getMessage()); } } @Override public void doUpdateFieldsAndDeleteByKeysSpecific() { IMdoBean newBean = null; String code = "C"; // Use the existing data in database Restaurant restaurant = null; try { restaurant = (Restaurant) DaoServicesFactory.getRestaurantsDao().findByPrimaryKey(1L); } catch (MdoException e) { fail("Could not found the restaurant."); } assertNotNull("Restaurant must not be null", restaurant); ValueAddedTax vat = new ValueAddedTax(); try { vat = (ValueAddedTax) DaoServicesFactory.getValueAddedTaxesDao().findByPrimaryKey(1L); } catch (MdoException e) { fail("Could not found the vat."); } assertNotNull("VAT must not be null", vat); BigDecimal price = new BigDecimal(12); Set<ProductCategory> categories = new HashSet<ProductCategory>(); Map<Long, String> labels = new HashMap<Long, String>(); Long localeId = 1L; String label = "Soupe Phnom Penh"; labels.put(localeId, label); localeId = 2L; label = "Soupe Phnom Penh ES"; labels.put(localeId, label); newBean = createNewBean(restaurant, vat, code, price, categories, labels); ProductCategory productCategory = new ProductCategory(); Category category = new Category(); category.setId(1L); productCategory.setCategory(category); BigDecimal quantity = new BigDecimal(1.7); productCategory.setQuantity(quantity); ((Product) newBean).addCategory(productCategory); try { // Create new bean to be updated IMdoBean beanToBeUpdated = this.getInstance().insert(newBean); assertTrue("IMdoBean must be instance of " + Product.class, beanToBeUpdated instanceof Product); Product castedBean = (Product) beanToBeUpdated; assertNotNull("Product code must not be null", castedBean.getCode()); assertEquals("Product name must be equals to the inserted value", code.toString(), castedBean.getCode().toString()); assertNotNull("Product labels must not be null", castedBean.getLabels()); assertEquals("Check Product labels size", labels.size(), castedBean.getLabels().size()); assertNotNull("Product categories must not be null", castedBean.getCategories()); assertEquals("Check Product categories size", categories.size(), castedBean.getCategories().size()); assertFalse("Product must not be deleted", castedBean.isDeleted()); // Update the created bean Map<String, Object> fields = new HashMap<String, Object>(); Map<String, Object> keys = new HashMap<String, Object>(); castedBean.setCode("C#"); castedBean.setColorRGB("RGB"); castedBean.setPrice(BigDecimal.ZERO); fields.put("code", castedBean.getCode()); fields.put("colorRGB", castedBean.getColorRGB()); fields.put("price", castedBean.getPrice()); keys.put("id", castedBean.getId()); this.getInstance().updateFieldsByKeys(fields, keys); // Reload the modified bean Product updatedBean = (Product) createNewBean(); updatedBean.setId(castedBean.getId()); this.getInstance().load(updatedBean); assertEquals("Check updated fields ", castedBean.getCode(), updatedBean.getCode()); assertEquals("Check updated fields ", castedBean.getColorRGB(), updatedBean.getColorRGB()); assertEquals("Check updated fields ", castedBean.getPrice(), updatedBean.getPrice()); // Delete the bean by keys // Take the fields as keys try { super.doDeleteByKeysSpecific(updatedBean, keys, true); } catch (Exception e) { // We Have to delete following tables in the following order deleting the table t_product. // 1) t_product_category // 2) t_product_language // 3) t_order_line // 4) t_product_sold Object parentId = keys.get("id"); Map<String, Object> childrenKeys = new HashMap<String, Object>(); childrenKeys.put("product.id", parentId); super.doDeleteByKeysSpecific(ProductCategory.class, childrenKeys); childrenKeys = new HashMap<String, Object>(); childrenKeys.put("parentId", parentId); super.doDeleteByKeysSpecific(ProductLanguage.class, childrenKeys); super.doDeleteByKeysSpecific(updatedBean, keys); } } catch (Exception e) { fail(MdoTestCase.DEFAULT_FAILED_MESSAGE + " " + e.getMessage()); } } private IMdoBean createNewBean(Restaurant restaurant, ValueAddedTax vat, String code, BigDecimal price, Set<ProductCategory> categories, Map<Long, String> labels) { Product newBean = new Product(); newBean.setCode(code); newBean.setPrice(price); newBean.setRestaurant(restaurant); newBean.setVat(vat); newBean.setCategories(categories); newBean.setLabels(labels); return newBean; } public void testFindAllByPrefixCode() { // 1 restaurant was created at HSQLDB startup Long restaurantId = 1L; // 11 code was created at HSQLDB startup String prefixCode = "1"; try { IProductsDao productsDao = (IProductsDao) this.getInstance(); List<IMdoBean> list = productsDao.findAllByPrefixCode(restaurantId, prefixCode + "%"); assertNotNull("List<IMdoBean> must not be null", list); assertFalse("List<IMdoBean> must not be empty", list.isEmpty()); IMdoBean bean = list.get(0); assertTrue("IMdoBean must be instance of " + Product.class, bean instanceof Product); Product castedBean = (Product) bean; assertTrue("Check Product code ", castedBean.getCode().contains(prefixCode)); assertNotNull("Product Restaurant must not be null", castedBean.getRestaurant()); assertEquals("Product Restaurant must be equals to unique key", restaurantId, castedBean.getRestaurant().getId()); assertEquals("Product Restaurant Reference must be equals to unique key", DefaultDaoServicesTestCase.RESTAURANT_FIRST_REFERENCE, castedBean.getRestaurant() .getReference()); assertFalse("Product must not be deleted", castedBean.isDeleted()); } catch (Exception e) { fail(MdoTestCase.DEFAULT_FAILED_MESSAGE + ": " + e.getMessage()); } } public void testFindByCode() { // 1 restaurant was created at HSQLDB startup Long restaurantId = 1L; // 11 code was created at HSQLDB startup String code = "11"; try { IProductsDao productsDao = (IProductsDao) this.getInstance(); IMdoBean bean = productsDao.find(DefaultDaoServicesTestCase.RESTAURANT_FIRST_REFERENCE, code); assertTrue("IMdoBean must be instance of " + Product.class, bean instanceof Product); Product castedBean = (Product) bean; assertEquals("Product code must be equals to unique key", code, castedBean.getCode()); assertNotNull("Product Restaurant must not be null", castedBean.getRestaurant()); assertEquals("Product Restaurant must be equals to unique key", restaurantId, castedBean.getRestaurant().getId()); assertEquals("Product Restaurant Reference must be equals to unique key", DefaultDaoServicesTestCase.RESTAURANT_FIRST_REFERENCE, castedBean.getRestaurant() .getReference()); assertFalse("Product must not be deleted", castedBean.isDeleted()); bean = productsDao.find(restaurantId, code); assertTrue("IMdoBean must be instance of " + Product.class, bean instanceof Product); castedBean = (Product) bean; assertEquals("Product code must be equals to unique key", code, castedBean.getCode()); assertNotNull("Product Restaurant must not be null", castedBean.getRestaurant()); assertEquals("Product Restaurant must be equals to unique key", restaurantId, castedBean.getRestaurant().getId()); assertEquals("Product Restaurant Reference must be equals to unique key", DefaultDaoServicesTestCase.RESTAURANT_FIRST_REFERENCE, castedBean.getRestaurant() .getReference()); assertFalse("Product must not be deleted", castedBean.isDeleted()); } catch (Exception e) { fail(MdoTestCase.DEFAULT_FAILED_MESSAGE + ": " + e.getMessage()); } } /** * Test the findAllByRestaurant method. */ public void testFindAllByRestaurant() { // 1 restaurant was created at HSQLDB startup Long restaurantId = 1L; try { IProductsDao productsDao = (IProductsDao) this.getInstance(); List<IMdoBean> list = productsDao.findAllByRestaurant(restaurantId); assertNotNull("List of IMdoBean must not be null", list); assertFalse("List of IMdoBean must not be empty", list.isEmpty()); } catch (Exception e) { fail(MdoTestCase.DEFAULT_FAILED_MESSAGE + ": " + e.getMessage()); } } public void testFindCodesByPrefixCode() { // 1 restaurant was created at HSQLDB startup Long restaurantId = 1L; String prefixProductCode = "1"; try { IProductsDao productsDao = (IProductsDao) this.getInstance(); Map<Long, String> map = productsDao.findCodesByPrefixCode(restaurantId, prefixProductCode); assertNotNull("Map of product code must not be null", map); assertFalse("Map of product code must not be empty", map.isEmpty()); } catch (Exception e) { fail(MdoTestCase.DEFAULT_FAILED_MESSAGE + ": " + e.getMessage()); } } public void testFindProductByCode() { Long restaurantId = 1L; String prefixProductCode = "11"; try { IProductsDao productsDao = (IProductsDao) this.getInstance(); Product product = (Product) productsDao.find(restaurantId, prefixProductCode); assertNotNull("Product must not be null", product); Long locId = 1L; ProductLabel productLabel = (ProductLabel) productsDao.find(restaurantId, prefixProductCode, locId); assertNotNull("Product must not be null", productLabel); assertNotNull("Product Label must not be null", productLabel.getLabel()); assertNotNull("Product VAT must not be null", productLabel.getVat()); assertNotNull("Product VAT must not be null", productLabel.getVat().getId()); locId = 3L; productLabel = (ProductLabel) productsDao.find(restaurantId, prefixProductCode, locId); assertNotNull("Product must not be null", productLabel); assertNull("Product Label must be null", productLabel.getLabel()); assertNotNull("Product VAT must not be null", productLabel.getVat()); assertNotNull("Product VAT must not be null", productLabel.getVat().getId()); } catch (Exception e) { fail(MdoTestCase.DEFAULT_FAILED_MESSAGE + ": " + e.getMessage()); } } public void testDeleteProductCategories() { // Use the existing data in database String code = "ABC"; Restaurant restaurant = new Restaurant(); restaurant.setId(1L); // Used for Restaurant.equals restaurant.setReference(DefaultDaoServicesTestCase.RESTAURANT_FIRST_REFERENCE); ValueAddedTax vat = new ValueAddedTax(); vat.setId(1L); BigDecimal price = new BigDecimal(12); Set<ProductCategory> categories = new HashSet<ProductCategory>(); Map<Long, String> labels = new HashMap<Long, String>(); Long localeId = 1L; String label = "Boulette de porc à la vapeur"; labels.put(localeId, label); Product product = (Product) createNewBean(restaurant, vat, code, price, categories, labels); ProductCategory productCategory = new ProductCategory(); Category category = new Category(); category.setId(1L); productCategory.setCategory(category); BigDecimal quantity = new BigDecimal(1.7); productCategory.setQuantity(quantity); product.addCategory(productCategory); Product inserted = null; try { inserted = (Product) this.getInstance().insert(product); } catch (MdoException e) { fail(MdoTestCase.DEFAULT_FAILED_MESSAGE + ": " + e.getMessage()); } Product bean = new Product(); bean.setId(inserted.getId()); try { this.getInstance().delete(bean); } catch (MdoException e) { fail(MdoTestCase.DEFAULT_FAILED_MESSAGE + ": " + e.getMessage()); } } }