Java Code Examples for com.couchbase.client.deps.io.netty.buffer.Unpooled

The following examples show how to use com.couchbase.client.deps.io.netty.buffer.Unpooled. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: localization_nifi   Source File: TestGetCouchbaseKey.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testBinaryDocument() throws Exception {

    Bucket bucket = mock(Bucket.class);
    String inFileDataStr = "doc-in";
    String content = "binary";
    ByteBuf buf = Unpooled.copiedBuffer(content.getBytes(StandardCharsets.UTF_8));
    when(bucket.get(inFileDataStr, BinaryDocument.class))
        .thenReturn(BinaryDocument.create(inFileDataStr, buf));
    setupMockBucket(bucket);


    byte[] inFileData = inFileDataStr.getBytes(StandardCharsets.UTF_8);
    testRunner.enqueue(inFileData);
    testRunner.setProperty(DOCUMENT_TYPE, DocumentType.Binary.toString());
    testRunner.run();

    testRunner.assertTransferCount(REL_SUCCESS, 1);
    testRunner.assertTransferCount(REL_ORIGINAL, 1);
    testRunner.assertTransferCount(REL_RETRY, 0);
    testRunner.assertTransferCount(REL_FAILURE, 0);
    MockFlowFile outFile = testRunner.getFlowFilesForRelationship(REL_SUCCESS).get(0);
    outFile.assertContentEquals(content);
    MockFlowFile orgFile = testRunner.getFlowFilesForRelationship(REL_ORIGINAL).get(0);
    orgFile.assertContentEquals(inFileDataStr);
}
 
Example 2
Source Project: nifi   Source File: TestGetCouchbaseKey.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testBinaryDocument() throws Exception {

    Bucket bucket = mock(Bucket.class);
    String inFileDataStr = "doc-in";
    String content = "binary";
    ByteBuf buf = Unpooled.copiedBuffer(content.getBytes(StandardCharsets.UTF_8));
    when(bucket.get(inFileDataStr, BinaryDocument.class))
        .thenReturn(BinaryDocument.create(inFileDataStr, buf));
    setupMockBucket(bucket);


    byte[] inFileData = inFileDataStr.getBytes(StandardCharsets.UTF_8);
    testRunner.enqueue(inFileData);
    testRunner.setProperty(DOCUMENT_TYPE, DocumentType.Binary.toString());
    testRunner.run();

    testRunner.assertTransferCount(REL_SUCCESS, 1);
    testRunner.assertTransferCount(REL_ORIGINAL, 1);
    testRunner.assertTransferCount(REL_RETRY, 0);
    testRunner.assertTransferCount(REL_FAILURE, 0);
    MockFlowFile outFile = testRunner.getFlowFilesForRelationship(REL_SUCCESS).get(0);
    outFile.assertContentEquals(content);
    MockFlowFile orgFile = testRunner.getFlowFilesForRelationship(REL_ORIGINAL).get(0);
    orgFile.assertContentEquals(inFileDataStr);
}
 
Example 3
Source Project: nifi   Source File: TestGetCouchbaseKey.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testBinaryDocumentToAttribute() throws Exception {

    Bucket bucket = mock(Bucket.class);
    String inFileDataStr = "doc-in";
    String content = "binary";
    ByteBuf buf = Unpooled.copiedBuffer(content.getBytes(StandardCharsets.UTF_8));
    when(bucket.get(inFileDataStr, BinaryDocument.class))
        .thenReturn(BinaryDocument.create(inFileDataStr, buf));
    setupMockBucket(bucket);

    byte[] inFileData = inFileDataStr.getBytes(StandardCharsets.UTF_8);
    testRunner.enqueue(inFileData);
    testRunner.setProperty(DOCUMENT_TYPE, DocumentType.Binary.toString());
    testRunner.setProperty(PUT_VALUE_TO_ATTRIBUTE, "targetAttribute");
    testRunner.run();

    testRunner.assertTransferCount(REL_SUCCESS, 1);
    testRunner.assertTransferCount(REL_ORIGINAL, 0);
    testRunner.assertTransferCount(REL_RETRY, 0);
    testRunner.assertTransferCount(REL_FAILURE, 0);
    MockFlowFile outFile = testRunner.getFlowFilesForRelationship(REL_SUCCESS).get(0);
    outFile.assertContentEquals(inFileDataStr);
    outFile.assertAttributeEquals("targetAttribute", "binary");
}
 
