| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
package org.aeonbits.owner.loaders; |
| 10 |
|
| 11 |
import org.aeonbits.owner.Config; |
| 12 |
import org.aeonbits.owner.ConfigFactory; |
| 13 |
import org.aeonbits.owner.Factory; |
| 14 |
import org.apache.curator.RetryPolicy; |
| 15 |
import org.apache.curator.RetrySleeper; |
| 16 |
import org.apache.curator.framework.CuratorFramework; |
| 17 |
import org.apache.curator.framework.CuratorFrameworkFactory; |
| 18 |
import org.apache.curator.test.TestingServer; |
| 19 |
import org.apache.curator.utils.ZKPaths; |
| 20 |
import org.junit.After; |
| 21 |
import org.junit.Before; |
| 22 |
import org.junit.Test; |
| 23 |
|
| 24 |
import java.io.IOException; |
| 25 |
import java.util.List; |
| 26 |
|
| 27 |
import static java.util.Arrays.asList; |
| 28 |
import static java.util.concurrent.TimeUnit.SECONDS; |
| 29 |
import static org.aeonbits.owner.Config.Sources; |
| 30 |
import static org.junit.Assert.*; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public class ZooKeeperLoaderTest { |
| 37 |
private TestingServer server; |
| 38 |
private Factory configFactory; |
| 39 |
|
| 40 |
@Sources("zookeeper://127.0.0.1:65403/test") |
| 41 |
public static interface ZooKeeperConfig extends Config { |
| 42 |
String thanks(); |
| 43 |
List<String> greetings(); |
| 44 |
String notAvailable(); |
| 45 |
} |
| 46 |
|
| 47 |
@Sources("zookeeper://127.0.0.1:65403/wrong") |
| 48 |
public static interface ZooKeeperWrongPathConfig extends Config { |
| 49 |
String thanks(); |
| 50 |
List<String> greetings(); |
| 51 |
String notAvailable(); |
| 52 |
} |
| 53 |
|
| 54 |
@Test |
| 55 |
public void shouldLoadPropertiesFromZookeeperSource() throws Exception { |
| 56 |
ZooKeeperConfig sample = configFactory.create(ZooKeeperConfig.class); |
| 57 |
assertEquals("welcome", sample.thanks()); |
| 58 |
assertTrue(sample.greetings().containsAll(asList("hi", "bonjour", "hiya", "hi!"))); |
| 59 |
assertNull(sample.notAvailable()); |
| 60 |
} |
| 61 |
|
| 62 |
@Test |
| 63 |
public void whenPathIsWrongEverythingIsNull() throws Exception { |
| 64 |
ZooKeeperWrongPathConfig config = configFactory.create(ZooKeeperWrongPathConfig.class); |
| 65 |
assertNull(config.notAvailable()); |
| 66 |
assertNull(config.greetings()); |
| 67 |
assertNull(config.thanks()); |
| 68 |
} |
| 69 |
|
| 70 |
@Before |
| 71 |
public void before() throws Exception { |
| 72 |
server = new TestingServer(65403); |
| 73 |
server.start(); |
| 74 |
|
| 75 |
String connectString = server.getConnectString(); |
| 76 |
|
| 77 |
CuratorFramework client = CuratorFrameworkFactory.newClient(connectString, 50, 50, new RetryPolicy() { |
| 78 |
public boolean allowRetry(int retryCount, long elapsedTimeMs, RetrySleeper sleeper) { |
| 79 |
return false; |
| 80 |
} |
| 81 |
}); |
| 82 |
try { |
| 83 |
client.start(); |
| 84 |
client.blockUntilConnected(30, SECONDS); |
| 85 |
String basePath = "/test"; |
| 86 |
setDataInZookeperServer(client, basePath, "thanks", "welcome"); |
| 87 |
setDataInZookeperServer(client, basePath, "greetings", "hi,bonjour,hiya,hi!"); |
| 88 |
} finally { |
| 89 |
client.close(); |
| 90 |
} |
| 91 |
|
| 92 |
configFactory = ConfigFactory.newInstance(); |
| 93 |
configFactory.registerLoader(new ZooKeeperLoader()); |
| 94 |
} |
| 95 |
|
| 96 |
private void setDataInZookeperServer(CuratorFramework client, |
| 97 |
String basePath, String property, String value) throws Exception { |
| 98 |
String path = ZKPaths.makePath(basePath, property); |
| 99 |
client.create().creatingParentsIfNeeded().forPath(path, value.getBytes()); |
| 100 |
} |
| 101 |
|
| 102 |
@After |
| 103 |
public void after() throws IOException { |
| 104 |
server.stop(); |
| 105 |
} |
| 106 |
} |