package meerkat.bulletinboard;

import meerkat.comm.CommunicationException;

import meerkat.protobuf.Voting.*;

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 Deutsch Peled on 05-Dec-15.
 */
public class ThreadedBulletinBoardClientIntegrationTest {

    // Server data

    private static String PROP_GETTY_URL = "gretty.httpBaseURI";
    private static String DEFAULT_BASE_URL = "http://localhost:8081";
    private static String BASE_URL =  System.getProperty(PROP_GETTY_URL, DEFAULT_BASE_URL);

    // Tester
    private GenericBulletinBoardClientTester clientTest;

    public ThreadedBulletinBoardClientIntegrationTest(){

        ThreadedBulletinBoardClient client = new ThreadedBulletinBoardClient(3,0,500);

        List<String> testDB = new LinkedList<>();
        testDB.add(BASE_URL);

        client.init(BulletinBoardClientParams.newBuilder()
                .addAllBulletinBoardAddress(testDB)
                .setMinRedundancy((float) 1.0)
                .build());

        clientTest = new GenericBulletinBoardClientTester(client, 52351);

    }

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

    }

}
