gnodet-bot commented on code in PR #27314: URL: https://github.com/apache/camel/pull/27314#discussion_r4215655004
########## components/camel-hibernate/src/test/java/org/apache/camel/component/hibernate/HibernateBootstrapTest.java: ########## @@ -0,0 +1,1330 @@ +/* + * 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" }; + for (String action : actions) { + HibernateComponent comp = new HibernateComponent(); + comp.setSchemaAction(action); + assertEquals(action, comp.getSchemaAction()); + } + } + + @Test + public void testExplicitEntityClassesBootstrap() throws Exception { + HibernateComponent comp = createComponent( + "entityDs", + "jdbc:h2:mem:entitydb;DB_CLOSE_DELAY=-1", + HibernateTestEntity.class); + + SessionFactory sf = comp.getSessionFactory(); + assertNotNull(sf); + assertNotNull(sf.getMetamodel().entity(HibernateTestEntity.class)); + + comp.stop(); + } + + @Test + public void testSelectionQueryProducerReadOnly() throws Exception { + HibernateComponent comp = createComponent( + "readOnlyDs", + "jdbc:h2:mem:readonlydb;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.setReadOnly(true); + + endpoint.start(); + + assertTrue(endpoint.isReadOnly()); + + 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); + + endpoint.stop(); + comp.stop(); + } + + @Test + public void testNaturalIdLookupProducer() throws Exception { + HibernateComponent comp = createComponent( + "naturalIdDs", + "jdbc:h2:mem:naturaliddb;DB_CLOSE_DELAY=-1", + HibernateTestEntity.class); + + Session session = comp.getSessionFactory().openSession(); + Transaction transaction = session.beginTransaction(); + + HibernateTestEntity entity = new HibernateTestEntity(); + entity.setId(1L); + entity.setName("test"); + session.persist(entity); + + transaction.commit(); + session.close(); + + HibernateEndpoint endpoint = new HibernateEndpoint(); + endpoint.setCamelContext(context); + endpoint.setSessionFactory(comp.getSessionFactory()); + endpoint.setEntityClassName(HibernateTestEntity.class.getName()); + endpoint.setNaturalIdParameters(Map.of("name", "test")); + + endpoint.start(); + + Exchange exchange = context.getEndpoint("direct:test").createExchange(); + + try (HibernateProducer producer = new HibernateProducer(endpoint)) { + producer.doStart(); + producer.process(exchange); + } + + HibernateTestEntity result = exchange.getMessage().getBody(HibernateTestEntity.class); + + assertNotNull(result); + assertEquals(1L, result.getId()); + assertEquals("test", result.getName()); + + endpoint.stop(); + comp.stop(); + } + + @Test + public void testNaturalIdPrefixUriBinding() throws Exception { + HibernateComponent comp = createComponent( + "naturalIdPrefixDs", + "jdbc:h2:mem:naturalidprefixdb;DB_CLOSE_DELAY=-1", + HibernateTestEntity.class); + + HibernateEndpoint endpoint = (HibernateEndpoint) comp.createEndpoint( + "hibernate:" + HibernateTestEntity.class.getName() + "?naturalId.name=test"); + + assertEquals(Map.of("name", "test"), endpoint.getNaturalIdParameters()); + + comp.stop(); + } + + @Test + public void testNaturalIdBeanParameter() throws Exception { + HibernateComponent comp = createComponent( + "naturalIdBeanDs", + "jdbc:h2:mem:naturalidbeandb;DB_CLOSE_DELAY=-1", + HibernateTestEntity.class); + + context.getRegistry().bind("naturalIdMap", Map.of("name", "test")); + + HibernateEndpoint endpoint = (HibernateEndpoint) comp.createEndpoint( + "hibernate:" + HibernateTestEntity.class.getName() + "?naturalIdParameters=#naturalIdMap"); + + assertEquals(Map.of("name", "test"), endpoint.getNaturalIdParameters()); + + comp.stop(); + } + + @Test + public void testNaturalIdLookupFromMessageHeader() throws Exception { + HibernateComponent comp = createComponent( + "naturalIdHeaderDs", + "jdbc:h2:mem:naturalidheaderdb;DB_CLOSE_DELAY=-1", + HibernateTestEntity.class); + + Session session = comp.getSessionFactory().openSession(); + Transaction transaction = session.beginTransaction(); + + HibernateTestEntity entity = new HibernateTestEntity(); + entity.setId(1L); + entity.setName("test"); + session.persist(entity); + + transaction.commit(); + session.close(); + + HibernateEndpoint endpoint = new HibernateEndpoint(); + endpoint.setCamelContext(context); + endpoint.setSessionFactory(comp.getSessionFactory()); + endpoint.setEntityClassName(HibernateTestEntity.class.getName()); + endpoint.setNaturalIdParameters(Map.of("name", "${header.lookupName}")); + + endpoint.start(); + + Exchange exchange = context.getEndpoint("direct:test").createExchange(); + exchange.getMessage().setHeader("lookupName", "test"); + + try (HibernateProducer producer = new HibernateProducer(endpoint)) { + producer.doStart(); + producer.process(exchange); + } + + HibernateTestEntity result = exchange.getMessage().getBody(HibernateTestEntity.class); + + assertNotNull(result); + assertEquals(1L, result.getId()); + assertEquals("test", result.getName()); + + endpoint.stop(); + comp.stop(); + } + + @Test + public void testNaturalIdHeaderParametersAreNotSimpleEvaluated() throws Exception { + SessionFactory sessionFactory = Mockito.mock(SessionFactory.class); + Session session = Mockito.mock(Session.class); + Transaction transaction = Mockito.mock(Transaction.class); + + Mockito.when(sessionFactory.openSession()).thenReturn(session); + Mockito.when(session.beginTransaction()).thenReturn(transaction); + Mockito.when(session.find( + Mockito.eq(HibernateTestEntity.class), + Mockito.eq(Map.of("name", "${header.lookupName}")), + Mockito.eq(KeyType.NATURAL))).thenReturn(null); + + stubSessionFactoryJta(sessionFactory, false); + + HibernateEndpoint endpoint = new HibernateEndpoint(); + endpoint.setCamelContext(context); + endpoint.setSessionFactory(sessionFactory); + endpoint.setEntityType(HibernateTestEntity.class); + endpoint.setNaturalIdParameters(Map.of("name", "configured")); + + endpoint.start(); + + Exchange exchange = endpoint.createExchange(); + exchange.getMessage().setHeader( + HibernateConstants.HIBERNATE_PARAMETERS, + Map.of("name", "${header.lookupName}")); + + try (HibernateProducer producer = new HibernateProducer(endpoint)) { + producer.process(exchange); Review Comment: 💡 **Missing `producer.doStart()` — test doesn't exercise the full production path** The two other natural-ID tests in this same commit (`testNaturalIdLookupProducer`, `testNaturalIdLookupFromMessageHeader`) both call `producer.doStart()` before `process()`. Without it, `naturalIdParameters` (populated in `doStart()`) is empty at call time. The endpoint has `setNaturalIdParameters(Map.of("name", "configured"))`, but that value is never loaded into the producer's internal map — so the header override on an empty map is not the same as a header override that wins over a URI-configured value. The security invariant (headers are never Simple-evaluated) is still checked, but the realistic production scenario (URI param + header coexist) is not. ```suggestion try (HibernateProducer producer = new HibernateProducer(endpoint)) { producer.doStart(); producer.process(exchange); ``` -- 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]
