Skip to content

Commit

Permalink
Adjusted tests to actually run in localhost.
Browse files Browse the repository at this point in the history
Co-authored-by: kjanderson1 <kjanderson1@users.noreply.github.com>
Co-authored-by: ChristophianSulaiman <ChristophianSulaiman@users.noreply.github.com>
Co-authored-by: dashluu <dashluu@users.noreply.github.com>
Co-authored-by: Timoji <Timoji@users.noreply.github.com>
Co-authored-by: Samantha Prestrelski <sprestrelski@users.noreply.github.com>
  • Loading branch information
6 people committed Dec 6, 2023
1 parent 9add16d commit ebf5a2d
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 14 deletions.
2 changes: 1 addition & 1 deletion app/src/main/java/code/client/Model/AppConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class AppConfig {
public static final String MONGO_RECIPE_COLLECTION = "recipes";
public static final String MONGO_USER_COLLECTION = "users";
// server
public static final String SERVER_HOST = "192.168.1.123";
public static final String SERVER_HOST = "localhost";
public static final int SERVER_PORT = 8100;
public static final String SERVER_URL = "http://" + SERVER_HOST + ":" + SERVER_PORT;
public static final String RECIPE_PATH = "/recipe";
Expand Down
5 changes: 4 additions & 1 deletion app/src/main/java/code/client/Model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ public String performRecipeRequest(String method, String recipe, String userId)
while ((line = in.readLine()) != null) {
tempResponse += line + "\n";
}
if( !(tempResponse.toLowerCase().contains("error")) ) {
if (!(tempResponse.toLowerCase().contains("error"))) {
response = tempResponse;
}
in.close();
Expand Down Expand Up @@ -170,6 +170,9 @@ public String performWhisperRequest(String method, String type) throws Malformed
}

in.close();
} catch (Exception e) {
e.printStackTrace();
response = "Error: " + e;
}

