package ch.hoene.perzist.jdbc;

import ch.hoene.perzist.access.filter.FieldFilter;
import ch.hoene.perzist.access.filter.Operator;
import ch.hoene.perzist.jdbc.db.Tables;
import ch.hoene.perzist.jdbc.db.db2instance.Db2Product;
import ch.hoene.perzist.model.Product;
import org.junit.Before;
import org.junit.Test;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertFalse;
import static junit.framework.Assert.assertTrue;

public class DatabaseJdbcTest {

    final TestDatabaseJdbc database = new TestDatabaseJdbc();

    final FieldFilter<Tables.Products, Product, String> cat1Filter =
            new FieldFilter<Tables.Products, Product, String>(
                    Tables.Products.CATALOG,
                    Operator.EQ,
                    "cat1"
            );

    @Before
    public void setUp() throws Exception {
        database.create();
    }

    @Test
    public void testPush() throws Exception {
        assertTrue(database.persistProduct(new Product(0, "desc0", "cat0")));
    }

    @Test
    public void testIsEmpty() throws Exception {
        assertTrue(database.isEmpty(null, Tables.PRODUCTS));
        assertTrue(database.persistProduct(new Product(0, "desc0", "cat0")));
        assertFalse(database.hasNoProducts());
    }

    @Test
    public void testCount() throws Exception {

        assertEquals(0, database.count(null, Tables.PRODUCTS));
        assertTrue(database.persistProduct(new Product(0, "desc0", "cat0")));
        assertEquals(1, database.count(null, Tables.PRODUCTS));
        assertTrue(database.persistProduct(new Product(1, "desc1", "cat1")));
        assertEquals(2, database.count(null, Tables.PRODUCTS));

        assertEquals(1, database.count(cat1Filter, Tables.PRODUCTS));
        assertTrue(database.persistProduct(new Product(2, "desc2", "cat1")));
        assertEquals(2, database.count(cat1Filter, Tables.PRODUCTS));
    }

    @Test
    public void testGetList() throws Exception {
        Set<Product> products = new HashSet<Product>();
        products.add(new Product(0, "desc0", "cat0"));
        products.add(new Product(1, "desc1", "cat0"));
        products.add(new Product(2, "desc2", "cat1"));
        products.add(new Product(3, "desc3", "cat1"));
        products.add(new Product(3, "desc3", "cat1"));

        for(Product product : products)
        {
            database.persistProduct(product);
        }

        Set<Product> productsExpected = new HashSet<Product>(products);
        List<Product> storedProducts = database.getList(null, new Db2Product(), Tables.PRODUCTS, Tables.PRODUCTS);
        for(Product storedProduct : storedProducts)
        {
            productsExpected.remove(storedProduct);
        }
        assertTrue(productsExpected.size() == 0);

        Set<Product> productsNotExpected = new HashSet<Product>(products);
        storedProducts = database.getList(cat1Filter, new Db2Product(), Tables.PRODUCTS, Tables.PRODUCTS);
        for(Product storedProduct : storedProducts)
        {
            productsNotExpected.remove(storedProduct);
        }
        assertTrue(productsNotExpected.size() == 2);


    }

    @Test
    public void testDrop() throws Exception {
        assertTrue(false);
    }
}

@@ -124,16 +124,63 @@
 erator;%0A
+import ch.hoene.perzist.access.sort.SortOrder;%0A
 import c
@@ -211,16 +211,16 @@
 Tables;%0A
-
 import c
@@ -302,24 +302,82 @@
 el.Product;%0A
+import ch.hoene.perzist.source.relational.FieldSortOrder;%0A
 import org.j
@@ -462,16 +462,38 @@
 l.List;%0A
+import java.util.Map;%0A
 import j
@@ -2481,33 +2481,33 @@
 add(new Product(
-3
+4
 , %22desc3%22, %22cat1
@@ -2977,24 +2977,46 @@
 e() == 0);%0A%0A
+        //with filter%0A
         Set%3C
@@ -3366,16 +3366,1063 @@
 == 2);%0A%0A
+        //with sort%0A        storedProducts = database.getList(null, new Db2Product(), Tables.PRODUCTS, Tables.PRODUCTS, new FieldSortOrder(Tables.Products.DESC, SortOrder.ASC));%0A        assertEquals(storedProducts.get(0).getDescription(), %22desc0%22);%0A        assertEquals(storedProducts.get(1).getDescription(), %22desc1%22);%0A        assertEquals(storedProducts.get(2).getDescription(), %22desc2%22);%0A        assertEquals(storedProducts.get(3).getDescription(), %22desc3%22);%0A        assertEquals(storedProducts.get(4).getDescription(), %22desc3%22);%0A%0A        storedProducts = database.getList(null, new Db2Product(), Tables.PRODUCTS, Tables.PRODUCTS, new FieldSortOrder(Tables.Products.DESC, SortOrder.DESC));%0A        assertEquals(storedProducts.get(0).getDescription(), %22desc3%22);%0A        assertEquals(storedProducts.get(1).getDescription(), %22desc3%22);%0A        assertEquals(storedProducts.get(2).getDescription(), %22desc2%22);%0A        assertEquals(storedProducts.get(3).getDescription(), %22desc1%22);%0A        assertEquals(storedProducts.get(4).getDescription(), %22desc0%22);
 %0A    %7D%0A%0A

test read with sort