private static JsonObject getTree(KeyValueList list, int prefix, String delimiter) { JsonObject tree = new JsonObject(); for (KeyValue keyValue : list.getList()) { if (keyValue.getKey().endsWith(delimiter)) { continue; } JsonObject json = tree; String[] arr = keyValue.getKey().substring(prefix).split(delimiter); for (int i = 0; i < arr.length; i++) { String key = arr[i]; if (i == arr.length - 1) { json.put(key, keyValue.getValue()); } else { JsonObject next = json.getJsonObject(key); if (next == null) { next = new JsonObject(); json.put(key, next); } json = next; } } } return tree; }
private void getEntries(TestContext tc, String prefix, Handler<List<String>> resultHandler) { ctx.readClient().getValues(prefix, tc.asyncAssertSuccess(list -> { resultHandler.handle(list.getList().stream() .map(kv -> kv.getKey() + "/" + kv.getValue()).collect(Collectors.toList())); })); }
private void checkKeyValueList(KeyValueList expected, KeyValueList actual) { assertEquals(expected, actual); assertEquals(expected.hashCode(), actual.hashCode()); assertEquals(expected.getIndex(), actual.getIndex()); List<KeyValue> expectedList = expected.getList(); List<KeyValue> actualList = actual.getList(); assertTrue(expectedList.containsAll(actualList)); assertTrue(actualList.containsAll(expectedList)); }
private void valuesAccess(TestContext tc, ConsulClient accessClient) { String key1 = randomFooBarAlpha(); String value1 = randomAlphaString(10); String key2 = randomFooBarAlpha(); String value2 = randomAlphaString(10); ctx.writeClient().putValue(key1, value1, tc.asyncAssertSuccess(b1 -> { tc.assertTrue(b1); ctx.writeClient().putValue(key2, value2, tc.asyncAssertSuccess(b2 -> { tc.assertTrue(b2); accessClient.getValues("foo/bar", tc.asyncAssertSuccess(kvList -> { List<KeyValue> list = kvList.getList(); tc.assertEquals(list.size(), 2); tc.assertTrue(list.stream() .filter(kv -> kv.getKey().equals(key1) && kv.getValue().equals(value1)) .count() == 1); tc.assertTrue(list.stream() .filter(kv -> kv.getKey().equals(key2) && kv.getValue().equals(value2)) .count() == 1); ctx.writeClient().deleteValues("foo/bar", tc.asyncAssertSuccess()); })); })); })); }
@Test public void testKeyPrefix() throws InterruptedException { StateConsumer<String> consumer = new StateConsumer<>(); String keyPrefix = ConsulContext.KEY_RW_PREFIX + randomAlphaString(10); String k1 = keyPrefix + randomAlphaString(10); String k2 = keyPrefix + randomAlphaString(10); String v1 = randomAlphaString(10); String v2 = randomAlphaString(10); assertTrue(getAsync(h -> ctx.writeClient().putValue(k1, v1, h))); Watch<KeyValueList> watch = Watch.keyPrefix(keyPrefix, vertx, ctx.readClientOptions()) .setHandler(kv -> { if (kv.succeeded()) { if (kv.nextResult().isPresent()) { consumer.consume(kv.nextResult().getList().stream().map(KeyValue::getValue).sorted().collect(Collectors.joining("/"))); } else { consumer.consume(EMPTY_MESSAGE); } } else { consumer.consume(kv.cause().getMessage()); } }) .start(); consumer.await(v1); assertTrue(getAsync(h -> ctx.writeClient().putValue(k2, v2, h))); consumer.await(Stream.of(v1, v2).sorted().collect(Collectors.joining("/"))); runAsync(h -> ctx.writeClient().deleteValues(keyPrefix, h)); consumer.await(EMPTY_MESSAGE); consumer.check(); watch.stop(); }
private void blockingQuery(TestContext tc, BiConsumer<String, Handler<Long>> indexSupplier) { String key = randomFooBarAlpha(); String value = randomAlphaString(10); ctx.writeClient() .putValue(key, randomAlphaString(10), tc.asyncAssertSuccess(b1 -> { tc.assertTrue(b1); indexSupplier.accept(key, consulIndex -> { Async async = tc.async(2); vertx.setTimer(TimeUnit.SECONDS.toMillis(2), l -> { ctx.writeClient().putValue(key, value, tc.asyncAssertSuccess(b2 -> { tc.assertTrue(b2); ctx.readClient().getValueWithOptions(key, new BlockingQueryOptions().setIndex(consulIndex), tc.asyncAssertSuccess(kv -> { tc.assertTrue(kv.getModifyIndex() > consulIndex); tc.assertEquals(kv.getValue(), value); async.countDown(); })); ctx.readClient().getValuesWithOptions("foo/bar", new BlockingQueryOptions().setIndex(consulIndex), tc.asyncAssertSuccess(kv -> { tc.assertTrue(kv.getIndex() > consulIndex); tc.assertTrue(kv.getList().size() == 1); async.countDown(); })); })); }); async.handler(v -> ctx.writeClient().deleteValue(key, tc.asyncAssertSuccess())); }); })); } }