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.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.PapRiwayatPsikososialDao;
import com.jasamedika.medifirst2000.dao.PapRiwayatPsikososialDao;
import com.jasamedika.medifirst2000.entities.GenericView;
import com.jasamedika.medifirst2000.entities.PapRiwayatPsikososial;

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

	@Autowired
	private GenericViewDao genericViewDao;
	@Autowired
	private PapRiwayatPsikososialDao papRiwayatPsikososialDao;
	
	@Test
	@Transactional
	public void testGenericDaoSelect() {
		GenericView statusPsikologi = populateGenericView("Status Psikologi");
		GenericView tempatTinggal = populateGenericView("Tempat Tinggal");
		assertNotNull(statusPsikologi);
		assertNotNull(tempatTinggal);
    	assertEquals("1", String.valueOf(statusPsikologi.getKdGenericView()));
    	assertEquals("1", String.valueOf(tempatTinggal.getKdGenericView()));
	}

	@Test
	@Transactional
	public void testPAPRiwayatPsikososialDaoSave() {
		PapRiwayatPsikososial model = populateModel();
		PapRiwayatPsikososial resultModel=papRiwayatPsikososialDao.save(model);
		assertNotNull(resultModel);
//		assertNotNull(resultModel.getStatusPsikologi());
//		assertNotNull(resultModel.getTempatTinggal());
//		assertEquals(true, resultModel.getIsHubunganPasien());
//		assertEquals("1",resultModel.getKecenderunganLainnya());
    	PapRiwayatPsikososial resultModel2=papRiwayatPsikososialDao.findOne(resultModel.getNoRec());
		assertNotNull(resultModel2);
//		assertNotNull(resultModel2.getStatusPsikologi());
//		assertNotNull(resultModel2.getTempatTinggal());
//		assertEquals(true, resultModel2.getIsHubunganPasien());
//		assertEquals("1",resultModel2.getKecenderunganLainnya());
	}

	private PapRiwayatPsikososial populateModel() {
		PapRiwayatPsikososial model=new PapRiwayatPsikososial();
		GenericView statusPsikologi = populateGenericView("Status Psikologi");
		GenericView tempatTinggal = populateGenericView("Tempat Tinggal");
//		model.setStatusPsikologi(statusPsikologi);
//		model.setTempatTinggal(tempatTinggal);
//		model.setIsHubunganPasien(true);
//		model.setKecenderunganLainnya("1");
		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 testPAPRiwayatPsikososialDaoFindAll() {
		PapRiwayatPsikososial model1 = populateModel();
		PapRiwayatPsikososial resultModel1=papRiwayatPsikososialDao.save(model1);
		PapRiwayatPsikososial model2 = populateModel();
		PapRiwayatPsikososial resultModel2=papRiwayatPsikososialDao.save(model2);
		assertNotNull(resultModel1);
		assertNotNull(resultModel2);
		List<PapRiwayatPsikososial> results = (List<PapRiwayatPsikososial>) papRiwayatPsikososialDao.findAll();
		assertTrue(results.size() > 0);
		assertNotNull(results);
		assertTrue(results.size() == 2);
	}
	
}
