package net.pascalalma.myservices;

import java.util.Hashtable;
import java.util.List;
import java.util.concurrent.Callable;
import net.pascalalma.model.Customer;
import org.slf4j.Logger;
import org.junit.*;
import org.slf4j.LoggerFactory;


public class TestCustomerService extends EJBTestBase {

    private Logger log;
    private CustomerServiceLocal csb = null;

    public TestCustomerService() {
        log = LoggerFactory.getLogger(getClass());
    }

    @Before
    public void setUpTest() throws Exception
    {
        log.debug("setUpTest");
        csb = (CustomerServiceLocal)context.lookup("CustomerServiceBeanLocal");
    }

    @After
    public void tearDownTest()
            throws Exception {
        log.debug("tearDownTest");
        csb = null;
    }


    @Test
    public void checkGetAllCustomer()
            throws Exception {
        log.debug("checkGetAllCustomer");

        List<Customer> result = csb.findAllCustomers();

        assert result.size() == 2;

        Hashtable env = context.getEnvironment();
    }

    
    private void checkGetCustomer()
            throws Exception {
        log.info("checkGetCustomer");

        Customer result = csb.getCustomer(1);
        log.info("Gevonden customers: " + result);

        assert result.getId() == 1;

    }
     @Test
      public void testCheckGetCustomerWithTransaction() throws Exception {
        Caller transactionBean = (Caller) context.lookup("TransactionBeanLocal");

        transactionBean.call(new Callable(){
            public Object call() throws Exception {
                checkGetCustomer();
                return null;
            }
        });
    }

    @Test
    public void checkFindCustomer()
            throws Exception {
        log.info("checkFindCustomer");

        Customer result = csb.findCustomer(1);
        log.info("Gevonden customers: " + result);

        assert result.getId() == 1;
    }

    @Test
    public void checkInsertCustomer()
            throws Exception {
        log.info("checkInsertCustomer");

        Customer c = new Customer();
        c.setId(8);
        c.setName("New customer");
        c.setAddress("Dorpsplein 8");

        csb.insert(c);

        Customer result = csb.findCustomer(8);

        assert result != null;

        log.debug("Gevonden product: " + result);

        assert 8 == result.getId();

    }


    private void checkDeleteCustomer()
            throws Exception {
        log.info("checkDeleteCustomer");

        Customer c = new Customer();
        c.setId(9);
        c.setName("New customer");
        c.setAddress("Dorpsplein 8");

         csb.insert(c);

        Customer result = csb.getCustomer(9);
        assert result != null;

        csb.delete(result);

        result = csb.findCustomer(9);

        log.debug("Gevonden customer: " + result);

        assert result == null;

    }
     @Test
      public void testDeleteCustomerWithTransaction() throws Exception {
        Caller transactionBean = (Caller) context.lookup("TransactionBeanLocal");

        transactionBean.call(new Callable(){
            public Object call() throws Exception {
                checkDeleteCustomer();
                return null;
            }
        });
    }
}
