Skip to content

Commit

Permalink
HSEARCH-4867 Refactor MassIndexer/JSR-352 tests in documentation
Browse files Browse the repository at this point in the history
To avoid weird inter-dependencies that get in the way of later changes.
  • Loading branch information
yrodiere committed Aug 31, 2023
1 parent e22d3dd commit 8017023
Show file tree
Hide file tree
Showing 3 changed files with 116 additions and 119 deletions.
@@ -0,0 +1,111 @@
/*
* Hibernate Search, full-text search for your domain model
*
* License: GNU Lesser General Public License (LGPL), version 2.1 or later
* See the lgpl.txt file in the root directory or <http://www.gnu.org/licenses/lgpl-2.1.html>.
*/
package org.hibernate.search.documentation.mapper.orm.indexing;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hibernate.search.util.impl.integrationtest.mapper.orm.OrmUtils.with;

import java.time.LocalDate;

import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;

import org.hibernate.search.documentation.testsupport.BackendConfigurations;
import org.hibernate.search.documentation.testsupport.DocumentationSetupHelper;
import org.hibernate.search.mapper.orm.Search;
import org.hibernate.search.mapper.orm.cfg.HibernateOrmMapperSettings;
import org.hibernate.search.mapper.orm.session.SearchSession;

import org.junit.Before;
import org.junit.Rule;

abstract class AbstractHibernateOrmMassIndexingIT {

protected static final int NUMBER_OF_BOOKS = 1000;
private static final int INIT_DATA_TRANSACTION_SIZE = 500;

@Rule
public DocumentationSetupHelper setupHelper = DocumentationSetupHelper.withSingleBackend( BackendConfigurations.simple() );

protected EntityManagerFactory entityManagerFactory;

@Before
public void setup() {
this.entityManagerFactory = setupHelper.start()
.withProperty( HibernateOrmMapperSettings.INDEXING_LISTENERS_ENABLED, false )
.setup( Book.class, Author.class );
initData( entityManagerFactory );
with( entityManagerFactory ).runInTransaction( entityManager -> {
assertBookCount( entityManager, 0 );
assertAuthorCount( entityManager, 0 );
} );
}

static void assertBookCount(EntityManager entityManager, int expectedCount) {
SearchSession searchSession = Search.session( entityManager );
assertThat(
searchSession.search( Book.class )
.where( f -> f.matchAll() )
.fetchTotalHitCount()
)
.isEqualTo( expectedCount );
}

protected void assertAuthorCount(EntityManager entityManager, int expectedCount) {
SearchSession searchSession = Search.session( entityManager );
assertThat(
searchSession.search( Author.class )
.where( f -> f.matchAll() )
.fetchTotalHitCount()
)
.isEqualTo( expectedCount );
}

protected void initData(EntityManagerFactory entityManagerFactory) {
with( entityManagerFactory ).runNoTransaction( entityManager -> {
try {
int i = 0;
while ( i < NUMBER_OF_BOOKS ) {
entityManager.getTransaction().begin();
int end = Math.min( i + INIT_DATA_TRANSACTION_SIZE, NUMBER_OF_BOOKS );
for ( ; i < end; ++i ) {
Author author = newAuthor( i );

Book book = newBook( i );
book.setAuthor( author );
author.getBooks().add( book );

entityManager.persist( author );
entityManager.persist( book );
}
entityManager.getTransaction().commit();
}
}
catch (RuntimeException e) {
entityManager.getTransaction().rollback();
throw e;
}
} );
}

private Book newBook(int id) {
Book book = new Book();
book.setId( id );
book.setTitle( "This is the title of book #" + id );
book.setPublicationYear( 1450 + id );
return book;
}

protected Author newAuthor(int id) {
Author author = new Author();
author.setId( id );
author.setFirstName( "John" + id );
author.setLastName( "Smith" + id );
author.setBirthDate( LocalDate.ofYearDay( 1450 + id, 33 ) );
return author;
}
}
Expand Up @@ -7,10 +7,6 @@
package org.hibernate.search.documentation.mapper.orm.indexing;

import static org.assertj.core.api.Assertions.assertThat;
import static org.hibernate.search.documentation.mapper.orm.indexing.HibernateOrmMassIndexerIT.NUMBER_OF_BOOKS;
import static org.hibernate.search.documentation.mapper.orm.indexing.HibernateOrmMassIndexerIT.assertAuthorCount;
import static org.hibernate.search.documentation.mapper.orm.indexing.HibernateOrmMassIndexerIT.assertBookCount;
import static org.hibernate.search.documentation.mapper.orm.indexing.HibernateOrmMassIndexerIT.initData;
import static org.hibernate.search.util.impl.integrationtest.mapper.orm.OrmUtils.with;

import java.util.Properties;
Expand All @@ -19,37 +15,17 @@
import jakarta.batch.runtime.BatchRuntime;
import jakarta.batch.runtime.BatchStatus;
import jakarta.batch.runtime.JobExecution;
import jakarta.persistence.EntityManagerFactory;

import org.hibernate.search.batch.jsr352.core.massindexing.MassIndexingJob;
import org.hibernate.search.documentation.testsupport.BackendConfigurations;
import org.hibernate.search.documentation.testsupport.DocumentationSetupHelper;
import org.hibernate.search.mapper.orm.Search;
import org.hibernate.search.mapper.orm.cfg.HibernateOrmMapperSettings;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

