Search in sources :

Example 96 with JsonObject

use of com.couchbase.client.java.json.JsonObject in project connectors-se by Talend.

the class CouchbaseOutputTest method partialUpdate.

@Test
@DisplayName("Document partial update")
void partialUpdate() {
    log.info("Test start: partialUpdate");
    final String PARTIAL_UPDATE_ID_PREFIX = "partialUpdate";
    // prepare data
    Bucket bucket = couchbaseCluster.bucket(BUCKET_NAME);
    Collection collection = bucket.defaultCollection();
    for (int i = 0; i < 2; i++) {
        JsonObject js = new TestData().createJson(PARTIAL_UPDATE_ID_PREFIX);
        collection.insert(generateDocId(PARTIAL_UPDATE_ID_PREFIX, i), js);
    }
    // update data
    CouchbaseOutputConfiguration config = getOutputConfiguration();
    config.setPartialUpdate(true);
    componentsHandler.setInputData(createPartialUpdateRecords(PARTIAL_UPDATE_ID_PREFIX));
    executeJob(config);
    // 
    List<JsonObject> resultList = retrieveDataFromDatabase(PARTIAL_UPDATE_ID_PREFIX, 2);
    assertEquals(2, resultList.size());
    TestData testData = new TestData();
    Stream.iterate(0, o -> o + 1).limit(2).forEach(idx -> {
        // untouched properties
        assertEquals(Integer.valueOf(testData.getColIntMax()), resultList.get(idx).getInt("t_int_max"));
        assertEquals(Long.valueOf(testData.getColLongMin()), resultList.get(idx).getLong("t_long_min"));
        assertEquals(Long.valueOf(testData.getColLongMax()), resultList.get(idx).getLong("t_long_max"));
        assertEquals(testData.getColFloatMin(), resultList.get(idx).getNumber("t_float_min").floatValue());
        assertEquals(testData.getColFloatMax(), resultList.get(idx).getNumber("t_float_max").floatValue());
        assertEquals(testData.getColDoubleMin(), resultList.get(idx).getDouble("t_double_min"));
        assertEquals(testData.getColDoubleMax(), resultList.get(idx).getDouble("t_double_max"));
        assertEquals(testData.getColDateTime().toString(), resultList.get(idx).getString("t_datetime"));
        assertArrayEquals(testData.getColList().toArray(), resultList.get(idx).getArray("t_array").toList().toArray());
        // upserted proterties
        if (idx == 0) {
            assertEquals(1971, resultList.get(idx).getInt("t_int_min"));
            assertEquals(testData.isColBoolean(), resultList.get(idx).getBoolean("t_boolean"));
            assertEquals("path new", resultList.get(idx).getString("extra_content"));
            assertNull(resultList.get(idx).getString("extra_content2"));
        } else {
            assertEquals(Integer.valueOf(testData.getColIntMin()), resultList.get(idx).getInt("t_int_min"));
            assertEquals(Boolean.FALSE, resultList.get(idx).getBoolean("t_boolean"));
            assertEquals("path zap", resultList.get(idx).getString("extra_content2"));
            assertNull(resultList.get(idx).getString("extra_content"));
        }
    });
}
Also used : TestData(org.talend.components.couchbase.TestData) Bucket(com.couchbase.client.java.Bucket) Collection(com.couchbase.client.java.Collection) JsonObject(com.couchbase.client.java.json.JsonObject) Test(org.junit.jupiter.api.Test) CouchbaseUtilTest(org.talend.components.couchbase.CouchbaseUtilTest) DisplayName(org.junit.jupiter.api.DisplayName)

Example 97 with JsonObject

use of com.couchbase.client.java.json.JsonObject in project connectors-se by Talend.

the class CouchbaseOutputTest method executeSimpleN1QLQueryWithNoParameters.

@Test
@DisplayName("Simple N1QL query with no parameters")
void executeSimpleN1QLQueryWithNoParameters() {
    log.info("Test start: executeSimpleN1QLQueryWithNoParameters");
    final String N1QL_WITH_NO_PARAMETERS_ID_PREFIX = "n1qlNoParametersIdPrefix";
    CouchbaseOutputConfiguration configuration = getOutputConfiguration();
    configuration.setUseN1QLQuery(true);
    TestData td = new TestData();
    td.setColDoubleMax(Integer.MAX_VALUE);
    td.setColFloatMax(Integer.MAX_VALUE);
    td.setColDoubleMin(Float.MIN_VALUE);
    td.setColLongMin(Integer.MIN_VALUE);
    String js = td.createJson("").toString();
    String id = generateDocId(N1QL_WITH_NO_PARAMETERS_ID_PREFIX, 0);
    String qry = String.format("UPSERT INTO `%s` (KEY, VALUE) VALUES (\"%s\", %s)", BUCKET_NAME, id, js);
    configuration.setQuery(qry);
    componentsHandler.setInputData(createRecords(new TestData(), N1QL_WITH_NO_PARAMETERS_ID_PREFIX));
    executeJob(configuration);
    List<JsonObject> resultList = retrieveDataFromDatabase(N1QL_WITH_NO_PARAMETERS_ID_PREFIX, 1);
    assertEquals(1, resultList.size());
    JsonObject result = resultList.get(0);
    assertJsonEquals(td, result);
    assertEquals(generateDocId(N1QL_WITH_NO_PARAMETERS_ID_PREFIX, 0), result.getString(META_ID_FIELD));
}
Also used : TestData(org.talend.components.couchbase.TestData) JsonObject(com.couchbase.client.java.json.JsonObject) Test(org.junit.jupiter.api.Test) CouchbaseUtilTest(org.talend.components.couchbase.CouchbaseUtilTest) DisplayName(org.junit.jupiter.api.DisplayName)

