use of io.etcd.jetcd.kv.GetResponse in project jetcd by coreos.
the class KVTest method testNestedTxn.
@Test
public void testNestedTxn() throws Exception {
ByteSequence foo = bytesOf("txn_foo");
ByteSequence bar = bytesOf("txn_bar");
ByteSequence barz = bytesOf("txn_barz");
ByteSequence abc = bytesOf("txn_abc");
ByteSequence oneTwoThree = bytesOf("txn_123");
Txn txn = kvClient.txn();
Cmp cmp = new Cmp(foo, Cmp.Op.EQUAL, CmpTarget.version(0));
CompletableFuture<io.etcd.jetcd.kv.TxnResponse> txnResp = txn.If(cmp).Then(Op.put(foo, bar, PutOption.DEFAULT), Op.txn(null, new Op[] { Op.put(abc, oneTwoThree, PutOption.DEFAULT) }, null)).Else(Op.put(foo, barz, PutOption.DEFAULT)).commit();
txnResp.get();
GetResponse getResp = kvClient.get(foo).get();
assertThat(getResp.getKvs()).hasSize(1);
assertThat(getResp.getKvs().get(0).getValue().toString(UTF_8)).isEqualTo(bar.toString(UTF_8));
GetResponse getResp2 = kvClient.get(abc).get();
assertThat(getResp2.getKvs()).hasSize(1);
assertThat(getResp2.getKvs().get(0).getValue().toString(UTF_8)).isEqualTo(oneTwoThree.toString(UTF_8));
}
use of io.etcd.jetcd.kv.GetResponse in project jetcd by coreos.
the class KVTest method testGetAndDeleteWithPrefix.
@Test
public void testGetAndDeleteWithPrefix() throws Exception {
String prefix = randomString();
ByteSequence key = bytesOf(prefix);
int numPrefixes = 10;
putKeysWithPrefix(prefix, numPrefixes);
// verify get withPrefix.
CompletableFuture<GetResponse> getFuture = kvClient.get(key, GetOption.newBuilder().isPrefix(true).build());
GetResponse getResp = getFuture.get();
assertThat(getResp.getCount()).isEqualTo(numPrefixes);
// verify del withPrefix.
DeleteOption deleteOpt = DeleteOption.newBuilder().isPrefix(true).build();
CompletableFuture<DeleteResponse> delFuture = kvClient.delete(key, deleteOpt);
DeleteResponse delResp = delFuture.get();
assertThat(delResp.getDeleted()).isEqualTo(numPrefixes);
}
use of io.etcd.jetcd.kv.GetResponse in project jetcd by coreos.
the class KVTest method testGet.
@Test
public void testGet() throws Exception {
CompletableFuture<PutResponse> feature = kvClient.put(SAMPLE_KEY_2, SAMPLE_VALUE_2);
feature.get();
CompletableFuture<GetResponse> getFeature = kvClient.get(SAMPLE_KEY_2);
GetResponse response = getFeature.get();
assertThat(response.getKvs()).hasSize(1);
assertThat(response.getKvs().get(0).getValue().toString(UTF_8)).isEqualTo(SAMPLE_VALUE_2.toString(UTF_8));
assertThat(!response.isMore()).isTrue();
}
use of io.etcd.jetcd.kv.GetResponse in project jetcd by coreos.
the class KVTest method testGetSortedPrefix.
@Test
public void testGetSortedPrefix() throws Exception {
String prefix = randomString();
int numPrefix = 3;
putKeysWithPrefix(prefix, numPrefix);
GetOption option = GetOption.newBuilder().withSortField(SortTarget.KEY).withSortOrder(SortOrder.DESCEND).isPrefix(true).build();
CompletableFuture<GetResponse> getFeature = kvClient.get(bytesOf(prefix), option);
GetResponse response = getFeature.get();
assertThat(response.getKvs()).hasSize(numPrefix);
for (int i = 0; i < numPrefix; i++) {
assertThat(response.getKvs().get(i).getKey().toString(UTF_8)).isEqualTo(prefix + (numPrefix - i - 1));
assertThat(response.getKvs().get(i).getValue().toString(UTF_8)).isEqualTo(String.valueOf(numPrefix - i - 1));
}
}
use of io.etcd.jetcd.kv.GetResponse in project jetcd by coreos.
the class KVNamespaceTest method assertNonexistentKey.
private static void assertNonexistentKey(KV kvClient, ByteSequence key) throws Exception {
CompletableFuture<GetResponse> getFeature = kvClient.get(key);
GetResponse response = getFeature.get();
assertThat(response.getKvs().size()).isEqualTo(0);
}
Aggregations