Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for MassIndexerService #1240

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/test/java/org/openelisglobal/AppTestConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
"org.openelisglobal.systemusermodule.service", "org.openelisglobal.rolemodule.service",
"org.openelisglobal.systemusermodule.daoimpl", "org.openelisglobal.systemusermodule.service",
"org.openelisglobal.login.service", "org.openelisglobal.view", "org.openelisglobal.search.service",
"org.openelisglobal.sample.daoimpl", }, excludeFilters = {
"org.openelisglobal.sample.daoimpl", "org.openelisglobal.hibernate.search.massindexer" }, excludeFilters = {
@ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.openelisglobal.patient.controller.*"),
@ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.openelisglobal.dictionary.controller.*.java"),
@ComponentScan.Filter(type = FilterType.REGEX, pattern = "org.openelisglobal.config.*"),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
package org.openelisglobal.search;

import java.sql.Timestamp;
import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.transaction.Transactional;
import org.hibernate.search.mapper.orm.Search;
import org.hibernate.search.mapper.orm.session.SearchSession;
import org.junit.After;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.openelisglobal.BaseWebContextSensitiveTest;
import org.openelisglobal.hibernate.search.massindexer.MassIndexerService;
import org.openelisglobal.patient.service.PatientService;
import org.openelisglobal.patient.valueholder.Patient;
import org.openelisglobal.person.service.PersonService;
import org.openelisglobal.person.valueholder.Person;
import org.springframework.beans.factory.annotation.Autowired;

public class MassIndexerServiceTest extends BaseWebContextSensitiveTest {

@PersistenceContext
EntityManager entityManager;

@Autowired
MassIndexerService massIndexerService;

@Autowired
PatientService patientService;

@Autowired
PersonService personService;

@Before
public void init() throws Exception {
patientService.deleteAll(patientService.getAll());
personService.deleteAll(personService.getAll());
}

@After
public void tearDown() {
patientService.deleteAll(patientService.getAll());
personService.deleteAll(personService.getAll());
}

@Test
@Transactional
public void reindex_shouldReindexEntities() throws Exception {
String firstName = "John";
String lastname = "Doe";
String dob = "12/12/1992";
String gender = "M";
Patient pat = createPatient(firstName, lastname, dob, gender);
patientService.insert(pat);

SearchSession searchSession = Search.session(entityManager);
// remove all entities from the Lucene index but not from the database
searchSession.workspace(Object.class).purge();
long totalHitCount = searchSession.search(Patient.class).where(f -> f.matchAll()).fetchTotalHitCount();
Assert.assertEquals(0, totalHitCount);

massIndexerService.reindex();
totalHitCount = searchSession.search(Patient.class).where(f -> f.matchAll()).fetchTotalHitCount();
Assert.assertEquals(1, totalHitCount);

}

private Patient createPatient(String firstName, String LastName, String birthDate, String gender)
throws ParseException {
Person person = new Person();
person.setFirstName(firstName);
person.setLastName(LastName);
personService.save(person);

DateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
Date date = dateFormat.parse(birthDate);
long time = date.getTime();
Timestamp dob = new Timestamp(time);

Patient pat = new Patient();
pat.setPerson(person);
pat.setBirthDate(dob);
pat.setGender(gender);

return pat;
}
}
Loading