Search in sources :

Example 1 with RangeResponse

use of com.coreos.jetcd.api.RangeResponse in project jetcd by coreos.

the class KVTest method testDelete.

@Test(dependsOnMethods = "testPut")
public void testDelete() throws Exception {
    ByteString keyToDelete = ByteString.copyFrom("sample_key", "UTF-8");
    try {
        // count keys about to delete
        ListenableFuture<RangeResponse> getFeature = kvClient.get(keyToDelete);
        RangeResponse resp = getFeature.get();
        // delete the keys
        ListenableFuture<DeleteRangeResponse> deleteFuture = kvClient.delete(keyToDelete);
        DeleteRangeResponse delResp = deleteFuture.get();
        test.assertEquals(resp.getKvsList().size(), delResp.getDeleted());
    } catch (Exception e) {
    // empty
    }
}
Also used : DeleteRangeResponse(com.coreos.jetcd.api.DeleteRangeResponse) RangeResponse(com.coreos.jetcd.api.RangeResponse) ByteString(com.google.protobuf.ByteString) DeleteRangeResponse(com.coreos.jetcd.api.DeleteRangeResponse) ExecutionException(java.util.concurrent.ExecutionException) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 2 with RangeResponse

use of com.coreos.jetcd.api.RangeResponse in project jetcd by coreos.

the class KVTest method testTxn.

@Test
public void testTxn() throws Exception {
    ByteString sampleKey = ByteString.copyFrom("txn_key", "UTF-8");
    ByteString sampleValue = ByteString.copyFrom("xyz", "UTF-8");
    ByteString cmpValue = ByteString.copyFrom("abc", "UTF-8");
    ByteString putValue = ByteString.copyFrom("XYZ", "UTF-8");
    ByteString putValueNew = ByteString.copyFrom("ABC", "UTF-8");
    ListenableFuture<PutResponse> feature = kvClient.put(sampleKey, sampleValue);
    try {
        // put the original txn key value pair
        PutResponse putResp = feature.get();
        // construct txn operation
        Cmp cmp = new Cmp(sampleKey, Cmp.Op.GREATER, CmpTarget.value(cmpValue));
        Txn txn = Txn.newBuilder().If(cmp).Then(Op.put(sampleKey, putValue, PutOption.DEFAULT)).Else(Op.put(sampleKey, putValueNew, PutOption.DEFAULT)).build();
        ListenableFuture<TxnResponse> txnResp = kvClient.commit(txn);
        txnResp.get();
        // get the value
        RangeResponse getResp = kvClient.get(sampleKey).get();
        test.assertEquals(getResp.getKvsList().size(), 1);
        test.assertEquals(getResp.getKvs(0).getValue().toStringUtf8(), "XYZ");
    } catch (Exception e) {
    // empty
    }
}
Also used : DeleteRangeResponse(com.coreos.jetcd.api.DeleteRangeResponse) RangeResponse(com.coreos.jetcd.api.RangeResponse) ByteString(com.google.protobuf.ByteString) Cmp(com.coreos.jetcd.op.Cmp) Txn(com.coreos.jetcd.op.Txn) TxnResponse(com.coreos.jetcd.api.TxnResponse) PutResponse(com.coreos.jetcd.api.PutResponse) ExecutionException(java.util.concurrent.ExecutionException) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 3 with RangeResponse

use of com.coreos.jetcd.api.RangeResponse in project jetcd by coreos.

the class AuthClientTest method testKVWithAuth.

/**
   * put and range with auth client
   */
@Test(groups = "testAuth", dependsOnGroups = "authEnable", priority = 1)
public void testKVWithAuth() throws ExecutionException, InterruptedException {
    Throwable err = null;
    try {
        this.secureClient.getKVClient().put(testKey, testName).get();
        RangeResponse rangeResponse = this.secureClient.getKVClient().get(testKey).get();
        this.test.assertTrue(rangeResponse.getCount() != 0 && rangeResponse.getKvs(0).getValue().equals(testName));
    } catch (StatusRuntimeException sre) {
        err = sre;
    }
    this.test.assertNull(err, "KV put range test with auth");
}
Also used : RangeResponse(com.coreos.jetcd.api.RangeResponse) StatusRuntimeException(io.grpc.StatusRuntimeException) BeforeTest(org.testng.annotations.BeforeTest) Test(org.testng.annotations.Test)

