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


##########
components/camel-hibernate/src/test/java/org/apache/camel/component/hibernate/HibernateBootstrapTest.java:
##########
@@ -0,0 +1,1331 @@
+/*
+ * 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.hibernate;
+
+import java.util.ArrayList;
+import java.util.List;
+import java.util.Map;
+import java.util.stream.Stream;
+
+import org.apache.camel.Consumer;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.Route;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.hibernate.entity.HibernateTestEntity;
+import org.apache.camel.impl.DefaultCamelContext;
+import org.apache.camel.impl.engine.DefaultUnitOfWork;
+import org.apache.camel.test.junit6.CamelTestSupport;
+import org.h2.jdbcx.JdbcDataSource;
+import org.hibernate.Filter;
+import org.hibernate.KeyType;
+import org.hibernate.LockMode;
+import org.hibernate.Session;
+import org.hibernate.SessionBuilder;
+import org.hibernate.SessionFactory;
+import org.hibernate.Timeouts;
+import org.hibernate.Transaction;
+import org.hibernate.engine.spi.SessionFactoryImplementor;
+import org.hibernate.query.MutationQuery;
+import org.hibernate.query.SelectionQuery;
+import org.hibernate.resource.transaction.spi.TransactionCoordinatorBuilder;
+import org.hibernate.service.spi.ServiceRegistryImplementor;
+import org.junit.jupiter.api.Test;
+import org.mockito.Mockito;
+
+import static org.junit.jupiter.api.Assertions.assertDoesNotThrow;
+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.assertNotNull;
+import static org.junit.jupiter.api.Assertions.assertNull;
+import static org.junit.jupiter.api.Assertions.assertSame;
+import static org.junit.jupiter.api.Assertions.assertThrows;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class HibernateBootstrapTest extends CamelTestSupport {
+
+    @Test
+    public void testExplicitSessionFactoryReuseAndNoClose() throws Exception {
+        SessionFactory mockSf = Mockito.mock(SessionFactory.class);
+
+        HibernateComponent comp = new HibernateComponent();
+        comp.setCamelContext(context);
+        comp.setSessionFactory(mockSf);
+        comp.start();
+
+        assertSame(mockSf, comp.getSessionFactory());
+        comp.stop();
+
+        Mockito.verify(mockSf, Mockito.never()).close();
+    }
+
+    @Test
+    public void testRegistrySessionFactoryReuseAndNoClose() throws Exception {
+        SessionFactory mockSf = Mockito.mock(SessionFactory.class);
+        context.getRegistry().bind("registrySf", mockSf);
+
+        HibernateComponent comp = new HibernateComponent();
+        comp.setCamelContext(context);
+        comp.start();
+
+        assertSame(mockSf, comp.getSessionFactory());
+        comp.stop();
+
+        Mockito.verify(mockSf, Mockito.never()).close();
+    }
+
+    @Test
+    public void testComponentCreatedSessionFactoryClosesOnStop() throws 
Exception {
+        HibernateComponent comp = createComponent("testDs", 
"jdbc:h2:mem:testdb;DB_CLOSE_DELAY=-1");
+
+        SessionFactory sf = comp.getSessionFactory();
+        assertNotNull(sf);
+        assertFalse(sf.isClosed());
+
+        comp.stop();
+        assertTrue(sf.isClosed());
+    }
+
+    @Test
+    public void testInvalidDataSourceRegistryNameFails() {
+        HibernateComponent comp = new HibernateComponent();
+        comp.setCamelContext(context);
+        comp.setDataSource("nonExistentDs");
+
+        Exception ex = assertThrows(Exception.class, comp::start);
+        assertTrue(ex.getMessage().contains("DataSource bean with name 
'nonExistentDs' could not be found"));
+    }
+
+    @Test
+    public void testInvalidSchemaActionFails() {
+        HibernateComponent comp = new HibernateComponent();
+        comp.setCamelContext(context);
+        comp.setSchemaAction("invalid-action");
+
+        Exception ex = assertThrows(Exception.class, comp::start);
+        assertTrue(ex.getMessage().contains("Invalid schemaAction"));
+    }
+
+    @Test
+    public void testSelectionQueryProducer() throws Exception {
+        HibernateComponent comp = createComponent(
+                "selectionDs",
+                "jdbc:h2:mem:selectiondb;DB_CLOSE_DELAY=-1",
+                HibernateTestEntity.class);
+
+        HibernateEndpoint endpoint = new HibernateEndpoint();
+        endpoint.setCamelContext(context);
+        endpoint.setSessionFactory(comp.getSessionFactory());
+        endpoint.setEntityClassName(HibernateTestEntity.class.getName());
+        endpoint.setSelectionQuery("from HibernateTestEntity");
+
+        endpoint.start();
+
+        Exchange exchange = 
context.getEndpoint("direct:test").createExchange();
+        
exchange.getMessage().setHeader(HibernateConstants.HIBERNATE_PARAMETERS, 
Map.of());
+
+        try (HibernateProducer producer = new HibernateProducer(endpoint)) {
+            producer.process(exchange);
+        }
+
+        assertNotNull(exchange.getMessage().getBody());
+        assertTrue(exchange.getMessage().getBody() instanceof List);
+        assertTrue(((List<?>) exchange.getMessage().getBody()).isEmpty());
+
+        endpoint.stop();
+        comp.stop();
+    }
+
+    @Test
+    public void testMutationQueryProducer() throws Exception {
+        HibernateComponent comp = createComponent(
+                "mutationDs",
+                "jdbc:h2:mem:mutationdb;DB_CLOSE_DELAY=-1",
+                HibernateTestEntity.class);
+
+        HibernateEndpoint endpoint = new HibernateEndpoint();
+        endpoint.setCamelContext(context);
+        endpoint.setSessionFactory(comp.getSessionFactory());
+        endpoint.setEntityClassName(HibernateTestEntity.class.getName());
+        endpoint.setMutationQuery("delete from HibernateTestEntity");
+
+        endpoint.start();
+
+        Exchange exchange = 
context.getEndpoint("direct:test").createExchange();
+        
exchange.getMessage().setHeader(HibernateConstants.HIBERNATE_PARAMETERS, 
Map.of());
+
+        try (HibernateProducer producer = new HibernateProducer(endpoint)) {
+            producer.process(exchange);
+        }
+
+        assertEquals(0, exchange.getMessage().getBody());
+
+        endpoint.stop();
+        comp.stop();
+    }
+
+    @Test
+    public void testSchemaActionsAcceptedValues() {
+        String[] actions = { "none", "validate", "update", "create" };

Review Comment:
   💡 **Minor — incomplete coverage:** `testSchemaActionsAcceptedValues` tests 
only 4 of the 7 values accepted by `validateSchemaAction()`. The values 
`"create-drop"`, `"create-only"`, and `"drop"` are missing from the array.
   
   Suggested fix:
   ```suggestion
           String[] actions = { "none", "validate", "update", "create", 
"create-drop", "create-only", "drop" };
   ```
   Also consider adding a negative test that calls `doStart()` with an invalid 
value and asserts that an exception is thrown — the current test only exercises 
the getter/setter and never triggers `validateSchemaAction()` via `doStart()`.



-- 
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