package marketanalysis.db;

import java.util.List;
import org.apache.log4j.Logger;
import org.hibernate.Query;
import org.hibernate.Session;
import org.junit.Test;
import static org.junit.Assert.*;
//import org.junit.Ignore;

/**
 * Portfolio information object.
 * 
 * @author rk
 */
public class PortfolioTest {
    private final Logger logger = Logger.getLogger(PortfolioTest.class);
    
    /**
     * Test of constructor, of class Portfolio.
     */
    @Test
    public void testConstructor() {
        logger.info("constructor");
        Portfolio instance = new Portfolio();
        assertNotNull(instance);
    }

    /**
     * Test of constructor with arguments, of class Portfolio.
     */
    @Test
    @SuppressWarnings("unchecked")
    public void testConstructorWithArguments() {
        logger.info("constructorWithArguments");
        int id = 0;
        Session session = HibernateUtil.getSessionFactory().openSession();
        Query q_history = session.getNamedQuery("History.findLastItem");
        List<History> l_history = q_history.list();
        assertFalse(l_history.isEmpty());
        Query q_pname = session.getNamedQuery("Portfolioname.findAll");
        List<Portfolioname> l_pname = q_pname.list();
        assertFalse(l_pname.isEmpty());
        Portfolio instance = new Portfolio(l_pname.get(id), 'b', l_history.get(id), 100);
        int result = instance.getId();
        assertEquals(0, result);
        instance.setInstrument(l_history.get(0).getInstrument());
        session.beginTransaction();
        session.save(instance);
        session.getTransaction().commit();
        int result2 = instance.getId();
        assertTrue(result2>0);
        session.beginTransaction();
        session.delete(instance);
        session.getTransaction().commit();
        session.close();
    }
    
}
