Java Code Examples for com.couchbase.client.java.Bucket

The following examples show how to use com.couchbase.client.java.Bucket. 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 testDocIdExp() throws Exception {
    String docIdExp = "${'someProperty'}";
    String somePropertyValue = "doc-p";

    Bucket bucket = mock(Bucket.class);
    String content = "{\"key\":\"value\"}";
    when(bucket.get(somePropertyValue, RawJsonDocument.class))
        .thenReturn(RawJsonDocument.create(somePropertyValue, content));
    setupMockBucket(bucket);

    testRunner.setProperty(DOC_ID, docIdExp);

    byte[] inFileData = "input FlowFile data".getBytes(StandardCharsets.UTF_8);
    Map<String, String> properties = new HashMap<>();
    properties.put("someProperty", somePropertyValue);
    testRunner.enqueue(inFileData, properties);
    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);
}
 
Example 2
Source Project: localization_nifi   Source File: TestGetCouchbaseKey.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testInputFlowFileContent() throws Exception {

    Bucket bucket = mock(Bucket.class);
    String inFileDataStr = "doc-in";
    String content = "{\"key\":\"value\"}";
    when(bucket.get(inFileDataStr, RawJsonDocument.class))
        .thenReturn(RawJsonDocument.create(inFileDataStr, content));
    setupMockBucket(bucket);


    byte[] inFileData = inFileDataStr.getBytes(StandardCharsets.UTF_8);
    testRunner.enqueue(inFileData);
    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: 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 4
Source Project: localization_nifi   Source File: TestGetCouchbaseKey.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testCouchbaseTempClusterError() throws Exception {
    String docIdExp = "doc-c";

    Bucket bucket = mock(Bucket.class);
    CouchbaseException exception = new BackpressureException();
    when(bucket.get(docIdExp, RawJsonDocument.class))
        .thenThrow(exception);
    setupMockBucket(bucket);

    testRunner.setProperty(DOC_ID, docIdExp);

    String inputFileDataStr = "input FlowFile data";
    byte[] inFileData = inputFileDataStr.getBytes(StandardCharsets.UTF_8);
    testRunner.enqueue(inFileData);
    testRunner.run();

    testRunner.assertTransferCount(REL_SUCCESS, 0);
    testRunner.assertTransferCount(REL_ORIGINAL, 0);
    testRunner.assertTransferCount(REL_RETRY, 1);
    testRunner.assertTransferCount(REL_FAILURE, 0);
    MockFlowFile orgFile = testRunner.getFlowFilesForRelationship(REL_RETRY).get(0);
    orgFile.assertContentEquals(inputFileDataStr);
    orgFile.assertAttributeEquals(Exception.key(), exception.getClass().getName());
}
 
Example 5
Source Project: localization_nifi   Source File: TestGetCouchbaseKey.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testCouchbaseTempFlowFileError() throws Exception {
    String docIdExp = "doc-c";

    Bucket bucket = mock(Bucket.class);
    // There is no suitable CouchbaseException for temp flowfile error, currently.
    CouchbaseException exception = new DurabilityException();
    when(bucket.get(docIdExp, RawJsonDocument.class))
        .thenThrow(exception);
    setupMockBucket(bucket);

    testRunner.setProperty(DOC_ID, docIdExp);

    String inputFileDataStr = "input FlowFile data";
    byte[] inFileData = inputFileDataStr.getBytes(StandardCharsets.UTF_8);
    testRunner.enqueue(inFileData);
    testRunner.run();

    testRunner.assertTransferCount(REL_SUCCESS, 0);
    testRunner.assertTransferCount(REL_ORIGINAL, 0);
    testRunner.assertTransferCount(REL_RETRY, 1);
    testRunner.assertTransferCount(REL_FAILURE, 0);
    MockFlowFile orgFile = testRunner.getFlowFilesForRelationship(REL_RETRY).get(0);
    orgFile.assertContentEquals(inputFileDataStr);
    orgFile.assertAttributeEquals(Exception.key(), exception.getClass().getName());
}
 
Example 6
Source Project: nifi   Source File: TestGetCouchbaseKey.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testCouchbaseTempClusterError() throws Exception {
    String docIdExp = "doc-c";

    Bucket bucket = mock(Bucket.class);
    CouchbaseException exception = new BackpressureException();
    when(bucket.get(docIdExp, RawJsonDocument.class))
        .thenThrow(exception);
    setupMockBucket(bucket);

    testRunner.setProperty(DOC_ID, docIdExp);

    String inputFileDataStr = "input FlowFile data";
    byte[] inFileData = inputFileDataStr.getBytes(StandardCharsets.UTF_8);
    testRunner.enqueue(inFileData);
    testRunner.run();

    testRunner.assertTransferCount(REL_SUCCESS, 0);
    testRunner.assertTransferCount(REL_ORIGINAL, 0);
    testRunner.assertTransferCount(REL_RETRY, 1);
    testRunner.assertTransferCount(REL_FAILURE, 0);
    MockFlowFile orgFile = testRunner.getFlowFilesForRelationship(REL_RETRY).get(0);
    orgFile.assertContentEquals(inputFileDataStr);
    orgFile.assertAttributeEquals(Exception.key(), exception.getClass().getName());
}
 
Example 7
Source Project: localization_nifi   Source File: TestGetCouchbaseKey.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testDocumentNotFound() throws Exception {
    String docIdExp = "doc-n";

    Bucket bucket = mock(Bucket.class);
    when(bucket.get(docIdExp, RawJsonDocument.class))
        .thenReturn(null);
    setupMockBucket(bucket);

    testRunner.setProperty(DOC_ID, docIdExp);

    String inputFileDataStr = "input FlowFile data";
    byte[] inFileData = inputFileDataStr.getBytes(StandardCharsets.UTF_8);
    testRunner.enqueue(inFileData);
    testRunner.run();

    testRunner.assertTransferCount(REL_SUCCESS, 0);
    testRunner.assertTransferCount(REL_ORIGINAL, 0);
    testRunner.assertTransferCount(REL_RETRY, 0);
    testRunner.assertTransferCount(REL_FAILURE, 1);
    MockFlowFile orgFile = testRunner.getFlowFilesForRelationship(REL_FAILURE).get(0);
    orgFile.assertContentEquals(inputFileDataStr);
    orgFile.assertAttributeEquals(Exception.key(), DocumentDoesNotExistException.class.getName());
}
 
Example 8
Source Project: tutorials   Source File: N1QLLiveTest.java    License: MIT License 6 votes vote down vote up
@Test
public void givenDocuments_whenBatchInsert_thenResult() {
    Bucket bucket = bucketFactory.getTravelSampleBucket();

    List<JsonDocument> documents = IntStream.rangeClosed(0,10)
      .mapToObj( i -> {
        JsonObject content = JsonObject.create()
          .put("id", i)
          .put("type", "airline")
          .put("name", "Sample Airline "  + i);
        return JsonDocument.create("cust_" + i, content);
      })
      .collect(Collectors.toList());

    List<JsonDocument> r5 = Observable
      .from(documents)
      .flatMap(doc -> bucket.async().insert(doc))
      .toList()
      .last()
      .toBlocking()
      .single();

    r5.forEach(System.out::println);
}
 
Example 9
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 10
Source Project: nifi   Source File: TestGetCouchbaseKey.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testCouchbaseInvalidInputError() throws Exception {
    String docIdExp = "doc-c";

    Bucket bucket = mock(Bucket.class);
    CouchbaseException exception = new RequestTooBigException();
    when(bucket.get(docIdExp, RawJsonDocument.class))
        .thenThrow(exception);
    setupMockBucket(bucket);

    testRunner.setProperty(DOC_ID, docIdExp);

    String inputFileDataStr = "input FlowFile data";
    byte[] inFileData = inputFileDataStr.getBytes(StandardCharsets.UTF_8);
    testRunner.enqueue(inFileData);
    testRunner.run();

    testRunner.assertTransferCount(REL_SUCCESS, 0);
    testRunner.assertTransferCount(REL_ORIGINAL, 0);
    testRunner.assertTransferCount(REL_RETRY, 0);
    testRunner.assertTransferCount(REL_FAILURE, 1);
    MockFlowFile orgFile = testRunner.getFlowFilesForRelationship(REL_FAILURE).get(0);
    orgFile.assertContentEquals(inputFileDataStr);
    orgFile.assertAttributeEquals(Exception.key(), exception.getClass().getName());
}
 
Example 11
Source Project: tutorials   Source File: N1QLLiveTest.java    License: MIT License 6 votes vote down vote up
@Test
public void givenSelectStatementWithDSL2_whenQuery_thenResult() {
    Bucket bucket = bucketFactory.getTravelSampleBucket();
    Statement st2 = select(
            x("t.city, t.airportname")
                    .concat(s(" (")).concat(x("t.faa")).concat(s(")")).as("portname_faa"))
            .from(i("travel-sample").as("t"))
            .where( x("t.type").eq(s("airport"))
                    .and(x("t.country").like(s("%States")))
                    .and(x("t.geo.lat").gte(70)))
            .limit(2);
    N1qlQueryResult r5 = bucket.query(N1qlQuery.simple(st2));
    List<JsonNode> list5 = extractJsonResult(r5);
    System.out.println("First Doc : " + list5.get(0));
    System.out.println("Query from Statement2: " + st2.toString());
}
 
Example 12
Source Project: nifi   Source File: TestGetCouchbaseKey.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testInputFlowFileContent() throws Exception {

    Bucket bucket = mock(Bucket.class);
    String inFileDataStr = "doc-in";
    String content = "{\"key\":\"value\"}";
    when(bucket.get(inFileDataStr, RawJsonDocument.class))
        .thenReturn(RawJsonDocument.create(inFileDataStr, content));
    setupMockBucket(bucket);


    byte[] inFileData = inFileDataStr.getBytes(StandardCharsets.UTF_8);
    testRunner.enqueue(inFileData);
    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 13
Source Project: samza   Source File: TestCouchbaseBucketRegistry.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * This unit test uses CouchbaseBucketRegistry to register two mocked buckets with same name but in different clusters.
 * Calling registry.getBucket with same bucketName but different clusterNodes should return different Bucket instances.
 */
@Test
public void testOpenSameBucketNameFromDifferentClusters() {
  String bucketName = "bucket";
  List<String> clusterNodes1 = Arrays.asList("cluster1");
  List<String> clusterNodes2 = Arrays.asList("cluster2");
  CouchbaseEnvironmentConfigs configs = new CouchbaseEnvironmentConfigs();
  CouchbaseCluster cluster1 = mock(CouchbaseCluster.class);
  CouchbaseCluster cluster2 = mock(CouchbaseCluster.class);
  when(cluster1.openBucket(bucketName)).thenReturn(mock(Bucket.class));
  when(cluster2.openBucket(bucketName)).thenReturn(mock(Bucket.class));
  mockStatic(CouchbaseCluster.class);
  when(CouchbaseCluster.create(any(CouchbaseEnvironment.class), eq(clusterNodes1))).thenReturn(cluster1);
  when(CouchbaseCluster.create(any(CouchbaseEnvironment.class), eq(clusterNodes2))).thenReturn(cluster2);
  CouchbaseBucketRegistry registry = new CouchbaseBucketRegistry();
  Bucket bucketInCluster1 = registry.getBucket(bucketName, clusterNodes1, configs);
  Bucket bucketInCluster2 = registry.getBucket(bucketName, clusterNodes2, configs);
  assertNotEquals(bucketInCluster1, bucketInCluster2);
}
 
Example 14
Source Project: samza   Source File: TestCouchbaseBucketRegistry.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * This unit test simulates 10 tasks using the same bucket. Each task will call registry.getBucket once. Then
 * each task will also call registry.closeBucket once. After that, registry.closeBucket should return false if we
 * close the bucket one more time. And the bucket should have already been closed.
 */
@Test
public void testCloseBucket() {
  String bucketName = "bucket";
  List<String> clusterNodes = Arrays.asList("cluster");
  CouchbaseEnvironmentConfigs configs = new CouchbaseEnvironmentConfigs();
  CouchbaseCluster cluster = mock(CouchbaseCluster.class);
  Bucket bucket = mock(Bucket.class);
  when(bucket.close()).thenReturn(true).thenReturn(false);
  when(cluster.openBucket(bucketName)).thenReturn(bucket);
  when(cluster.disconnect()).thenReturn(true).thenReturn(false);
  mockStatic(CouchbaseCluster.class);
  when(CouchbaseCluster.create(any(CouchbaseEnvironment.class), eq(clusterNodes))).thenReturn(cluster);
  CouchbaseBucketRegistry registry = new CouchbaseBucketRegistry();
  int numOfThreads = 10;
  for (int i = 0; i < numOfThreads; i++) {
    registry.getBucket(bucketName, clusterNodes, configs);
  }
  for (int i = 0; i < numOfThreads; i++) {
    assertTrue(registry.closeBucket(bucketName, clusterNodes));
  }
  // Close one more time. Should return false.
  assertFalse(registry.closeBucket(bucketName, clusterNodes));
  // Bucket should has been closed
  assertFalse(bucket.close());
}
 
Example 15
Source Project: components   Source File: CouchbaseInputTestIT.java    License: Apache License 2.0 6 votes vote down vote up
private void populateBucket() {
    CouchbaseEnvironment env = DefaultCouchbaseEnvironment
            .builder()
            .socketConnectTimeout(60000)
            .connectTimeout(60000)
            .keepAliveInterval(60000)
            .keyValueServiceConfig(KeyValueServiceConfig.create(60)) // If skip this config, we may get TimeoutException https://forums.couchbase.com/t/kv-upsert-throwing-timeoutexception-couchbase-4-5/9399
            .build();
    CouchbaseCluster cluster = CouchbaseCluster.create(env, bootstrapNodes);
    Bucket bucket = cluster.openBucket(bucketName, password);
    LOGGER.info("Connected to bucket - " + bucketName);
    assertTrue(bucket.bucketManager().flush());
    JsonDocument document = JsonDocument.create("foo", JsonObject.create().put("bar", 42));
    bucket.upsert(document, PersistTo.MASTER);
    bucket.close();
    LOGGER.info("Bucket is closed after upserting data");
    if (cluster != null) {
        cluster.disconnect();
    }
}
 
Example 16
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 17
Source Project: nifi   Source File: TestGetCouchbaseKey.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testCouchbaseFatalError() throws Exception {
    String docIdExp = "doc-c";

    Bucket bucket = mock(Bucket.class);
    CouchbaseException exception = new NotConnectedException();
    when(bucket.get(docIdExp, RawJsonDocument.class))
        .thenThrow(exception);
    setupMockBucket(bucket);

    testRunner.setProperty(DOC_ID, docIdExp);

    String inputFileDataStr = "input FlowFile data";
    byte[] inFileData = inputFileDataStr.getBytes(StandardCharsets.UTF_8);
    testRunner.enqueue(inFileData);
    testRunner.run();

    testRunner.assertTransferCount(REL_SUCCESS, 0);
    testRunner.assertTransferCount(REL_ORIGINAL, 0);
    testRunner.assertTransferCount(REL_RETRY, 1);
    testRunner.assertTransferCount(REL_FAILURE, 0);
    MockFlowFile orgFile = testRunner.getFlowFilesForRelationship(REL_RETRY).get(0);
    orgFile.assertContentEquals(inputFileDataStr);
    orgFile.assertAttributeEquals(Exception.key(), exception.getClass().getName());
}
 
Example 18
Source Project: nifi   Source File: TestGetCouchbaseKey.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testDocIdExpWithInvalidExpression() throws Exception {
    String docIdExp = "${nonExistingFunction('doc-s')}";
    String docId = "doc-s";

    Bucket bucket = mock(Bucket.class);
    String content = "{\"key\":\"value\"}";
    when(bucket.get(docId, RawJsonDocument.class))
        .thenReturn(RawJsonDocument.create(docId, content));
    setupMockBucket(bucket);

    testRunner.setProperty(DOC_ID, docIdExp);
    testRunner.enqueue(new byte[0]);

    try {
        testRunner.run();
        fail("Exception should be thrown.");
    } catch (AssertionError e) {
        Assert.assertTrue(e.getCause().getClass().equals(AttributeExpressionLanguageException.class));
    }

    testRunner.assertTransferCount(REL_SUCCESS, 0);
    testRunner.assertTransferCount(REL_ORIGINAL, 0);
    testRunner.assertTransferCount(REL_RETRY, 0);
    testRunner.assertTransferCount(REL_FAILURE, 0);
}
 
Example 19
Source Project: java-specialagent   Source File: CouchbaseClientITest.java    License: Apache License 2.0 5 votes vote down vote up
public static void main(final String[] args) throws BucketAlreadyExistsException, InterruptedException, IOException {
  final CouchbaseMock couchbaseMock = new CouchbaseMock("localhost", 8091, 2, 1);
  final BucketConfiguration bucketConfiguration = new BucketConfiguration();
  bucketConfiguration.name = bucketName;
  bucketConfiguration.numNodes = 1;
  bucketConfiguration.numReplicas = 1;
  bucketConfiguration.password = "";
  couchbaseMock.start();
  couchbaseMock.waitForStartup();
  couchbaseMock.createBucket(bucketConfiguration);

  final Cluster cluster = CouchbaseCluster.create(DefaultCouchbaseEnvironment.builder().connectTimeout(TimeUnit.SECONDS.toMillis(60)).build());
  final Bucket bucket = cluster.openBucket(bucketName);

  final JsonObject arthur = JsonObject
    .create().put("name", "Arthur")
    .put("email", "[email protected]")
    .put("interests", JsonArray.from("Holy Grail", "African Swallows"));

  bucket.upsert(JsonDocument.create("u:king_arthur", arthur));

  System.out.println(bucket.get("u:king_arthur"));

  cluster.disconnect(60, TimeUnit.SECONDS);
  couchbaseMock.stop();

  TestUtil.checkSpan(new ComponentSpanCount("couchbase-java-client.*", 2));
}
 
Example 20
Source Project: tutorials   Source File: ClusterServiceImpl.java    License: MIT License 5 votes vote down vote up
@Override
public List<JsonDocument> getDocuments(Bucket bucket, Iterable<String> keys) {
    List<JsonDocument> docs = new ArrayList<>();
    for (String key : keys) {
        JsonDocument doc = bucket.get(key);
        if (doc != null) {
            docs.add(doc);
        }
    }
    return docs;
}
 
Example 21
Source Project: tutorials   Source File: PersonServiceImplLiveTest.java    License: MIT License 5 votes vote down vote up
@BeforeClass
public static void setupBeforeClass() {
    final Cluster cluster = CouchbaseCluster.create(MultiBucketCouchbaseConfig.NODE_LIST);
    final Bucket bucket = cluster.openBucket(MultiBucketCouchbaseConfig.DEFAULT_BUCKET_NAME, MultiBucketCouchbaseConfig.DEFAULT_BUCKET_PASSWORD);
    bucket.upsert(JsonDocument.create(johnSmithId, jsonJohnSmith));
    bucket.upsert(JsonDocument.create(foobarId, jsonFooBar));
    bucket.close();
    cluster.disconnect();
}
 
Example 22
Source Project: nifi   Source File: TestPutCouchbaseKey.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testInputFlowFileUuid() throws Exception {

    String uuid = "00029362-5106-40e8-b8a9-bf2cecfbc0d7";
    String inFileData = "{\"key\":\"value\"}";
    byte[] inFileDataBytes = inFileData.getBytes(StandardCharsets.UTF_8);

    Bucket bucket = mock(Bucket.class);
    when(bucket.upsert(any(RawJsonDocument.class), eq(PersistTo.NONE), eq(ReplicateTo.NONE)))
        .thenReturn(RawJsonDocument.create(uuid, inFileData));
    setupMockBucket(bucket);

    Map<String, String> properties = new HashMap<>();
    properties.put(CoreAttributes.UUID.key(), uuid);
    testRunner.enqueue(inFileDataBytes, properties);
    testRunner.run();

    ArgumentCaptor<RawJsonDocument> capture = ArgumentCaptor.forClass(RawJsonDocument.class);
    verify(bucket, times(1)).upsert(capture.capture(), eq(PersistTo.NONE), eq(ReplicateTo.NONE));
    assertEquals(inFileData, capture.getValue().content());

    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);
}
 
Example 23
Source Project: tutorials   Source File: N1QLLiveTest.java    License: MIT License 5 votes vote down vote up
@Test
public void givenUnestUpdateStatement_whenQuery_thenResult() {
    Bucket bucket = bucketFactory.getTravelSampleBucket();
    String query3 = "UPDATE `travel-sample` USE KEYS \"cust_2\" " +
            "UNSET name RETURNING *";
    N1qlQueryResult result1 = bucket.query(N1qlQuery.simple(query3));
    result1.forEach(System.out::println);
}
 
Example 24
Source Project: tutorials   Source File: CodeSnippets.java    License: MIT License 5 votes vote down vote up
static JsonDocument getLatestReplicaVersion(Bucket bucket, String id) {
    long maxCasValue = -1;
    JsonDocument latest = null;
    for (JsonDocument replica : bucket.getFromReplica(id, ReplicaMode.ALL)) {
        if (replica.cas() > maxCasValue) {
            latest = replica;
            maxCasValue = replica.cas();
        }
    }
    return latest;
}
 
Example 25
Source Project: localization_nifi   Source File: TestPutCouchbaseKey.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testDurabilityConstraint() throws Exception {
    String docId = "doc-a";

    String inFileData = "{\"key\":\"value\"}";
    byte[] inFileDataBytes = inFileData.getBytes(StandardCharsets.UTF_8);

    Bucket bucket = mock(Bucket.class);
    when(bucket.upsert(any(RawJsonDocument.class), eq(PersistTo.MASTER), eq(ReplicateTo.ONE)))
        .thenReturn(RawJsonDocument.create(docId, inFileData));
    setupMockBucket(bucket);

    testRunner.enqueue(inFileDataBytes);
    testRunner.setProperty(DOC_ID, docId);
    testRunner.setProperty(PutCouchbaseKey.PERSIST_TO, PersistTo.MASTER.toString());
    testRunner.setProperty(PutCouchbaseKey.REPLICATE_TO, ReplicateTo.ONE.toString());
    testRunner.run();

    verify(bucket, times(1)).upsert(any(RawJsonDocument.class), eq(PersistTo.MASTER), eq(ReplicateTo.ONE));

    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);
}
 
