Search in sources :

Example 51 with Bucket

use of com.couchbase.client.java.Bucket in project nifi by apache.

the class TestGetCouchbaseKey method testCouchbaseTempFlowFileError.

@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());
    Assert.assertEquals(true, orgFile.isPenalized());
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) CouchbaseException(com.couchbase.client.core.CouchbaseException) Bucket(com.couchbase.client.java.Bucket) Matchers.anyString(org.mockito.Matchers.anyString) DurabilityException(com.couchbase.client.java.error.DurabilityException) Test(org.junit.Test)

Example 52 with Bucket

use of com.couchbase.client.java.Bucket in project nifi by apache.

the class TestGetCouchbaseKey method testStaticDocId.

@Test
public void testStaticDocId() throws Exception {
    String bucketName = "bucket-1";
    String docId = "doc-a";
    Bucket bucket = mock(Bucket.class);
    String content = "{\"key\":\"value\"}";
    int expiry = 100;
    long cas = 200L;
    when(bucket.get(docId, RawJsonDocument.class)).thenReturn(RawJsonDocument.create(docId, expiry, content, cas));
    setupMockBucket(bucket);
    testRunner.setProperty(BUCKET_NAME, bucketName);
    testRunner.setProperty(DOC_ID, docId);
    testRunner.enqueue(new byte[0]);
    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);
    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));
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) Bucket(com.couchbase.client.java.Bucket) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 53 with Bucket

use of com.couchbase.client.java.Bucket in project nifi by apache.

the class TestGetCouchbaseKey method testDocIdExpWithInvalidExpression.

@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);
}
Also used : Bucket(com.couchbase.client.java.Bucket) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 54 with Bucket

use of com.couchbase.client.java.Bucket in project nifi by apache.

the class TestGetCouchbaseKey method testDocIdExpWithInvalidExpressionOnFlowFile.

@Test
public void testDocIdExpWithInvalidExpressionOnFlowFile() throws Exception {
    String docIdExp = "${nonExistingFunction(someProperty)}";
    Bucket bucket = mock(Bucket.class);
    setupMockBucket(bucket);
    testRunner.setProperty(DOC_ID, docIdExp);
    String inputFileDataStr = "input FlowFile data";
    byte[] inFileData = inputFileDataStr.getBytes(StandardCharsets.UTF_8);
    Map<String, String> properties = new HashMap<>();
    properties.put("someProperty", "someValue");
    testRunner.enqueue(inFileData, 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_ORIGINAL, 0);
    testRunner.assertTransferCount(REL_RETRY, 0);
    testRunner.assertTransferCount(REL_FAILURE, 0);
}
Also used : Bucket(com.couchbase.client.java.Bucket) HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) Test(org.junit.Test)

Example 55 with Bucket

use of com.couchbase.client.java.Bucket in project nifi by apache.

the class TestPutCouchbaseKey method testInputFlowFileUuid.

@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);
}
Also used : MockFlowFile(org.apache.nifi.util.MockFlowFile) Bucket(com.couchbase.client.java.Bucket) HashMap(java.util.HashMap) Matchers.anyString(org.mockito.Matchers.anyString) RawJsonDocument(com.couchbase.client.java.document.RawJsonDocument) Test(org.junit.Test)

Aggregations

Bucket (com.couchbase.client.java.Bucket)72 Test (org.junit.Test)53 Matchers.anyString (org.mockito.Matchers.anyString)21 JsonObject (com.couchbase.client.java.document.json.JsonObject)18 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)16 N1qlQueryResult (com.couchbase.client.java.query.N1qlQueryResult)15 MockFlowFile (org.apache.nifi.util.MockFlowFile)15 AsyncBucket (com.couchbase.client.java.AsyncBucket)13 TimeUnit (java.util.concurrent.TimeUnit)12 CouchbaseCluster (com.couchbase.client.java.CouchbaseCluster)11 CouchbaseException (com.couchbase.client.core.CouchbaseException)9 Cluster (com.couchbase.client.java.Cluster)8 RawJsonDocument (com.couchbase.client.java.document.RawJsonDocument)8 JsonDocument (com.couchbase.client.java.document.JsonDocument)7 CouchbaseEnvironment (com.couchbase.client.java.env.CouchbaseEnvironment)7 StringSerde (org.apache.samza.serializers.StringSerde)7 HashMap (java.util.HashMap)6 JsonNode (com.fasterxml.jackson.databind.JsonNode)5 N1qlQuery (com.couchbase.client.java.query.N1qlQuery)4 N1qlQueryRow (com.couchbase.client.java.query.N1qlQueryRow)4