/* ===================================================================
* RecipeSearchIndexBizTest.java
*
* Created Sep 24, 2004 1:36:00 PM
*
* Copyright (c) 2004 Matt Magoffin (spamsqr@msqr.us)
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License as
* published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
* ===================================================================
* $Id: RecipeSearchIndexBizTest.java 60 2009-05-11 09:18:28Z msqr $
* ===================================================================
*/
package magoffin.matt.ieat.biz.test;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import magoffin.matt.ieat.biz.RecipeBiz;
import magoffin.matt.ieat.biz.RecipeSearchBiz;
import magoffin.matt.ieat.biz.RecipeSearchIndexBiz;
import magoffin.matt.ieat.domain.Ingredient;
import magoffin.matt.ieat.domain.UiIngredientMatch;
import magoffin.matt.ieat.domain.UiSearchResults;
import magoffin.matt.ieat.test.AbstractSpringEnabledTest;
import magoffin.matt.ieat.util.IngredientCriteriaImpl;
import org.apache.log4j.Logger;
/**
* Test case for RecipeSearchIndexBiz.
*
*
These tests can only work when the test data is loaded in the database!
*
* @author Matt Magoffin (spamsqr@msqr.us)
* @version $Revision: 60 $ $Date: 2009-05-11 21:18:28 +1200 (Mon, 11 May 2009) $
*/
public class RecipeSearchIndexBizTest extends AbstractSpringEnabledTest {
private static final Logger LOG = Logger.getLogger(RecipeSearchIndexBizTest.class);
/**
* Test indexing existing ingredient.
*/
public void testIndexExistingIngredient() {
// get all ingredients, and index one of them
RecipeBiz recipeBiz = getRecipeBiz();
List ingredients = recipeBiz.getAvailableIngredients();
assertTrue("At least 1 ingredient must be present", ingredients.size() > 0);
Ingredient ingredient = ingredients.get(0);
RecipeSearchIndexBiz indexBiz = getRecipeSearchIndexBiz();
indexBiz.indexIngredient(ingredient.getIngredientId());
snooze(2);
}
/**
* Test reindex ingredients.
*/
@SuppressWarnings("unchecked")
public void testReindexIngredients() {
RecipeSearchIndexBiz indexBiz = getRecipeSearchIndexBiz();
indexBiz.recreateIngredientIndex();
snooze(1);
// verify can now search
RecipeSearchBiz searchBiz = getRecipeSearchBiz();
IngredientCriteriaImpl criteria = new IngredientCriteriaImpl("milk");
UiSearchResults sr = searchBiz.findIngredientsSimilarlyNamed(criteria);
assertNotNull("Search results should never be null", sr);
assertTrue("Ingredient list should not be empty", sr.getIngredient().size() > 0);
if ( LOG.isDebugEnabled() ) {
int i = 1;
for ( Iterator itr = sr.getIngredient().iterator(); itr.hasNext(); i++) {
UiIngredientMatch match = (UiIngredientMatch)itr.next();
LOG.debug("Match " +i +": " +match.getName());
}
}
}
/**
* Test delete ingredient from index.
*/
@SuppressWarnings("unchecked")
public void testDeleteIngredientFromIndex() {
// create a new ingredient with the time as the name, so it is unique
Ingredient ing = getDomainObjectFactory().getIngredientInstance();
String name = new Date().toString();
ing.setName(name);
// store the new ingredient
RecipeBiz recipeBiz = getRecipeBiz();
ing = recipeBiz.storeIngredient(ing,null);
snooze(2);
// verify search for ingredient returns match
IngredientCriteriaImpl criteria = new IngredientCriteriaImpl(name);
criteria.setApproximateSearch(false);
RecipeSearchBiz searchBiz = getRecipeSearchBiz();
UiSearchResults sr = searchBiz.findIngredientsSimilarlyNamed(criteria);
assertNotNull("Search results should never be null", sr);
assertTrue("Ingredient list should not be empty", sr.getIngredient().size() > 0);
// delete from index
RecipeSearchIndexBiz indexBiz = getRecipeSearchIndexBiz();
indexBiz.removeIngredientFromIndex(ing.getIngredientId());
snooze(2);
// delete the new ingredient (this might automatically remove from index, too)
recipeBiz.removeIngredient(ing.getIngredientId());
snooze(1);
// perform same search for ingredient, verify not found
sr = searchBiz.findIngredientsSimilarlyNamed(criteria);
assertNotNull("Search results should never be null", sr);
if ( LOG.isDebugEnabled() ) {
for ( Iterator itr = sr.getIngredient().iterator(); itr.hasNext(); ) {
UiIngredientMatch match = (UiIngredientMatch)itr.next();
LOG.debug("Got ingredient match: " +match.getIngredientId() +" (" +match.getName() +")");
}
}
assertTrue("Ingredient list should be empty", sr.getIngredient().size() == 0);
}
/**
* Test reindex recipes.
*/
public void testReindexRecipes() {
RecipeSearchIndexBiz indexBiz = getRecipeSearchIndexBiz();
indexBiz.recreateRecipeIndex();
/* TODO verify can now search
RecipeSearchBiz searchBiz = getRecipeSearchBiz();
RecipeCriteriaImpl criteria = new IngredientCriteriaImpl("chip");
UiSearchResults sr = searchBiz.findIngredientsSimilarlyNamed(criteria);
assertTrue("Search results should never be null", sr != null);
assertTrue("Ingredient list should not be empty", sr.getIngredient().size() > 0);
if ( LOG.isDebugEnabled() ) {
int i = 1;
for ( Iterator itr = sr.getIngredient().iterator(); itr.hasNext(); i++) {
UiIngredientMatch match = (UiIngredientMatch)itr.next();
LOG.debug("Match " +i +": " +match.getName());
}
}*/
}
/**
* Test reindex users.
*/
public void testReindexUsers() {
RecipeSearchIndexBiz indexBiz = getRecipeSearchIndexBiz();
indexBiz.recreateUserIndex();
}
private void snooze(int seconds) {
synchronized (this) {
try {
Thread.sleep(1000*seconds);
} catch ( InterruptedException e ) {
LOG.error("Interrupted from my nap!",e);
}
}
}
}