package com.jasamedika.medifirst2000.service.test;

import static org.junit.Assert.*;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.transaction.annotation.Transactional;
import com.jasamedika.medifirst2000.converter.PasienConverter;
import com.jasamedika.medifirst2000.dao.PasienDao;
import com.jasamedika.medifirst2000.entities.PapNeurologi;
import com.jasamedika.medifirst2000.entities.Pasien;
import com.jasamedika.medifirst2000.service.PapNeurologiService;
import com.jasamedika.medifirst2000.vo.PapNeurologiVO;
import com.jasamedika.medifirst2000.vo.PasienVO;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:com/jasamedika/**/applicationContext.xml")
public class PapNeurologiServiceImplTest {

	@Autowired
	private PapNeurologiService papNeurologiService;
	@Autowired
	private PasienDao pasienDao;
	@Autowired
	private PasienConverter pasienConverter;
	
	@Test
	public void testAddNeurologi() throws ParseException {
		PapNeurologiVO papNeurologiVO = populateModelVO();
		assertNotNull(papNeurologiVO);
		Map<String,Object> result = papNeurologiService.addNeurologi(papNeurologiVO);
		//assertEquals("15",((PapNeurologi) result.get("papNeurologi")).getLingkarPerut());
		assertEquals(true,((PapNeurologi) result.get("papNeurologi")).getIsNormal());
		assertNotNull(result);
		
	}
	
	@Test
	@Transactional
	public void testUpdateNeurologi() throws ParseException {
		PapNeurologiVO papNeurologiVO = populateModelVO();
		Map<String,Object> result = papNeurologiService.addNeurologi(papNeurologiVO);
		assertNotNull(result);
		
		PapNeurologiVO papNeurologiVOUpdate = populateModelVOUpdate();
		papNeurologiVOUpdate.setNoRec(((PapNeurologi) result.get("papNeurologi")).getNoRec());
		assertNotNull(papNeurologiVOUpdate);
		assertEquals(papNeurologiVOUpdate.getNoRec(),((PapNeurologi) result.get("papNeurologi")).getNoRec());
		
		Map<String,Object> resultUpdate = papNeurologiService.addNeurologi(papNeurologiVOUpdate);
		assertEquals(false,((PapNeurologi) resultUpdate.get("papNeurologi")).getIsNormal());
		assertEquals(false,((PapNeurologi) resultUpdate.get("papNeurologi")).getStatusEnabled());
	}
	
	
	private PapNeurologiVO populateModelVO() throws ParseException{
		PapNeurologiVO papNeurologiVO = new PapNeurologiVO();
		Pasien pasien = pasienDao.findOne(547);
		PasienVO pasienVO = pasienConverter.transferModelToVO(pasien,new PasienVO());
		
		papNeurologiVO.setTglInput(generateDate());
		papNeurologiVO.setPasien(pasienVO);
		papNeurologiVO.setIsNormal(true);
		papNeurologiVO.setStatusEnabled(true);
		
		return papNeurologiVO;
	}
	
	private PapNeurologiVO populateModelVOUpdate() throws ParseException{
		PapNeurologiVO papNeurologiVO = new PapNeurologiVO();
		Pasien pasien = pasienDao.findOne(547);
		PasienVO pasienVO = pasienConverter.transferModelToVO(pasien,new PasienVO());
		papNeurologiVO.setTglInput(generateDate());
		papNeurologiVO.setPasien(pasienVO);
		papNeurologiVO.setIsNormal(false);
		papNeurologiVO.setStatusEnabled(false);
		
		return papNeurologiVO;
	}
	
	@Test
	public void testPopulate() throws ParseException{
		/*Cek Method Populate*/
		PapNeurologiVO papNeurologiVO = populateModelVO();
		assertNotNull(papNeurologiVO);
		
		/*Cek Dao*/
		Pasien pasien = pasienDao.findOne(544);
		assertNotNull(pasien);
		assertEquals("Reynaldi Ramadhani Pratama",pasien.getNamaPasien());
		
		/*Cek Converter*/
		PasienVO pasienVO = pasienConverter.transferModelToVO(pasien,new PasienVO());
		assertNotNull(pasienVO);
		assertEquals("Reynaldi Ramadhani Pratama",pasienVO.getNamaPasien());
	}
	
	private Date generateDate() throws ParseException{
		SimpleDateFormat formatter = new SimpleDateFormat("EEEE, MMM dd, yyyy HH:mm:ss a");
		String dateInString = "Friday, Jun 7, 2013 11:40:39 PM";		

		Date date = formatter.parse(dateInString);
		return date;

	}

}
