@Test public void testBasicConvertIntKey() throws Exception { int key = 5; String testContent = "hello world"; Record record = new Record(key, testContent); Converter<String, String, Object, RawJsonDocument> recordConverter = new AnyToCouchbaseJsonConverter(); RawJsonDocument returnDoc = recordConverter.convertRecord("", record, null).iterator().next(); Assert.assertEquals(key+"", returnDoc.id(), "key should be equal"); Record convertedRecord = GSON.fromJson(returnDoc.content(), Record.class); Assert.assertEquals(convertedRecord.key, key); Assert.assertEquals(convertedRecord.value, testContent, "value in content should be equal"); }
private void testBasicConvert(String keyField, boolean setConfig) throws Exception { String key = "hello"; String testContent = "hello world"; Map<String, String> content = new HashMap<>(); content.put(keyField, key); content.put("value", testContent); AnyToCouchbaseJsonConverter recordConverter = new AnyToCouchbaseJsonConverter(); WorkUnitState workUnitState = mock(WorkUnitState.class); if (setConfig) { when(workUnitState.getProp(AnyToCouchbaseJsonConverter.KEY_FIELD_CONFIG)).thenReturn(keyField); when(workUnitState.contains(AnyToCouchbaseJsonConverter.KEY_FIELD_CONFIG)).thenReturn(true); recordConverter.init(workUnitState); } else { recordConverter.init(workUnitState); } RawJsonDocument returnDoc = recordConverter.convertRecord("", content, null).iterator().next(); System.out.println(returnDoc.toString()); Assert.assertEquals(key.getBytes(), returnDoc.id().getBytes(), "key should be equal"); Map<String, String> convertedMap = GSON.fromJson(returnDoc.content(), Map.class); Assert.assertEquals(key, convertedMap.get(keyField), "key in content should be equal"); Assert.assertEquals(testContent, convertedMap.get("value"), "value in content should be equal"); Assert.assertEquals(2, convertedMap.keySet().size(), "should have 2 fields"); }
@Override public <T>Observable<T> findBySpatialView(SpatialViewQuery query, Class<T> entityClass) { return querySpatialView(query) .flatMap(spatialViewResult -> spatialViewResult.error() .flatMap(error -> Observable.error(new CouchbaseQueryExecutionException("Unable to execute spatial view query due to error:" + error.toString()))) .switchIfEmpty(spatialViewResult.rows())) .map(row -> { AsyncSpatialViewRow asyncSpatialViewRow = (AsyncSpatialViewRow) row; return asyncSpatialViewRow.document(RawJsonDocument.class) .map(doc -> mapToEntity(doc.id(), doc, entityClass)) .toBlocking().single(); }) .doOnError(throwable -> Observable.error(new CouchbaseQueryExecutionException("Unable to execute spatial view query", throwable))); }
@Override public <T>Observable<T> findBySpatialView(SpatialViewQuery query, Class<T> entityClass) { return querySpatialView(query) .flatMap(spatialViewResult -> spatialViewResult.error() .flatMap(error -> Observable.error(new CouchbaseQueryExecutionException("Unable to execute spatial view query due to error:" + error.toString()))) .switchIfEmpty(spatialViewResult.rows())) .map(row -> { AsyncSpatialViewRow asyncSpatialViewRow = (AsyncSpatialViewRow) row; return asyncSpatialViewRow.document(RawJsonDocument.class) .map(doc -> mapToEntity(doc.id(), doc, entityClass)) .toBlocking().single(); }) .doOnError(throwable -> Observable.error(new CouchbaseQueryExecutionException("Unable to execute spatial view query", throwable))); }
/** * Creates a copy from a different {@link RawJsonDocument}, but changes the CAS value. * * @param doc the original {@link RawJsonDocument} to copy. * @param cas the CAS (compare and swap) value for optimistic concurrency. * @return a copied {@link RawJsonDocument} with the changed properties. */ public static RawJsonDocument from(RawJsonDocument doc, long cas) { return RawJsonDocument.create(doc.id(), doc.expiry(), doc.content(), cas, doc.mutationToken()); }
@Override public <T>Observable<T> findByView(ViewQuery query, Class<T> entityClass) { if (!query.isIncludeDocs() || !query.includeDocsTarget().equals(RawJsonDocument.class)) { if (query.isOrderRetained()) { query.includeDocsOrdered(RawJsonDocument.class); } else { query.includeDocs(RawJsonDocument.class); } } //we'll always map the document to the entity, hence reduce never makes sense. query.reduce(false); return queryView(query) .flatMap(asyncViewResult -> asyncViewResult.error() .flatMap(error -> Observable.error(new CouchbaseQueryExecutionException("Unable to execute view query due to error:" + error.toString()))) .switchIfEmpty(asyncViewResult.rows())) .map(row -> { AsyncViewRow asyncViewRow = (AsyncViewRow) row; return asyncViewRow.document(RawJsonDocument.class) .map(doc -> mapToEntity(doc.id(), doc, entityClass)).toBlocking().single(); }) .doOnError(throwable -> Observable.error(new CouchbaseQueryExecutionException("Unable to execute view query", throwable))); }
@Override public <T>Observable<T> findByView(ViewQuery query, Class<T> entityClass) { if (!query.isIncludeDocs() || !query.includeDocsTarget().equals(RawJsonDocument.class)) { if (query.isOrderRetained()) { query.includeDocsOrdered(RawJsonDocument.class); } else { query.includeDocs(RawJsonDocument.class); } } //we'll always map the document to the entity, hence reduce never makes sense. query.reduce(false); return queryView(query) .flatMap(asyncViewResult -> asyncViewResult.error() .flatMap(error -> Observable.error(new CouchbaseQueryExecutionException("Unable to execute view query due to error:" + error.toString()))) .switchIfEmpty(asyncViewResult.rows())) .map(row -> { AsyncViewRow asyncViewRow = (AsyncViewRow) row; return asyncViewRow.document(RawJsonDocument.class) .map(doc -> mapToEntity(doc.id(), doc, entityClass)).toBlocking().single(); }) .doOnError(throwable -> Observable.error(new CouchbaseQueryExecutionException("Unable to execute view query", throwable))); }