package testutils;

import static org.junit.jupiter.api.Assertions.assertArrayEquals;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;

import org.dom4j.Document;

import routines.LcagLogger;

/*
 * user specification: the function's comment should contain keys as follows: 1. write about the function's comment.but
 * it must be before the "{talendTypes}" key.
 * 
 * 2. {talendTypes} 's value must be talend Type, it is required . its value should be one of: String, char | Character,
 * long | Long, int | Integer, boolean | Boolean, byte | Byte, Date, double | Double, float | Float, Object, short |
 * Short
 * 
 * 3. {Category} define a category for the Function. it is required. its value is user-defined .
 * 
 * 4. {param} 's format is: {param} <type>[(<default value or closed list values>)] <name>[ : <comment>]
 * 
 * <type> 's value should be one of: string, int, list, double, object, boolean, long, char, date. <name>'s value is the
 * Function's parameter name. the {param} is optional. so if you the Function without the parameters. the {param} don't
 * added. you can have many parameters for the Function.
 * 
 * 5. {example} gives a example for the Function. it is optional.
 */

/**
 * Utility functions to use in JUnit Test code
 * 
 * @author C. Ditze
 *
 */
public class LcagJunitUtils {

	public  static boolean doRecreateExpectedOutput = false;
       
	public static void assertEqualsToFileContent(String text, String tcName, String fileName) throws IOException {
		File file = new File("src/test/resources/expected", tcName + "-" + fileName);
		String fPath = file.getAbsolutePath();
		Path filePath = Paths.get(fPath);
		
		if (doRecreateExpectedOutput || !file.exists()) {
		        FileWriter myWriter = new FileWriter(file);
		        myWriter.write(text);
		        myWriter.close();
		}
		
		byte[] fileContent = Files.readAllBytes(filePath);
		
		assertArrayEquals(fileContent, text.getBytes());
	}	

	public static void assertEqualsToFileContent(Document doc, String tcName, String fileName) throws IOException {
		String text = doc.toString();
		
		assertEqualsToFileContent(text, tcName , fileName);
	}
}
