/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.storage.mywarehouse;

import com.storage.mywarehouse.Entity.Product;
import com.storage.mywarehouse.Hibernate.NewHibernateUtil;
import com.storage.mywarehouse.Util;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.transaction.Transactional;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.MatchMode;
import org.hibernate.criterion.Restrictions;
import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import static org.junit.Assert.*;

/**
 *
 * @author veruz
 */
public class UtilsImportProductsFromCsvTest {

    private Session session = null;
    private File file = null;
    public UtilsImportProductsFromCsvTest() {
    }
    
    @BeforeClass
    public static void setUpClass() {
    }
    
    @AfterClass
    public static void tearDownClass() {
    }
    
    @Before
    public void setUp() {
        ClassLoader classLoader = getClass().getClassLoader();
        
        file = new File("src/test/resources/products.csv");
//        file = new File(classLoader.getResource("products.csv").getFile());
        session = NewHibernateUtil.getSessionFactory().openSession();
    }
    
    @After
    public void tearDown() {
        file = null;
        
        deleteTestEntries();
        session.close();
    }

    // TODO add test methods here.
    // The methods must be annotated with annotation @Test. For example:
    //
    // @Test
    // public void hello() {}
    
    private void deleteTestEntries(){
        List products = session.createCriteria(Product.class)
                .add(Restrictions.and(Restrictions.eq("brand", "unit"),Restrictions.like("type", "test", MatchMode.START))).list();
        
        for(Product p : (List<Product>) products){
            session.delete(p);
            
        }
    }
    
    @Test
    public void testExistense(){
    List products = session.createCriteria(Product.class)
                .add(Restrictions.and(Restrictions.eq("brand", "unit"),Restrictions.like("type", "test", MatchMode.START))).list();
        
        assertEquals(0, products.size());
    }
    
    @Test
    public void testParseProducts() throws IOException{
        
        Util.parseProducts(file);
        
        List products = session.createCriteria(Product.class)
                .add(Restrictions.and(Restrictions.eq("brand", "unit"),Restrictions.like("type", "test", MatchMode.START))).list();
        assertNotNull(products);
        assertEquals(2, products.size());
        
    }
}

@@ -400,24 +400,52 @@
 OException;%0A
+import java.io.InputStream;%0A
 import java.
@@ -562,16 +562,50 @@
 actory;%0A
+import org.hibernate.Transaction;%0A
 import o
@@ -812,14 +812,187 @@
 nit.
+jupiter.api.AfterAll;%0Aimport org.junit.jupiter.api.AfterEach;%0Aimport org.junit.jupiter.api.BeforeAll;%0Aimport org.junit.jupiter.api.BeforeEach;%0Aimport org.junit.jupiter.api.
 Test;%0A
+%0A
 impo
@@ -1231,21 +1231,19 @@
  @Before
-Class
+All
 %0A    pub
@@ -1294,21 +1294,19 @@
   @After
-Class
+All
 %0A    pub
@@ -1361,16 +1361,20 @@
  @Before
+Each
 %0A    pub
@@ -1404,115 +1404,26 @@
     
-ClassLoader classLoader = getClass().getClassLoader();%0A        %0A        file = new File(%22src/test/resources
+file = new File(%22.
 /pro
@@ -1438,60 +1438,29 @@
 v%22);
-%0A
 //
-        file = new File(classLoader.getR
+src/test/r
 esource
-(%22
+s/
 prod
@@ -1471,22 +1471,8 @@
 .csv
-%22).getFile());
 %0A   
@@ -1559,16 +1559,20 @@
   @After
+Each
 %0A    pub
@@ -1688,523 +1688,8 @@
  %7D%0A%0A
-    // TODO add test methods here.%0A    // The methods must be annotated with annotation @Test. For example:%0A    //%0A    // @Test%0A    // public void hello() %7B%7D%0A    %0A    private void deleteTestEntries()%7B%0A        List products = session.createCriteria(Product.class)%0A                .add(Restrictions.and(Restrictions.eq(%22brand%22, %22unit%22),Restrictions.like(%22type%22, %22test%22, MatchMode.START))).list();%0A        %0A        for(Product p : (List%3CProduct%3E) products)%7B%0A            session.delete(p);%0A            %0A        %7D%0A    %7D%0A
     
@@ -1732,32 +1732,41 @@
 ense()%7B%0A    List
+%3CProduct%3E
  products = sess
@@ -2123,16 +2123,25 @@
     List
+%3CProduct%3E
  product
@@ -2326,32 +2326,8 @@
     
-assertNotNull(products);
 %0A   
@@ -2380,10 +2380,439 @@
  %0A    %7D%0A
+    %0A    private void deleteTestEntries()%7B%0A        List%3CProduct%3E products = session.createCriteria(Product.class)%0A                .add(Restrictions.and(Restrictions.eq(%22brand%22, %22unit%22),Restrictions.like(%22type%22, %22test%22, MatchMode.START))).list();%0A        %0A        Transaction transaction = session.beginTransaction();%0A        for(Product p : products)%7B%0A            session.delete(p);%0A        %7D%0A        transaction.commit();%0A    %7D%0A
 %7D%0A

delete unnecessary lines/commands ClassLoader in setup