Example 4
Source Project: nifi   Source File: TestCouchbaseMapCacheClient.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testGet() throws Exception {
    final CouchbaseMapCacheClient client = new CouchbaseMapCacheClient();
    final CouchbaseClusterControllerService couchbaseService = mock(CouchbaseClusterControllerService.class);
    final Bucket bucket = mock(Bucket.class);

    final MockControllerServiceInitializationContext serviceInitializationContext
            = new MockControllerServiceInitializationContext(couchbaseService, "couchbaseService");
    final Map<PropertyDescriptor, String> properties = new HashMap<>();
    properties.put(COUCHBASE_CLUSTER_SERVICE, "couchbaseService");
    properties.put(BUCKET_NAME, "bucketA");

    final ByteBuf contents = Unpooled.copiedBuffer("value".getBytes(StandardCharsets.UTF_8));
    final BinaryDocument doc = BinaryDocument.create("key", contents);
    when(couchbaseService.openBucket(eq("bucketA"))).thenReturn(bucket);
    when(bucket.get(any(BinaryDocument.class))).thenReturn(doc);

    final MockConfigurationContext context = new MockConfigurationContext(properties, serviceInitializationContext);
    client.configure(context);
    final String cacheEntry = client.get("key", stringSerializer, stringDeserializer);

    assertEquals("value", cacheEntry);
}
 
Example 5
Source Project: samza   Source File: CouchbaseTableWriteFunction.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public CompletableFuture<Void> putAsync(String key, V record) {
  Preconditions.checkArgument(StringUtils.isNotBlank(key), "key must not be null, empty or blank");
  Preconditions.checkArgument(!key.contains(" "), String.format("key should not contain spaces: %s", key));
  Preconditions.checkNotNull(record);
  Document<?> document = record instanceof JsonObject
      ? JsonDocument.create(key, (int) ttl.getSeconds(), (JsonObject) record)
      : BinaryDocument.create(key, (int) ttl.getSeconds(), Unpooled.copiedBuffer(valueSerde.toBytes(record)));
  return asyncWriteHelper(
      bucket.async().upsert(document, timeout.toMillis(), TimeUnit.MILLISECONDS),
      String.format("Failed to insert key %s into bucket %s", key, bucketName));
}
 
Example 6
Source Project: samza   Source File: TestCouchbaseTableReadFunction.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testGetAsyncStringValue() throws Exception {
  String key = "key";
  String value = "value";
  StringSerde stringSerde = new StringSerde();
  Bucket bucket = mock(Bucket.class);
  AsyncBucket asyncBucket = mock(AsyncBucket.class);
  CouchbaseTableReadFunction readFunction = createAndInit(String.class, stringSerde, bucket, asyncBucket);
  when(asyncBucket.get(eq(key), anyObject(), anyLong(), any(TimeUnit.class))).thenReturn(
      Observable.just(BinaryDocument.create(key, Unpooled.wrappedBuffer(stringSerde.toBytes(value)))));
  assertEquals(value, readFunction.getAsync(key).get());
}
 
Example 7
@Override
public Iterable<TupleDocument> convertRecord(String outputSchema, GenericRecord inputRecord, WorkUnitState workUnit)
    throws DataConversionException {
  String key = inputRecord.get(keyField).toString();
  GenericRecord data = (GenericRecord) inputRecord.get(dataRecordField);

  ByteBuffer dataBytes = (ByteBuffer) data.get(valueField);
  Integer flags = (Integer) data.get(flagsField);

  ByteBuf buffer = Unpooled.copiedBuffer(dataBytes);
  return new SingleRecordIterable<>(new TupleDocument(key, Tuple.create(buffer, flags)));
}
 
Example 8
Source Project: nifi   Source File: TestPutCouchbaseKey.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testBinaryDoc() throws Exception {
    String bucketName = "bucket-1";
    String docId = "doc-a";
    int expiry = 100;
    long cas = 200L;

    String inFileData = "12345";
    byte[] inFileDataBytes = inFileData.getBytes(StandardCharsets.UTF_8);

    Bucket bucket = mock(Bucket.class);
    when(bucket.upsert(any(ByteArrayDocument.class), eq(PersistTo.NONE), eq(ReplicateTo.NONE)))
            .thenReturn(ByteArrayDocument.create(docId, expiry, Unpooled.copiedBuffer(inFileData.getBytes(StandardCharsets.UTF_8)).array(), cas));
    setupMockBucket(bucket);

    testRunner.enqueue(inFileDataBytes);
    testRunner.setProperty(BUCKET_NAME, bucketName);
    testRunner.setProperty(DOC_ID, docId);
    testRunner.setProperty(DOCUMENT_TYPE, DocumentType.Binary.name());
    testRunner.run();

    verify(bucket, times(1)).upsert(any(ByteArrayDocument.class), eq(PersistTo.NONE), eq(ReplicateTo.NONE));

    testRunner.assertAllFlowFilesTransferred(REL_SUCCESS);
    testRunner.assertTransferCount(REL_SUCCESS, 1);
    testRunner.assertTransferCount(REL_RETRY, 0);
    testRunner.assertTransferCount(REL_FAILURE, 0);
    MockFlowFile outFile = testRunner.getFlowFilesForRelationship(REL_SUCCESS).get(0);
    outFile.assertContentEquals(inFileData);
    outFile.assertAttributeEquals(CouchbaseAttributes.Cluster.key(), SERVICE_ID);
    outFile.assertAttributeEquals(CouchbaseAttributes.Bucket.key(), bucketName);
    outFile.assertAttributeEquals(CouchbaseAttributes.DocId.key(), docId);
    outFile.assertAttributeEquals(CouchbaseAttributes.Cas.key(), String.valueOf(cas));
    outFile.assertAttributeEquals(CouchbaseAttributes.Expiry.key(), String.valueOf(expiry));
}
 
