package br.com.senacagil.springmvc.dao;

import java.util.Date;
import java.util.List;

import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.test.jpa.AbstractJpaTests;

import br.com.senacagil.springmvc.model.Address;
import br.com.senacagil.springmvc.model.Customer;


/**
 * (c) 2007, D George Spring / JPA / TestNG / Maven Example Code
 * 
 * TestNG harness
 * 
 * reference: http://www.ibm.com/developerworks/edu/j-dw-java-spring2-i.html
 */
public class CustomerDAOIntegrationTest extends AbstractJpaTests {
	private CustomerDAO customerDAO;

	private long testId;

	public void setCustomerService(CustomerDAO customerDAO) {
		this.customerDAO = customerDAO;
	}

	@Override
	protected String[] getConfigLocations() {
		return new String[] {"classpath:/applicationContext.xml" };
	}

	@BeforeClass
	protected final void onSetUpInTransaction() throws Exception {
		Customer c1 = new Customer("Paddy", "Poivre", "034-3409-34098",
				new Address(10, "Main Street"), new Date());
		Customer c2 = new Customer("Claire", "Razzal", "032-4933-34982",
				new Address(20, "Main Street"), new Date());
		Customer c3 = new Customer("Harry", "Rosenblak", "032-8976-09372",
				new Address(20, "Beauregard Av"), new Date());

		customerDAO.save(c1);
		customerDAO.save(c2);
		customerDAO.save(c3);

		testId = c1.getCustid();
	}

	@Test
	public void testFindByCustomerId() {
		Customer c = customerDAO.findById(testId);
		assertNotNull(c);
		assertEquals("Paddy", c.getFirstName());
	}

	@Test
	public void testFindByBadCustomerId() {
		Customer c = customerDAO.findById(12345);
		assertNull(c);
	}

	@Test
	public void testFindByLastName() {
		List<Customer> cList = customerDAO.findByCustomerLastName("Razzal");
		assertEquals(1, cList.size());
		Customer c = cList.get(0);
		assertEquals("Claire", c.getFirstName());
	}
	
	@Test
	public void testFindByPartialName() {
		List<Customer> cList = customerDAO.findByCustomerPartialName("R%");
		assertEquals(2, cList.size());
		Customer c = cList.get(1);
		assertEquals("Harry", c.getFirstName());
	}

	@Test
	public void testFindByNameWhereNoCustomer() {
		List<Customer> cList = customerDAO.findByCustomerLastName("Smith");
		assertEquals(0, cList.size());
	}

	@Test
	public void testFindByStreetName() {
		List<Customer> cList= customerDAO
				.findByAddressStreetName("Main Street");
		assertEquals(2, cList.size());
	}

	@Test
	public void testFindByStreetNameBadName() {
		List<Customer> cList = customerDAO.findByAddressStreetName("Monge Way");
		assertEquals(0, cList.size());
	}

	@Test
	public void testModifyCustomer() {
		String oldLastName = "Razzal";
		String newLastName = "Correnson";
		Customer c = customerDAO.findByCustomerLastName(oldLastName).get(0);
		c.setLastName(newLastName);

		Customer c2 = customerDAO.update(c);
		assertEquals(newLastName, c2.getLastName());

		List<Customer> results = customerDAO
				.findByCustomerLastName(oldLastName);
		assertEquals(0, results.size());

		results = customerDAO.findByCustomerLastName(newLastName);
		assertEquals(1, results.size());

	}

	@Test
	public void testDeleteEmployeeCascade() {
		String empName = "Razzal";
		Customer c = customerDAO.findByCustomerLastName(empName).get(0);
		int empCountBefore = countRowsInTable("customer");
		int addrCountBefore = countRowsInTable("address");
		customerDAO.delete(c);
		List<Customer> res = customerDAO.findByCustomerLastName(empName);
		assertEquals(0, res.size());
		int empCountAfter = countRowsInTable("customer");
		int addrCountAfter = countRowsInTable("address");
		assertEquals(empCountBefore - 1, empCountAfter);
		assertEquals(addrCountBefore - 1, addrCountAfter);

	}

	@Test
	public void testFindAll() {
		List<Customer> cList = customerDAO.findAll();
		assertEquals(3, cList.size());
	}
}	