package com.bazaarvoice.soa.zookeeper.dropwizard;

import org.codehaus.jackson.map.MappingJsonFactory;
import org.codehaus.jackson.map.ObjectMapper;
import org.junit.Test;

import java.io.IOException;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;

public class ZooKeeperConfigurationTest {
    private static final ObjectMapper JSON = new MappingJsonFactory().getCodec();

    @Test
    public void testJsonDefaults() throws IOException {
        ZooKeeperConfiguration config = fromJson("{}");
        assertEquals("localhost:2181", config.getConnectString());
        assertTrue(config.getRetryPolicy() instanceof com.netflix.curator.retry.BoundedExponentialBackoffRetry);
    }

    @Test
    public void testJsonConnectString() throws IOException {
        ZooKeeperConfiguration config = fromJson("{\"connectString\":\"prod-zk-1:12345\"}");
        assertEquals("prod-zk-1:12345", config.getConnectString());
    }

    @Test
    public void testJsonBoundedExponentialBackoffRetry() throws IOException {
        ZooKeeperConfiguration config = fromJson("{\"retry\":{\"baseSleepTimeMs\":100,\"maxSleepTimeMs\":1000,\"maxAttempts\":3}}");
        assertTrue(config.getRetryPolicy() instanceof com.netflix.curator.retry.BoundedExponentialBackoffRetry);
    }

    @Test
    public void testNamespace() throws IOException {
        ZooKeeperConfiguration config = fromJson("{\"namespace\":\"global\"}");
        assertEquals("global", config.getNamespace());
    }

    private ZooKeeperConfiguration fromJson(String json) throws IOException {
        return JSON.readValue(json, ZooKeeperConfiguration.class);
    }
}

@@ -44,16 +44,56 @@
 izard;%0A%0A
+import org.codehaus.jackson.JsonParser;%0A
 import o
@@ -136,16 +136,16 @@
 actory;%0A
-
 import o
@@ -439,18 +439,100 @@
 ry()
-.getCodec(
+%0A            .getCodec()%0A            .configure(JsonParser.Feature.ALLOW_SINGLE_QUOTES, true
 );%0A%0A
@@ -962,18 +962,17 @@
 mJson(%22%7B
-%5C%22
+'
 connectS
@@ -976,21 +976,19 @@
 ctString
-%5C%22:%5C%22
+':'
 prod-zk-
@@ -994,18 +994,17 @@
 -1:12345
-%5C%22
+'
 %7D%22);%0A   
@@ -1218,21 +1218,18 @@
 n(%22%7B
-%5C%22
+'
 retry
-%5C%22:%7B%5C%22
+':%7B'
 base
@@ -1243,17 +1243,13 @@
 meMs
-%5C%22:100,%5C%22
+':1,'
 maxS
@@ -1262,18 +1262,14 @@
 meMs
-%5C%22:1000,%5C%22
+':10,'
 maxA
@@ -1275,18 +1275,17 @@
 Attempts
-%5C%22
+'
 :3%7D%7D%22);%0A
@@ -1518,18 +1518,17 @@
 mJson(%22%7B
-%5C%22
+'
 namespac
@@ -1532,21 +1532,18 @@
 pace
-%5C%22:%5C%22
+':'
 global
-%5C%22
+'
 %7D%22);

Update quoting of strings inside of json for ZooKeeperConfigurationTest