Example 26
Source Project: localization_nifi   Source File: TestPutCouchbaseKey.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testInvalidDocIdExp() throws Exception {
    String docIdExp = "${invalid_function(someProperty)}";
    String somePropertyValue = "doc-p";

    String inFileData = "{\"key\":\"value\"}";
    byte[] inFileDataBytes = inFileData.getBytes(StandardCharsets.UTF_8);

    Bucket bucket = mock(Bucket.class);
    when(bucket.upsert(any(RawJsonDocument.class), eq(PersistTo.NONE), eq(ReplicateTo.NONE)))
        .thenReturn(RawJsonDocument.create(somePropertyValue, inFileData));
    setupMockBucket(bucket);

    testRunner.setProperty(DOC_ID, docIdExp);

    Map<String, String> properties = new HashMap<>();
    properties.put("someProperty", somePropertyValue);
    testRunner.enqueue(inFileDataBytes, properties);
    try {
        testRunner.run();
        fail("Exception should be thrown.");
    } catch (AssertionError e){
        Assert.assertTrue(e.getCause().getClass().equals(AttributeExpressionLanguageException.class));
    }

    testRunner.assertTransferCount(REL_SUCCESS, 0);
    testRunner.assertTransferCount(REL_RETRY, 0);
    testRunner.assertTransferCount(REL_FAILURE, 0);
}
 
