Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,11 @@
<artifactId>nifi-couchbase-processors</artifactId>
<version>2.7.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-couchbase-services</artifactId>
<version>2.7.0-SNAPSHOT</version>
</dependency>
<dependency>
<groupId>org.apache.nifi</groupId>
<artifactId>nifi-couchbase-services-api-nar</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@
import org.apache.nifi.processor.ProcessSession;
import org.apache.nifi.processor.exception.ProcessException;
import org.apache.nifi.services.couchbase.CouchbaseClient;
import org.apache.nifi.services.couchbase.CouchbaseConnectionService;
import org.apache.nifi.services.couchbase.exception.CouchbaseException;
import org.apache.nifi.services.couchbase.utils.CouchbaseContext;
import org.apache.nifi.services.couchbase.utils.CouchbaseUpsertResult;
Expand Down Expand Up @@ -69,7 +68,6 @@ public void onTrigger(ProcessContext context, ProcessSession session) throws Pro
}

final long startNanos = System.nanoTime();
final CouchbaseConnectionService connectionService = context.getProperty(COUCHBASE_CONNECTION_SERVICE).asControllerService(CouchbaseConnectionService.class);
final String documentId = context.getProperty(DOCUMENT_ID).evaluateAttributeExpressions(flowFile).getValue();

final CouchbaseContext couchbaseContext = getCouchbaseContext(context, flowFile);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.nifi.processors.couchbase;

import org.apache.nifi.services.couchbase.CouchbaseClient;
import org.apache.nifi.services.couchbase.CouchbaseConnectionService;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

