use of org.hibernate.search.util.impl.integrationtest.mapper.orm.SimpleSessionFactoryBuilder 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());
}
}
use of org.hibernate.search.util.impl.integrationtest.mapper.orm.SimpleSessionFactoryBuilder in project hibernate-search by hibernate.
the class PerSessionFactoryIndexingCountHelper method bind.
public OrmSetupHelper.SetupContext bind(OrmSetupHelper.SetupContext context) {
StaticCountersKeys counterKeysForSessionFactory = new StaticCountersKeys();
return context.withConfiguration(// I've been unable to reproduce the problem locally.
new Consumer<SimpleSessionFactoryBuilder>() {
@Override
public void accept(SimpleSessionFactoryBuilder builder) {
builder.setProperty(HibernateOrmMapperSettings.MAPPING_CONFIGURER, new HibernateOrmSearchMappingConfigurer() {
@Override
public void configure(HibernateOrmMappingConfigurationContext context) {
context.programmaticMapping().type(Object.class).binder(new Binder(counterKeysForSessionFactory));
}
});
}
}, sessionFactory -> {
counterKeys.add(counterKeysForSessionFactory);
sessionFactories.add(sessionFactory);
});
}
Aggregations