RecipeBizTest.java 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  1. /* ===================================================================
  2. * RecipeBizTest.java
  3. *
  4. * Created Oct 13, 2004 8:34:24 AM
  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: RecipeBizTest.java 30 2009-05-04 01:53:34Z msqr $
  24. * ===================================================================
  25. */
  26. package magoffin.matt.ieat.biz.test;
  27. import java.util.List;
  28. import magoffin.matt.ieat.AuthorizationException;
  29. import magoffin.matt.ieat.biz.BizContext;
  30. import magoffin.matt.ieat.biz.RecipeBiz;
  31. import magoffin.matt.ieat.domain.Ingredient;
  32. import magoffin.matt.ieat.domain.Recipe;
  33. import magoffin.matt.ieat.domain.RecipeIngredient;
  34. import magoffin.matt.ieat.domain.System;
  35. import magoffin.matt.ieat.domain.Unit;
  36. import magoffin.matt.ieat.domain.User;
  37. import magoffin.matt.ieat.test.AbstractSpringEnabledTest;
  38. /**
  39. * Test case for RecipeBiz.
  40. *
  41. * @author Matt Magoffin (spamsqr@msqr.us)
  42. * @version $Revision: 30 $ $Date: 2009-05-04 13:53:34 +1200 (Mon, 04 May 2009) $
  43. */
  44. public class RecipeBizTest extends AbstractSpringEnabledTest {
  45. private boolean flip = false;
  46. /**
  47. * @return a random ingredient
  48. */
  49. protected Ingredient getRandomIngredient() {
  50. List<Ingredient> l = getRecipeBiz().getAvailableIngredients();
  51. int rndIdx = (int)Math.round(Math.random()*l.size());
  52. return l.get(rndIdx);
  53. }
  54. /**
  55. * Get a random unit for a given system.
  56. * @param system the unit system
  57. * @return unit
  58. */
  59. protected Unit getRandomUnit(System system) {
  60. List<Unit> l = getRecipeBiz().getAvailableUnitsForSystem(system);
  61. int rndIdx = (int)Math.round(Math.random()*l.size());
  62. return l.get(rndIdx);
  63. }
  64. /**
  65. * Get a test recipe ingredient for a given system.
  66. * @param system the unit system
  67. * @return the recipe ingredient
  68. */
  69. protected RecipeIngredient getDummyRecipeIngredient(System system) {
  70. RecipeIngredient ri = getDomainObjectFactory().getRecipeIngredientInstance();
  71. ri.setQuantity((float)Math.random()*10.0f);
  72. double rand = Math.random();
  73. if ( rand > 0.5 ) {
  74. while ( true ) {
  75. Unit u = getRandomUnit(system);
  76. if ( flip ) {
  77. if ( u.getSystemId() == null ) {
  78. continue;
  79. }
  80. }
  81. ri.setUnit(u);
  82. break;
  83. }
  84. flip = !flip;
  85. }
  86. Ingredient ing = getRandomIngredient();
  87. ri.setIngredient(ing);
  88. return ri;
  89. }
  90. /**
  91. * Test converting recipe from US to Metric.
  92. * @throws AuthorizationException if auth error occurs
  93. */
  94. @SuppressWarnings("unchecked")
  95. public void testConvertRecipeFromUSToMetric() throws AuthorizationException {
  96. Recipe r = getDummyRecipe();
  97. System us = getDomainObjectFactory().getSystemInstance();
  98. us.setSystemId(RecipeBiz.US_SYSTEM_ID);
  99. r.setSystem(us);
  100. // populate a random # of ingredients
  101. int maxIngredients = (int)Math.round(Math.random()*9) + 3;
  102. for ( int i = 0; i < maxIngredients; i++ ) {
  103. RecipeIngredient ri = getDummyRecipeIngredient(r.getSystem());
  104. r.getIngredient().add(ri);
  105. }
  106. RecipeBiz recipeBiz = getRecipeBiz();
  107. BizContext context = getBizContext();
  108. User u = context.getActingUser();
  109. try {
  110. // store user in back end
  111. u = getUserBiz().storeUser(u,context);
  112. // store recipe in back end
  113. r = recipeBiz.storeRecipe(r,context);
  114. if ( log.isDebugEnabled() ) {
  115. log.debug("Original recipe: " +debugRecipe(r));
  116. }
  117. // convert recipe
  118. Recipe converted = getRecipeBiz().convertRecipeSystem(r.getRecipeId(),RecipeBiz.METRIC_SYSTEM_ID);
  119. if ( log.isDebugEnabled() ) {
  120. log.debug("Converted recipe: " +debugRecipe(converted));
  121. }
  122. } finally {
  123. cleanOutRecipe(r);
  124. cleanOutUser(u);
  125. }
  126. }
  127. @SuppressWarnings("unchecked")
  128. private String debugRecipe(Recipe r) {
  129. StringBuilder buf = new StringBuilder();
  130. buf.append("Recipe ").append(r.getRecipeId()).append(": ").append(r.getName()).append("\n");
  131. for ( RecipeIngredient ri : (List<RecipeIngredient>)r.getIngredient() ) {
  132. buf.append(" ").append(ri.getQuantity());
  133. if ( ri.getUnit() != null ) {
  134. buf.append(" ").append(ri.getUnit().getAbbreviation());
  135. }
  136. buf.append(" ").append(ri.getIngredient().getName()).append("\n");
  137. }
  138. return buf.toString();
  139. }
  140. }