System.out.println("Whisper response: " + response);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ public void handle(HttpExchange httpExchange) throws IOException {
String mealType = typeIngredients[0];
String ingredients = typeIngredients[1];

if (mealType.equals("Breakfast")) {
if (mealType.toLowerCase().equals("breakfast")) {
response = sampleRecipe;
}
} catch (IndexOutOfBoundsException e) {
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/code/server/mocking/MockServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public void start() throws IOException {
// create a map to store data
// create a server
httpServer = HttpServer.create(
new InetSocketAddress("0.0.0.0", port),
new InetSocketAddress(hostName, port),
0);
// create the context to map urls
httpServer.createContext(AppConfig.RECIPE_PATH, new RecipeRequestHandler(recipeDb));
Expand Down
36 changes: 34 additions & 2 deletions app/src/test/java/code/EndToEndScenario2_1.java
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,19 @@ public static void setUp() throws IOException {
// Initialize a helper model object
model = new Model();
// Start up the server before Chef Caitlyn opens up the app
server.start();
}

/**
* Test that Chef Caitlyn was able to successfully create an account on MongoDB.
*/
@Test
public void createAccountTest() {
try {
server.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
try (MongoClient mongoClient = MongoClients.create(AppConfig.MONGODB_CONN)) {
MongoDatabase mongoDb = mongoClient.getDatabase(AppConfig.MONGO_DB);
MongoCollection<Document> accountCollection = mongoDb.getCollection(AppConfig.MONGO_USER_COLLECTION);
Expand All @@ -68,6 +73,7 @@ public void createAccountTest() {
} catch (Exception e) {
e.printStackTrace();
}
server.stop();
}

/**
Expand All @@ -76,6 +82,12 @@ public void createAccountTest() {
*/
@Test
public void automaticLoginTest() throws IOException {
try {
server.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Save the username and password to a file called "UserCredentialsTest.csv"
AccountCSVWriter writer = new AccountCSVWriter(new FileWriter("UserCredentialsTest.csv"));
writer.writeAccount(account.getUsername(), account.getPassword());
Expand All @@ -89,13 +101,20 @@ public void automaticLoginTest() throws IOException {
String expectedPassword = "Caitlyn";
assertEquals(expectedUsername, userCredentials.get(0));
assertEquals(expectedPassword, userCredentials.get(1));
server.stop();
}

/**
* Test that Chef Caitlyn can successfully generate a recipe after logging in.
*/
@Test
public void createRecipeTest() {
try {
server.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Perform a mock ChatGPT request for Chef Caitlyn's fried chicken recipe
String mealType = "breakfast";
String ingredients = "chicken, eggs";
Expand All @@ -113,6 +132,7 @@ public void createRecipeTest() {
""";
// Check that the recipe was created successfully from the ChatGPT response
assertEquals(expectedResponse, initialResponse);
server.stop();
}

/**
Expand All @@ -121,7 +141,12 @@ public void createRecipeTest() {
*/
@Test
public void refreshRecipeTest() {

try {
server.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
/* START OF COPIED TEST CONTENT */

String mealType = "breakfast";
Expand All @@ -146,13 +171,20 @@ public void refreshRecipeTest() {
String refreshResponse = model.performChatGPTRequest("PUT", mealType, ingredients);
// Check that the recipe body is no longer the same
assertNotEquals(initialResponse, refreshResponse);
server.stop();
}

/**
* Test that Chef Caitlyn can successfully save a recipe to MongoDB.
*/
@Test
public void saveRecipeTest() {
try {
server.start();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// Build a recipe based on the mocked refreshed recipe from the previous test
RecipeBuilder builder = new RecipeBuilder(account.getId(), "Fried Chicken and Egg Fried Rice");
builder.setMealTag("breakfast");
Expand Down
26 changes: 18 additions & 8 deletions app/src/test/java/code/EndToEndScenario2_2.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package code;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

import java.io.FileReader;
Expand Down Expand Up @@ -107,23 +109,22 @@ public static void setUp() throws IOException {
initialRecipeList.add(r2);
initialRecipeList.add(r3);
initialRecipeList.add(r4);
server.stop();
}

@Test
public void serverUnavailableTest() throws MalformedURLException, IOException {
server.stop();
String response = model.performAccountRequest("GET", "user", "password");
assertTrue(response.contains("Username is not found"));
assertTrue(response.contains("Error"));

response = model.performRecipeRequest("GET", "recipe", "userId");
assertTrue(response.contains("No recipes found"));
assertTrue(response.contains("Error"));

response = model.performWhisperRequest("GET", "wah");
assertTrue(response.contains("Error"));

response = model.performChatGPTRequest("GET", "mealType", "ingredients");
String expected = "";
assertEquals(expected, response);
assertTrue(response.contains("Error"));

response = model.performDallERequest("GET", "recipeTitle");
Expand All @@ -140,6 +141,7 @@ public void loginSuccessfulTest() {
}
String successMessage = "success";
userCredentials = new ArrayList<>();

try {
writer = new AccountCSVWriter(new FileWriter("UserCredentialsTest.csv"));
writer.writeAccount(account.getUsername(), account.getPassword());
Expand All @@ -153,10 +155,18 @@ public void loginSuccessfulTest() {
assertTrue(userCredentials.get(0).equals(account.getUsername()));
assertTrue(userCredentials.get(1).equals(account.getPassword()));

String loginResp = model.performAccountRequest("GET", account.getUsername(), account.getPassword());
// String expected = "";
// assertEquals(expected, loginResp);
assertTrue(loginResp.contains(successMessage), "Username is not found");
try (MongoClient mongoClient = MongoClients.create(AppConfig.MONGODB_CONN)) {
MongoDatabase mongoDb = mongoClient.getDatabase(AppConfig.MONGO_DB);
MongoCollection<Document> accountCollection = mongoDb.getCollection(AppConfig.MONGO_USER_COLLECTION);
AccountMongoDB accountDb = new AccountMongoDB(accountCollection);
accountDb.add(account);
String loginResp = model.performAccountRequest("GET", account.getUsername(), account.getPassword());
assertNotEquals("Username is not found", loginResp);
assertNotEquals("Incorrect password", loginResp);
accountDb.removeByUsername("Chef");
} catch (Exception e) {
e.printStackTrace();
}
} catch (IOException e) {
e.printStackTrace();
System.err.println("Failed test setup");
Expand Down

0 comments on commit ebf5a2d

Please sign in to comment.