/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package cz.muni.fi.pa165.dao.test;

import cz.muni.fi.pa165.dao.MemberDao;
import cz.muni.fi.pa165.entity.Member;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;
import javax.inject.Inject;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.PersistenceException;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 *
 * @author xkubist
 */
@RunWith(SpringJUnit4ClassRunner.class)
public class MemberDaoTest {
    @PersistenceContext
    private EntityManager em;
    
    @Inject
    MemberDao memberDao;
    
    private Member member1;
    private Member member2;
    
    @Before
    public void setUp() {
        member1 = new Member();
        member1.setGivenName("Blanka");
        member1.setSurname("Protrhla");
        member1.setEmail("BerUska15@pokec.sk");
        Date date = new Date(0);
        member1.setRegistrationDate(date);
        member2 = new Member();
        member2.setGivenName("Petr");
        member2.setSurname("Soustal");
        member2.setEmail("soustal@gmail.com");
        date = new Date(2);
        member2.setRegistrationDate(date);
        em.persist(member1);
        em.persist(member2);
        em.flush();
        
    }
    
    @Test
    public void testCreate() {
        memberDao.create(member1);
        memberDao.create(member2);
        assertNotNull(member1.getId());
        assertNotNull(member2.getId());
    }
    
    @Test
    public void testCreateNullName() {
        Member member = new Member();
        memberDao.create(member);
    }
    
    @Test
    public void testUpdate() {
        memberDao.create(member1);
        member1.setGivenName("Janka");
        memberDao.update(member1);
        assertEquals("Janka", em.find(Member.class, member1.getId())
                .getGivenName());
    }
    
    @Test(expected = PersistenceException.class)
    public void testUpdateNonexistent() {
        memberDao.update(member1);
    }
    
    @Test
    public void testFindById() {
        memberDao.create(member1);
        memberDao.create(member2);
        assertSame(member1, memberDao.findById(member1.getId()));
        assertSame(member2, memberDao.findById(member2.getId()));
        assertNull(memberDao.findById(Long.MAX_VALUE));
    }
    @Test
    public void testFindAll() {
        Set<Member> members = new HashSet<>();
        members.add(member1);
        members.add(member2);
        memberDao.create(member1);
        memberDao.create(member2);
        Set<Member> result = new HashSet<>();
        result.addAll(memberDao.findAll());
        assertEquals(result, members);
    }
    
    @Test
    public void testDelete() {
        memberDao.create(member1);
        memberDao.delete(member1);
        assertNull(em.find(Member.class, member1));
    }

    @Test(expected = PersistenceException.class)
    public void testDeleteNonexistent() {
        memberDao.delete(member1);
    }
    
}

@@ -395,55 +395,8 @@
 xt;%0A
-import javax.persistence.PersistenceException;%0A
 impo
@@ -649,16 +649,78 @@
 unWith;%0A
+import org.springframework.test.context.ContextConfiguration;%0A
 import o
@@ -787,78 +787,271 @@
 er;%0A
-%0A/**%0A *%0A * @author xkubist%0A */%0A@RunWith(SpringJUnit4ClassRunner.class)
+import cz.muni.fi.pa165.spring.LibrarySpringContext;%0Aimport org.springframework.transaction.annotation.Transactional;%0A%0A/**%0A *%0A * @author xkubist%0A */%0A@RunWith(SpringJUnit4ClassRunner.class)%0A@ContextConfiguration(classes = LibrarySpringContext.class)%0A@Transactional
 %0Apub
@@ -1233,21 +1233,16 @@
 ember2;%0A
-    %0A
     @Bef
@@ -1808,24 +1808,94 @@
 em.flush();%0A
+        memberDao.create(member1);%0A        memberDao.create(member2);%0A
         %0A   
@@ -1904,32 +1904,51 @@
 %0A    %0A    @Test%0A
+    @Transactional%0A
     public void 
@@ -1966,78 +1966,8 @@
 ) %7B%0A
-        memberDao.create(member1);%0A        memberDao.create(member2);%0A
     
@@ -2061,649 +2061,304 @@
     
-@Test%0A    public void testCreateNullName() %7B%0A        Member member = new Member();%0A        memberDao.create(member);%0A    %7D%0A    %0A    @Test%0A    public void testUpdate() %7B%0A        memberDao.create(member1);%0A        member1.setGivenName(%22Janka%22);%0A        memberDao.update(member1);%0A        assertEquals(%22Janka%22, em.find(Member.class, member1.getId())%0A                .getGivenName());%0A    %7D%0A    %0A    @Test(expected = PersistenceException.class)%0A    public void testUpdateNonexistent() %7B%0A        memberDao.update(member1);%0A    %7D%0A    %0A    @Test%0A    public void testFindById() %7B%0A        memberDao.create(member1);%0A        memberDao.create(member2);
+%0A    @Test%0A    @Transactional%0A    public void testUpdate() %7B%0A        member1.setGivenName(%22Janka%22);%0A        memberDao.update(member1);%0A        assertEquals(%22Janka%22, em.find(Member.class, member1.getId())%0A                .getGivenName());%0A    %7D%0A    %0A    %0A    @Test%0A    public void testFindById() %7B
 %0A   
@@ -2586,24 +2586,31 @@
 FindAll() %7B%0A
+      %0A
         Set%3C
@@ -2708,78 +2708,8 @@
 2);%0A
-        memberDao.create(member1);%0A        memberDao.create(member2);%0A
     
@@ -2751,33 +2751,8 @@
 t%3C%3E(
-);%0A        result.addAll(
 memb
@@ -2864,43 +2864,8 @@
 ) %7B%0A
-        memberDao.create(member1);%0A
     
@@ -2943,16 +2943,24 @@
  member1
+.getId()
 ));%0A    
@@ -2983,27 +2983,31 @@
 ected = 
-Persistence
+IllegalArgument
 Exceptio
@@ -3053,24 +3053,62 @@
 xistent() %7B%0A
+        Member member = new Member();%0A
         memb
@@ -3122,25 +3122,24 @@
 elete(member
-1
 );%0A    %7D%0A   

modify equlas and hascode methods in Loan class