Example 9
Source Project: localization_nifi   Source File: PutCouchbaseKey.java    License: Apache License 2.0 4 votes vote down vote up
@Override
public void onTrigger(final ProcessContext context, final ProcessSession session) throws ProcessException {
    final ComponentLog logger = getLogger();
    FlowFile flowFile = session.get();
    if (flowFile == null) {
        return;
    }

    final byte[] content = new byte[(int) flowFile.getSize()];
    session.read(flowFile, new InputStreamCallback() {
        @Override
        public void process(final InputStream in) throws IOException {
            StreamUtils.fillBuffer(in, content, true);
        }
    });

    String docId = flowFile.getAttribute(CoreAttributes.UUID.key());
    if (!StringUtils.isEmpty(context.getProperty(DOC_ID).getValue())) {
        docId = context.getProperty(DOC_ID).evaluateAttributeExpressions(flowFile).getValue();
    }

    try {
        Document<?> doc = null;
        final DocumentType documentType = DocumentType.valueOf(context.getProperty(DOCUMENT_TYPE).getValue());
        switch (documentType) {
            case Json: {
                doc = RawJsonDocument.create(docId, new String(content, StandardCharsets.UTF_8));
                break;
            }
            case Binary: {
                final ByteBuf buf = Unpooled.copiedBuffer(content);
                doc = BinaryDocument.create(docId, buf);
                break;
            }
        }

        final PersistTo persistTo = PersistTo.valueOf(context.getProperty(PERSIST_TO).getValue());
        final ReplicateTo replicateTo = ReplicateTo.valueOf(context.getProperty(REPLICATE_TO).getValue());
        doc = openBucket(context).upsert(doc, persistTo, replicateTo);

        final Map<String, String> updatedAttrs = new HashMap<>();
        updatedAttrs.put(CouchbaseAttributes.Cluster.key(), context.getProperty(COUCHBASE_CLUSTER_SERVICE).getValue());
        updatedAttrs.put(CouchbaseAttributes.Bucket.key(), context.getProperty(BUCKET_NAME).getValue());
        updatedAttrs.put(CouchbaseAttributes.DocId.key(), docId);
        updatedAttrs.put(CouchbaseAttributes.Cas.key(), String.valueOf(doc.cas()));
        updatedAttrs.put(CouchbaseAttributes.Expiry.key(), String.valueOf(doc.expiry()));

        flowFile = session.putAllAttributes(flowFile, updatedAttrs);
        session.getProvenanceReporter().send(flowFile, getTransitUrl(context, docId));
        session.transfer(flowFile, REL_SUCCESS);
    } catch (final CouchbaseException e) {
        String errMsg = String.format("Writing document %s to Couchbase Server using %s failed due to %s", docId, flowFile, e);
        handleCouchbaseException(context, session, logger, flowFile, e, errMsg);
    }
}
 
