package com.teamlans.lepta.database.daos;

import com.teamlans.lepta.database.HibernateTestConfiguration;
import com.teamlans.lepta.database.entities.Bill;
import com.teamlans.lepta.database.enums.Status;

import org.hibernate.SessionFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.support.AnnotationConfigContextLoader;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

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

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(classes = {HibernateTestConfiguration.class,
    DaoConfig.class}, loader = AnnotationConfigContextLoader.class)
@Transactional
public class BillDaoImplTest {

  @Autowired
  BillDao billDao;

  @Autowired
  UserDao userDao;

  @Autowired ItemDao itemDao;

  @Autowired
  SessionFactory sessionFactory;


  @Test
  public void createBill_validBill_billCreated() throws Exception {
    int billCount = billDao.listBills().size();
    Bill bill = new Bill("name", new Date(), userDao.listUsers().get(0));
    billDao.addBill(bill);
    int newBillCount = billDao.listBills().size();
    assertEquals(billCount + 1, newBillCount);
  }

  @Test
  public void updateBill_updateStatus_billUpdated() throws Exception {
    Bill bill = billDao.listBills().get(0);
    bill.setStatus(Status.ASSIGNED);

    billDao.updateBill(bill);

    sessionFactory.getCurrentSession().flush();
    Bill updatedBill = billDao.listBills().get(0);
    assertEquals(updatedBill.getStatus(), Status.ASSIGNED);
  }

  @Test
  public void updateBill_updateItems_itemsRemoved() throws Exception {
    Bill bill = billDao.listBills().get(0);
    int itemCount = itemDao.listItems().size();
    bill.getItems().clear();

    billDao.updateBill(bill);

    sessionFactory.getCurrentSession().flush();
    int newItemCount = itemDao.listItems().size();
    assertTrue(itemCount > newItemCount);
  }

  @Test
  public void deleteBill_validBill_billDeleted() throws Exception {
    int billCount = billDao.listBills().size();
    billDao.deleteBill(1);
    int newBillCount = billDao.listBills().size();
    assertEquals(billCount - 1, newBillCount);
  }

  @Test(expected = DataAccessException.class)
  public void deleteBill_invalidBill_exceptionThrown() throws Exception {
    billDao.deleteBill(-1);
  }


}

@@ -203,45 +203,8 @@
 s;%0A%0A
-import org.hibernate.SessionFactory;%0A
 impo
@@ -1062,16 +1062,18 @@
 utowired
+%0A 
  ItemDao
@@ -1087,56 +1087,8 @@
 o;%0A%0A
-  @Autowired%0A  SessionFactory sessionFactory;%0A%0A%0A
   @T
@@ -1607,56 +1607,8 @@
 );%0A%0A
-    sessionFactory.getCurrentSession().flush();%0A
     
@@ -1955,56 +1955,8 @@
 );%0A%0A
-    sessionFactory.getCurrentSession().flush();%0A
     
@@ -2165,32 +2165,33 @@
 Bills().size();%0A
+%0A
     billDao.dele
@@ -2201,16 +2201,17 @@
 ill(1);%0A
+%0A
     int 
@@ -2301,24 +2301,277 @@
 ount);%0A  %7D%0A%0A
+  @Test%0A  public void deleteBill_validBill_itemsDeleted() throws Exception %7B%0A    int itemCount = itemDao.listItems().size();%0A%0A    billDao.deleteBill(1);%0A%0A    int newItemCount = itemDao.listItems().size();%0A    assertTrue(itemCount %3E newItemCount);%0A  %7D%0A%0A%0A
   @Test(expe

Add more tests [#21]