Example 4 with RangeResponse

use of com.coreos.jetcd.api.RangeResponse in project jetcd by coreos.

the class KVTest method testGetAndDeleteWithPrefix.

@Test
public void testGetAndDeleteWithPrefix() throws Exception {
    String prefix = randomString();
    ByteString key = ByteString.copyFromUtf8(prefix);
    int numPrefixes = 10;
    putKeysWithPrefix(prefix, numPrefixes);
    // verify get withPrefix.
    ListenableFuture<RangeResponse> getFuture = kvClient.get(key, GetOption.newBuilder().withPrefix(key).build());
    RangeResponse getResp = getFuture.get();
    test.assertEquals(getResp.getCount(), numPrefixes);
    // verify del withPrefix.
    DeleteOption deleteOpt = DeleteOption.newBuilder().withPrefix(key).build();
    ListenableFuture<DeleteRangeResponse> delFuture = kvClient.delete(key, deleteOpt);
    DeleteRangeResponse delResp = delFuture.get();
    test.assertEquals(delResp.getDeleted(), numPrefixes);
}
Also used : DeleteOption(com.coreos.jetcd.options.DeleteOption) DeleteRangeResponse(com.coreos.jetcd.api.DeleteRangeResponse) RangeResponse(com.coreos.jetcd.api.RangeResponse) ByteString(com.google.protobuf.ByteString) ByteString(com.google.protobuf.ByteString) DeleteRangeResponse(com.coreos.jetcd.api.DeleteRangeResponse) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 5 with RangeResponse

use of com.coreos.jetcd.api.RangeResponse in project jetcd by coreos.

the class KVTest method testGet.

@Test
public void testGet() throws Exception {
    ByteString sampleKey = ByteString.copyFrom("sample_key2", "UTF-8");
    ByteString sampleValue = ByteString.copyFrom("sample_value2", "UTF-8");
    ListenableFuture<PutResponse> feature = kvClient.put(sampleKey, sampleValue);
    try {
        feature.get();
        ListenableFuture<RangeResponse> getFeature = kvClient.get(sampleKey);
        RangeResponse response = getFeature.get();
        test.assertEquals(response.getKvsCount(), 1);
        test.assertEquals(response.getKvs(0).getValue().toStringUtf8(), "sample_value2");
        test.assertTrue(!response.getMore());
    } catch (Exception e) {
    // empty
    }
}
Also used : DeleteRangeResponse(com.coreos.jetcd.api.DeleteRangeResponse) RangeResponse(com.coreos.jetcd.api.RangeResponse) ByteString(com.google.protobuf.ByteString) PutResponse(com.coreos.jetcd.api.PutResponse) ExecutionException(java.util.concurrent.ExecutionException) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

RangeResponse (com.coreos.jetcd.api.RangeResponse)7 BeforeTest (org.testng.annotations.BeforeTest)7 Test (org.testng.annotations.Test)7 DeleteRangeResponse (com.coreos.jetcd.api.DeleteRangeResponse)6 ByteString (com.google.protobuf.ByteString)6 ExecutionException (java.util.concurrent.ExecutionException)5 PutResponse (com.coreos.jetcd.api.PutResponse)3 GetOption (com.coreos.jetcd.options.GetOption)2 TxnResponse (com.coreos.jetcd.api.TxnResponse)1 Cmp (com.coreos.jetcd.op.Cmp)1 Txn (com.coreos.jetcd.op.Txn)1 DeleteOption (com.coreos.jetcd.options.DeleteOption)1 StatusRuntimeException (io.grpc.StatusRuntimeException)1