Print export Unit tests are written

This commit is contained in:
Rithvik Sriram 2025-12-05 15:21:19 +01:00
commit 7d32799f13
2 changed files with 70 additions and 1 deletions

View file

@ -19,7 +19,7 @@ public class PrintExportService {
for(int i =0; i<recipe.getIngredients().size();i++){ // For loop adding ingredients one by one
result += recipe.getIngredients().get(i) + ", ";
}
result += "Steps:\n";
result += "\nSteps:\n";
for(int i =0; i<recipe.getPreparationSteps().size();i++){ // Preparation Steps separated by new lines
result += (i+1) + ": " + recipe.getPreparationSteps().get(i) + "\n";
}

View file

@ -0,0 +1,69 @@
package client.scenes;
import client.utils.PrintExportService;
import client.utils.ServerUtils;
import commons.Recipe;
import org.junit.jupiter.api.Assumptions;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.io.TempDir;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.ArrayList;
import java.util.List;
import static org.junit.jupiter.api.Assertions.*;
public class PrintExportTest {
static ServerUtils dv = new ServerUtils();
@BeforeEach
public void setup(){
Assumptions.assumeTrue(dv.isServerAvailable(), "Server not available");
}
@Test
public void buildRecipeTextTest(){
List<String> ingredients = new ArrayList<>();
ingredients.add("Banana");
ingredients.add("Bread");
List<String> preparationSteps = new ArrayList<>();
preparationSteps.add("Mix Ingredients");
preparationSteps.add("Heat in Oven");
Recipe recipe1 = new Recipe(1234L, "Banana Bread", ingredients, preparationSteps);
assertEquals("""
Title: Banana Bread
Recipe ID: 1234
Ingredients: Banana, Bread,\s
Steps:
1: Mix Ingredients
2: Heat in Oven
""", PrintExportService.buildRecipeText(recipe1));
}
@TempDir
Path tempDir;
@Test
public void validateFolderWithValidFolderTest(){
assertDoesNotThrow(() -> PrintExportService.validateFolder(tempDir));
}
@Test
public void validateFolderWithNullPathTest(){
IllegalArgumentException i = assertThrows(IllegalArgumentException.class,
()->PrintExportService.validateFolder(null));
assertEquals("Path is empty", i.getMessage());
}
@Test
public void validateFolderWithFilePathTest() throws IOException {
Path filePath = Files.createFile(tempDir.resolve("TestFile"));
IllegalArgumentException i = assertThrows(IllegalArgumentException.class,
()->PrintExportService.validateFolder(filePath));
assertEquals("Given path is not a folder", i.getMessage());
}
}