package com.cluster.employeeproject.employee.dataaccess.testcase;

import static org.junit.Assert.*;



import java.sql.Timestamp;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

import com.cluster.employeeproject.common.dataaccess.BaseDAOHibernateImpl;
import com.cluster.employeeproject.common.entity.BaseTO;
import com.cluster.employeeproject.employee.dataaccess.EmployeeDAO;
import com.cluster.employeeproject.employee.dataaccess.EmployeeDAOHibernateImpl;
import com.cluster.employeeproject.entity.AddressType;
import com.cluster.employeeproject.entity.City;
import com.cluster.employeeproject.entity.Country;
import com.cluster.employeeproject.entity.Department;
import com.cluster.employeeproject.entity.Employee;
import com.cluster.employeeproject.entity.EmployeeAddress;
import com.cluster.employeeproject.entity.EmployeePhone;
import com.cluster.employeeproject.entity.EmployeeRole;
import com.cluster.employeeproject.entity.Login;
import com.cluster.employeeproject.entity.PhoneType;
import com.cluster.employeeproject.entity.Role;
import com.cluster.employeeproject.entity.Security;
import com.cluster.employeeproject.entity.State;
import com.cluster.employeeproject.entity.Status;


public class EmployeeDAOHibernateImplTest {

	EmployeeDAO employeedao;
	
	@Before
	public void setUp() throws Exception {
		ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
		employeedao = (EmployeeDAOHibernateImpl) context.getBean("employeedao");
		
	}

	@After
	public void tearDown() throws Exception {
		employeedao=null;
	}

	
	
	@Test // (expected=IndexOutOfBoundsException.class)
	public void testFindEmployee2() {
		Employee employee=null;
		try{
			
			employee= employeedao.findEmployee("vivek2", "rrr@gmail.com");
			 
		 }catch(Exception e){
						
				fail("Employee with these records does'nt exists in the database");
			}
	}
	
	
	
	@Test              
	public void testFindEmployee() {
		Employee employee=null;
		
		  employee= employeedao.findEmployee("FFF", "rrr@gmail.com");
		  System.out.println("111111111111111111"+employee.getFirstName());
		 assertNotNull("employee already exist in database",employee);
		 
		 		 	}

	@Test
	public void testFindActiveEmployees() {
		List<Employee> findActiveEmployees = employeedao.findActiveEmployees();
		for (Employee employee : findActiveEmployees) {
			System.out.println("firstname:          "+employee.getFirstName());
		}
		
	}

	@Test
	public void testSave() {
		Employee employee;
		employee = getEmployee();
		Employee employeePersisted = (Employee) employeedao.save(employee);
		assertNotNull(employee);
	}

	@Test
	public void testFindByPrimaryKey() {
	Employee employee =(Employee) employeedao.findByPrimaryKey(101);
	System.out.println("11111111111111111"+employee.getFirstName());
	}

	@Test
	public void testUpdate() {
		Employee employee =(Employee) employeedao.findByPrimaryKey(103);
		employee.setFirstName("mohanlal");
		employeedao.update(employee);
	}

	@Test
	public void testDelete() {
		employeedao.delete( employeedao.findByPrimaryKey(112));
	}

	@Test
	public void testFindAll() {
		List findAll = employeedao.findAll();
		for (Object object : findAll) {
			Employee employee=(Employee)object;
			System.out.println("Employee firstname is"+employee.getFirstName());
		}
	}
	
	
	
	
	

	private Employee getEmployee() {
		// File file = new File("C:\\Users\\chitra\\Desktop");

		Employee employee = new Employee();
		employee.setFirstName("ravir");
		employee.setMiddleName("HH");
		employee.setLastName("KKKK");
		employee.setGender("Male");

		Timestamp timestamp = new Timestamp(System.currentTimeMillis());

		employee.setDob(timestamp);

		// FileUtil fileUtil = new FileUtil();

		// employee.setImage(fileUtil.getBytesFromFile(file));
		employee.setEmailId("rrr@gmail.com");
		employee.setAlternateEmailId("rr@gmail.com");

		// department
		Department department = new Department();
		department.setDepartmentId(10);

		employee.setDepartment(department);

		Set<Employee> hsEmployees = new HashSet<Employee>();
		hsEmployees.add(employee);
		department.setEmployees(hsEmployees);

		// address
		EmployeeAddress employeeAddress = new EmployeeAddress();
		employeeAddress.setAddressline1("6thblock");
		employeeAddress.setAddressline2("Rajajinagar");
		employeeAddress.setZipcode(5666065);

		AddressType addressType = new AddressType();
		addressType.setAddressTypeId(10);

		employeeAddress.setAddressType(addressType);

		Set<EmployeeAddress> hsEmployeeAddresses = new HashSet<EmployeeAddress>();
		hsEmployeeAddresses.add(employeeAddress);

		City city = new City();
		city.setCityId(10);
		city.setEmployeeAddresses(hsEmployeeAddresses);
		employeeAddress.setCity(city);

		State state = new State();
		state.setStateId(10);

		Set<State> hsStates = new HashSet<State>();
		hsStates.add(state);

		Set<City> hsCities = new HashSet<City>();
		hsCities.add(city);

		state.setCities(hsCities);
		city.setState(state);

		Country country = new Country();
		country.setCountryId(10);
		country.setStates(hsStates);

		state.setCountry(country);

		employee.setEmployeeAddresses(hsEmployeeAddresses);
		employeeAddress.setEmployee(employee);

		// phone
		EmployeePhone employeePhone = new EmployeePhone();
		employeePhone.setPhoneNo("988898989");

		PhoneType phoneType = new PhoneType();
		phoneType.setPhoneTypeId(10);

		Set<EmployeePhone> hsEmployeePhones = new HashSet<EmployeePhone>();
		hsEmployeePhones.add(employeePhone);

		phoneType.setEmployeePhones(hsEmployeePhones);

		employeePhone.setPhoneType(phoneType);
		employeePhone.setEmployee(employee);
		employee.setEmployeePhones(hsEmployeePhones);

		// login
		Login login = new Login();
		login.setUsername("FFF");
		login.setPassword("FFF");
		login.setDateLastLogin(timestamp);
		login.setTotalLoginAttempts(0);

		Set<Login> hsLogins = new HashSet<Login>();
		hsLogins.add(login);

		employee.setLogins(hsLogins);
		login.setEmployee(employee);

		// role
		Status status = new Status();
		status.setStatusId(10);
		employee.setStatus(status);

		EmployeeRole employeeRole = new EmployeeRole();
		Set<EmployeeRole> hsEmployeeRoles = new HashSet<EmployeeRole>();
		hsEmployeeRoles.add(employeeRole);
		employeeRole.setEmployee(employee);
		employee.setEmployeeRoles(hsEmployeeRoles);

		Role role = new Role();
		role.setRoleId(10);
		role.setEmployeeRoles(hsEmployeeRoles);
		employeeRole.setRole(role);

		employeeRole.setStatus(status);
		status.setEmployees(hsEmployees);

		// security
		Security security = new Security();
		security.setApplication("application2");
		Set<Security> hsSecurities = new HashSet<Security>();
		hsSecurities.add(security);
		role.setSecurities(hsSecurities);
		security.setRole(role);

		return employee;


	}
	
}
