diff --git a/app/src/main/java/cse/gradle/Server/LoginHandler.java b/app/src/main/java/cse/gradle/Server/LoginHandler.java index 8f8172e..951419c 100644 --- a/app/src/main/java/cse/gradle/Server/LoginHandler.java +++ b/app/src/main/java/cse/gradle/Server/LoginHandler.java @@ -96,7 +96,7 @@ private String handleGet(HttpExchange httpExchange) throws IOException { } } else { // If the user does not exist, return an error message - response += "User " + username + " does not exist"; + response += "Error! User " + username + " does not exist"; throw new Exception(response); } } catch (Exception e) { diff --git a/app/src/test/java/cse/gradle/Feature15Tests.java b/app/src/test/java/cse/gradle/Feature15Tests.java index 155a98e..e681ec6 100644 --- a/app/src/test/java/cse/gradle/Feature15Tests.java +++ b/app/src/test/java/cse/gradle/Feature15Tests.java @@ -7,7 +7,6 @@ package cse.gradle; -import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Test; import java.util.List; diff --git a/app/src/test/java/cse/gradle/Feature20Tests.java b/app/src/test/java/cse/gradle/Feature20Tests.java index e86aed3..3cccb1e 100644 --- a/app/src/test/java/cse/gradle/Feature20Tests.java +++ b/app/src/test/java/cse/gradle/Feature20Tests.java @@ -9,15 +9,15 @@ import static org.junit.jupiter.api.Assertions.assertEquals; +import java.util.ArrayList; +import java.util.List; + import org.junit.jupiter.api.Test; import cse.gradle.Server.Server; public class Feature20Tests extends HTTPServerTests{ - /* - * --------------------------------- UNIT TESTS --------------------------------- - */ - + /* --------------------------------- UNIT TESTS --------------------------------- */ // Login to test user @Test public void testLoginEndpoint() { @@ -52,4 +52,57 @@ void completeServerErrorTest() { assertEquals(error, response); } + /* --------------------------------- BDD TESTS --------------------------------- */ + @Test + public void testLoginExistingRecipeList() { + // Create a new List with 3 recipes + List recipes = new ArrayList(); + + recipes.add(new Recipe("eggs, bacon", "cook for 10 minutes", "breakfast", "American breakfast")); + recipes.add(new Recipe("salmon, salad", "cook for 20 minutes", "lunch", "Healthy Lunch")); + recipes.add(new Recipe("potatoes", "boil the potatoes", "brunch", "boiled potatoes")); + + + // Create a new Mock Model that accesses test_user in the database + Model model = new MockModel(); + + // Log into test_user's account + model.performLoginRequest("test_user", "password"); + + // Add the recipes to the database + for (Recipe recipe : recipes) { + model.postRecipe(recipe); + } + + // Get all the recipes from the database sorted alphabetically A-Z (not case sensitive) + String getAllResponse = model.getRecipeList("z-a", ""); + + // Delete the recipes from the database + for (Recipe recipe : recipes) { + model.deleteRecipe(recipe.getId().toString()); + } + + // Parse the getAllResponse into a List + List recipeArrayList = Recipe.parseRecipeListFromString(getAllResponse); + + System.out.println("recipeArrayList: " + recipeArrayList); + + // Check that the recieved recipes matches the test_user's list of recipes + assertEquals(3, recipeArrayList.size()); + assert(recipeArrayList.get(2).getName().equals("American breakfast")); + // System.out.println(recipeArrayList.get(1).getName()); + assert(recipeArrayList.get(1).getName().equals("boiled potatoes")); + assert(recipeArrayList.get(0).getName().equals("Healthy Lunch")); + } + + @Test + public void testLoginIncorrectCredentials() { + Model model = new MockModel(); + + // Attempt to log in with incorrect credentials + String result = model.performLoginRequest("incorrect_user", "incorrect_password"); + + // Check that the result contains an error message + assertEquals(true, result.contains("Error!")); + } }