Search in sources :

Example 31 with GetResult

use of com.couchbase.client.java.kv.GetResult in project couchbase-jvm-clients by couchbase.

the class KeyValueIntegrationTest method fullDocWithExpirationAndCustomTranscoder.

@Test
@IgnoreWhen(clusterTypes = ClusterType.MOCKED)
void fullDocWithExpirationAndCustomTranscoder() {
    String id = UUID.randomUUID().toString();
    JsonObject content = JsonObject.create().put("foo", "bar").put("created", true).put("age", 12);
    MutationResult mutationResult = collection.upsert(id, content.toBytes(), upsertOptions().expiry(Duration.ofSeconds(5)).transcoder(RawBinaryTranscoder.INSTANCE));
    assertTrue(mutationResult.cas() != 0);
    GetResult getResult = collection.get(id, getOptions().withExpiry(true).transcoder(RawBinaryTranscoder.INSTANCE));
    assertTrue(getResult.expiryTime().isPresent());
    assertTrue(getResult.expiryTime().get().toEpochMilli() > 0);
    assertEquals(content, JsonObject.fromJson(getResult.contentAs(byte[].class)));
}
Also used : GetResult(com.couchbase.client.java.kv.GetResult) JsonObject(com.couchbase.client.java.json.JsonObject) MutationResult(com.couchbase.client.java.kv.MutationResult) IgnoreWhen(com.couchbase.client.test.IgnoreWhen) JavaIntegrationTest(com.couchbase.client.java.util.JavaIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 32 with GetResult

use of com.couchbase.client.java.kv.GetResult in project couchbase-jvm-clients by couchbase.

the class KeyValueIntegrationTest method fullDocWithExpiration.

/**
 * Right now the mock does not support xattr/macro expansion so this test is
 * ignored on the mock. Once the mock supports it, please remove the ignore
 * annotation.
 *
 * <p>See https://github.com/couchbase/CouchbaseMock/issues/46</p>
 */
@Test
@IgnoreWhen(clusterTypes = { ClusterType.MOCKED })
void fullDocWithExpiration() {
    String id = UUID.randomUUID().toString();
    JsonObject content = JsonObject.create().put("foo", "bar").put("created", true).put("age", 12);
    MutationResult mutationResult = collection.upsert(id, content, upsertOptions().expiry(Duration.ofSeconds(5)));
    assertTrue(mutationResult.cas() != 0);
    GetResult getResult = collection.get(id, getOptions().withExpiry(true));
    assertTrue(getResult.expiryTime().isPresent());
    assertTrue(getResult.expiryTime().get().toEpochMilli() > 0);
    assertEquals(content, getResult.contentAsObject());
}
Also used : GetResult(com.couchbase.client.java.kv.GetResult) JsonObject(com.couchbase.client.java.json.JsonObject) MutationResult(com.couchbase.client.java.kv.MutationResult) IgnoreWhen(com.couchbase.client.test.IgnoreWhen) JavaIntegrationTest(com.couchbase.client.java.util.JavaIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 33 with GetResult

use of com.couchbase.client.java.kv.GetResult in project couchbase-jvm-clients by couchbase.

the class KeyValueIntegrationTest method getWithProjection.

@Test
void getWithProjection() {
    String id = UUID.randomUUID().toString();
    JsonObject content = JsonObject.create().put("foo", "bar").put("created", true).put("age", 12);
    MutationResult mutationResult = collection.upsert(id, content);
    assertTrue(mutationResult.cas() != 0);
    GetResult getResult = collection.get(id, getOptions().project("foo", "created", "notfound"));
    assertTrue(getResult.cas() != 0);
    assertFalse(getResult.expiryTime().isPresent());
    JsonObject decoded = getResult.contentAsObject();
    assertEquals("bar", decoded.getString("foo"));
    assertEquals(true, decoded.getBoolean("created"));
    assertFalse(decoded.containsKey("age"));
}
Also used : GetResult(com.couchbase.client.java.kv.GetResult) JsonObject(com.couchbase.client.java.json.JsonObject) MutationResult(com.couchbase.client.java.kv.MutationResult) JavaIntegrationTest(com.couchbase.client.java.util.JavaIntegrationTest) Test(org.junit.jupiter.api.Test)

Example 34 with GetResult

use of com.couchbase.client.java.kv.GetResult in project couchbase-jvm-clients by couchbase.

the class KeyValueIntegrationTest method assertExpiry.

void assertExpiry(String documentId, Duration expiryDuration) {
    GetResult result = collection.get(documentId, GetOptions.getOptions().withExpiry(true));
    Instant actualExpiry = result.expiryTime().orElseThrow(() -> new AssertionError("expected expiry"));
    Instant expectedExpiry = Instant.ofEpochMilli(System.currentTimeMillis()).plus(expiryDuration);
    long secondsDifference = actualExpiry.getEpochSecond() - expectedExpiry.getEpochSecond();
    long acceptanceThresholdSeconds = Duration.ofMinutes(5).getSeconds();
    assertTrue(Math.abs(secondsDifference) < acceptanceThresholdSeconds);
}
Also used : GetResult(com.couchbase.client.java.kv.GetResult) Instant(java.time.Instant)

Example 35 with GetResult

use of com.couchbase.client.java.kv.GetResult in project couchbase-jvm-clients by couchbase.

the class KeyValueIntegrationTest method projectionWithExpiration.

/**
 * Right now the mock does not support xattr/macro expansion so this test is
 * ignored on the mock. Once the mock supports it, please remove the ignore
 * annotation.
 *
 * <p>See https://github.com/couchbase/CouchbaseMock/issues/46</p>
 */
@Test
@IgnoreWhen(clusterTypes = { ClusterType.MOCKED })
void projectionWithExpiration() {
    String id = UUID.randomUUID().toString();
    JsonObject content = JsonObject.create().put("foo", "bar").put("created", true).put("age", 12);
    MutationResult mutationResult = collection.upsert(id, content, upsertOptions().expiry(Duration.ofSeconds(5)));
    assertTrue(mutationResult.cas() != 0);
    GetResult getResult = collection.get(id, getOptions().project("foo", "created").withExpiry(true));
    assertTrue(getResult.cas() != 0);
    assertTrue(getResult.expiryTime().isPresent());
    assertTrue(getResult.expiryTime().get().toEpochMilli() > 0);
    JsonObject decoded = getResult.contentAsObject();
    assertEquals("bar", decoded.getString("foo"));
    assertEquals(true, decoded.getBoolean("created"));
    assertFalse(decoded.containsKey("age"));
}
Also used : GetResult(com.couchbase.client.java.kv.GetResult) JsonObject(com.couchbase.client.java.json.JsonObject) MutationResult(com.couchbase.client.java.kv.MutationResult) IgnoreWhen(com.couchbase.client.test.IgnoreWhen) JavaIntegrationTest(com.couchbase.client.java.util.JavaIntegrationTest) Test(org.junit.jupiter.api.Test)

Aggregations

GetResult (com.couchbase.client.java.kv.GetResult)39 Test (org.junit.jupiter.api.Test)25 JavaIntegrationTest (com.couchbase.client.java.util.JavaIntegrationTest)20 JsonObject (com.couchbase.client.java.json.JsonObject)18 IgnoreWhen (com.couchbase.client.test.IgnoreWhen)15 MutationResult (com.couchbase.client.java.kv.MutationResult)11 Collection (com.couchbase.client.java.Collection)7 DocumentNotFoundException (com.couchbase.client.core.error.DocumentNotFoundException)6 Bucket (com.couchbase.client.java.Bucket)5 Cluster (com.couchbase.client.java.Cluster)4 Record (org.talend.sdk.component.api.record.Record)4 CouchbaseException (com.couchbase.client.core.error.CouchbaseException)3 ReplaceBodyWithXattr (com.couchbase.client.java.kv.ReplaceBodyWithXattr)3 CasMismatchException (com.couchbase.client.core.error.CasMismatchException)2 TimeoutException (com.couchbase.client.core.error.TimeoutException)2 JsonArray (com.couchbase.client.java.json.JsonArray)2 Instant (java.time.Instant)2 ArrayList (java.util.ArrayList)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2