/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package org.books.data.entity.dao;

import org.books.data.dao.BookDAOBean;
import org.testng.Assert;
import org.testng.annotations.Test;

import javax.persistence.EntityNotFoundException;
import javax.persistence.EntityTransaction;
import java.math.BigDecimal;
import org.books.data.entity.PopulateDBJpaTest;
import org.books.data.entity.BasisJpaTest;
import org.books.data.entity.Book;
import org.testng.annotations.BeforeClass;

/**
 * @author tjd
 */
public class BookCRUDTest extends BasisJpaTest {

    private static final String ISBN = "isbn12345";
    private BookDAOBean bean;
    private Book book = new Book(ISBN, "title", "authors", "publisher", 2015, Book.Binding.Ebook, 300, new BigDecimal(12.34));
    

    @BeforeClass
    public void initDAO() throws Exception {
        bean = new BookDAOBean();
        bean.setEntityManager(em);
    }

    @Test
    public void createBook() {
        //Create
        transaction.begin();
        book = bean.create(book);
        transaction.commit();
        Assert.assertNotNull(book.getId());
        Assert.assertEquals(1,1);
    }

    @Test(dependsOnMethods = "createBook")
    public void crudBook() {

        //em.clear();
        //Find
        Book findBook = bean.find(book.getId());
        Assert.assertEquals(book.getId(), findBook.getId());

        //Update
        String newTitle = "new title";
        findBook.setTitle(newTitle);
        transaction.begin();
        Book updateBook = bean.update(findBook);
        transaction.commit();
        Assert.assertTrue(updateBook.getTitle().equals(newTitle));

        //Delete
        transaction.begin();
        bean.remove(updateBook);
        transaction.commit();

        try {
            bean.find(updateBook.getId());
            Assert.assertTrue(false);
        } catch (EntityNotFoundException e) {
            Assert.assertTrue(true);
        }
    }
}

@@ -381,116 +381,24 @@
 java
-x.persistence.EntityTransaction;%0Aimport java.math.BigDecimal;%0Aimport org.books.data.entity.PopulateDBJpaTest
+.math.BigDecimal
 ;%0Aim
@@ -798,21 +798,16 @@
 12.34));
-%0A    
 %0A%0A    @B
@@ -1137,42 +1137,8 @@
 ));%0A
-        Assert.assertEquals(1,1);%0A
     
@@ -1203,30 +1203,33 @@
 oid 
-cru
+fin
 dBook
+ById
 () %7B%0A
-%0A
         
 //em
@@ -1224,18 +1224,16 @@
         
-//
 em.clear
@@ -1248,33 +1248,9 @@
     
-//Find%0A        Book findB
+b
 ook 
@@ -1288,78 +1288,138 @@
     
-Assert.assertEquals(book.getId(), findBook.getId());%0A%0A        //Update
+em.clear();%0A        Assert.assertNotNull(book);%0A    %7D%0A%0A    @Test(dependsOnMethods = %22findBookById%22)%0A    public void updateBook() %7B
 %0A   
@@ -1462,21 +1462,17 @@
         
-findB
+b
 ook.setT
@@ -1528,20 +1528,9 @@
     
-Book updateB
+b
 ook 
@@ -1543,21 +1543,17 @@
 .update(
-findB
+b
 ook);%0A  
@@ -1572,32 +1572,52 @@
 ction.commit();%0A
+        em.clear();%0A
         Assert.a
@@ -1626,23 +1626,17 @@
 ertTrue(
-updateB
+b
 ook.getT
@@ -1665,25 +1665,138 @@
 ));%0A
-%0A        //Delete
+    %7D%0A%0A    @Test(dependsOnMethods = %22updateBook%22, expectedExceptions = EntityNotFoundException.class)%0A    public void crudBook() %7B
 %0A   
@@ -1841,23 +1841,17 @@
 .remove(
-updateB
+b
 ook);%0A  
@@ -1878,25 +1878,24 @@
 mmit();%0A
-%0A
         
 try %7B%0A  
@@ -1890,18 +1890,20 @@
     
-try %7B%0A    
+em.clear();%0A
     
@@ -1916,23 +1916,17 @@
 an.find(
-updateB
+b
 ook.getI
@@ -1935,139 +1935,8 @@
 ));%0A
-            Assert.assertTrue(false);%0A        %7D catch (EntityNotFoundException e) %7B%0A            Assert.assertTrue(true);%0A        %7D%0A
     

update BookCRUDTest