Example 10
Source Project: datacollector   Source File: TestCouchbaseTarget.java    License: Apache License 2.0 4 votes vote down vote up
@SuppressWarnings("unchecked")
private static CouchbaseConnector getConnector(Class responseClass) throws Exception {
  ENV = DefaultCouchbaseEnvironment.create();

  CouchbaseCore core = mock(CouchbaseCore.class);
  CouchbaseAsyncBucket asyncBucket = new CouchbaseAsyncBucket(core,
      ENV,
      BUCKET,
      USERNAME,
      PASSWORD,
      Collections.emptyList()
  );

  final CouchbaseRequest requestMock = mock(CouchbaseRequest.class);

  Subject<CouchbaseResponse, CouchbaseResponse> response = AsyncSubject.create();

  if(responseClass == SimpleSubdocResponse.class) {
    final BinarySubdocRequest subdocRequestMock = mock(BinarySubdocRequest.class);
    when(subdocRequestMock.span()).thenReturn(mock(Span.class));

    response.onNext(new SimpleSubdocResponse(ResponseStatus.SUCCESS,
        KeyValueStatus.SUCCESS.code(),
        BUCKET,
        Unpooled.EMPTY_BUFFER,
        subdocRequestMock,
        1234,
        null
    ));

    response.onCompleted();
  } else {

    Constructor con = responseClass.getConstructor(ResponseStatus.class,
        short.class,
        long.class,
        String.class,
        ByteBuf.class,
        MutationToken.class,
        CouchbaseRequest.class
    );

    response.onNext((CouchbaseResponse) con.newInstance(ResponseStatus.SUCCESS,
        KeyValueStatus.SUCCESS.code(),
        1234,
        BUCKET,
        Unpooled.EMPTY_BUFFER,
        null,
        requestMock
    ));
    response.onCompleted();
  }

  when(core.send(any(BinarySubdocRequest.class))).thenReturn(response);
  when(core.send(any())).thenReturn(response);
  when(requestMock.span()).thenReturn(mock(Span.class));

  CouchbaseConnector connector = mock(CouchbaseConnector.class);
  when(connector.getScheduler()).thenReturn(ENV.scheduler());
  when(connector.bucket()).thenReturn(asyncBucket);

  return connector;
}
 
Example 11
Source Project: nifi   Source File: CouchbaseMapCacheClient.java    License: Apache License 2.0 4 votes vote down vote up
private <V> Document toDocument(String docId, V value, Serializer<V> valueSerializer, long revision) throws IOException {
    ByteArrayOutputStream bos = new ByteArrayOutputStream();
    valueSerializer.serialize(value, bos);
    final ByteBuf byteBuf = Unpooled.wrappedBuffer(bos.toByteArray());
    return BinaryDocument.create(docId, byteBuf, revision);
}
 
Example 12
Source Project: nifi   Source File: TestCouchbaseUtils.java    License: Apache License 2.0 4 votes vote down vote up
@Ignore("This test method requires a live Couchbase Server instance")
@Test
public void testDocumentTypesAndStringConversion() {
    final CouchbaseCluster cluster = CouchbaseCluster.fromConnectionString("couchbase://192.168.99.100:8091");
    final Bucket bucket = cluster.openBucket("b1", "b1password");

    bucket.upsert(JsonDocument.create("JsonDocument", JsonObject.create().put("one", 1)));
    bucket.upsert(JsonArrayDocument.create("JsonArray", JsonArray.create().add(1).add(2).add(3)));
    bucket.upsert(JsonDoubleDocument.create("JsonDouble", 0.123));
    bucket.upsert(JsonStringDocument.create("JsonString", "value"));
    bucket.upsert(JsonBooleanDocument.create("JsonBoolean", true));
    bucket.upsert(JsonLongDocument.create("JsonLong", 123L));

    bucket.upsert(RawJsonDocument.create("RawJsonDocument", "value"));
    bucket.upsert(StringDocument.create("StringDocument", "value"));

    bucket.upsert(BinaryDocument.create("BinaryDocument", Unpooled.copiedBuffer("value".getBytes(StandardCharsets.UTF_8))));
    bucket.upsert(ByteArrayDocument.create("ByteArrayDocument", "value".getBytes(StandardCharsets.UTF_8)));

    final String[][] expectations = {
            {"JsonDocument", "String", "{\"one\":1}"},
            {"JsonArray", "String", "[1,2,3]"},
            {"JsonDouble", "String", "0.123"},
            {"JsonString", "String", "\"value\""},
            {"JsonBoolean", "String", "true"},
            {"JsonLong", "String", "123"},
            {"RawJsonDocument", "String", "value"},
            {"StringDocument", "String", "value"},
            {"BinaryDocument", "byte[]", "value"},
            {"ByteArrayDocument", "byte[]", "value"},
    };

    for (String[] expectation : expectations) {
        final LegacyDocument document = bucket.get(LegacyDocument.create(expectation[0]));
        assertEquals(expectation[1], document.content().getClass().getSimpleName());
        assertEquals(expectation[2], CouchbaseUtils.getStringContent(document.content()));
    }

    final BinaryDocument binaryDocument = bucket.get(BinaryDocument.create("BinaryDocument"));
    final String stringFromByteBuff = CouchbaseUtils.getStringContent(binaryDocument.content());
    assertEquals("value", stringFromByteBuff);

    try {
        bucket.get(BinaryDocument.create("JsonDocument"));
        fail("Getting a JSON document as a BinaryDocument fails");
    } catch (TranscodingException e) {
        assertTrue(e.getMessage().contains("Flags (0x2000000) indicate non-binary document for id JsonDocument"));
    }

}