package com.jasamedika.medifirst2000.dao.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;

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

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.dao.GenericViewDao;
import com.jasamedika.medifirst2000.dao.PapEliminasiDao;
import com.jasamedika.medifirst2000.dao.RegistrasiPelayananPasienDao;
import com.jasamedika.medifirst2000.entities.GenericView;
import com.jasamedika.medifirst2000.entities.PapEliminasi;
import com.jasamedika.medifirst2000.util.DateUtil;

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

	@Autowired
	private GenericViewDao genericViewDao;
	@Autowired
	private PapEliminasiDao papEliminasiDao;
	@Autowired
	private RegistrasiPelayananPasienDao registrasiPelayananPasienDao;
	
	@Test
	@Transactional
	public void testFindByNoCmAndTglInput() {
		//registrasiPelayananPasienDao.findOne("40288c99557b0f0f01557b155bcc0000");
		Date tgl=DateUtil.toDate("2016-06-19");
		System.out.println("DATEEEEEEEEEE : "+ tgl);
//		PapEliminasi abc=papEliminasiDao.findPapEliminasiByNoCmAndTglInput("00001", DateUtil.toDate("2016-06-19"));
		PapEliminasi abc=papEliminasiDao.findByNoRec("40288c99559075a1015590762cb40000");
		assertNotNull(abc);
		assertNotNull(abc.getPasien());
    	assertEquals("00001", abc.getPasien().getNoCm());	
	}
/*
	@Test
	@Transactional
	public void testGenericDaoSelect() {
		GenericView pengeluaranDefeaksi = populateGenericView("Pengeluaran Defeaksi");
		GenericView pengeluaranUrin = populateGenericView("Pengeluaran Urin");
		GenericView karakteristikFeses = populateGenericView("Karakteristik Feses");
		assertNotNull(pengeluaranDefeaksi);
		assertNotNull(pengeluaranUrin);
		assertNotNull(karakteristikFeses);
    	assertEquals("1", String.valueOf(pengeluaranDefeaksi.getKdGenericView()));
    	assertEquals("1", String.valueOf(pengeluaranUrin.getKdGenericView()));
    	assertEquals("1", String.valueOf(karakteristikFeses.getKdGenericView()));
	}

	@Test
	@Transactional
	public void testPAPEliminasiDaoSave() {
		PapEliminasi model = populateModel();
		PapEliminasi resultModel=papEliminasiDao.save(model);
		assertNotNull(resultModel);
    	assertEquals("1", resultModel.getFrekuensiDefeaksi());
    	assertEquals("2", resultModel.getKonsistensiDefeaksi());
    	assertEquals("3", resultModel.getDiuresisUrin());
    	assertNotNull(resultModel.getPengeluaranDefeaksi());
    	assertNotNull(resultModel.getPengeluaranUrin());
    	assertNotNull(resultModel.getKarakteristikFeses());
    	PapEliminasi resultModel2=papEliminasiDao.findOne(resultModel.getNoRec());
		assertNotNull(resultModel2);
    	assertEquals("1", resultModel2.getFrekuensiDefeaksi());
    	assertEquals("2", resultModel2.getKonsistensiDefeaksi());
    	assertEquals("3", resultModel2.getDiuresisUrin());
    	assertNotNull(resultModel2.getPengeluaranDefeaksi());
    	assertNotNull(resultModel2.getPengeluaranUrin());
    	assertNotNull(resultModel2.getKarakteristikFeses());
    	assertEquals("1",String.valueOf(resultModel2.getPengeluaranDefeaksi().getKdGenericView()));
    	assertEquals("1",String.valueOf(resultModel2.getPengeluaranUrin().getKdGenericView()));
    	assertEquals("1",String.valueOf(resultModel2.getKarakteristikFeses().getKdGenericView()));
	}

	private PapEliminasi populateModel() {
		PapEliminasi model=new PapEliminasi();
		GenericView pengeluaranDefeaksi = populateGenericView("Pengeluaran Defeaksi");
		GenericView pengeluaranUrin = populateGenericView("Pengeluaran Urin");
		GenericView karakteristikFeses = populateGenericView("Karakteristik Feses");
		model.setPengeluaranDefeaksi(pengeluaranDefeaksi);
		model.setFrekuensiDefeaksi("1");
		model.setKonsistensiDefeaksi("2");
		model.setKarakteristikFeses(karakteristikFeses);
		model.setPengeluaranUrin(pengeluaranUrin);
		model.setIsKelainanUrin(true);
		model.setDiuresisUrin("3");
		return model;
	}
	
	private GenericView populateGenericView(String previx) {
		GenericView genericView=new GenericView();
		List<GenericView> list=genericViewDao.findGenericViewByPrevix(previx);
		for (GenericView gv:list) {
			if(gv.getKdGenericView()==1){
				genericView=gv;
			}
		}
		return genericView;
	}
	
	@Test
	@Transactional
	public void testPAPEliminasiDaoFindAll() {
		PapEliminasi model1 = populateModel();
		PapEliminasi resultModel1=papEliminasiDao.save(model1);
		PapEliminasi model2 = populateModel();
		PapEliminasi resultModel2=papEliminasiDao.save(model2);
		assertNotNull(resultModel1);
		assertNotNull(resultModel2);
		List<PapEliminasi> results = (List<PapEliminasi>) papEliminasiDao.findAll();
		assertTrue(results.size() > 0);
		assertNotNull(results);
		assertTrue(results.size() == 2);
	}
*/	
}
