package meerkat.bulletinboard;

import meerkat.bulletinboard.sqlserver.BulletinBoardSQLServer;
import meerkat.bulletinboard.sqlserver.H2QueryProvider;
import meerkat.comm.CommunicationException;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.security.SignatureException;
import java.util.LinkedList;
import java.util.List;

/**
 * Created by Arbel on 6/27/2016.
 */
public class CachedBulletinBoardClientTest {

    private static final int THREAD_NUM = 3;

    private static final String LOCAL_DB_NAME = "localDB";
    private static final String REMOTE_DB_NAME = "remoteDB";
    private static final String QUEUE_DB_NAME = "queueDB";

    private static final int SUBSRCIPTION_DELAY = 500;
    private static final int SYNC_DELAY = 500;

    // Testers
    private CachedBulletinBoardClient cachedClient;
    private GenericBulletinBoardClientTester clientTest;
    private GenericSubscriptionClientTester subscriptionTester;

    public CachedBulletinBoardClientTest() throws CommunicationException {

        DeletableBulletinBoardServer localServer = new BulletinBoardSQLServer(new H2QueryProvider(LOCAL_DB_NAME));
        localServer.init();
        LocalBulletinBoardClient localClient = new LocalBulletinBoardClient(localServer, THREAD_NUM, SUBSRCIPTION_DELAY);

        DeletableBulletinBoardServer remoteServer = new BulletinBoardSQLServer(new H2QueryProvider(REMOTE_DB_NAME));
        remoteServer.init();
        LocalBulletinBoardClient remoteClient = new LocalBulletinBoardClient(remoteServer, THREAD_NUM, SUBSRCIPTION_DELAY);

        DeletableBulletinBoardServer queueServer = new BulletinBoardSQLServer(new H2QueryProvider(QUEUE_DB_NAME));
        queueServer.init();
        LocalBulletinBoardClient queueClient = new LocalBulletinBoardClient(queueServer, THREAD_NUM, SUBSRCIPTION_DELAY);

        List<SubscriptionBulletinBoardClient> clientList = new LinkedList<>();
        clientList.add(remoteClient);

        BulletinBoardSubscriber subscriber = new ThreadedBulletinBoardSubscriber(clientList, localClient);

        cachedClient = new CachedBulletinBoardClient(localClient, remoteClient, subscriber, queueClient, SYNC_DELAY, SYNC_DELAY);
        subscriptionTester = new GenericSubscriptionClientTester(cachedClient);
        clientTest = new GenericBulletinBoardClientTester(cachedClient, 87351);

    }

    // Test methods

    /**
     * Takes care of initializing the client and the test resources
     */
    @Before
    public void init(){

        clientTest.init();

    }

    /**
     * Closes the client and makes sure the test fails when an exception occurred in a separate thread
     */

    @After
    public void close() {

        cachedClient.close();
        clientTest.close();

    }

    @Test
    public void testPost() {

        clientTest.testPost();

    }

    @Test
    public void testBatchPost() throws CommunicationException, SignatureException, InterruptedException {

        clientTest.testBatchPost();
    }

    @Test
    public void testCompleteBatchPost() throws CommunicationException, SignatureException, InterruptedException {

        clientTest.testCompleteBatchPost();

    }

    @Test
    public void testSubscription() throws SignatureException, CommunicationException {

//        subscriptionTester.init();
//        subscriptionTester.subscriptionTest();
//        subscriptionTester.close();

    }

}