Example 27
Source Project: localization_nifi   Source File: TestPutCouchbaseKey.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testInputFlowFileUuid() throws Exception {

    String uuid = "00029362-5106-40e8-b8a9-bf2cecfbc0d7";
    String inFileData = "{\"key\":\"value\"}";
    byte[] inFileDataBytes = inFileData.getBytes(StandardCharsets.UTF_8);

    Bucket bucket = mock(Bucket.class);
    when(bucket.upsert(any(RawJsonDocument.class), eq(PersistTo.NONE), eq(ReplicateTo.NONE)))
        .thenReturn(RawJsonDocument.create(uuid, inFileData));
    setupMockBucket(bucket);

    Map<String, String> properties = new HashMap<>();
    properties.put(CoreAttributes.UUID.key(), uuid);
    testRunner.enqueue(inFileDataBytes, properties);
    testRunner.run();

    ArgumentCaptor<RawJsonDocument> capture = ArgumentCaptor.forClass(RawJsonDocument.class);
    verify(bucket, times(1)).upsert(capture.capture(), eq(PersistTo.NONE), eq(ReplicateTo.NONE));
    assertEquals(uuid, capture.getValue().id());
    assertEquals(inFileData, capture.getValue().content());

    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);
}
 
Example 28
Source Project: localization_nifi   Source File: TestPutCouchbaseKey.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCouchbaseFailure() throws Exception {

    String docId = "doc-a";

    String inFileData = "{\"key\":\"value\"}";
    byte[] inFileDataBytes = inFileData.getBytes(StandardCharsets.UTF_8);

    Bucket bucket = mock(Bucket.class);
    when(bucket.upsert(any(RawJsonDocument.class), eq(PersistTo.NONE), eq(ReplicateTo.ONE)))
        .thenThrow(new ServiceNotAvailableException());
    setupMockBucket(bucket);

    testRunner.enqueue(inFileDataBytes);
    testRunner.setProperty(DOC_ID, docId);
    testRunner.setProperty(PutCouchbaseKey.REPLICATE_TO, ReplicateTo.ONE.toString());
    try {
        testRunner.run();
        fail("ProcessException should be thrown.");
    } catch (AssertionError e){
        Assert.assertTrue(e.getCause().getClass().equals(ProcessException.class));
    }

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

    testRunner.assertAllFlowFilesTransferred(REL_FAILURE);
    testRunner.assertTransferCount(REL_SUCCESS, 0);
    testRunner.assertTransferCount(REL_RETRY, 0);
    testRunner.assertTransferCount(REL_FAILURE, 0);
}
 
