Skip to content

Commit

Permalink
#63 add value assertions in Java SE integration test
Browse files Browse the repository at this point in the history
  • Loading branch information
mincong-h committed Jul 3, 2016
1 parent e6a5699 commit 32f6b3b
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 6 deletions.
Expand Up @@ -2,12 +2,18 @@

import static org.junit.Assert.assertEquals;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.batch.operations.JobOperator;
import javax.batch.runtime.BatchRuntime;
import javax.batch.runtime.BatchStatus;
import javax.batch.runtime.JobExecution;
import javax.batch.runtime.Metric;
import javax.batch.runtime.StepExecution;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
Expand All @@ -31,10 +37,27 @@ public class MassIndexerIT {

private JobOperator jobOperator;

// mass indexer configuration values
private final boolean OPTIMIZE_AFTER_PURGE = true;
private final boolean OPTIMIZE_AT_END = true;
private final boolean PURGE_AT_START = true;
private final int ARRAY_CAPACITY = 500;
private final int FETCH_SIZE = 100000;
private final int MAX_RESULTS = 200 * 1000;
private final int PARTITION_CAPACITY = 250;
private final int PARTITIONS = 1;
private final int THREADS = 1;

// example dataset
private final long DB_COMP_ROWS = 3;
private final long DB_COMP_ROWS_LOADED = 3;
private final Company COMPANY_1 = new Company("Google");
private final Company COMPANY_2 = new Company("Red Hat");
private final Company COMPANY_3 = new Company("Microsoft");

private static final int JOB_MAX_TRIES = 240; // 240 second
private static final int JOB_THREAD_SLEEP = 1000;

private static final Logger logger = Logger.getLogger(MassIndexerIT.class);

@Before
Expand All @@ -52,13 +75,19 @@ public void setup() {
}

@Test
public void testMassIndexer() {
public void testMassIndexer() throws InterruptedException {

logger.infof("finding company called %s ...", "google");
List<Company> companies = findCompanyByName("google");
assertEquals(0, companies.size());

indexCompany();
long executionId = indexCompany();
JobExecution jobExecution = jobOperator.getJobExecution(executionId);
jobExecution = keepTestAlive(jobExecution);
List<StepExecution> stepExecutions = jobOperator.getStepExecutions(executionId);
for (StepExecution stepExecution: stepExecutions) {
logger.infof("step %s executed.", stepExecution.getStepName());
}

companies = findCompanyByName("google");
// issue #78 - Cannot find indexed results after mass index
Expand All @@ -77,7 +106,7 @@ private List<Company> findCompanyByName(String name) {
return result;
}

private void indexCompany() {
private long indexCompany() throws InterruptedException {
Set<Class<?>> rootEntities = new HashSet<>();
rootEntities.add(Company.class);
// org.hibernate.search.jsr352.MassIndexer
Expand All @@ -86,7 +115,9 @@ private void indexCompany() {
.entityManager(em)
.jobOperator(jobOperator);
long executionId = massIndexer.start();

logger.infof("job execution id = %d", executionId);
return executionId;
// try {
// Search.getFullTextEntityManager( em )
// .createIndexer()
Expand All @@ -100,6 +131,98 @@ private void indexCompany() {
// throw new RuntimeException( e );
// }
}

public JobExecution keepTestAlive(JobExecution jobExecution) throws InterruptedException {
int tries = 0;
while (!jobExecution.getBatchStatus().equals(BatchStatus.COMPLETED)) {
if (tries < JOB_MAX_TRIES) {
tries++;
Thread.sleep(JOB_THREAD_SLEEP);
jobExecution = jobOperator.getJobExecution(jobExecution.getExecutionId());
} else {
break;
}
}
return jobExecution;
}

private void testBatchStatus(StepExecution stepExecution) {
BatchStatus batchStatus = stepExecution.getBatchStatus();
switch (stepExecution.getStepName()) {

case "loadId":
long expectedEntityCount = DB_COMP_ROWS;
// assertEquals(expectedEntityCount, indexingContext.getEntityCount());
assertEquals(BatchStatus.COMPLETED, batchStatus);
break;

case "purgeDecision":
assertEquals(BatchStatus.COMPLETED, batchStatus);
break;

case "purgeIndex":
if (PURGE_AT_START) {
assertEquals(BatchStatus.COMPLETED, batchStatus);
}
break;

case "afterPurgeDecision":
assertEquals(BatchStatus.COMPLETED, batchStatus);
break;

case "optimizeAfterPurge":
if (OPTIMIZE_AFTER_PURGE) {
assertEquals(BatchStatus.COMPLETED, batchStatus);
}
break;

case "produceLuceneDoc":
Metric[] metrics = stepExecution.getMetrics();
testChunk(getMetricsMap(metrics));
assertEquals(BatchStatus.COMPLETED, batchStatus);
break;

case "afterIndexDecision":
assertEquals(BatchStatus.COMPLETED, batchStatus);
break;

case "optimizeAfterIndex":
assertEquals(BatchStatus.COMPLETED, batchStatus);
break;

default:
break;
}
}

private void testChunk(Map<Metric.MetricType, Long> metricsMap) {
long companyCount = (long) Math.ceil((double) DB_COMP_ROWS_LOADED / ARRAY_CAPACITY);
// The read count.
long expectedReadCount = companyCount;
long actualReadCount = metricsMap.get(Metric.MetricType.READ_COUNT);
assertEquals(expectedReadCount, actualReadCount);
// The write count
long expectedWriteCount = companyCount;
long actualWriteCount = metricsMap.get(Metric.MetricType.WRITE_COUNT);
assertEquals(expectedWriteCount, actualWriteCount);
}

/**
* Convert the Metric array contained in StepExecution to a key-value map
* for easy access to Metric parameters.
*
* @param metrics
* a Metric array contained in StepExecution.
*
* @return a map view of the metrics array.
*/
public Map<Metric.MetricType, Long> getMetricsMap(Metric[] metrics) {
Map<Metric.MetricType, Long> metricsMap = new HashMap<>();
for (Metric metric : metrics) {
metricsMap.put(metric.getType(), metric.getValue());
}
return metricsMap;
}

@After
public void shutdownJPA() {
Expand Down
Expand Up @@ -19,7 +19,7 @@ public class Company implements Serializable {
@Id
@GeneratedValue
@DocumentId
private long id;
private int id;

@Field
private String name;
Expand All @@ -32,11 +32,11 @@ public Company(String name) {
this.name = name;
}

public long getId() {
public int getId() {
return id;
}

public void setId(long id) {
public void setId(int id) {
this.id = id;
}

Expand Down

0 comments on commit 32f6b3b

Please sign in to comment.