RecipeSearchIndexBizTest.java 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  1. /* ===================================================================
  2. * RecipeSearchIndexBizTest.java
  3. *
  4. * Created Sep 24, 2004 1:36:00 PM
  5. *
  6. * Copyright (c) 2004 Matt Magoffin (spamsqr@msqr.us)
  7. *
  8. * This program is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU General Public License as
  10. * published by the Free Software Foundation; either version 2 of
  11. * the License, or (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  21. * 02111-1307 USA
  22. * ===================================================================
  23. * $Id: RecipeSearchIndexBizTest.java 60 2009-05-11 09:18:28Z msqr $
  24. * ===================================================================
  25. */
  26. package magoffin.matt.ieat.biz.test;
  27. import java.util.Date;
  28. import java.util.Iterator;
  29. import java.util.List;
  30. import magoffin.matt.ieat.biz.RecipeBiz;
  31. import magoffin.matt.ieat.biz.RecipeSearchBiz;
  32. import magoffin.matt.ieat.biz.RecipeSearchIndexBiz;
  33. import magoffin.matt.ieat.domain.Ingredient;
  34. import magoffin.matt.ieat.domain.UiIngredientMatch;
  35. import magoffin.matt.ieat.domain.UiSearchResults;
  36. import magoffin.matt.ieat.test.AbstractSpringEnabledTest;
  37. import magoffin.matt.ieat.util.IngredientCriteriaImpl;
  38. import org.apache.log4j.Logger;
  39. /**
  40. * Test case for RecipeSearchIndexBiz.
  41. *
  42. * <p>These tests can only work when the test data is loaded in the database!</p>
  43. *
  44. * @author Matt Magoffin (spamsqr@msqr.us)
  45. * @version $Revision: 60 $ $Date: 2009-05-11 21:18:28 +1200 (Mon, 11 May 2009) $
  46. */
  47. public class RecipeSearchIndexBizTest extends AbstractSpringEnabledTest {
  48. private static final Logger LOG = Logger.getLogger(RecipeSearchIndexBizTest.class);
  49. /**
  50. * Test indexing existing ingredient.
  51. */
  52. public void testIndexExistingIngredient() {
  53. // get all ingredients, and index one of them
  54. RecipeBiz recipeBiz = getRecipeBiz();
  55. List<Ingredient> ingredients = recipeBiz.getAvailableIngredients();
  56. assertTrue("At least 1 ingredient must be present", ingredients.size() > 0);
  57. Ingredient ingredient = ingredients.get(0);
  58. RecipeSearchIndexBiz indexBiz = getRecipeSearchIndexBiz();
  59. indexBiz.indexIngredient(ingredient.getIngredientId());
  60. snooze(2);
  61. }
  62. /**
  63. * Test reindex ingredients.
  64. */
  65. @SuppressWarnings("unchecked")
  66. public void testReindexIngredients() {
  67. RecipeSearchIndexBiz indexBiz = getRecipeSearchIndexBiz();
  68. indexBiz.recreateIngredientIndex();
  69. snooze(1);
  70. // verify can now search
  71. RecipeSearchBiz searchBiz = getRecipeSearchBiz();
  72. IngredientCriteriaImpl criteria = new IngredientCriteriaImpl("milk");
  73. UiSearchResults sr = searchBiz.findIngredientsSimilarlyNamed(criteria);
  74. assertNotNull("Search results should never be null", sr);
  75. assertTrue("Ingredient list should not be empty", sr.getIngredient().size() > 0);
  76. if ( LOG.isDebugEnabled() ) {
  77. int i = 1;
  78. for ( Iterator itr = sr.getIngredient().iterator(); itr.hasNext(); i++) {
  79. UiIngredientMatch match = (UiIngredientMatch)itr.next();
  80. LOG.debug("Match " +i +": " +match.getName());
  81. }
  82. }
  83. }
  84. /**
  85. * Test delete ingredient from index.
  86. */
  87. @SuppressWarnings("unchecked")
  88. public void testDeleteIngredientFromIndex() {
  89. // create a new ingredient with the time as the name, so it is unique
  90. Ingredient ing = getDomainObjectFactory().getIngredientInstance();
  91. String name = new Date().toString();
  92. ing.setName(name);
  93. // store the new ingredient
  94. RecipeBiz recipeBiz = getRecipeBiz();
  95. ing = recipeBiz.storeIngredient(ing,null);
  96. snooze(2);
  97. // verify search for ingredient returns match
  98. IngredientCriteriaImpl criteria = new IngredientCriteriaImpl(name);
  99. criteria.setApproximateSearch(false);
  100. RecipeSearchBiz searchBiz = getRecipeSearchBiz();
  101. UiSearchResults sr = searchBiz.findIngredientsSimilarlyNamed(criteria);
  102. assertNotNull("Search results should never be null", sr);
  103. assertTrue("Ingredient list should not be empty", sr.getIngredient().size() > 0);
  104. // delete from index
  105. RecipeSearchIndexBiz indexBiz = getRecipeSearchIndexBiz();
  106. indexBiz.removeIngredientFromIndex(ing.getIngredientId());
  107. snooze(2);
  108. // delete the new ingredient (this might automatically remove from index, too)
  109. recipeBiz.removeIngredient(ing.getIngredientId());
  110. snooze(1);
  111. // perform same search for ingredient, verify not found
  112. sr = searchBiz.findIngredientsSimilarlyNamed(criteria);
  113. assertNotNull("Search results should never be null", sr);
  114. if ( LOG.isDebugEnabled() ) {
  115. for ( Iterator itr = sr.getIngredient().iterator(); itr.hasNext(); ) {
  116. UiIngredientMatch match = (UiIngredientMatch)itr.next();
  117. LOG.debug("Got ingredient match: " +match.getIngredientId() +" (" +match.getName() +")");
  118. }
  119. }
  120. assertTrue("Ingredient list should be empty", sr.getIngredient().size() == 0);
  121. }
  122. /**
  123. * Test reindex recipes.
  124. */
  125. public void testReindexRecipes() {
  126. RecipeSearchIndexBiz indexBiz = getRecipeSearchIndexBiz();
  127. indexBiz.recreateRecipeIndex();
  128. /* TODO verify can now search
  129. RecipeSearchBiz searchBiz = getRecipeSearchBiz();
  130. RecipeCriteriaImpl criteria = new IngredientCriteriaImpl("chip");
  131. UiSearchResults sr = searchBiz.findIngredientsSimilarlyNamed(criteria);
  132. assertTrue("Search results should never be null", sr != null);
  133. assertTrue("Ingredient list should not be empty", sr.getIngredient().size() > 0);
  134. if ( LOG.isDebugEnabled() ) {
  135. int i = 1;
  136. for ( Iterator itr = sr.getIngredient().iterator(); itr.hasNext(); i++) {
  137. UiIngredientMatch match = (UiIngredientMatch)itr.next();
  138. LOG.debug("Match " +i +": " +match.getName());
  139. }
  140. }*/
  141. }
  142. /**
  143. * Test reindex users.
  144. */
  145. public void testReindexUsers() {
  146. RecipeSearchIndexBiz indexBiz = getRecipeSearchIndexBiz();
  147. indexBiz.recreateUserIndex();
  148. }
  149. private void snooze(int seconds) {
  150. synchronized (this) {
  151. try {
  152. Thread.sleep(1000*seconds);
  153. } catch ( InterruptedException e ) {
  154. LOG.error("Interrupted from my nap!",e);
  155. }
  156. }
  157. }
  158. }