Search in sources :

Example 11 with InvalidArgumentException

use of com.couchbase.client.core.error.InvalidArgumentException in project couchbase-jvm-clients by couchbase.

the class AsyncCollection method unlockRequest.

/**
 * Helper method to create the unlock request.
 *
 * @param id the id of the document.
 * @param cas the CAS value which is needed to unlock it.
 * @param options the options to customize.
 * @return the unlock request.
 */
UnlockRequest unlockRequest(final String id, final long cas, final UnlockOptions options) {
    notNullOrEmpty(id, "Id", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    notNull(options, "UnlockOptions", () -> ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    if (cas == 0) {
        throw new InvalidArgumentException("CAS cannot be 0", null, ReducedKeyValueErrorContext.create(id, collectionIdentifier));
    }
    UnlockOptions.Built opts = options.build();
    Duration timeout = opts.timeout().orElse(environment.timeoutConfig().kvTimeout());
    RetryStrategy retryStrategy = opts.retryStrategy().orElse(environment.retryStrategy());
    RequestSpan span = environment.requestTracer().requestSpan(TracingIdentifiers.SPAN_REQUEST_KV_UNLOCK, opts.parentSpan().orElse(null));
    UnlockRequest request = new UnlockRequest(timeout, coreContext, collectionIdentifier, retryStrategy, id, cas, span);
    request.context().clientContext(opts.clientContext());
    return request;
}
Also used : InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) UnlockOptions(com.couchbase.client.java.kv.UnlockOptions) Duration(java.time.Duration) RetryStrategy(com.couchbase.client.core.retry.RetryStrategy) UnlockRequest(com.couchbase.client.core.msg.kv.UnlockRequest) RequestSpan(com.couchbase.client.core.cnc.RequestSpan)

Example 12 with InvalidArgumentException

use of com.couchbase.client.core.error.InvalidArgumentException in project couchbase-jvm-clients by couchbase.

the class BuilderPropertySetterTest method setDouble.

@Test
void setDouble() {
    CoreEnvironment.Builder<?> builder = newEnvironmentBuilder();
    setter.set(builder, "compression.minRatio", "3.14159");
    CompressionConfig compression = builder.compressionConfig().build();
    assertEquals(3.14159, compression.minRatio());
    InvalidArgumentException e = assertThrows(InvalidArgumentException.class, () -> setter.set(builder, "compression.minRatio", "garbage"));
    assertEquals("Expected a double but got \"garbage\".", e.getCause().getMessage());
}
Also used : InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) Test(org.junit.jupiter.api.Test)

Example 13 with InvalidArgumentException

use of com.couchbase.client.core.error.InvalidArgumentException in project couchbase-jvm-clients by couchbase.

the class BuilderPropertySetterTest method setEnumArray.

@Test
void setEnumArray() {
    CoreEnvironment.Builder<?> builder = newEnvironmentBuilder();
    setter.set(builder, "io.captureTraffic", " KV , ANALYTICS ");
    IoConfig io = builder.ioConfig().build();
    Set<ServiceType> expected = EnumSet.of(ServiceType.KV, ServiceType.ANALYTICS);
    assertEquals(expected, io.servicesToCapture());
    setter.set(builder, "io.captureTraffic", "");
    assertEquals(EnumSet.allOf(ServiceType.class), builder.ioConfig().build().servicesToCapture());
    InvalidArgumentException e = assertThrows(InvalidArgumentException.class, () -> setter.set(builder, "io.captureTraffic", "garbage"));
    assertEquals("Expected one of " + EnumSet.allOf(ServiceType.class) + " but got \"garbage\"", e.getCause().getMessage());
}
Also used : InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) ServiceType(com.couchbase.client.core.service.ServiceType) Test(org.junit.jupiter.api.Test)

Example 14 with InvalidArgumentException

use of com.couchbase.client.core.error.InvalidArgumentException in project couchbase-jvm-clients by couchbase.

the class BuilderPropertySetterTest method triesAllOverloads.

@Test
void triesAllOverloads() {
    HasOverloads overloads = new HasOverloads();
    setter.set(overloads, "value", "23");
    setter.set(overloads, "value", "2s");
    setter.set(overloads, "value", "{\"luckyNumber\":37}");
    assertEquals(23, overloads.i);
    assertEquals(Duration.ofSeconds(2), overloads.d);
    assertEquals(singletonMap("luckyNumber", 37), overloads.m);
    InvalidArgumentException e = assertThrows(InvalidArgumentException.class, () -> setter.set(overloads, "value", "garbage"));
    assertTrue(e.getCause().getMessage().startsWith("Found multiple one-arg setters"));
    final Set<String> suppressedMessages = Arrays.stream(e.getCause().getSuppressed()).map(Throwable::getMessage).collect(toSet());
    final Set<String> expectedSuppressedMessages = new HashSet<>(Arrays.asList("Expected an int but got \"garbage\".", "Expected a duration qualified by a time unit (like \"2.5s\" or \"300ms\") but got \"garbage\".", "Expected a value Jackson can bind to java.util.Map<java.lang.String, java.lang.Integer> but got \"garbage\"."));
    assertEquals(expectedSuppressedMessages, suppressedMessages);
}
Also used : InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 15 with InvalidArgumentException

use of com.couchbase.client.core.error.InvalidArgumentException in project couchbase-jvm-clients by couchbase.

the class JsonArrayTest method shouldThrowOnBadSubMap.

@Test
void shouldThrowOnBadSubMap() {
    Map<Integer, String> badMap1 = mapOf(1, "test");
    Map<String, Object> badMap2 = mapOf("key1", new CloneNotSupportedException());
    InvalidArgumentException e = assertThrows(InvalidArgumentException.class, () -> JsonArray.from(listOf(badMap1)));
    assertTrue(e.getCause() instanceof ClassCastException);
    assertThrows(InvalidArgumentException.class, () -> JsonArray.from(listOf(badMap2)));
}
Also used : BigInteger(java.math.BigInteger) InvalidArgumentException(com.couchbase.client.core.error.InvalidArgumentException) Test(org.junit.jupiter.api.Test)

Aggregations

InvalidArgumentException (com.couchbase.client.core.error.InvalidArgumentException)17 Test (org.junit.jupiter.api.Test)10 ArrayList (java.util.ArrayList)4 Duration (java.time.Duration)3 RequestSpan (com.couchbase.client.core.cnc.RequestSpan)2 JsonNode (com.couchbase.client.core.deps.com.fasterxml.jackson.databind.JsonNode)2 RetryStrategy (com.couchbase.client.core.retry.RetryStrategy)2 BigInteger (java.math.BigInteger)2 Collection (java.util.Collection)2 TypeReference (com.couchbase.client.core.deps.com.fasterxml.jackson.core.type.TypeReference)1 Builder.newQueryString (com.couchbase.client.core.endpoint.http.CoreHttpRequest.Builder.newQueryString)1 CompilationFailureException (com.couchbase.client.core.error.CompilationFailureException)1 CouchbaseException (com.couchbase.client.core.error.CouchbaseException)1 AnalyticsErrorContext (com.couchbase.client.core.error.context.AnalyticsErrorContext)1 Mapper (com.couchbase.client.core.json.Mapper)1 SubdocGetRequest (com.couchbase.client.core.msg.kv.SubdocGetRequest)1 UnlockRequest (com.couchbase.client.core.msg.kv.UnlockRequest)1 ServiceType (com.couchbase.client.core.service.ServiceType)1 Golang (com.couchbase.client.core.util.Golang)1 JsonArray (com.couchbase.client.java.json.JsonArray)1