/**
 * 
 */
package com.github.phantomthief.zookeeper.test;

import static com.google.common.util.concurrent.Uninterruptibles.sleepUninterruptibly;

import java.io.IOException;
import java.util.concurrent.TimeUnit;

import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.apache.curator.test.TestingServer;
import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.github.phantomthief.zookeeper.ZkBasedNodeResource;

/**
 * @author w.vela
 */
public class ZkNodeTest {

    private static TestingServer testingServer;
    private static CuratorFramework curatorFramework;

    @BeforeClass
    public static void init() throws Exception {
        testingServer = new TestingServer(true);
        curatorFramework = CuratorFrameworkFactory.newClient(testingServer.getConnectString(),
                new ExponentialBackoffRetry(10000, 20));
        curatorFramework.start();
        curatorFramework.create().forPath("/test", "test1".getBytes());
    }

    @Test
    public void testChange() throws Exception {
        ZkBasedNodeResource<String> node = ZkBasedNodeResource.<String> newBuilder() //
                .withCacheFactory("/test", curatorFramework) //
                .withFactory(bs -> new String(bs)) //
                .onResourceChange((current, old) -> {
                    System.out.println("current:" + current + ",old:" + old);
                    assert(current.equals("test2"));
                    assert(old.equals("test1"));
                }) //
                .build();
        System.out.println("current:" + node.get());
        assert(node.get().equals("test1"));
        curatorFramework.setData().forPath("/test", "test2".getBytes());
        sleepUninterruptibly(1, TimeUnit.SECONDS);
        System.out.println("current:" + node.get());
        assert(node.get().equals("test2"));
        sleepUninterruptibly(5, TimeUnit.SECONDS);
    }

    @AfterClass
    public static void destroy() throws IOException {
        curatorFramework.close();
        testingServer.close();
    }
}

@@ -2027,24 +2027,963 @@
 DS);%0A    %7D%0A%0A
+    @Test%0A    public void testEmpty() throws Exception %7B%0A        ZkBasedNodeResource%3CString%3E node = ZkBasedNodeResource.%3CString%3E newBuilder() //%0A                .withCacheFactory(%22/test2%22, curatorFramework) //%0A                .withFactory(bs -%3E new String(bs)) //%0A                .withEmptyObject(%22EMPTY%22) //%0A                .build();%0A        System.out.println(%22current:%22 + node.get());%0A        assert(node.get().equals(%22EMPTY%22));%0A        curatorFramework.create().creatingParentsIfNeeded().forPath(%22/test2%22, %22haha%22.getBytes());%0A        sleepUninterruptibly(1, TimeUnit.SECONDS);%0A        System.out.println(%22current:%22 + node.get());%0A        assert(node.get().equals(%22haha%22)); //%0A        sleepUninterruptibly(1, TimeUnit.SECONDS);%0A        curatorFramework.delete().forPath(%22/test2%22);%0A        sleepUninterruptibly(1, TimeUnit.SECONDS);%0A        System.out.println(%22current:%22 + node.get());%0A        assert(node.get().equals(%22EMPTY%22));%0A    %7D%0A%0A
     @AfterCl

add test case for empty node.