public class HibernateOrmBatchJsr352IT {
public class HibernateOrmBatchJsr352IT extends AbstractHibernateOrmMassIndexingIT {

private static final int JOB_TIMEOUT_MS = 30_000;
private static final int THREAD_SLEEP = 1000;

@Rule
public DocumentationSetupHelper setupHelper = DocumentationSetupHelper.withSingleBackend(
BackendConfigurations.simple() );

private EntityManagerFactory entityManagerFactory;

@Before
public void setup() {
this.entityManagerFactory = setupHelper.start()
.withProperty( HibernateOrmMapperSettings.INDEXING_LISTENERS_ENABLED, false )
.setup( Book.class, Author.class );
initData( entityManagerFactory, HibernateOrmBatchJsr352IT::newAuthor );
}

@Test
public void simple() throws Exception {
// tag::simple[]
Expand Down Expand Up @@ -95,7 +71,8 @@ public void hql() throws Exception {
} );
}

private static Author newAuthor(int id) {
@Override
protected Author newAuthor(int id) {
Author author = new Author();
author.setId( id );
author.setFirstName( "John" + id );
Expand All @@ -104,6 +81,7 @@ private static Author newAuthor(int id) {
return author;
}


private static JobExecution waitForTermination(JobOperator jobOperator, JobExecution jobExecution, int timeoutInMs)
throws InterruptedException {
long endTime = System.currentTimeMillis() + timeoutInMs;
Expand Down
Expand Up @@ -6,56 +6,28 @@
*/
package org.hibernate.search.documentation.mapper.orm.indexing;

import static org.assertj.core.api.Assertions.assertThat;
import static org.awaitility.Awaitility.await;
import static org.hibernate.search.util.impl.integrationtest.mapper.orm.OrmUtils.with;
import static org.hibernate.search.util.impl.test.FutureAssert.assertThatFuture;

import java.lang.invoke.MethodHandles;
import java.time.LocalDate;
import java.util.concurrent.Future;
import java.util.function.Function;

import jakarta.persistence.EntityManager;
import jakarta.persistence.EntityManagerFactory;

import org.hibernate.search.documentation.testsupport.BackendConfigurations;
import org.hibernate.search.documentation.testsupport.DocumentationSetupHelper;
import org.hibernate.search.mapper.orm.Search;
import org.hibernate.search.mapper.orm.cfg.HibernateOrmMapperSettings;
import org.hibernate.search.mapper.orm.massindexing.MassIndexer;
import org.hibernate.search.mapper.orm.session.SearchSession;
import org.hibernate.search.util.common.logging.impl.Log;
import org.hibernate.search.util.common.logging.impl.LoggerFactory;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;

public class HibernateOrmMassIndexerIT {
public class HibernateOrmMassIndexerIT extends AbstractHibernateOrmMassIndexingIT {

private static final Log log = LoggerFactory.make( Log.class, MethodHandles.lookup() );

static final int NUMBER_OF_BOOKS = 1000;
static final int INIT_DATA_TRANSACTION_SIZE = 500;

@Rule
public DocumentationSetupHelper setupHelper = DocumentationSetupHelper.withSingleBackend( BackendConfigurations.simple() );

private EntityManagerFactory entityManagerFactory;

@Before
public void setup() {
this.entityManagerFactory = setupHelper.start()
.withProperty( HibernateOrmMapperSettings.INDEXING_LISTENERS_ENABLED, false )
.setup( Book.class, Author.class );
initData( entityManagerFactory, HibernateOrmMassIndexerIT::newAuthor );
with( entityManagerFactory ).runNoTransaction( entityManager -> {
assertBookCount( entityManager, 0 );
assertAuthorCount( entityManager, 0 );
} );
}

@Test
public void simple() {
with( entityManagerFactory ).runNoTransaction( entityManager -> {
Expand Down Expand Up @@ -179,68 +151,4 @@ public void parameters() {
assertAuthorCount( entityManager, NUMBER_OF_BOOKS );
} );
}

static void assertBookCount(EntityManager entityManager, int expectedCount) {
SearchSession searchSession = Search.session( entityManager );
assertThat(
searchSession.search( Book.class )
.where( f -> f.matchAll() )
.fetchTotalHitCount()
)
.isEqualTo( expectedCount );
}

static void assertAuthorCount(EntityManager entityManager, int expectedCount) {
SearchSession searchSession = Search.session( entityManager );
assertThat(
searchSession.search( Author.class )
.where( f -> f.matchAll() )
.fetchTotalHitCount()
)
.isEqualTo( expectedCount );
}

static void initData(EntityManagerFactory entityManagerFactory, Function<Integer, Author> authorInit) {
with( entityManagerFactory ).runNoTransaction( entityManager -> {
try {
int i = 0;
while ( i < NUMBER_OF_BOOKS ) {
entityManager.getTransaction().begin();
int end = Math.min( i + INIT_DATA_TRANSACTION_SIZE, NUMBER_OF_BOOKS );
for ( ; i < end; ++i ) {
Author author = authorInit.apply( i );

Book book = newBook( i );
book.setAuthor( author );
author.getBooks().add( book );

entityManager.persist( author );
entityManager.persist( book );
}
entityManager.getTransaction().commit();
}
}
catch (RuntimeException e) {
entityManager.getTransaction().rollback();
throw e;
}
} );
}

private static Book newBook(int id) {
Book book = new Book();
book.setId( id );
book.setTitle( "This is the title of book #" + id );
book.setPublicationYear( 1450 + id );
return book;
}

private static Author newAuthor(int id) {
Author author = new Author();
author.setId( id );
author.setFirstName( "John" + id );
author.setLastName( "Smith" + id );
author.setBirthDate( LocalDate.ofYearDay( 1450 + id, 33 ) );
return author;
}
}

0 comments on commit 8017023

Please sign in to comment.