package com.smartbullet.easyorm;

import com.smartbullet.easyorm.config.EasyORMTest;
import com.smartbullet.easyorm.database.query.sql.common.LogicalOperator;
import com.smartbullet.easyorm.domain.Book;
import com.smartbullet.easyorm.domain.BookType;
import java.util.List;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import org.junit.FixMethodOrder;
import org.junit.Test;
import org.junit.runners.MethodSorters;

/**
 * Basic Auto query tests. This class serves as test class and implementation
 * example on how to use easyORM.
 *
 */
@FixMethodOrder(MethodSorters.NAME_ASCENDING)
public class EasyClassDaoTest extends EasyORMTest {

    @Test
    public void filterTest() throws Exception {
        // GET ALL BOOKS
        List<Book> findAllBooks = EasyORM.autoSQL(datasource).findMany(new Book(),
                LogicalOperator.AND, false);
        assertTrue(findAllBooks.size() == 7);

        findAllBooks.forEach((book) -> {
            EasyORM.autoSQL(datasource).delete(book);
        });

        List<Book> findBooks = EasyORM.autoSQL(datasource).findMany(new Book(),
                LogicalOperator.AND, false);
        assertTrue(findBooks.isEmpty());
    }

    @Test
    public void insertTest() throws Exception {
        Book insertBook = new Book();
        //INSERT TEST
        for (int x = 0; x < 10; x++) {
            insertBook.setTitle("ORM_" + x);
            EasyORM.autoSQL(datasource).insert(insertBook);
        }
        // GET ALL BOOKS
        List<Book> insertedBooks = EasyORM.autoSQL(datasource).findMany(new Book(),
                LogicalOperator.AND, false);
        assertTrue(!insertedBooks.isEmpty());
    }

    @Test
    public void deleteTest() throws Exception {
        //DELETE ALL RECORDS
        List<Book> books = EasyORM.autoSQL(datasource).findMany(new Book(),
                LogicalOperator.AND, false);
        books.forEach((book) -> {
            EasyORM.autoSQL(datasource).delete(book);
        });
        //GET ALL BOOKS
        List<Book> deletedBooks = EasyORM.autoSQL(datasource).findMany(new Book(),
                LogicalOperator.AND, false);
        assertTrue(deletedBooks.isEmpty());
    }

    @Test
    public void updateTest() throws Exception {
        Book insertBook = new Book();
        //INSERT TEST
        for (int x = 0; x < 5; x++) {
            insertBook.setTitle("ORM_" + x);
            EasyORM.autoSQL(datasource).insert(insertBook);
        }

        for (int x = 0; x < 5; x++) {
            Book findBook = new Book();
            findBook.setTitle("ORM_" + x);
            Book book = EasyORM.autoSQL(datasource).find(findBook, LogicalOperator.AND, false);
            book.setTitle("ORM_Updated_" + x);
            EasyORM.autoSQL(datasource).update(book);
            //GET ALL BOOKS
            Book updateBooks = EasyORM.autoSQL(datasource).find(findBook,
                    LogicalOperator.AND, false);
            assertNull(updateBooks);
        }
    }

    @Test
    public void doParentChildClassFilter() throws Exception {
        //SET UP TEST DATA TO FIND RESULTS
        Book book = new Book();
        book.setTitle("ORM");
        EasyORM.autoSQL(datasource).insert(book);
        Book bookInserted = EasyORM.autoSQL(datasource).find(book, LogicalOperator.AND, false);

        BookType type = new BookType();
        type.setType("Refelection");
        type.setBookId(bookInserted.getId());
        EasyORM.autoSQL(datasource).insert(type);

        //SELECT BOOK
        Book findBookByTitle = new Book();
        findBookByTitle.setTitle("ORM");
        Book bookFound = EasyORM.autoSQL(datasource).find(findBookByTitle, LogicalOperator.AND, true);
        assertNotNull(bookFound.getBookType().getTypeId());
    }

}