Example 29
Source Project: localization_nifi   Source File: TestPutCouchbaseKey.java    License: Apache License 2.0 5 votes vote down vote up
@Test
public void testCouchbaseTempFlowFileError() throws Exception {

    String docId = "doc-a";

    String inFileData = "{\"key\":\"value\"}";
    byte[] inFileDataBytes = inFileData.getBytes(StandardCharsets.UTF_8);

    Bucket bucket = mock(Bucket.class);
    CouchbaseException exception = new DurabilityException();
    when(bucket.upsert(any(RawJsonDocument.class), eq(PersistTo.NONE), eq(ReplicateTo.ONE)))
        .thenThrow(exception);
    setupMockBucket(bucket);

    testRunner.enqueue(inFileDataBytes);
    testRunner.setProperty(DOC_ID, docId);
    testRunner.setProperty(PutCouchbaseKey.REPLICATE_TO, ReplicateTo.ONE.toString());
    testRunner.run();

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

    testRunner.assertTransferCount(REL_SUCCESS, 0);
    testRunner.assertTransferCount(REL_RETRY, 1);
    testRunner.assertTransferCount(REL_FAILURE, 0);
    MockFlowFile orgFile = testRunner.getFlowFilesForRelationship(REL_RETRY).get(0);
    orgFile.assertContentEquals(inFileData);
    orgFile.assertAttributeEquals(Exception.key(), exception.getClass().getName());
}
 
Example 30
Source Project: localization_nifi   Source File: TestGetCouchbaseKey.java    License: Apache License 2.0 5 votes vote down vote up
private void setupMockBucket(Bucket bucket) throws InitializationException {
    CouchbaseClusterControllerService service = mock(CouchbaseClusterControllerService.class);
    when(service.getIdentifier()).thenReturn(SERVICE_ID);
    when(service.openBucket(anyString())).thenReturn(bucket);
    testRunner.addControllerService(SERVICE_ID, service);
    testRunner.enableControllerService(service);
    testRunner.setProperty(COUCHBASE_CLUSTER_SERVICE, SERVICE_ID);
}