package com.jasamedika.medifirst2000.dao.test;

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

/**Agama Class Test
 */

import static org.junit.Assert.assertNull;

import java.util.ArrayList;
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.entities.GenericView;
import com.jasamedika.medifirst2000.entities.JenisKelamin;
import com.jasamedika.medifirst2000.entities.PapCatatan;
import com.jasamedika.medifirst2000.entities.PapGenatalia;
import com.jasamedika.medifirst2000.entities.PapRiwayatPsikososial;
import com.jasamedika.medifirst2000.entities.Pasien;
import com.jasamedika.medifirst2000.entities.Pendidikan;
import com.jasamedika.medifirst2000.entities.RegistrasiPelayananPasien;
import com.jasamedika.medifirst2000.dao.GenericViewDao;
import com.jasamedika.medifirst2000.dao.PapGenataliaDao;
import com.jasamedika.medifirst2000.dao.PasienDao;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath*:com/jasamedika/**/applicationContext.xml")
public class PapGenataliaDaoTest {
	
	@Autowired
	private PapGenataliaDao papGenataliaDao;
	
	@Autowired
	private PasienDao pasienDao;
	
	@Test
	@Transactional
	public void testPapGenetaliaDaoSave(){
		PapGenatalia model = populateModel();
		PapGenatalia result = papGenataliaDao.save(model);
		assertNotNull(result);
		assertEquals(result.getKeterangan(),"Keterangan Genatalia");
		assertEquals(result.getStatus(),"Status Genatalia");
		assertNotNull(result.getRegistrasiPelayananPasien());
		assertEquals(result.getRegistrasiPelayananPasien().getNamaTempatTujuan(),"Nama Tempat Tujuan");
		assertNotNull(result.getPasien());
		assertEquals(result.getPasien().getUmur(),"Umur Pasien");
	}
	
	@Test
	@Transactional
	public void testPapGenataliaDaoUpdate(){
		PapGenatalia model = populateModel();
		PapGenatalia resultModel1 = papGenataliaDao.save(model);
		assertNotNull(resultModel1);
		
		/*Proses Update*/
		PapGenatalia resultModel2 = papGenataliaDao.findOne(model.getNoRec());
		resultModel2.setKeterangan("Keterangan Update");
		resultModel2.setStatus("Status Update");
		Pasien pasien = new Pasien();
		pasien.setUmur("Umur Update");
		resultModel2.setPasien(pasien);
		RegistrasiPelayananPasien registrasiPelayananPasien = new RegistrasiPelayananPasien();
		registrasiPelayananPasien.setNamaTempatTujuan("Tujuan Update");
		resultModel2.setRegistrasiPelayananPasien(registrasiPelayananPasien);
		papGenataliaDao.save(resultModel2);
		
		/*Cek Nilai Update di DB*/
		PapGenatalia resultModel3 = papGenataliaDao.findOne(model.getNoRec());
		assertEquals("Keterangan Update",resultModel3.getKeterangan());
		assertEquals("Status Update",resultModel3.getStatus());
		assertEquals("Umur Update",resultModel3.getPasien().getUmur());
		assertEquals("Tujuan Update",resultModel3.getRegistrasiPelayananPasien().getNamaTempatTujuan());
	}
	
//	@Test
	@Transactional
	public void testPapGenetaliaDaoFindAll(){
		/* Model 1 */
		PapGenatalia model = populateModel();
		PapGenatalia resultModel = papGenataliaDao.save(model);
		assertNotNull(resultModel);
		assertNotNull(resultModel.getKeterangan());
		
		/* Model 2*/
		PapGenatalia model2 = populateModel();
		PapGenatalia resultModel2 = papGenataliaDao.save(model2);
		assertNotNull(resultModel2);
		assertNotNull(resultModel2.getKeterangan());
		
		/* Cek Model di Database */
		try {
			List<PapGenatalia> hasil = toList(papGenataliaDao.findAll());
			assertNotNull(hasil);
		} catch (Exception e) {
		}
	}
	
	@Test
	@Transactional
	public void testPapGenetaliaDaoDelete(){
		PapGenatalia model1 = populateModel();
		PapGenatalia resultModel = papGenataliaDao.save(model1);
		assertNotNull(resultModel);
		assertNotNull(resultModel.getKeterangan());
		PapGenatalia result1 = papGenataliaDao.findOne(model1.getNoRec());
		assertNotNull(result1);
		
		papGenataliaDao.delete(model1);
		result1 = papGenataliaDao.findOne(model1.getNoRec());
		assertNull(result1);
	}
	
	/* Source : */
	/* http://www.java2s.com/Code/Java/Collections-Data-Structure/ConvertIterabletoList.htm */
	public static <E> List<E> toList(Iterable<E> iterable) {
		if(iterable instanceof List) {
			return (List<E>) iterable;
		}
		ArrayList<E> list = new ArrayList<E>();
		if(iterable != null) {
			for(E e: iterable) {
				list.add(e);
			}
		}
		return list;
	}
	
	public PapGenatalia populateModel(){
		PapGenatalia model = new PapGenatalia();
		RegistrasiPelayananPasien registrasiPelayananPasien = new RegistrasiPelayananPasien();
		registrasiPelayananPasien.setNamaTempatTujuan("Nama Tempat Tujuan");
		Pasien pasien = pasienDao.findOne(541);
		pasien.setUmur("Umur Pasien");
		
		model.setRegistrasiPelayananPasien(registrasiPelayananPasien);
		model.setPasien(pasien);
		model.setStatus("Status Genatalia");
		model.setKeterangan("Keterangan Genatalia");
		return model;
	}
	
}