public abstract class AbstractCouchbaseProcessorTest {

protected static final String SERVICE_ID = "couchbaseConnectionService";
protected static final String TEST_DOCUMENT_ID = "test-document-id";
protected static final String TEST_DOCUMENT_CONTENT = "{\"key\":\"value\"}";
protected static final String TEST_SERVICE_LOCATION = "couchbase://test-location";
protected static final long TEST_CAS = 1L;

protected static CouchbaseConnectionService mockConnectionService(CouchbaseClient client) {
final CouchbaseConnectionService connectionService = mock(CouchbaseConnectionService.class);
when(connectionService.getIdentifier()).thenReturn(SERVICE_ID);
when(connectionService.getClient(any())).thenReturn(client);
when(connectionService.getServiceLocation()).thenReturn(TEST_SERVICE_LOCATION);
return connectionService;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
Expand Down Expand Up @@ -60,12 +60,7 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class GetCouchbaseTest {

private static final String SERVICE_ID = "couchbaseConnectionService";
private static final String TEST_DOCUMENT_ID = "test-document-id";
private static final String TEST_SERVICE_LOCATION = "couchbase://test-location";
private static final long TEST_CAS = 1L;
public class GetCouchbaseTest extends AbstractCouchbaseProcessorTest {

private TestRunner runner;

Expand All @@ -76,20 +71,15 @@ public void init() {

@Test
public void testOnTriggerWithProvidedDocumentId() throws CouchbaseException, InitializationException {
final String content = "{\"key\":\"value\"}";

final CouchbaseGetResult result = new CouchbaseGetResult(content.getBytes(), TEST_CAS);
final CouchbaseGetResult result = new CouchbaseGetResult(TEST_DOCUMENT_CONTENT.getBytes(), TEST_CAS);

final CouchbaseClient client = mock(CouchbaseClient.class);
when(client.getDocument(anyString())).thenReturn(result);

final CouchbaseConnectionService service = mock(CouchbaseConnectionService.class);
when(service.getIdentifier()).thenReturn(SERVICE_ID);
when(service.getClient(any())).thenReturn(client);
when(service.getServiceLocation()).thenReturn(TEST_SERVICE_LOCATION);
final CouchbaseConnectionService connectionService = mockConnectionService(client);

runner.addControllerService(SERVICE_ID, service);
runner.enableControllerService(service);
runner.addControllerService(SERVICE_ID, connectionService);
runner.enableControllerService(connectionService);
runner.setProperty(DOCUMENT_ID, TEST_DOCUMENT_ID);
runner.setProperty(COUCHBASE_CONNECTION_SERVICE, SERVICE_ID);
runner.enqueue(new byte[0]);
Expand All @@ -101,7 +91,7 @@ public void testOnTriggerWithProvidedDocumentId() throws CouchbaseException, Ini
runner.assertTransferCount(REL_FAILURE, 0);

final MockFlowFile outFile = runner.getFlowFilesForRelationship(REL_SUCCESS).getFirst();
outFile.assertContentEquals(content);
outFile.assertContentEquals(TEST_DOCUMENT_CONTENT);
outFile.assertAttributeEquals(BUCKET_ATTRIBUTE, DEFAULT_BUCKET);
outFile.assertAttributeEquals(SCOPE_ATTRIBUTE, DEFAULT_SCOPE);
outFile.assertAttributeEquals(COLLECTION_ATTRIBUTE, DEFAULT_COLLECTION);
Expand All @@ -116,24 +106,18 @@ public void testOnTriggerWithProvidedDocumentId() throws CouchbaseException, Ini

@Test
public void testWithDocumentIdAsFlowFileAttribute() throws CouchbaseException, InitializationException {
final String content = "{\"key\":\"value\"}";

final CouchbaseGetResult result = new CouchbaseGetResult(content.getBytes(), TEST_CAS);
final CouchbaseGetResult result = new CouchbaseGetResult(TEST_DOCUMENT_CONTENT.getBytes(), TEST_CAS);

final CouchbaseClient client = mock(CouchbaseClient.class);
when(client.getDocument(anyString())).thenReturn(result);

final CouchbaseConnectionService service = mock(CouchbaseConnectionService.class);
when(service.getIdentifier()).thenReturn(SERVICE_ID);
when(service.getClient(any())).thenReturn(client);
final CouchbaseConnectionService connectionService = mockConnectionService(client);

final MockFlowFile flowFile = new MockFlowFile(0);
final Map<String, String> attributes = new HashMap<>();
attributes.put("flowfile_document_id", TEST_DOCUMENT_ID);
flowFile.putAttributes(attributes);
flowFile.putAttributes(Collections.singletonMap("flowfile_document_id", TEST_DOCUMENT_ID));

runner.addControllerService(SERVICE_ID, service);
runner.enableControllerService(service);
runner.addControllerService(SERVICE_ID, connectionService);
runner.enableControllerService(connectionService);
runner.setProperty(DOCUMENT_ID, "${flowfile_document_id}");
runner.setProperty(COUCHBASE_CONNECTION_SERVICE, SERVICE_ID);
runner.enqueue(flowFile);
Expand All @@ -145,7 +129,7 @@ public void testWithDocumentIdAsFlowFileAttribute() throws CouchbaseException, I
runner.assertTransferCount(REL_FAILURE, 0);

final MockFlowFile outFile = runner.getFlowFilesForRelationship(REL_SUCCESS).getFirst();
outFile.assertContentEquals(content);
outFile.assertContentEquals(TEST_DOCUMENT_CONTENT);
outFile.assertAttributeEquals(BUCKET_ATTRIBUTE, DEFAULT_BUCKET);
outFile.assertAttributeEquals(SCOPE_ATTRIBUTE, DEFAULT_SCOPE);
outFile.assertAttributeEquals(COLLECTION_ATTRIBUTE, DEFAULT_COLLECTION);
Expand All @@ -154,24 +138,20 @@ public void testWithDocumentIdAsFlowFileAttribute() throws CouchbaseException, I

@Test
public void testWithFlowFileAttributes() throws CouchbaseException, InitializationException {
final String documentId = "test-document-id";
final String content = "{\"key\":\"value\"}";
final String testBucket = "test-bucket";
final String testScope = "test-scope";
final String testCollection = "test-collection";

final CouchbaseGetResult result = new CouchbaseGetResult(content.getBytes(), TEST_CAS);
final CouchbaseGetResult result = new CouchbaseGetResult(TEST_DOCUMENT_CONTENT.getBytes(), TEST_CAS);

final CouchbaseClient client = mock(CouchbaseClient.class);
when(client.getDocument(anyString())).thenReturn(result);

final CouchbaseConnectionService service = mock(CouchbaseConnectionService.class);
when(service.getIdentifier()).thenReturn(SERVICE_ID);
when(service.getClient(any())).thenReturn(client);
final CouchbaseConnectionService connectionService = mockConnectionService(client);

runner.addControllerService(SERVICE_ID, service);
runner.enableControllerService(service);
runner.setProperty(DOCUMENT_ID, documentId);
runner.addControllerService(SERVICE_ID, connectionService);
runner.enableControllerService(connectionService);
runner.setProperty(DOCUMENT_ID, TEST_DOCUMENT_ID);
runner.setProperty(BUCKET_NAME, "${bucket.attribute}");
runner.setProperty(SCOPE_NAME, "${scope.attribute}");
runner.setProperty(COLLECTION_NAME, "${collection.attribute}");
Expand All @@ -181,17 +161,16 @@ public void testWithFlowFileAttributes() throws CouchbaseException, Initializati
attributes.put("bucket.attribute", testBucket);
attributes.put("scope.attribute", testScope);
attributes.put("collection.attribute", testCollection);
final byte[] input = documentId.getBytes(StandardCharsets.UTF_8);
runner.enqueue(input, attributes);
runner.enqueue(new byte[0], attributes);
runner.run();

verify(client, times(1)).getDocument(eq(documentId));
verify(client, times(1)).getDocument(eq(TEST_DOCUMENT_ID));

runner.assertTransferCount(REL_SUCCESS, 1);
runner.assertTransferCount(REL_FAILURE, 0);

final MockFlowFile outFile = runner.getFlowFilesForRelationship(REL_SUCCESS).getFirst();
outFile.assertContentEquals(content);
outFile.assertContentEquals(TEST_DOCUMENT_CONTENT);
outFile.assertAttributeEquals(BUCKET_ATTRIBUTE, testBucket);
outFile.assertAttributeEquals(SCOPE_ATTRIBUTE, testScope);
outFile.assertAttributeEquals(COLLECTION_ATTRIBUTE, testCollection);
Expand All @@ -204,13 +183,11 @@ public void testWithFailure() throws CouchbaseException, InitializationException
when(client.getExceptionCategory(any())).thenReturn(ExceptionCategory.FAILURE);
when(client.getDocument(anyString())).thenThrow(new CouchbaseException("", new TestCouchbaseException("Test exception")));

final CouchbaseConnectionService service = mock(CouchbaseConnectionService.class);
when(service.getIdentifier()).thenReturn(SERVICE_ID);
when(service.getClient(any())).thenReturn(client);
final CouchbaseConnectionService connectionService = mockConnectionService(client);

runner.setProperty(DOCUMENT_ID, TEST_DOCUMENT_ID);
runner.addControllerService(SERVICE_ID, service);
runner.enableControllerService(service);
runner.addControllerService(SERVICE_ID, connectionService);
runner.enableControllerService(connectionService);
runner.setProperty(COUCHBASE_CONNECTION_SERVICE, SERVICE_ID);
runner.enqueue(new byte[0]);
runner.run();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,7 @@
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

public class PutCouchbaseTest {

private static final String SERVICE_ID = "couchbaseConnectionService";
private static final String TEST_DOCUMENT_ID = "test-document-id";
private static final String TEST_SERVICE_LOCATION = "couchbase://test-location";
private static final long TEST_CAS = 1L;
public class PutCouchbaseTest extends AbstractCouchbaseProcessorTest {

private TestRunner runner;

Expand All @@ -76,18 +71,15 @@ public void testWithDocumentIdAsFlowFileAttribute() throws CouchbaseException, I
final CouchbaseClient client = mock(CouchbaseClient.class);
when(client.upsertDocument(anyString(), any())).thenReturn(new CouchbaseUpsertResult(TEST_CAS));

final CouchbaseConnectionService service = mock(CouchbaseConnectionService.class);
when(service.getIdentifier()).thenReturn(SERVICE_ID);
when(service.getClient(any())).thenReturn(client);
when(service.getServiceLocation()).thenReturn(TEST_SERVICE_LOCATION);
final CouchbaseConnectionService connectionService = mockConnectionService(client);

final MockFlowFile flowFile = new MockFlowFile(0);
final Map<String, String> attributes = new HashMap<>();
attributes.put("flowfile_document_id", TEST_DOCUMENT_ID);
flowFile.putAttributes(attributes);

runner.addControllerService(SERVICE_ID, service);
runner.enableControllerService(service);
runner.addControllerService(SERVICE_ID, connectionService);
runner.enableControllerService(connectionService);
runner.setProperty(DOCUMENT_ID, "${flowfile_document_id}");
runner.setProperty(COUCHBASE_CONNECTION_SERVICE, SERVICE_ID);
runner.setValidateExpressionUsage(false);
Expand Down Expand Up @@ -120,12 +112,10 @@ public void testWithFailure() throws CouchbaseException, InitializationException
when(client.getExceptionCategory(any())).thenReturn(ExceptionCategory.FAILURE);
when(client.upsertDocument(anyString(), any())).thenThrow(new CouchbaseException("", new TestCouchbaseException("Test exception")));

final CouchbaseConnectionService service = mock(CouchbaseConnectionService.class);
when(service.getIdentifier()).thenReturn(SERVICE_ID);
when(service.getClient(any())).thenReturn(client);
final CouchbaseConnectionService connectionService = mockConnectionService(client);

runner.addControllerService(SERVICE_ID, service);
runner.enableControllerService(service);
runner.addControllerService(SERVICE_ID, connectionService);
runner.enableControllerService(connectionService);
runner.setProperty(DOCUMENT_ID, TEST_DOCUMENT_ID);
runner.setProperty(COUCHBASE_CONNECTION_SERVICE, SERVICE_ID);
runner.enqueue(new byte[0]);
Expand All @@ -143,12 +133,10 @@ public void testWithRetry() throws CouchbaseException, InitializationException {
when(client.getExceptionCategory(any())).thenReturn(ExceptionCategory.RETRY);
when(client.upsertDocument(anyString(), any())).thenThrow(new CouchbaseException("", new TestCouchbaseException("Test exception")));

final CouchbaseConnectionService service = mock(CouchbaseConnectionService.class);
when(service.getIdentifier()).thenReturn(SERVICE_ID);
when(service.getClient(any())).thenReturn(client);
final CouchbaseConnectionService connectionService = mockConnectionService(client);

runner.addControllerService(SERVICE_ID, service);
runner.enableControllerService(service);
runner.addControllerService(SERVICE_ID, connectionService);
runner.enableControllerService(connectionService);
runner.setProperty(DOCUMENT_ID, TEST_DOCUMENT_ID);
runner.setProperty(COUCHBASE_CONNECTION_SERVICE, SERVICE_ID);
runner.enqueue(new byte[0]);
Expand All @@ -166,12 +154,10 @@ public void testWithRollback() throws CouchbaseException, InitializationExceptio
when(client.getExceptionCategory(any())).thenReturn(ExceptionCategory.ROLLBACK);
when(client.upsertDocument(anyString(), any())).thenThrow(new CouchbaseException("", new TestCouchbaseException("Test exception")));

final CouchbaseConnectionService service = mock(CouchbaseConnectionService.class);
when(service.getIdentifier()).thenReturn(SERVICE_ID);
when(service.getClient(any())).thenReturn(client);
final CouchbaseConnectionService connectionService = mockConnectionService(client);

runner.addControllerService(SERVICE_ID, service);
runner.enableControllerService(service);
runner.addControllerService(SERVICE_ID, connectionService);
runner.enableControllerService(connectionService);
runner.setProperty(DOCUMENT_ID, TEST_DOCUMENT_ID);
runner.setProperty(COUCHBASE_CONNECTION_SERVICE, SERVICE_ID);
runner.enqueue(new byte[0]);
Expand All @@ -189,12 +175,10 @@ public void testWithUnknownException() throws CouchbaseException, Initialization
when(client.getExceptionCategory(any())).thenReturn(ExceptionCategory.FAILURE);
when(client.upsertDocument(anyString(), any())).thenThrow(new CouchbaseException("", new TestCouchbaseException("Test exception")));

final CouchbaseConnectionService service = mock(CouchbaseConnectionService.class);
when(service.getIdentifier()).thenReturn(SERVICE_ID);
when(service.getClient(any())).thenReturn(client);
final CouchbaseConnectionService connectionService = mockConnectionService(client);

runner.addControllerService(SERVICE_ID, service);
runner.enableControllerService(service);
runner.addControllerService(SERVICE_ID, connectionService);
runner.enableControllerService(connectionService);
runner.setProperty(DOCUMENT_ID, TEST_DOCUMENT_ID);
runner.setProperty(COUCHBASE_CONNECTION_SERVICE, SERVICE_ID);
runner.enqueue(new byte[0]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import org.apache.nifi.services.couchbase.exception.CouchbaseException;
import org.apache.nifi.services.couchbase.exception.ExceptionCategory;
import org.apache.nifi.services.couchbase.utils.CouchbaseGetResult;
import org.apache.nifi.services.couchbase.utils.CouchbaseLookupInResult;
import org.apache.nifi.services.couchbase.utils.CouchbaseUpsertResult;

public interface CouchbaseClient {
Expand All @@ -27,5 +28,15 @@ public interface CouchbaseClient {

CouchbaseUpsertResult upsertDocument(String documentId, byte[] content) throws CouchbaseException;

boolean documentExists(String documentId) throws CouchbaseException;

void insertDocument(String documentId, byte[] content) throws CouchbaseException;

void removeDocument(String documentId) throws CouchbaseException;

void replaceDocument(String documentId, byte[] content) throws CouchbaseException;

CouchbaseLookupInResult lookUpIn(String documentId, String subDocPath) throws CouchbaseException;

ExceptionCategory getExceptionCategory(Throwable throwable);
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ public CouchbaseException(final String message) {
}

public CouchbaseException(final String message, final Throwable cause) {
super(cause);
super(message, cause);
}
}
Loading