Example 98 with JsonObject

use of com.couchbase.client.java.json.JsonObject in project connectors-se by Talend.

the class CouchbaseOutputTest method toJsonDocumentWithBytesType.

@Test
@DisplayName("Document with encoded string")
void toJsonDocumentWithBytesType() {
    byte[] bytes = "aloha".getBytes(Charset.defaultCharset());
    String encoded = new String(Base64.getEncoder().encode(bytes));
    String idValue = "fixBytes";
    Record test = recordBuilderFactory.newRecordBuilder().withString("ID", idValue).withInt("id", 101).withString("name", "kamikaze").withString("byties", encoded).build();
    JsonObject jsonObject = JsonObject.fromJson(test.toString());
    assertEquals(idValue, jsonObject.getString("ID"));
    assertEquals(101, jsonObject.getInt("id"));
    assertEquals("kamikaze", jsonObject.getString("name"));
    byte[] rbytes = Base64.getDecoder().decode(jsonObject.getString("byties"));
    assertEquals(bytes.length, rbytes.length);
    assertEquals("aloha", new String(rbytes, Charset.defaultCharset()));
}
Also used : JsonObject(com.couchbase.client.java.json.JsonObject) Record(org.talend.sdk.component.api.record.Record) Test(org.junit.jupiter.api.Test) CouchbaseUtilTest(org.talend.components.couchbase.CouchbaseUtilTest) DisplayName(org.junit.jupiter.api.DisplayName)

Example 99 with JsonObject

use of com.couchbase.client.java.json.JsonObject in project ShedLock by lukas-krecan.

the class CouchbaseLockProviderIntegrationTest method assertUnlocked.

@Override
public void assertUnlocked(String lockName) {
    GetResult result = bucket.defaultCollection().get(lockName);
    JsonObject lockDocument = result.contentAsObject();
    assertThat(parse((String) lockDocument.get(LOCK_UNTIL))).isBeforeOrEqualTo(now());
    assertThat(parse((String) lockDocument.get(LOCKED_AT))).isBefore(now());
    assertThat(lockDocument.get(LOCKED_BY)).asString().isNotEmpty();
}
Also used : GetResult(com.couchbase.client.java.kv.GetResult) JsonObject(com.couchbase.client.java.json.JsonObject)

Example 100 with JsonObject

use of com.couchbase.client.java.json.JsonObject in project spring-data-couchbase by spring-projects.

the class N1QLQuery method n1ql.

public JsonObject n1ql() {
    JsonObject query = JsonObject.create().put("statement", expression.toString());
    options.build().injectParams(query);
    return query;
}
Also used : JsonObject(com.couchbase.client.java.json.JsonObject)

Aggregations

JsonObject (com.couchbase.client.java.json.JsonObject)189 Test (org.junit.jupiter.api.Test)145 JavaIntegrationTest (com.couchbase.client.java.util.JavaIntegrationTest)119 IgnoreWhen (com.couchbase.client.test.IgnoreWhen)39 JsonArray (com.couchbase.client.java.json.JsonArray)18 QueryResult (com.couchbase.client.java.query.QueryResult)16 GetResult (com.couchbase.client.java.kv.GetResult)15 MutationResult (com.couchbase.client.java.kv.MutationResult)15 QueryOptions (com.couchbase.client.java.query.QueryOptions)15 ReactiveQueryResult (com.couchbase.client.java.query.ReactiveQueryResult)15 RequestSpan (com.couchbase.client.core.cnc.RequestSpan)6 RetryStrategy (com.couchbase.client.core.retry.RetryStrategy)6 Collection (com.couchbase.client.java.Collection)6 ReplaceBodyWithXattr (com.couchbase.client.java.kv.ReplaceBodyWithXattr)6 Duration (java.time.Duration)6 DisplayName (org.junit.jupiter.api.DisplayName)6 CouchbaseUtilTest (org.talend.components.couchbase.CouchbaseUtilTest)6 ArrayList (java.util.ArrayList)5 TestData (org.talend.components.couchbase.TestData)5 MutateInResult (com.couchbase.client.java.kv.MutateInResult)4