package com.unidev.polydata;

import com.unidev.polydata.domain.BasicPoly;
import com.unidev.polydata.domain.Poly;
import org.junit.Before;
import org.junit.Test;
import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
import static org.hamcrest.Matchers.not;
import static org.hamcrest.Matchers.nullValue;


import java.io.File;
import java.sql.Connection;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.Arrays;
import java.util.Optional;

/**
 * SQLite storage tests
 */
public class SQLiteStorageTest {

    SQLitePolyMigrator migrator = new SQLitePolyMigrator() {
        @Override
        public boolean canHandle(String poly) {
            return "poly".equalsIgnoreCase(poly);
        }

        @Override
        public void handle(String poly, Connection connection) throws SQLiteStorageException {
            try {
                Statement statement = connection.createStatement();
                statement.executeUpdate("CREATE TABLE IF NOT EXISTS "+poly+" (_id TEXT PRIMARY KEY, value TEXT)");
            } catch (SQLException e) {
                throw new SQLiteStorageException(e);
            }
        }
    };

    @Before
    public void setup() {
        new File("/tmp/testdb.db").delete();
    }

    @Test
    public void testStorageSaveLoad() throws SQLiteStorageException {

        SQLiteStorage sqLiteStorage = new SQLiteStorage("/tmp/testdb.db");
        sqLiteStorage.setPolyMigrators(Arrays.asList(migrator));

        BasicPoly basicPoly = BasicPoly.newPoly("potato");
        basicPoly.put("value", "tomato");


        sqLiteStorage.save("poly", basicPoly);

        Optional<BasicPoly> polyOptional = sqLiteStorage.fetch("poly", "potato");
        assertThat(polyOptional.isPresent(), is(true));

        Poly poly = polyOptional.get();
        assertThat(poly._id(), is("potato"));
        assertThat(poly.get("value"), is("tomato"));


        Optional<BasicPoly> polyOptional2 = sqLiteStorage.fetch("poly", "tomato");
        assertThat(polyOptional2.isPresent(), is(false));
    }

    @Test
    public void testSaveUpdate() throws SQLiteStorageException {
        SQLiteStorage sqLiteStorage = new SQLiteStorage("/tmp/testdb.db");
        sqLiteStorage.setPolyMigrators(Arrays.asList(migrator));

        BasicPoly basicPoly = BasicPoly.newPoly("potato");
        basicPoly.put("value", "tomato");

        sqLiteStorage.save("poly", basicPoly);

        BasicPoly poly = sqLiteStorage.fetch("poly", "potato").get();

        assertThat(poly.get("value"), is("tomato"));


        poly.put("value", "another potato");
        sqLiteStorage.save("poly", poly);

        poly = sqLiteStorage.fetch("poly", "potato").get();
        assertThat(poly.get("value"), is("another potato"));


    }


}

@@ -1600,25 +1600,24 @@
 %22tomato%22);%0A%0A
-%0A
         sqLi
@@ -2778,23 +2778,693 @@
 potato%22));%0A%0A
+    %7D%0A%0A    @Test%0A    public void testPolyRemoval() throws SQLiteStorageException %7B%0A%0A        SQLiteStorage sqLiteStorage = new SQLiteStorage(%22/tmp/testdb.db%22);%0A        sqLiteStorage.setPolyMigrators(Arrays.asList(migrator));%0A%0A        BasicPoly basicPoly = BasicPoly.newPoly(%22potato%22);%0A        basicPoly.put(%22value%22, %22tomato%22);%0A%0A        Optional%3CBasicPoly%3E fetch;%0A%0A        sqLiteStorage.save(%22poly%22, basicPoly);%0A%0A        fetch = sqLiteStorage.fetch(%22poly%22, %22potato%22);%0A        assertThat(fetch.isPresent(), is(true));%0A%0A        sqLiteStorage.remove(%22poly%22, %22potato%22);%0A%0A        fetch = sqLiteStorage.fetch(%22poly%22, %22potato%22);%0A        assertThat(fetch.isPresent(), is(false));%0A
 %0A    %7D%0A%0A%0A%7D%0A

Test case for poly removal