/*
Copyright 2006 - 2010 Under Dusken
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package no.dusken.aranea.control;
import no.dusken.aranea.AbstractTest;
import no.dusken.aranea.model.Person;
import no.dusken.aranea.service.PersonService;
import org.compass.core.support.search.CompassSearchCommand;
import org.compass.core.support.search.CompassSearchResults;
import org.compass.gps.CompassGps;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mock.web.MockHttpServletRequest;
import org.springframework.mock.web.MockHttpServletResponse;
import org.springframework.test.context.transaction.AfterTransaction;
import org.springframework.test.context.transaction.BeforeTransaction;
import java.util.Map;
import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;
/**
* Tests the searchController by performing some searches :-)
*/
public class TestPersonSearchController extends AbstractTest {
private BasicSearchController<Person> searchController;
private PersonService personService;
/**
* Has to be before the transaction. I guess this is because also
* compass uses transactions
*
* @throws Exception if...
*/
@BeforeTransaction
public void onSetUpBeforeTransaction() throws Exception {
searchController.setType("person");
// add two persons
Person p1 = new Person();
p1.setFirstname("Adrian");
p1.setSurname("Leverkühn");
p1.setUsername("adrian");
p1 = personService.saveOrUpdate(p1);
assertTrue("Person ID not set", p1.getID() > 0);
Person p2 = new Person();
p2.setFirstname("Adrian");
p2.setSurname("Leverkahm");
p2.setUsername("asennh");
p2 = personService.saveOrUpdate(p2);
assertTrue("Person ID not set", p2.getID() > 0);
CompassGps gps = (CompassGps) getApplicationContext().getBean("compassGps");
// create the index:
gps.index();
}
@Test
public void testFirstNameSearch() throws Exception {
CompassSearchResults results = getResults("Adrian");
assertTrue("Did not get hit for search", results.getHits().length == 2);
}
@Test
public void testLastNameSearch() throws Exception {
CompassSearchResults results = getResults("Leverkühn");
assertTrue("Did not get hit for search", results.getHits().length == 1);
Person p = (Person) results.getHits()[0].getData();
// this is a shadow object, only compare indexed data
assertEquals("Name did not match", "Adrian", p.getFirstname());
assertEquals("Name did not match", "Leverkühn", p.getSurname());
}
/*
public void testFuzzySearch() throws Exception {
CompassSearchResults results = getResults("Leverkuhm~");
assertTrue("Did not get hits for fuzzy search", results.getHits().length == 2);
}
*/
private CompassSearchResults getResults(String query) throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
CompassSearchCommand command = new CompassSearchCommand(query);
@SuppressWarnings("unchecked")
Map<String, Object> map = searchController
.handle(request, new MockHttpServletResponse(), command, null).getModel();
return (CompassSearchResults) map.get("searchResults");
}
@AfterTransaction
public void cleanDb(){
for(Person p : personService.getPersons()){
personService.remove(p);
}
}
@Autowired
public void setPersonService(PersonService personService) {
this.personService = personService;
}
@Autowired
public void setSearchController(BasicSearchController<Person> searchController) {
this.searchController = searchController;
}
}