Search in sources :

Example 1 with HibernateOrmMappingHandle

use of org.hibernate.search.util.impl.integrationtest.mapper.orm.HibernateOrmMappingHandle in project hibernate-search by hibernate.

the class HibernateOrmIntegrationBooterIT method twoPhaseBoot.

@Test
@SuppressWarnings({ "unchecked", "rawtypes" })
public void twoPhaseBoot() throws Exception {
    CompletableFuture<BackendMappingHandle> mappingHandlePromise = new CompletableFuture<>();
    HibernateOrmIntegrationBooter booter = createBooter(mappingHandlePromise, IndexedEntity.class);
    Map<String, Object> booterGeneratedProperties = new LinkedHashMap<>();
    // Pre-booting should lead to a schema definition in the backend.
    backendMock.expectSchema(INDEX_NAME, b -> b.field("text", String.class, b2 -> b2.analyzerName(AnalyzerNames.DEFAULT)));
    // Pre-booting should retrieve value-read handles
    // Simulate a custom handle from a framework, e.g. Quarkus
    when(valueHandleFactoryMock.createForField(IndexedEntity.ID_FIELD)).thenReturn((ValueReadHandle) idValueReadHandleMock);
    when(valueHandleFactoryMock.createForField(IndexedEntity.TEXT_FIELD)).thenReturn((ValueReadHandle) textValueReadHandleMock);
    booter.preBoot(booterGeneratedProperties::put);
    backendMock.verifyExpectationsMet();
    verify(valueHandleFactoryMock).createForField(IndexedEntity.ID_FIELD);
    verify(valueHandleFactoryMock).createForField(IndexedEntity.TEXT_FIELD);
    SimpleSessionFactoryBuilder builder = new SimpleSessionFactoryBuilder().addAnnotatedClass(IndexedEntity.class).setProperty(HibernateOrmMapperSettings.MAPPING_CONFIGURER, new HibernateOrmSearchMappingConfigurer() {

        @Override
        public void configure(HibernateOrmMappingConfigurationContext context) {
            Fail.fail("Hibernate Search did not re-use the mapping generated when pre-booting");
        }
    });
    for (Map.Entry<String, Object> booterGeneratedProperty : booterGeneratedProperties.entrySet()) {
        builder.setProperty(booterGeneratedProperty.getKey(), booterGeneratedProperty.getValue());
    }
    // Actually booting the session factory should lead to a schema creation in the backend.
    backendMock.expectSchemaManagementWorks(INDEX_NAME).work(StubSchemaManagementWork.Type.CREATE_OR_VALIDATE);
    try (SessionFactory sessionFactory = builder.build()) {
        mappingHandlePromise.complete(new HibernateOrmMappingHandle(sessionFactory));
        /*
			 * Building the session should NOT lead to a second schema creation in the backend:
			 * that would mean the pre-boot was ignored...
			 */
        backendMock.verifyExpectationsMet();
        with(sessionFactory).runInTransaction(session -> {
            IndexedEntity entity = new IndexedEntity();
            entity.id = 1;
            entity.text = "someText";
            session.persist(entity);
            when(textValueReadHandleMock.get(entity)).thenReturn(entity.text);
            backendMock.expectWorks(INDEX_NAME).add("1", b -> b.field("text", "someText"));
        });
        // If the entity was indexed, it means Hibernate Search booted correctly
        backendMock.verifyExpectationsMet();
        // Also check that the value handle has been called
        verify(textValueReadHandleMock).get(any());
    }
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) Strictness(org.mockito.quality.Strictness) SimpleSessionFactoryBuilder(org.hibernate.search.util.impl.integrationtest.mapper.orm.SimpleSessionFactoryBuilder) Mock(org.mockito.Mock) HibernateOrmMappingConfigurationContext(org.hibernate.search.mapper.orm.mapping.HibernateOrmMappingConfigurationContext) CompletableFuture(java.util.concurrent.CompletableFuture) StandardServiceRegistryBuilder(org.hibernate.boot.registry.StandardServiceRegistryBuilder) Metadata(org.hibernate.boot.Metadata) BootstrapContext(org.hibernate.boot.spi.BootstrapContext) ArrayList(java.util.ArrayList) LinkedHashMap(java.util.LinkedHashMap) FullTextField(org.hibernate.search.mapper.pojo.mapping.definition.annotation.FullTextField) ValueHandleFactory(org.hibernate.search.util.common.reflect.spi.ValueHandleFactory) HibernateOrmMapperSettings(org.hibernate.search.mapper.orm.cfg.HibernateOrmMapperSettings) Map(java.util.Map) MetadataImplementor(org.hibernate.boot.spi.MetadataImplementor) After(org.junit.After) BackendSettings(org.hibernate.search.engine.cfg.BackendSettings) MockitoJUnit(org.mockito.junit.MockitoJUnit) OrmUtils.with(org.hibernate.search.util.impl.integrationtest.mapper.orm.OrmUtils.with) StandardServiceRegistry(org.hibernate.boot.registry.StandardServiceRegistry) Id(javax.persistence.Id) ValueReadHandle(org.hibernate.search.util.common.reflect.spi.ValueReadHandle) Entity(javax.persistence.Entity) EngineSettings(org.hibernate.search.engine.cfg.EngineSettings) StubSchemaManagementWork(org.hibernate.search.util.impl.integrationtest.common.stub.backend.index.StubSchemaManagementWork) SessionFactory(org.hibernate.SessionFactory) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) Field(java.lang.reflect.Field) Indexed(org.hibernate.search.mapper.pojo.mapping.definition.annotation.Indexed) HibernateOrmMappingHandle(org.hibernate.search.util.impl.integrationtest.mapper.orm.HibernateOrmMappingHandle) Mockito.verify(org.mockito.Mockito.verify) MetadataSources(org.hibernate.boot.MetadataSources) Fail(org.assertj.core.api.Fail) HibernateOrmIntegrationBooter(org.hibernate.search.mapper.orm.bootstrap.spi.HibernateOrmIntegrationBooter) List(java.util.List) Rule(org.junit.Rule) AnalyzerNames(org.hibernate.search.engine.backend.analysis.AnalyzerNames) HibernateOrmSearchMappingConfigurer(org.hibernate.search.mapper.orm.mapping.HibernateOrmSearchMappingConfigurer) BackendMappingHandle(org.hibernate.search.util.impl.integrationtest.common.stub.backend.BackendMappingHandle) MockitoRule(org.mockito.junit.MockitoRule) Closer(org.hibernate.search.util.common.impl.Closer) BackendMock(org.hibernate.search.util.impl.integrationtest.common.rule.BackendMock) SessionFactory(org.hibernate.SessionFactory) HibernateOrmMappingConfigurationContext(org.hibernate.search.mapper.orm.mapping.HibernateOrmMappingConfigurationContext) HibernateOrmMappingHandle(org.hibernate.search.util.impl.integrationtest.mapper.orm.HibernateOrmMappingHandle) HibernateOrmSearchMappingConfigurer(org.hibernate.search.mapper.orm.mapping.HibernateOrmSearchMappingConfigurer) HibernateOrmIntegrationBooter(org.hibernate.search.mapper.orm.bootstrap.spi.HibernateOrmIntegrationBooter) BackendMappingHandle(org.hibernate.search.util.impl.integrationtest.common.stub.backend.BackendMappingHandle) LinkedHashMap(java.util.LinkedHashMap) CompletableFuture(java.util.concurrent.CompletableFuture) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) SimpleSessionFactoryBuilder(org.hibernate.search.util.impl.integrationtest.mapper.orm.SimpleSessionFactoryBuilder) Test(org.junit.Test)

