/* ===================================================================
 * RecipeSearchBizTest.java
 * 
 * Created Sep 15, 2004 5:07:32 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: RecipeSearchBizTest.java 30 2009-05-04 01:53:34Z msqr $
 * ===================================================================
 */

package magoffin.matt.ieat.biz.test;

import java.util.List;

import magoffin.matt.ieat.biz.RecipeSearchBiz;
import magoffin.matt.ieat.domain.Recipe;
import magoffin.matt.ieat.domain.UiSearchResults;
import magoffin.matt.ieat.test.AbstractSpringEnabledTest;
import magoffin.matt.ieat.util.IndexCriteriaImpl;
import magoffin.matt.ieat.util.IngredientCriteriaImpl;

/**
 * Test case for the RecipceSearchBiz.
 * 
 * <p>These tests can only work when the test data is loaded in the database!</p>
 * 
 * @author Matt Magoffin (spamsqr@msqr.us)
 * @version $Revision: 30 $ $Date: 2009-05-04 13:53:34 +1200 (Mon, 04 May 2009) $
 */
public class RecipeSearchBizTest extends AbstractSpringEnabledTest {

	/**
	 * Test that able to get index list for 't'.
	 */
	public void testGetRecipesForIndexLowercase() {
		RecipeSearchBiz.IndexCriteria criteria = new IndexCriteriaImpl("t",0,0);
		testRecipeIndexSearchResults(criteria); // get rows with 't'
	}

	/**
	 * Test that able to get index list for 'T'.
	 */
	public void testGetRecipesForIndexUppercase() {
		RecipeSearchBiz.IndexCriteria criteria = new IndexCriteriaImpl("T",0,0);
		testRecipeIndexSearchResults(criteria); // get rows with capital 'T'
	}
	
	/**
	 * Test that index lists for 't' and 'T' are the same.
	 */
	public void testGetRecipesForIndexUppercaseSameAsLowercase() {
		RecipeSearchBiz.IndexCriteria criteria1 = new IndexCriteriaImpl("t",0,0);
		RecipeSearchBiz.IndexCriteria criteria2 = new IndexCriteriaImpl("T",0,0);
		UiSearchResults lowerResults = testRecipeIndexSearchResults(criteria1);
		UiSearchResults upperResults = testRecipeIndexSearchResults(criteria2);
		
		assertTrue("recipe index search results for lower/upper case should be the same size",
				lowerResults.getRecipe().size() == upperResults.getRecipe().size());
		
		// verify all names the same...
		for ( int i = 0; i < lowerResults.getRecipe().size(); i++ ) {
			Recipe r1 = (Recipe)lowerResults.getRecipe().get(i);
			Recipe r2 = (Recipe)upperResults.getRecipe().get(i);
			assertEquals(r1.getName(),r2.getName());
		}
	}
	
	/**
	 * Test searching for "similar" ingredients.
	 */
	public void testSearchForIngredientsSimilaryNamed() {
		IngredientCriteriaImpl criteria = new IngredientCriteriaImpl("chip");
		RecipeSearchBiz searchBiz = getRecipeSearchBiz();
		UiSearchResults results = searchBiz.findIngredientsSimilarlyNamed(criteria);
		
		assertTrue("Search results for ingredients similarly named should never be null",results != null);
		
	}
	
	@SuppressWarnings("unchecked")
	private UiSearchResults testRecipeIndexSearchResults(RecipeSearchBiz.IndexCriteria criteria) {
		RecipeSearchBiz searchBiz = getRecipeSearchBiz();
		UiSearchResults results = searchBiz.findRecipesForIndex(criteria);
		assertNotNull("recipe index search results should never be null", results);
		
		List recipes = results.getRecipe();
		assertNotNull("recipe index search results recipe list should never be null",recipes);
		assertTrue("recipe index search results list should have at least 1 element", recipes.size() > 0);
		
		for ( int i = 0; i < recipes.size(); i++ ) {
			Recipe r = (Recipe)recipes.get(i);
			assertNotNull("recipe index search result " +i +" name should not be null",r.getName());
			assertTrue("recipe index search result " +i +" name should start with " 
					+criteria.getDisplaySection() +" (ignoring case)",
					r.getName().toLowerCase().startsWith(criteria.getDisplaySection().toLowerCase()));
		}
		return results;
	}
	
}
