|
| 1 | +package com.baeldung.apache.curator.configuration; |
| 2 | + |
| 3 | +import static org.assertj.core.api.Assertions.assertThat; |
| 4 | + |
| 5 | +import java.util.ArrayList; |
| 6 | +import java.util.List; |
| 7 | +import java.util.UUID; |
| 8 | +import java.util.concurrent.atomic.AtomicBoolean; |
| 9 | + |
| 10 | +import org.apache.curator.framework.CuratorFramework; |
| 11 | +import org.apache.curator.x.async.AsyncCuratorFramework; |
| 12 | +import org.junit.Test; |
| 13 | + |
| 14 | +import com.baeldung.apache.curator.BaseTest; |
| 15 | + |
| 16 | +public class ConfigurationManagementManualTest extends BaseTest { |
| 17 | + |
| 18 | + private static final String KEY_FORMAT = "/%s"; |
| 19 | + |
| 20 | + @Test |
| 21 | + public void givenPath_whenCreateKey_thenValueIsStored() throws Exception { |
| 22 | + try (CuratorFramework client = newClient()) { |
| 23 | + client.start(); |
| 24 | + AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client); |
| 25 | + String key = getKey(); |
| 26 | + String expected = "my_value"; |
| 27 | + |
| 28 | + // Create key nodes structure |
| 29 | + client |
| 30 | + .create() |
| 31 | + .forPath(key); |
| 32 | + |
| 33 | + // Set data value for our key |
| 34 | + async |
| 35 | + .setData() |
| 36 | + .forPath(key, expected.getBytes()); |
| 37 | + |
| 38 | + // Get data value |
| 39 | + AtomicBoolean isEquals = new AtomicBoolean(); |
| 40 | + async |
| 41 | + .getData() |
| 42 | + .forPath(key) |
| 43 | + .thenAccept(data -> isEquals.set(new String(data).equals(expected))); |
| 44 | + |
| 45 | + Thread.sleep(1000); |
| 46 | + |
| 47 | + assertThat(isEquals.get()).isTrue(); |
| 48 | + } |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + public void givenPath_whenWatchAKeyAndStoreAValue_thenWatcherIsTriggered() throws Exception { |
| 53 | + try (CuratorFramework client = newClient()) { |
| 54 | + client.start(); |
| 55 | + AsyncCuratorFramework async = AsyncCuratorFramework.wrap(client); |
| 56 | + String key = getKey(); |
| 57 | + String expected = "my_value"; |
| 58 | + |
| 59 | + // Create key structure |
| 60 | + async |
| 61 | + .create() |
| 62 | + .forPath(key); |
| 63 | + |
| 64 | + List<String> changes = new ArrayList<>(); |
| 65 | + |
| 66 | + // Watch data value |
| 67 | + async |
| 68 | + .watched() |
| 69 | + .getData() |
| 70 | + .forPath(key) |
| 71 | + .event() |
| 72 | + .thenAccept(watchedEvent -> { |
| 73 | + try { |
| 74 | + changes.add(new String(client |
| 75 | + .getData() |
| 76 | + .forPath(watchedEvent.getPath()))); |
| 77 | + } catch (Exception e) { |
| 78 | + // fail ... |
| 79 | + } |
| 80 | + }); |
| 81 | + |
| 82 | + // Set data value for our key |
| 83 | + async |
| 84 | + .setData() |
| 85 | + .forPath(key, expected.getBytes()); |
| 86 | + |
| 87 | + Thread.sleep(1000); |
| 88 | + |
| 89 | + assertThat(changes.size() > 0).isTrue(); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + private String getKey() { |
| 94 | + return String.format(KEY_FORMAT, UUID |
| 95 | + .randomUUID() |
| 96 | + .toString()); |
| 97 | + } |
| 98 | +} |
0 commit comments