package meerkat.bulletinboard;

import meerkat.bulletinboard.sqlserver.BulletinBoardSQLServer;
import meerkat.bulletinboard.sqlserver.SQLiteQueryProvider;
import meerkat.comm.CommunicationException;
import meerkat.protobuf.*;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.lang.management.ManagementFactory;
import java.lang.management.ThreadMXBean;
import java.security.*;
import java.security.cert.CertificateException;

import static org.junit.Assert.fail;

/**
 * Created by Arbel Deutsch Peled on 07-Dec-15.
 */
public class SQLiteBulletinBoardServerTest{

    private String testFilename = "SQLiteDBTest.db";

    private GenericBulletinBoardServerTest serverTest;

    private final ThreadMXBean threadBean = ManagementFactory.getThreadMXBean(); // Used to time the tests

    @Before
    public void init(){

        System.err.println("Starting to initialize SQLiteBulletinBoardServerTest");
        long start = threadBean.getCurrentThreadCpuTime();

        File old = new File(testFilename);
        old.delete();

        BulletinBoardServer bulletinBoardServer = new BulletinBoardSQLServer(new SQLiteQueryProvider(testFilename));
        try {
            bulletinBoardServer.init();

        } catch (CommunicationException e) {
            System.err.println(e.getMessage());
            fail(e.getMessage());
            return;
        }

        serverTest = new GenericBulletinBoardServerTest();
        try {
            serverTest.init(bulletinBoardServer);
        } catch (Exception e) {
            System.err.println(e.getMessage());
            fail(e.getMessage());
        }

        long end = threadBean.getCurrentThreadCpuTime();
        System.err.println("Finished initializing SQLiteBulletinBoardServerTest");
        System.err.println("Time of operation: " + (end - start));
    }

//    @Test
    public void bulkTest() {
        System.err.println("Starting bulkTest of SQLiteBulletinBoardServerTest");
        long start = threadBean.getCurrentThreadCpuTime();

        try {
            serverTest.testInsert();
        } catch (Exception e) {
            System.err.println(e.getMessage());
            fail(e.getMessage());
        }

        try{
            serverTest.testSimpleTagAndSignature();
        } catch (Exception e) {
            System.err.println(e.getMessage());
            fail(e.getMessage());
        }

        try{
            serverTest.testEnhancedTagsAndSignatures();
        } catch (Exception e) {
            System.err.println(e.getMessage());
            fail(e.getMessage());
        }

        long end = threadBean.getCurrentThreadCpuTime();
        System.err.println("Finished bulkTest of SQLiteBulletinBoardServerTest");
        System.err.println("Time of operation: " + (end - start));
    }

//    @Test
    public void testBatch() {

        final int BATCH_NUM = 20;

        try{
            for (int i = 0 ; i < BATCH_NUM ; i++) {
                serverTest.testPostBatch();
            }
        } catch (Exception e) {
            System.err.println(e.getMessage());
            fail(e.getMessage());
        }

        try{
            serverTest.testReadBatch();
        } catch (Exception e) {
            System.err.println(e.getMessage());
            fail(e.getMessage());
        }

    }

    @After
    public void close() {
        System.err.println("Starting to close SQLiteBulletinBoardServerTest");
        long start = threadBean.getCurrentThreadCpuTime();

        serverTest.close();

        long end = threadBean.getCurrentThreadCpuTime();
        System.err.println("Finished closing SQLiteBulletinBoardServerTest");
        System.err.println("Time of operation: " + (end - start));
    }

}
