package innovimax.mixthem;

import innovimax.mixthem.arguments.ParamValue;
import innovimax.mixthem.arguments.Rule;
import innovimax.mixthem.arguments.RuleParam;

import java.io.*;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.logging.Level;

import org.junit.Assert;
import org.junit.Test;
import org.junit.experimental.theories.*;
import org.junit.runner.RunWith;

/*
	Created by innovimax
	Generic tests for this application
*/
public class GenericTest {
	
   @Test
   public final void parameter() throws MixException, FileNotFoundException, IOException, NumberFormatException {
	   MixThem.setLogging(Level.FINE);
	   int testId = 1;
	   List<String> failed = new ArrayList<String>();
	   boolean result = true;
	   while (true) {
		   MixThem.LOGGER.info("TEST N° " + testId + "***********************************************************");
		   String prefix = "test" + String.format("%03d", testId) +"_";
		   URL url1 = getClass().getResource(prefix + "file1.txt");
		   URL url2 = getClass().getResource(prefix + "file2.txt");
		   MixThem.LOGGER.info("--> URL 1 (" + prefix + "file1.txt"+") : " + (url1 != null ? "FOUND" : "NOT FOUND"));
		   MixThem.LOGGER.info("--> URL 2 (" + prefix + "file2.txt"+") : " + (url2 != null ? "FOUND" : "NOT FOUND"));
		   if( url1 == null || url2 == null) break;
		   for(Rule rule : Rule.values()) {
			   MixThem.LOGGER.info("RULE " + rule + " (" + (rule.isImplemented() ? "" : "NOT ") + "IMPLEMENTED)");
			   if (rule.isImplemented()) {
				   String paramsFile = prefix + "params-" + rule.getExtension() + ".txt";
				   URL urlP = getClass().getResource(paramsFile);
				   if (urlP != null) {
					   MixThem.LOGGER.info("--> Params (" + paramsFile + ") : FOUND");
				   }
				   List<RuleRun> runs = RuleRuns.getRuns(rule, urlP);
				   for (RuleRun run : runs) {
					   String resultFile = prefix + "output-" + rule.getExtension();
					   if (run.hasSuffix()) {
						   resultFile += "-" + run.getSuffix();
					   }
					   resultFile += ".txt";
					   URL urlR = getClass().getResource(resultFile);
					   MixThem.LOGGER.info("--> Result (" + resultFile + ") : " + (urlR != null ? "FOUND" : "NOT FOUND"));
					   if (urlR != null) {
						   boolean res = check(new File(url1.getFile()), new File(url2.getFile()), new File(urlR.getFile()), rule, run.getParams());
						   MixThem.LOGGER.info("RUN " + (res ? "PASS" : "FAIL") + " WITH PARAMS " + run.getParams().toString());
						   result &= res;
						   if (!res) {
							   failed.add(Integer.toString(testId));
						   }
					   }
				   }
			   }
		   }
		   testId++;
	   }
	   MixThem.LOGGER.info("FAILED TESTS : " + (failed.size() > 0 ? failed.toString() : "None"));
	   Assert.assertTrue(result);
   }	   
   private final static boolean check(File file1, File file2, File expected, Rule rule, Map<RuleParam, ParamValue> params)  throws MixException, FileNotFoundException, IOException  {
	   ByteArrayOutputStream baos_rule = new ByteArrayOutputStream();
	   MixThem mixThem = new MixThem(file1, file2, baos_rule);
           mixThem.process(rule, params);	   	   
	   mixThem = new MixThem(file1, file2, System.out);
           mixThem.process(rule, params);
	   return checkFileEquals(expected, baos_rule.toByteArray());
   }

	private static boolean checkFileEquals(File fileExpected, byte[] result) throws FileNotFoundException, IOException {
	   FileInputStream fisExpected = new FileInputStream(fileExpected);
	   int c;
	   int offset = 0;
	   while ((c = fisExpected.read()) != -1) {
		   if (offset >= result.length) return false;
		   int d = result[offset++];
		   if (c != d) return false;
	   }
	   if (offset > result.length) return false;
	   return true;
	}

}

@@ -1155,19 +1155,13 @@
 fo(%22
---%3E URL 1 (
+File 
 %22 + 
@@ -1175,28 +1175,24 @@
 + %22file1.txt
-%22+%22)
  : %22 + (url1
@@ -1203,35 +1203,35 @@
 null ? %22
-FOUND%22 : %22NOT FOUND
+found%22 : %22not found
 %22));%0A%09%09 
@@ -1257,19 +1257,13 @@
 fo(%22
---%3E URL 2 (
+File 
 %22 + 
@@ -1285,12 +1285,8 @@
 .txt
-%22+%22)
  : %22
@@ -1305,35 +1305,35 @@
 null ? %22
-FOUND%22 : %22NOT FOUND
+found%22 : %22not found
 %22));%0A%09%09 
@@ -1416,114 +1416,14 @@
 )) %7B
-%0A
 %09%09%09   
-MixThem.LOGGER.info(%22RULE %22 + rule + %22 (%22 + (rule.isImplemented() ? %22%22 : %22NOT %22) + %22IMPLEMENTED)%22);
 %0A%09%09%09
@@ -1645,20 +1645,15 @@
 fo(%22
---%3E 
 Params 
-(
 %22 + 
@@ -1670,17 +1670,16 @@
  + %22
-)
  : 
-FOUND
+found
 %22);%0A
@@ -2054,20 +2054,15 @@
 fo(%22
---%3E 
 Result 
-(
 %22 + 
@@ -2075,17 +2075,16 @@
 File + %22
-)
  : %22 + (
@@ -2103,27 +2103,27 @@
  ? %22
-FOUND%22 : %22NOT FOUND
+found%22 : %22not found
 %22));
@@ -2317,10 +2317,10 @@
 o(%22R
-UN
+un
  %22 +
@@ -2332,12 +2332,12 @@
  ? %22
-PASS
+pass
 %22 : 
@@ -2352,19 +2352,19 @@
 + %22 
-WITH PARAMS
+with params
  %22 +
@@ -2581,75 +2581,285 @@
 fo(%22
-FAILED TESTS : %22 + (failed.size() %3E 0 ? failed.toString() : %22None%22)
+****************************************************************************%22);%0A%09   MixThem.LOGGER.info(%22FAILED TESTS : %22 + (failed.size() %3E 0 ? failed.toString() : %22None%22));%0A%09   MixThem.LOGGER.info(%22****************************************************************************%22
 );%0A%09

Update GenericTest.java