

package org.seaborne.delta.server.patchstores;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.test.TestingServer;
import org.junit.After;
import org.junit.Before;
import org.seaborne.delta.lib.LogX;
import org.seaborne.delta.server.ZkT;
import org.seaborne.delta.server.local.patchstores.PatchStorage;
import org.seaborne.delta.server.local.patchstores.zk.PatchStorageZk;
import org.seaborne.delta.zk.Zk;

public class TestPatchStorageZk extends AbstractTestPatchStorage {
    static { LogX.setJavaLogging(); }

     private static int counter = 0 ;
     private TestingServer server = null;
     private CuratorFramework client = null;
     private String patches = "/patches-"+(counter++);

     @Before public void before() {
        try {
            server = ZkT.localServer();
            server.start();
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new RuntimeException(ex);
        }
        String connectionString = "localhost:"+server.getPort();
        client = Zk.curator(connectionString);
        try {
            client.blockUntilConnected();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    @After public void after() {
        client.close();
        try {
            server.close();
        } catch (Exception ex) {
            ex.printStackTrace();
            throw new RuntimeException(ex);
        }
    }

    @Override
    protected PatchStorage patchStorage() {
        return new PatchStorageZk(client, "0", patches);
    }
}
