package meerkat.bulletinboard;

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

import java.security.SignatureException;

/**
 * Created by Arbel Deutsch Peled on 05-Dec-15.
 */
public class LocalBulletinBoardClientTest {

    private static final int THREAD_NUM = 3;
    private static final String DB_NAME = "TestDB";

    private static final int SUBSRCIPTION_DELAY = 3000;

    // Testers
    private GenericBulletinBoardClientTester clientTest;
    private GenericSubscriptionClientTester subscriptionTester;

    public LocalBulletinBoardClientTest() throws CommunicationException {

        H2QueryProvider queryProvider = new H2QueryProvider(DB_NAME);

        DeletableBulletinBoardServer server = new BulletinBoardSQLServer(queryProvider);
        server.init();

        LocalBulletinBoardClient client = new LocalBulletinBoardClient(server, THREAD_NUM, SUBSRCIPTION_DELAY);
        subscriptionTester = new GenericSubscriptionClientTester(client);
        clientTest = new GenericBulletinBoardClientTester(client, 98354);

    }

    // 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() {

        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();

    }

}
