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"));
}
});
}
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));
}
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()));
}
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();
}
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;
}
Aggregations