Merge branch 'feature/client_printExport' into 'main'
Print/Export functionality See merge request cse1105/2025-2026/teams/csep-team-76!19
This commit is contained in:
commit
4fc4ca7327
2 changed files with 137 additions and 0 deletions
67
client/src/main/java/client/utils/PrintExportService.java
Normal file
67
client/src/main/java/client/utils/PrintExportService.java
Normal file
|
|
@ -0,0 +1,67 @@
|
||||||
|
package client.utils;
|
||||||
|
|
||||||
|
import commons.Recipe;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.nio.file.Files;
|
||||||
|
import java.nio.file.Path;
|
||||||
|
|
||||||
|
public class PrintExportService {
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Builds the String with all the recipe data in a human-readable format and returns said string
|
||||||
|
* @param recipe - Recipe Object that needs to be converted
|
||||||
|
* @return - String Result that is the converted recipe object
|
||||||
|
*/
|
||||||
|
public static String buildRecipeText(Recipe recipe){
|
||||||
|
String result = "Title: " + recipe.getName() + "\nRecipe ID: " + recipe.getId() + "\n" +
|
||||||
|
"Ingredients: "; //Starts the string with name and recipe ID
|
||||||
|
for(int i =0; i<recipe.getIngredients().size();i++){ // For loop adding ingredients one by one
|
||||||
|
result += recipe.getIngredients().get(i) + ", ";
|
||||||
|
}
|
||||||
|
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";
|
||||||
|
}
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method that checks if the directory path of the selected directory is valid and throws and error
|
||||||
|
* if not valid
|
||||||
|
* @param path - Path to directory
|
||||||
|
*/
|
||||||
|
public static void validateFolder(Path path){
|
||||||
|
if (path == null) { //Null path value
|
||||||
|
throw new IllegalArgumentException("Path is empty");
|
||||||
|
}
|
||||||
|
if(!Files.exists(path)){ //If Folder doesn't exist
|
||||||
|
throw new IllegalArgumentException("Folder does not exist");
|
||||||
|
}
|
||||||
|
if(!Files.isDirectory(path)){ // If folder is not directory
|
||||||
|
throw new IllegalArgumentException("Given path is not a folder");
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Method that exports a plain-text String of recipe data, into a newly created file from
|
||||||
|
* a selected directory provided as a path to the method and names said file based on the
|
||||||
|
* nameOfFile param
|
||||||
|
* @param recipeData - String containing recipe data that will be written to file
|
||||||
|
* @param dirFilePath - Path to Folder
|
||||||
|
* @param nameOfFile - Name of file to be created
|
||||||
|
*/
|
||||||
|
public static void exportToFile(String recipeData, Path dirFilePath, String nameOfFile){
|
||||||
|
validateFolder(dirFilePath); //runs validate Folder method to check if Directory path is valid
|
||||||
|
Path filePath = dirFilePath.resolve(nameOfFile); //Creates a file with name provided in directory and stores path in PATH object
|
||||||
|
try {
|
||||||
|
Files.writeString(filePath, recipeData); // Writes the recipe data into the File.
|
||||||
|
}
|
||||||
|
catch (IOException e){
|
||||||
|
System.err.println("An error occurred while writing to the file");
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
70
client/src/test/java/client/scenes/PrintExportTest.java
Normal file
70
client/src/test/java/client/scenes/PrintExportTest.java
Normal file
|
|
@ -0,0 +1,70 @@
|
||||||
|
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");
|
||||||
|
final long testRecipeId = 1234L;
|
||||||
|
List<String> preparationSteps = new ArrayList<>();
|
||||||
|
preparationSteps.add("Mix Ingredients");
|
||||||
|
preparationSteps.add("Heat in Oven");
|
||||||
|
Recipe recipe1 = new Recipe(testRecipeId, "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());
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue