Repository navigation
CAMEL-25361: camel-couchbase - use the view value as the body and document the fullDocument default #27588
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
davsclaus
merged 3 commits into
apache:main
from
allthingssecurity:camel-couchbase-view-body-and-fulldocument-default
Oct 9, 2026
Merged
CAMEL-25361: camel-couchbase - use the view value as the body and document the fullDocument default #27588
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
f55d31e
CAMEL-25361: camel-couchbase - use the view value as the body and doc…
allthingssecurity ae67a6c
CAMEL-25361: camel-couchbase - control tests for the SQL++ row body a…
allthingssecurity a048d0b
CAMEL-25361: camel-couchbase - Address review: pin the mocked ViewRow…
allthingssecurity File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
174 changes: 174 additions & 0 deletions
174
...uchbase/src/test/java/org/apache/camel/component/couchbase/CouchbaseConsumerBodyTest.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| /* | ||
| * 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.camel.component.couchbase; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Map; | ||
| import java.util.Optional; | ||
|
|
||
| import com.couchbase.client.java.Bucket; | ||
| import com.couchbase.client.java.Cluster; | ||
| import com.couchbase.client.java.ClusterOptions; | ||
| import com.couchbase.client.java.Collection; | ||
| import com.couchbase.client.java.Scope; | ||
| import com.couchbase.client.java.json.JsonObject; | ||
| import com.couchbase.client.java.kv.GetOptions; | ||
| import com.couchbase.client.java.kv.GetResult; | ||
| import com.couchbase.client.java.query.QueryOptions; | ||
| import com.couchbase.client.java.query.QueryResult; | ||
| import com.couchbase.client.java.view.ViewOptions; | ||
| import com.couchbase.client.java.view.ViewResult; | ||
| import com.couchbase.client.java.view.ViewRow; | ||
| import org.apache.camel.builder.RouteBuilder; | ||
| import org.apache.camel.impl.DefaultCamelContext; | ||
| import org.junit.jupiter.api.AfterEach; | ||
| import org.junit.jupiter.api.BeforeEach; | ||
| import org.junit.jupiter.api.Test; | ||
| import org.mockito.MockedStatic; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.assertEquals; | ||
| import static org.junit.jupiter.api.Assertions.assertNull; | ||
| import static org.junit.jupiter.api.Assertions.assertSame; | ||
| import static org.mockito.ArgumentMatchers.any; | ||
| import static org.mockito.ArgumentMatchers.anyString; | ||
| import static org.mockito.ArgumentMatchers.eq; | ||
| import static org.mockito.Mockito.mock; | ||
| import static org.mockito.Mockito.mockStatic; | ||
| import static org.mockito.Mockito.when; | ||
|
|
||
| /** | ||
| * The message body the consumer creates for a row, with and without {@code fullDocument}. | ||
| * <p/> | ||
| * The scheduler of the consumer is not started; the test calls {@code poll()} itself against a mocked cluster. | ||
| */ | ||
| class CouchbaseConsumerBodyTest { | ||
|
|
||
| private static final String URI = "couchbase:http://localhost:8091?bucket=bucket&username=user&password=secret" | ||
| + "&startScheduler=false"; | ||
| private static final String VIEW = "&useView=true&designDocumentName=dd&viewName=view"; | ||
|
|
||
| private DefaultCamelContext context; | ||
| private MockedStatic<Cluster> clusters; | ||
| private Bucket bucket; | ||
| private Scope scope; | ||
| private Collection collection; | ||
| private final List<Object> bodies = new ArrayList<>(); | ||
|
|
||
| @BeforeEach | ||
| void setUp() { | ||
| bucket = mock(Bucket.class); | ||
| scope = mock(Scope.class); | ||
| collection = mock(Collection.class); | ||
| when(bucket.defaultScope()).thenReturn(scope); | ||
| when(bucket.defaultCollection()).thenReturn(collection); | ||
| Cluster cluster = mock(Cluster.class); | ||
| when(cluster.bucket(anyString())).thenReturn(bucket); | ||
| clusters = mockStatic(Cluster.class); | ||
| clusters.when(() -> Cluster.connect(anyString(), any(ClusterOptions.class))).thenReturn(cluster); | ||
| context = new DefaultCamelContext(); | ||
| } | ||
|
|
||
| @AfterEach | ||
| void tearDown() { | ||
| context.stop(); | ||
| clusters.close(); | ||
| } | ||
|
|
||
| private CouchbaseConsumer startRoute(String uri) throws Exception { | ||
| context.addRoutes(new RouteBuilder() { | ||
| @Override | ||
| public void configure() { | ||
| from(uri).routeId("couchbase").process(exchange -> bodies.add(exchange.getMessage().getBody())); | ||
| } | ||
| }); | ||
| context.start(); | ||
| return (CouchbaseConsumer) context.getRoute("couchbase").getConsumer(); | ||
| } | ||
|
|
||
| @SuppressWarnings("deprecation") | ||
| private void viewReturns(ViewRow... rows) { | ||
| ViewResult result = mock(ViewResult.class); | ||
| when(result.rows()).thenReturn(List.of(rows)); | ||
| when(bucket.viewQuery(anyString(), anyString(), any(ViewOptions.class))).thenReturn(result); | ||
| } | ||
|
|
||
| private void queryReturns(JsonObject... rows) { | ||
| QueryResult result = mock(QueryResult.class); | ||
| when(result.rowsAsObject()).thenReturn(List.of(rows)); | ||
| when(scope.query(anyString(), any(QueryOptions.class))).thenReturn(result); | ||
| } | ||
|
|
||
| /** A view row as the SDK returns it: the value is an {@code Optional}, empty when the view emitted null. */ | ||
| private static ViewRow viewRow(String id, Object value) { | ||
| ViewRow row = mock(ViewRow.class); | ||
| when(row.id()).thenReturn(Optional.of(id)); | ||
| // pins the valueAs(Class) and keyAs(Class) overloads that CouchbaseConsumer calls: if the consumer moves to | ||
| // another overload such as valueAs(TypeRef), stub that one here too, as an unstubbed call returns null and the | ||
| // tests would fail with a NullPointerException instead of a clear assertion | ||
| when(row.valueAs(Object.class)).thenReturn(Optional.ofNullable(value)); | ||
| when(row.keyAs(String.class)).thenReturn(Optional.of(id)); | ||
| return row; | ||
| } | ||
|
|
||
| @Test | ||
| void theValueEmittedByTheViewIsTheBodyWithoutFullDocument() throws Exception { | ||
| Map<String, Object> value = Map.of("name", "Alice"); | ||
| viewReturns(viewRow("doc-1", value), viewRow("doc-2", "Bob")); | ||
| CouchbaseConsumer consumer = startRoute(URI + VIEW + "&fullDocument=false"); | ||
|
|
||
| assertEquals(2, consumer.poll()); | ||
|
|
||
| assertEquals(List.of(value, "Bob"), bodies, "the body must be the value of the view row, not an Optional"); | ||
| } | ||
|
|
||
| @Test | ||
| void theBodyIsNullWhenTheViewEmitsNoValue() throws Exception { | ||
| viewReturns(viewRow("doc-1", null)); | ||
| CouchbaseConsumer consumer = startRoute(URI + VIEW + "&fullDocument=false"); | ||
|
|
||
| assertEquals(1, consumer.poll()); | ||
|
|
||
| assertEquals(1, bodies.size()); | ||
| assertNull(bodies.get(0), "the body must be null when the view emitted null, not an empty Optional"); | ||
| } | ||
|
|
||
| @Test | ||
| void theQueryRowIsTheBodyWithoutFullDocument() throws Exception { | ||
| JsonObject row = JsonObject.create().put(CouchbaseConsumer.SQL_DOCUMENT_ID_ALIAS, "doc-1").put("name", "Alice"); | ||
| queryReturns(row); | ||
| CouchbaseConsumer consumer = startRoute(URI + "&fullDocument=false"); | ||
|
|
||
| assertEquals(1, consumer.poll()); | ||
|
|
||
| assertEquals(List.of(row.toString()), bodies, "the body must be the SQL++ row as a JSON string"); | ||
| } | ||
|
|
||
| @Test | ||
| void theFullDocumentIsTheBodyByDefault() throws Exception { | ||
| queryReturns(JsonObject.create().put(CouchbaseConsumer.SQL_DOCUMENT_ID_ALIAS, "doc-1").put("name", "Alice")); | ||
| GetResult document = mock(GetResult.class); | ||
| when(collection.get(eq("doc-1"), any(GetOptions.class))).thenReturn(document); | ||
| // fullDocument is not set: it defaults to true, so the consumer fetches the document by its ID | ||
| CouchbaseConsumer consumer = startRoute(URI); | ||
|
|
||
| assertEquals(1, consumer.poll()); | ||
|
|
||
| assertEquals(1, bodies.size()); | ||
| assertSame(document, bodies.get(0), "the body must be the document fetched by the ID of the row"); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.