gnodet-bot commented on code in PR #26611:
URL: https://github.com/apache/camel/pull/26611#discussion_r4052650944


##########
core/camel-core/src/test/java/org/apache/camel/model/BeanModelHelperInferredBuilderTest.java:
##########
@@ -0,0 +1,348 @@
+/*
+ * 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.model;
+
+import java.time.Duration;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.support.PropertyBindingSupport;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertInstanceOf;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * CAMEL-24820: a bean whose type has no public no-arg constructor but a 
builder() or newBuilder() method (Lombok,
+ * Immutables, LangChain4j, AWS SDK, protobuf, ...) is created via its 
builder, so it can be declared with only the type
+ * and properties, without builderClass and builderMethod.
+ */
+public class BeanModelHelperInferredBuilderTest extends ContextTestSupport {
+
+    @Override
+    public boolean isUseRouteBuilder() {
+        return false;
+    }
+
+    private BeanFactoryDefinition<?> bean(String type, Map<String, Object> 
properties) {
+        BeanFactoryDefinition<?> def = new BeanFactoryDefinition<>();
+        def.setName("myBean");
+        def.setType(type);
+        def.setProperties(properties);
+        return def;
+    }
+
+    private static Map<String, Object> props(Object... kv) {
+        Map<String, Object> map = new HashMap<>();
+        for (int i = 0; i < kv.length; i += 2) {
+            map.put((String) kv[i], kv[i + 1]);
+        }
+        return map;
+    }
+
+    @Test
+    public void testBuilderInferred() throws Exception {
+        // the same shape as dev.langchain4j.model.ollama.OllamaChatModel: 
builder() and a nested builder with build()
+        Object out = BeanModelHelper.newInstance(
+                bean(ChatModel.class.getName(), props("baseUrl", 
"http://localhost:11434";, "modelName", "qwen2.5",
+                        "temperature", "0.0", "timeout", "120s")),
+                context);
+
+        ChatModel model = assertInstanceOf(ChatModel.class, out);
+        assertEquals("http://localhost:11434";, model.baseUrl);
+        assertEquals("qwen2.5", model.modelName);
+        assertEquals(0.0, model.temperature);
+        assertEquals(Duration.ofSeconds(120), model.timeout);
+    }
+
+    @Test
+    public void testBuilderInferredWithClassPrefix() throws Exception {
+        Object out = BeanModelHelper.newInstance(
+                bean("#class:" + ChatModel.class.getName(), props("modelName", 
"llama3")), context);
+        assertEquals("llama3", assertInstanceOf(ChatModel.class, 
out).modelName);
+    }
+
+    @Test
+    public void testBuilderInferredWithoutProperties() throws Exception {
+        Object out = 
BeanModelHelper.newInstance(bean(ChatModel.class.getName(), null), context);
+        ChatModel model = assertInstanceOf(ChatModel.class, out);
+        assertNull(model.modelName);
+        assertEquals(Duration.ofSeconds(60), model.timeout, "the builder 
default");
+    }
+
+    @Test
+    public void testNewBuilderAndNoBuildMethodInferred() throws Exception {
+        // newBuilder() as the JDK HttpClient and protobuf, and create() as 
the only method that returns the type
+        Object out = BeanModelHelper.newInstance(
+                bean(Channel.class.getName(), props("host", "localhost", 
"port", "8080")), context);
+        Channel channel = assertInstanceOf(Channel.class, out);
+        assertEquals("localhost:8080", channel.address);
+    }
+
+    @Test
+    public void testBuilderMethodOverridesInferred() throws Exception {
+        // the builder is still inferred, only the method that creates the 
bean is given
+        BeanFactoryDefinition<?> def = bean(Ambiguous.class.getName(), 
props("name", "x"));
+        def.setBuilderMethod("large");
+
+        Ambiguous out = assertInstanceOf(Ambiguous.class, 
BeanModelHelper.newInstance(def, context));
+        assertEquals("large x", out.size);
+    }
+
+    @Test
+    public void testPropertiesTheBuilderDoesNotTakeAreSetOnTheBean() throws 
Exception {
+        Object out = BeanModelHelper.newInstance(
+                bean(ChatModel.class.getName(), props("modelName", "qwen2.5", 
"label", "support")), context);
+        ChatModel model = assertInstanceOf(ChatModel.class, out);
+        assertEquals("qwen2.5", model.modelName);
+        assertEquals("support", model.label, "label has a setter on the bean, 
not on the builder");
+    }
+
+    @Test
+    public void testUnknownPropertyFails() {
+        Exception e = assertThrows(Exception.class, () -> 
BeanModelHelper.newInstance(
+                bean(ChatModel.class.getName(), props("modelName", "qwen2.5", 
"unknown", "x")), context));
+        e.printStackTrace();

Review Comment:
   🔧 **Nit:** `e.printStackTrace()` leaks output to stdout during test runs. 
The assertion on `e.getMessage()` already validates the finding — remove the 
stack trace print.
   
   ```suggestion
           String msg = e.getMessage();
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to