-
Notifications
You must be signed in to change notification settings - Fork 242
/
JobTestUtil.java
126 lines (106 loc) · 5.01 KB
/
JobTestUtil.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
/*
* 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.integrationtest.batch.jsr352.util;
import static org.assertj.core.api.Assertions.assertThat;
import java.lang.invoke.MethodHandles;
import java.util.List;
import java.util.Properties;
import javax.batch.operations.JobOperator;
import javax.batch.runtime.BatchRuntime;
import javax.batch.runtime.BatchStatus;
import javax.batch.runtime.JobExecution;
import javax.persistence.EntityManagerFactory;
import javax.persistence.metamodel.EntityType;
import javax.persistence.metamodel.SingularAttribute;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.search.batch.jsr352.core.logging.impl.Log;
import org.hibernate.search.batch.jsr352.core.massindexing.util.impl.EntityTypeDescriptor;
import org.hibernate.search.batch.jsr352.core.massindexing.util.impl.SingularIdOrder;
import org.hibernate.search.mapper.orm.Search;
import org.hibernate.search.mapper.orm.session.SearchSession;
import org.hibernate.search.util.common.logging.impl.LoggerFactory;
/**
* @author Yoann Rodiere
*/
public final class JobTestUtil {
private static final Log log = LoggerFactory.make( Log.class, MethodHandles.lookup() );
private static final int THREAD_SLEEP_MS = 100;
private static final String JSR325_TYPE_FOR_IDE_TESTS = "jbatch";
private JobTestUtil() {
}
public static JobOperator getAndCheckRuntime() {
JobOperator operator = BatchRuntime.getJobOperator();
String expectedType = System.getProperty( "org.hibernate.search.integrationtest.jsr352.type" );
// only for tests run from the IDE only
if ( expectedType == null ) {
expectedType = JSR325_TYPE_FOR_IDE_TESTS;
}
assertThat( operator ).extracting( Object::getClass ).asString()
.contains( expectedType );
log.infof( "JSR-352 operator type is %s (%s)", expectedType, operator.getClass() );
return operator;
}
public static void startJobAndWait(String jobName, Properties jobParams, int timeoutInMs) throws InterruptedException {
JobOperator jobOperator = getAndCheckRuntime();
long execId = jobOperator.start( jobName, jobParams );
JobExecution jobExec = jobOperator.getJobExecution( execId );
jobExec = JobTestUtil.waitForTermination( jobOperator, jobExec, timeoutInMs );
assertThat( jobExec.getBatchStatus() ).isEqualTo( BatchStatus.COMPLETED );
}
public static JobExecution waitForTermination(JobOperator jobOperator, JobExecution jobExecution, int timeoutInMs)
throws InterruptedException {
long endTime = System.nanoTime() + timeoutInMs * 1_000_000L;
while ( !jobExecution.getBatchStatus().equals( BatchStatus.COMPLETED )
&& !jobExecution.getBatchStatus().equals( BatchStatus.STOPPED )
&& !jobExecution.getBatchStatus().equals( BatchStatus.FAILED )
&& System.nanoTime() < endTime ) {
long executionId = jobExecution.getExecutionId();
log.infof(
"Job execution (id=%d) has status %s. Thread sleeps %d ms...",
executionId,
jobExecution.getBatchStatus(),
THREAD_SLEEP_MS
);
Thread.sleep( THREAD_SLEEP_MS );
jobExecution = jobOperator.getJobExecution( executionId );
}
return jobExecution;
}
public static <T> int nbDocumentsInIndex(EntityManagerFactory emf, Class<T> clazz) {
try ( Session session = emf.unwrap( SessionFactory.class ).openSession() ) {
SearchSession searchSession = Search.session( session );
searchSession.workspace().refresh();
long totalHitCount = searchSession.search( clazz ).where( f -> f.matchAll() ).fetchTotalHitCount();
return Math.toIntExact( totalHitCount );
}
}
public static <T> List<T> findIndexedResults(EntityManagerFactory emf, Class<T> clazz, String key, String value) {
SessionFactory sessionFactory = emf.unwrap( SessionFactory.class );
try ( Session session = sessionFactory.openSession() ) {
return find( session, clazz, key, value );
}
}
public static <T> List<T> findIndexedResultsInTenant(EntityManagerFactory emf, Class<T> clazz, String key, String value, String tenantId) {
SessionFactory sessionFactory = emf.unwrap( SessionFactory.class );
try ( Session session = sessionFactory.withOptions().tenantIdentifier( tenantId ).openSession() ) {
return find( session, clazz, key, value );
}
}
private static <T> List<T> find(Session session, Class<T> clazz, String key, String value) {
SearchSession searchSession = Search.session( session );
searchSession.workspace().refresh();
return searchSession.search( clazz )
.where( f -> f.match().field( key ).matching( value ) )
.fetchHits( 1000 );
}
public static EntityTypeDescriptor createSimpleEntityTypeDescriptor(EntityManagerFactory emf, Class<?> clazz) {
EntityType<?> entityType = emf.getMetamodel().entity( clazz );
SingularAttribute<?, ?> idAttribute = entityType.getId( entityType.getIdType().getJavaType() );
return new EntityTypeDescriptor( clazz, new SingularIdOrder( idAttribute.getName() ) );
}
}