Exemple #1
0
 public void mockFillProducts() {
   for (int i = 0; i < 10; i++) {
     Product product = new Product();
     product.setLabel("Product " + i);
     product.setCompany("Company " + i);
     product.setCategory("Category " + i);
     product.setPrice(65.2);
     genericDAO.persist(product);
   }
 }
 @Test
 public void addProduct() {
   Product product = new Product();
   product.setCompany("company1");
   product.setCategory("category1");
   product.setLabel("product1");
   product.setPrice(50.2);
   GenericDAO<Product> genericDAO = new GenericDAO<Product>(Product.class);
   genericDAO.persist(product);
   List<Product> products = genericDAO.findAll();
   Assert.assertNotNull(products);
   Product product1 = products.get(0);
   Assert.assertNotNull(genericDAO.findById(product1.getId()));
   Assert.assertEquals(product1.getLabel(), product.getLabel());
   genericDAO.delete(product1);
   Assert.assertEquals(0, genericDAO.findAll().size());
 }
Exemple #3
0
  @RolesAllowed("users")
  @POST
  @Path("submit")
  @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
  public Response editProduct(Product product) {
    Product productBean = null;
    if (product.getId() != 0) {
      productBean = (Product) genericDAO.findById(product.getId());
      if (productBean != null) {
        productBean.setCategory(product.getCategory());
        productBean.setPrice(product.getPrice());
        productBean.setLabel(product.getLabel());
        productBean.setCompany(product.getCompany());
      }
    }

    if (productBean == null && product != null) {
      genericDAO.persist(product);
    } else if (productBean != null) {
      genericDAO.merge(productBean);
    }
    return getProducts();
  }