Aggregations

Field (java.lang.reflect.Field)1 ArrayList (java.util.ArrayList)1 LinkedHashMap (java.util.LinkedHashMap)1 List (java.util.List)1 Map (java.util.Map)1 CompletableFuture (java.util.concurrent.CompletableFuture)1 Entity (javax.persistence.Entity)1 Id (javax.persistence.Id)1 Fail (org.assertj.core.api.Fail)1 SessionFactory (org.hibernate.SessionFactory)1 Metadata (org.hibernate.boot.Metadata)1 MetadataSources (org.hibernate.boot.MetadataSources)1 StandardServiceRegistry (org.hibernate.boot.registry.StandardServiceRegistry)1 StandardServiceRegistryBuilder (org.hibernate.boot.registry.StandardServiceRegistryBuilder)1 BootstrapContext (org.hibernate.boot.spi.BootstrapContext)1 MetadataImplementor (org.hibernate.boot.spi.MetadataImplementor)1 AnalyzerNames (org.hibernate.search.engine.backend.analysis.AnalyzerNames)1 BackendSettings (org.hibernate.search.engine.cfg.BackendSettings)1 EngineSettings (org.hibernate.search.engine.cfg.EngineSettings)1 HibernateOrmIntegrationBooter (org.hibernate.search.mapper.orm.bootstrap.spi.HibernateOrmIntegrationBooter)1