diff --git a/src/main/java/shef/data/Recipe.java b/src/main/java/shef/data/Recipe.java index 8653542..b4f0ebf 100644 --- a/src/main/java/shef/data/Recipe.java +++ b/src/main/java/shef/data/Recipe.java @@ -16,7 +16,9 @@ import java.util.Collection; import java.util.LinkedList; +import java.util.ArrayList; import java.util.List; +import java.util.Map; import java.util.Set; import java.util.HashSet; import java.util.LinkedHashSet; @@ -30,22 +32,67 @@ public class Recipe { private static final Logger LOGGER = Logger.getLogger(Logger.GLOBAL_LOGGER_NAME); + private Entity entity; private String key; private String name; private String description; private double time; private double servings; private String imageKey; - private Set tags; - private Set ingredients; - private Set equipment; - private List steps; + private Set tags = new LinkedHashSet<>(); + private Set ingredients = new LinkedHashSet<>(); + private Set equipment = new LinkedHashSet<>(); + private List steps = new LinkedList<>(); + private Set searchStrings = new HashSet<>(); private Set spinOffs; private long timestamp; + /** + * Creates a Recipe from a Datastore entity. + * Servlets can cleanly create JSON-ready Recipes by passing a recipe entity into this constructor. + */ + public Recipe(Entity recipeEntity) { + this.entity = recipeEntity; + this.name = (String) recipeEntity.getProperty("name"); + this.description = (String) recipeEntity.getProperty("description"); + this.time = (double) recipeEntity.getProperty("time"); + this.servings = (double) recipeEntity.getProperty("servings"); + this.imageKey = (String) recipeEntity.getProperty("imageKey"); + this.tags = new LinkedHashSet((ArrayList) recipeEntity.getProperty("tags")); + this.ingredients = getIngredientsFromEntity((Collection) recipeEntity.getProperty("ingredients")); + this.equipment = new LinkedHashSet((ArrayList) recipeEntity.getProperty("equipment")); + this.steps = new LinkedList((ArrayList) recipeEntity.getProperty("steps")); + this.timestamp = (long) recipeEntity.getProperty("timestamp"); + } + /** - * Copy constructor called when creating spin-offs. + * Creates a recipe from a map of parameters. + * This constructor allows servlets to cleanly create recipes by passing in an HTTPRequest's parameter map. */ + public Recipe(Map parameterMap) { + // First, store the parameter data in a Recipe object. + for (Map.Entry entry : parameterMap.entrySet()) { + handleParameter(entry.getKey(), entry.getValue()); + } + // Then build a recipe entity that is ready to be saved in Datastore. + this.entity = buildEntity(); + } + + /** Constructor called when creating a recipe to display on the recipe feed. */ + public Recipe(String key, String name, String description, Set tags, Set ingredients, List steps, long timestamp) { + this.key = key; + this.name = name; + this.tags = tags; + this.ingredients = ingredients; + this.description = description; + this.tags = tags; + this.ingredients = ingredients; + this.steps = steps; + this.timestamp = timestamp; + this.spinOffs = new HashSet<>(); + } + + /** Copy constructor called to create a spin-offs. */ public Recipe(Recipe recipe) { this.name = recipe.name; this.description = recipe.description; @@ -57,7 +104,7 @@ public Recipe(Recipe recipe) { } /** Default constructor called when creating a new recipe. */ - public Recipe(String name, String description, Set tags, Set ingredients, List steps, long timestamp) { + public Recipe(String name, String description, Set tags, Set ingredients, List steps, long timestamp) { this.name = name; this.description = description; this.tags = tags; @@ -68,34 +115,74 @@ public Recipe(String name, String description, Set tags, Set } /** - * Creates a Recipe from a Datastore entity. - * This constructor is used to easily convert an Entity into an object, which can be sent as JSON. + * A helper method that sets the Recipe's fields. + * It allows iteration over the parameter map by specifically setting each field with different behavior. */ - public Recipe(Entity recipeEntity) { - this.name = (String) recipeEntity.getProperty("name"); - this.description = (String) recipeEntity.getProperty("description"); - this.time = (double) recipeEntity.getProperty("time"); - this.servings = (double) recipeEntity.getProperty("servings"); - this.imageKey = (String) recipeEntity.getProperty("imageKey"); - this.tags = getTagsFromEntity((Collection) recipeEntity.getProperty("tags")); - this.ingredients = getIngredientsFromEntity((Collection) recipeEntity.getProperty("ingredients")); - this.equipment = getEquipmentFromEntity((Collection) recipeEntity.getProperty("equipment")); - this.steps = getStepsFromEntity((Collection) recipeEntity.getProperty("steps")); - this.timestamp = (long) recipeEntity.getProperty("timestamp"); + private void handleParameter(String name, String[] values) { + // No values associated with this parameter. + if (values == null || values.length < 1 || values[0] == null || values[0].equals("")) { + return; + } + + // Handle each parameter with a different case block for each field's specific behavior. + switch(name) { + case "name": + this.name = values[0]; + addToSearchStrings(this.name); + break; + case "description": + this.description = values[0]; + addToSearchStrings(this.description); + break; + case "imageKey": + this.imageKey = values[0]; + break; + case "time": + this.time = Double.parseDouble(values[0]); + break; + case "servings": + this.servings = Double.parseDouble(values[0]); + break; + default: + // Handle, tags, ingredients, equipment, and steps by adding them to the Recipe's lists. + if (name.contains("tag")) { + addTag(values[0]); + addToSearchStrings(values[0]); + } else if (name.contains("ingredient")) { + double amount = Double.parseDouble(values[0]); + addIngredient(new Ingredient(amount, values[1], values[2])); + addToSearchStrings(values[2]); + } else if (name.contains("equipment")) { + addEquipment(values[0]); + } else if (name.contains("step")) { + appendStep(values[0]); + } + } } - /** Constructor called when creating a recipe to display on the recipe feed. */ - public Recipe(String key, String name, String description, Set tags, Set ingredients, List steps, long timestamp) { - this.key = key; - this.name = name; - this.tags = tags; - this.ingredients = ingredients; - this.description = description; - this.tags = tags; - this.ingredients = ingredients; - this.steps = steps; - this.timestamp = timestamp; - this.spinOffs = new HashSet<>(); + /** + * Builds a recipe entity from a Recipe object. + * Automatically called by the Recipe constructor that builds a Recipe from a parameter map. + */ + public Entity buildEntity() { + Entity recipeEntity = new Entity("Recipe"); + recipeEntity.setProperty("name", name); + recipeEntity.setProperty("time", time); + recipeEntity.setProperty("servings", servings); + recipeEntity.setProperty("imageKey", imageKey); + recipeEntity.setProperty("description", description); + recipeEntity.setProperty("tags", tags); + recipeEntity.setProperty("ingredients", embeddedIngredients()); + recipeEntity.setProperty("equipment", equipment); + recipeEntity.setProperty("steps", steps); + recipeEntity.setProperty("search-strings", getSearchStrings()); + recipeEntity.setProperty("timestamp", System.currentTimeMillis()); + return recipeEntity; + } + + /** Gets the recipe's corresponding Datastore entity. */ + public Entity getEntity() { + return entity; } /** Gets the recipe's name. */ @@ -133,6 +220,14 @@ public Set getSpinOffs() { return spinOffs; } + /** + * Returns a recipe's search strings. + * Return type is an ArrayList because Filters and CompositeFilters require lists. + */ + public ArrayList getSearchStrings() { + return new ArrayList(searchStrings); + } + /** Adds a tag to the recipe. */ public void addTag(String tag) { tags.add(tag); @@ -143,11 +238,28 @@ public void addIngredient(Ingredient ingredient) { ingredients.add(ingredient); } + /** Adds equipment to the recipe. */ + public void addEquipment(String newEquipment) { + equipment.add(newEquipment); + } + + /** Adds a spin-off to the recipe. */ public void addSpinOff(SpinOff spinOff) { spinOffs.add(spinOff); } + /** + * Adds strings to a recipe's search strings. + * Each token in the input string is upper-cased and added as a separate search string. + */ + public void addToSearchStrings(String string) { + String[] tokens = string.split(" "); + for (String token : tokens) { + searchStrings.add(token.toUpperCase()); + } + } + /** Removes a tag from the recipe. */ public void removeTag(String tag) { tags.remove(tag); @@ -164,7 +276,7 @@ public void removeSpinOff(SpinOff spinOff) { } /** Appends a new step to a recipe's list of steps. */ - public void appendStep(Step newStep) { + public void appendStep(String newStep) { steps.add(newStep); } @@ -174,7 +286,7 @@ public void appendStep(Step newStep) { * @param newStep The new step to insert. * @throws IndexOutOfBoundsException Thrown if position is out of bounds of the recipe's list of steps. */ - public void addStep(int position, Step newStep) throws IndexOutOfBoundsException { + public void addStep(int position, String newStep) throws IndexOutOfBoundsException { if (position == steps.size()) { appendStep(newStep); } else if (!isValidStepPosition(position)) { @@ -191,7 +303,7 @@ public void addStep(int position, Step newStep) throws IndexOutOfBoundsException * @param newStep The replacing step. * @throws IndexOutOfBoundsException Thrown if position is out of bounds of the recipe's list of steps. */ - public void setStep(int position, Step newStep) throws IndexOutOfBoundsException { + public void setStep(int position, String newStep) throws IndexOutOfBoundsException { if (!isValidStepPosition(position)) { handleStepException("Position " + position + " is out of bounds [0, " + (steps.size() - 1) + "]. " + "Failed to set step \"" + newStep + "\"."); @@ -215,7 +327,7 @@ public void removeStep(int position) throws IndexOutOfBoundsException { } /** Returns the recipe's list of steps. */ - public List getSteps() { + public List getSteps() { return steps; } @@ -225,8 +337,8 @@ public String toString() { String str = String.format("\nName: %s", name); str += String.format("\nDescription: %s", description); str += "\nSteps:\n"; - for (Step step : steps) { - str += String.format("\t%s\n", step.getInstruction()); + for (String step : steps) { + str += String.format("\t%s\n", step); } return str; } @@ -246,15 +358,6 @@ protected boolean isValidStepPosition(int position) { return position >= 0 && position < steps.size(); } - /** Returns the tags of an EmbeddedEntity as a Set. */ - private Set getTagsFromEntity(Collection entityTags) { - Set tagsSet = new HashSet<>(); - for (EmbeddedEntity tag : entityTags) { - tagsSet.add((String) tag.getProperty("tag")); - } - return tagsSet; - } - /** Returns the ingredients of an EmbeddedEntity as a Set. */ private Set getIngredientsFromEntity(Collection entityIngredients) { Set ingredientsSet = new HashSet<>(); @@ -264,22 +367,17 @@ private Set getIngredientsFromEntity(Collection enti return ingredientsSet; } - /** Returns the equipment of an EmbeddedEntity as a Set. */ - private Set getEquipmentFromEntity(Collection entityEquipment) { - Set equipmentSet = new HashSet<>(); - for (EmbeddedEntity equipment : entityEquipment) { - equipmentSet.add((String) equipment.getProperty("equipment")); - } - return equipmentSet; - } - - /** Returns the steps of an EmbeddedEntity as a List. */ - private List getStepsFromEntity(Collection entitySteps) { - List stepsList = new LinkedList<>(); - for (EmbeddedEntity step : entitySteps) { - stepsList.add(new Step((String) step.getProperty("step"))); + /** Converts a set of Ingredients into a Datastore-ready Collection of EmbeddedEntities. */ + private Collection embeddedIngredients() { + Collection embeddedIngredients = new LinkedHashSet<>(); + for (Ingredient ingredient : ingredients) { + EmbeddedEntity ingredientEntity = new EmbeddedEntity(); + ingredientEntity.setProperty("amount", ingredient.amount()); + ingredientEntity.setProperty("unit", ingredient.unit()); + ingredientEntity.setProperty("name", ingredient.name()); + embeddedIngredients.add(ingredientEntity); } - return stepsList; + return embeddedIngredients; } private void handleStepException(String exceptionText) throws IndexOutOfBoundsException { @@ -291,12 +389,12 @@ private static boolean equals(Recipe a, Recipe b) { if (a.steps.size() != b.steps.size() || !a.name.equals(b.name) || !a.description.equals(b.description)) { return false; } - Iterator aSteps = a.steps.iterator(); - Iterator bSteps = b.steps.iterator(); + Iterator aSteps = a.steps.iterator(); + Iterator bSteps = b.steps.iterator(); while (aSteps.hasNext() && bSteps.hasNext()) { - Step aStep = aSteps.next(); - Step bStep = bSteps.next(); - if (!aStep.getInstruction().equals(bStep.getInstruction())) { + String aStep = aSteps.next(); + String bStep = bSteps.next(); + if (!aStep.equals(bStep)) { return false; } } diff --git a/src/main/java/shef/data/Step.java b/src/main/java/shef/data/Step.java deleted file mode 100644 index 8e6cf9a..0000000 --- a/src/main/java/shef/data/Step.java +++ /dev/null @@ -1,39 +0,0 @@ -// Copyright 2019 Google LLC -// -// 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 -// -// https://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 shef.data; - -public class Step { - - private String instruction; - - public Step(String instruction) { - this.instruction = instruction; - } - - public String getInstruction() { - return instruction; - } - - @Override - public String toString() { - return instruction; - } - - @Override - public boolean equals(Object other) { - return other instanceof Step && instruction.equals(((Step) other).getInstruction()); - } - -} diff --git a/src/main/java/shef/servlets/DisplayRecipesServlet.java b/src/main/java/shef/servlets/DisplayRecipesServlet.java index 24aca87..d3db2ea 100644 --- a/src/main/java/shef/servlets/DisplayRecipesServlet.java +++ b/src/main/java/shef/servlets/DisplayRecipesServlet.java @@ -23,7 +23,6 @@ import java.util.Set; import java.util.stream.Collectors; import shef.data.Recipe; -import shef.data.Step; import shef.servlets.NewRecipeServlet; import com.google.appengine.api.datastore.DatastoreService; import com.google.appengine.api.datastore.DatastoreServiceFactory; diff --git a/src/main/java/shef/servlets/NewRecipeServlet.java b/src/main/java/shef/servlets/NewRecipeServlet.java index 95b60fd..a718c17 100644 --- a/src/main/java/shef/servlets/NewRecipeServlet.java +++ b/src/main/java/shef/servlets/NewRecipeServlet.java @@ -22,28 +22,17 @@ import com.google.appengine.api.datastore.DatastoreService; import com.google.appengine.api.datastore.DatastoreServiceFactory; import com.google.appengine.api.datastore.Entity; -import com.google.appengine.api.datastore.EmbeddedEntity; import com.google.appengine.api.datastore.Key; import com.google.appengine.api.datastore.KeyFactory; import com.google.appengine.api.datastore.EntityNotFoundException; import com.google.gson.Gson; -import java.util.Collection; -import java.util.List; -import java.util.HashSet; -import java.util.LinkedHashSet; -import java.util.ArrayList; -import java.util.LinkedList; +import java.util.Map; import shef.data.Recipe; -import shef.data.Step; @WebServlet("/new-recipe") public class NewRecipeServlet extends HttpServlet { private DatastoreService datastore; - private final String TAG = "tag"; - private final String INGREDIENT = "ingredient"; - private final String EQUIPMENT = "equipment"; - private final String STEP = "step"; @Override public void init() { @@ -69,90 +58,10 @@ public void doGet(HttpServletRequest request, HttpServletResponse response) thro /** Posts a new recipe to the servlet. */ @Override public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException { - Collection searchStrings = new HashSet<>(); - String name = request.getParameter("name"); - searchStrings.add(name.toUpperCase()); - double time = request.getParameter("time").equals("") ? 0 : Double.parseDouble(request.getParameter("time")); - double servings = request.getParameter("servings").equals("") ? 0 : Double.parseDouble(request.getParameter("servings")); - String imageKey = BlobServlet.getUploadedFileBlobKey(request, "image"); - String description = request.getParameter("description"); - Collection tags = getParameters(request, TAG, searchStrings); - Collection ingredients = getIngredients(request, searchStrings); - Collection equipment = getParameters(request, EQUIPMENT, null); - Collection steps = getParameters(request, STEP, null); - int likes = request.getParameter("likes") == null ? 0 : Integer.parseInt(request.getParameter("likes")); - long timestamp = System.currentTimeMillis(); - - Entity recipe = new Entity("Recipe"); - recipe.setProperty("name", name); - recipe.setProperty("time", time); - recipe.setProperty("servings", servings); - recipe.setProperty("imageKey", imageKey); - recipe.setProperty("description", description); - recipe.setProperty("tags", tags); - recipe.setProperty("ingredients", ingredients); - recipe.setProperty("equipment", equipment); - recipe.setProperty("steps", steps); - recipe.setProperty("search-strings", new ArrayList(searchStrings)); - recipe.setProperty("timestamp", timestamp); - recipe.setProperty("likes", likes); - datastore.put(recipe); - - response.sendRedirect("/recipe.html?key=" + KeyFactory.keyToString(recipe.getKey())); - } - - /** - * Gets the parameters for fields that have different numbers of parameters from recipe to recipe. - * This method ensures that all tags, ingredients, and steps are recorded, no matter how many of each a recipe has. - * @return A Collection of EmbeddedEntities, where each EmbeddedEntity holds one parameter. - */ - private Collection getParameters(HttpServletRequest request, String field, Collection searchStrings) { - Collection parameters = new LinkedList<>(); - int parameterNum = 0; - String parameterName = field + parameterNum; - String parameter = request.getParameter(parameterName); - - // In the HTML form, parameters are named as [field name][index], ie step0. - // This loop increments the index of the parameter's name, exiting once it reaches an index for which there is no parameter. - while (parameter != null) { - addToSearchStrings(searchStrings, parameter); - EmbeddedEntity parameterEntity = new EmbeddedEntity(); - parameterEntity.setProperty(field, parameter); - parameters.add(parameterEntity); - - parameterName = field + (++parameterNum); - parameter = request.getParameter(parameterName); - } - return parameters; - } - - private Collection getIngredients(HttpServletRequest request, Collection searchStrings) { - Collection parameters = new LinkedList<>(); - int parameterNum = 0; - String parameterName = "ingredient" + parameterNum; - String[] parameterValues = request.getParameterValues(parameterName); - - // In the HTML form, parameters are named as [field name][index], ie step0. - // This loop increments the index of the parameter's name, exiting once it reaches an index for which there is no parameter. - while (parameterValues != null) { - addToSearchStrings(searchStrings, parameterValues[2]); - EmbeddedEntity parameterEntity = new EmbeddedEntity(); - parameterEntity.setProperty("amount", parameterValues[0].equals("") ? 0 : Double.parseDouble(parameterValues[0])); - parameterEntity.setProperty("unit", parameterValues[1]); - parameterEntity.setProperty("name", parameterValues[2]); - parameters.add(parameterEntity); - - parameterName = "ingredient" + (++parameterNum); - parameterValues = request.getParameterValues(parameterName); - } - return parameters; - } - - /** Adds a formatted search string to the set of search strings. */ - private void addToSearchStrings(Collection searchStrings, String stringToAdd) { - if (searchStrings == null) { - return; - } - searchStrings.add(stringToAdd.toUpperCase()); + Map parameterMap = request.getParameterMap(); + Entity recipeEntity = new Recipe(parameterMap).getEntity(); + recipeEntity.setProperty("imageKey", BlobServlet.getUploadedFileBlobKey(request, "image")); + datastore.put(recipeEntity); + response.sendRedirect("/recipe.html?key=" + KeyFactory.keyToString(recipeEntity.getKey())); } } diff --git a/src/test/java/RecipeTest.java b/src/test/java/RecipeTest.java index 3afc9e9..088f525 100644 --- a/src/test/java/RecipeTest.java +++ b/src/test/java/RecipeTest.java @@ -25,7 +25,6 @@ import java.util.Arrays; import shef.data.Recipe; import shef.data.SpinOff; -import shef.data.Step; import shef.data.Ingredient; @RunWith(JUnit4.class) @@ -38,10 +37,10 @@ public final class RecipeTest { new Ingredient(1.5, "oz.", "bread"), new Ingredient(2, "g", "cheese"), new Ingredient(4, "in", "butter"))); - private static final List STEPS = Arrays.asList( - new Step("Toast the bread for 2 minutes"), - new Step("Melt the cheese"), - new Step("Put the cheese in the bread") + private static final List STEPS = Arrays.asList( + "Toast the bread for 2 minutes", + "Melt the cheese", + "Put the cheese in the bread" ); private static final long TIMESTAMP = 0; private Recipe recipe; @@ -53,97 +52,97 @@ public void setup() { @Test public void appendStep() { - List expectedSteps = Arrays.asList( - new Step("Toast the bread for 2 minutes"), - new Step("Melt the cheese"), - new Step("Put the cheese in the bread"), - new Step("Butter the bread") + List expectedSteps = Arrays.asList( + "Toast the bread for 2 minutes", + "Melt the cheese", + "Put the cheese in the bread", + "Butter the bread" ); - recipe.appendStep(new Step("Butter the bread")); + recipe.appendStep("Butter the bread"); Assert.assertEquals(expectedSteps, recipe.getSteps()); } @Test public void addBeginningStep() { - List expectedSteps = Arrays.asList( - new Step("Index 0!"), - new Step("Toast the bread for 2 minutes"), - new Step("Melt the cheese"), - new Step("Put the cheese in the bread") + List expectedSteps = Arrays.asList( + "Index 0!", + "Toast the bread for 2 minutes", + "Melt the cheese", + "Put the cheese in the bread" ); - recipe.addStep(0, new Step("Index 0!")); + recipe.addStep(0, "Index 0!"); Assert.assertEquals(expectedSteps, recipe.getSteps()); } @Test public void addIntermediateStep() { - List expectedSteps = Arrays.asList( - new Step("Toast the bread for 2 minutes"), - new Step("Turn on the burner"), - new Step("Melt the cheese"), - new Step("Put the cheese in the bread") + List expectedSteps = Arrays.asList( + "Toast the bread for 2 minutes", + "Turn on the burner", + "Melt the cheese", + "Put the cheese in the bread" ); - recipe.addStep(1, new Step("Turn on the burner")); + recipe.addStep(1, "Turn on the burner"); Assert.assertEquals(expectedSteps, recipe.getSteps()); } @Test public void addEndStep() { - List expectedSteps = Arrays.asList( - new Step("Toast the bread for 2 minutes"), - new Step("Melt the cheese"), - new Step("Put the cheese in the bread"), - new Step("Index 3!") + List expectedSteps = Arrays.asList( + "Toast the bread for 2 minutes", + "Melt the cheese", + "Put the cheese in the bread", + "Index 3!" ); - recipe.addStep(3, new Step("Index 3!")); + recipe.addStep(3, "Index 3!"); Assert.assertEquals(expectedSteps, recipe.getSteps()); } @Test public void setBeginningStep() { - List expectedSteps = Arrays.asList( - new Step("New first step"), - new Step("Melt the cheese"), - new Step("Put the cheese in the bread") + List expectedSteps = Arrays.asList( + "New first step", + "Melt the cheese", + "Put the cheese in the bread" ); - recipe.setStep(0, new Step("New first step")); + recipe.setStep(0, "New first step"); Assert.assertEquals(expectedSteps, recipe.getSteps()); } @Test public void setIntermediateStep() { - List expectedSteps = Arrays.asList( - new Step("Toast the bread for 2 minutes"), - new Step("New middle step"), - new Step("Put the cheese in the bread") + List expectedSteps = Arrays.asList( + "Toast the bread for 2 minutes", + "New middle step", + "Put the cheese in the bread" ); - recipe.setStep(1, new Step("New middle step")); + recipe.setStep(1, "New middle step"); Assert.assertEquals(expectedSteps, recipe.getSteps()); } @Test public void setEndStep() { - List expectedSteps = Arrays.asList( - new Step("Toast the bread for 2 minutes"), - new Step("Melt the cheese"), - new Step("New last step") + List expectedSteps = Arrays.asList( + "Toast the bread for 2 minutes", + "Melt the cheese", + "New last step" ); - recipe.setStep(2, new Step("New last step")); + recipe.setStep(2, "New last step"); Assert.assertEquals(expectedSteps, recipe.getSteps()); } @Test public void removeBeginningStep() { - List expectedSteps = Arrays.asList( - new Step("Melt the cheese"), - new Step("Put the cheese in the bread") + List expectedSteps = Arrays.asList( + "Melt the cheese", + "Put the cheese in the bread" ); recipe.removeStep(0); @@ -152,9 +151,9 @@ public void removeBeginningStep() { @Test public void removeIntermediateStep() { - List expectedSteps = Arrays.asList( - new Step("Toast the bread for 2 minutes"), - new Step("Put the cheese in the bread") + List expectedSteps = Arrays.asList( + "Toast the bread for 2 minutes", + "Put the cheese in the bread" ); recipe.removeStep(1); @@ -163,9 +162,9 @@ public void removeIntermediateStep() { @Test public void removeEndStep() { - List expectedSteps = Arrays.asList( - new Step("Toast the bread for 2 minutes"), - new Step("Melt the cheese") + List expectedSteps = Arrays.asList( + "Toast the bread for 2 minutes", + "Melt the cheese" ); recipe.removeStep(2); @@ -174,22 +173,22 @@ public void removeEndStep() { @Test(expected = IndexOutOfBoundsException.class) public void addStepFailureTooHigh() { - recipe.addStep(99, new Step("never added")); + recipe.addStep(99, "never added"); } @Test(expected = IndexOutOfBoundsException.class) public void addStepFailureTooLow() { - recipe.addStep(-1, new Step("never added")); + recipe.addStep(-1, "never added"); } @Test(expected = IndexOutOfBoundsException.class) public void setStepFailureTooHigh() { - recipe.setStep(5, new Step("never set")); + recipe.setStep(5, "never set"); } @Test(expected = IndexOutOfBoundsException.class) public void setStepFailureTooLow() { - recipe.setStep(-1, new Step("never set")); + recipe.setStep(-1, "never set"); } @Test(expected = IndexOutOfBoundsException.class) @@ -279,7 +278,7 @@ public void toStringTest() { expected += "\nSteps:\n"; expected += "\tA step\n"; - Recipe testRecipe = new Recipe("NAME", "DESC", TAGS, INGREDIENTS, Arrays.asList(new Step("A step")), TIMESTAMP); + Recipe testRecipe = new Recipe("NAME", "DESC", TAGS, INGREDIENTS, Arrays.asList("A step"), TIMESTAMP); Assert.assertEquals(expected, testRecipe.toString()); } }