Importing WformServices and GlemmServices

Change-Id: Ifa95576d69e0d3863f63d3fdedb48c2c21cf64bc
diff --git a/Project-Tests/src/test/TestJson.java b/Project-Tests/src/test/TestJson.java
new file mode 100644
index 0000000..0160ff6
--- /dev/null
+++ b/Project-Tests/src/test/TestJson.java
@@ -0,0 +1,214 @@
+/* 
+ * Set of methods for testing Json parsing methods, wordform services and Glemm services.
+ * 
+ * 03.04.20/FB
+ */
+
+package test;
+
+import java.io.File;
+import java.io.IOException;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.Files;
+import java.util.ArrayList;
+import java.util.List;
+
+import com.fasterxml.jackson.databind.JsonNode;
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import de.korap.json.JsonTraverse;
+import de.korap.json.TraversedPath;
+import de.korap.services.glemm.client.GlemmClient;
+import de.korap.services.utils.Utils;
+import de.korap.services.WordformServices;
+
+public class TestJson
+
+{
+	static String inputFilename = "query.in.pretty.json";
+	static String outputFilename = "query.out.pretty.json";
+
+	final static boolean bSimulateWordformServices = true;
+	
+	public static void main(String[] args)
+	
+	{
+	File
+		inFile = new File(inputFilename);
+	File
+		outFile = new File(outputFilename);
+    ObjectMapper
+		objMapper = null;
+	JsonNode
+		jsonNode = null;
+	TraversedPath
+		tPath	 = null;
+	int
+		mode	 = JsonTraverse.REWRITE; 	// JsonTraverse.REWRITE or REWRITE_EXP
+	
+	if( bSimulateWordformServices )
+	{
+		System.out.printf("TestJson: starting simulation of WordformServices...\n");
+		simulateWordformServices(inputFilename, outputFilename);
+		return;
+	}
+	
+	System.out.printf("TestJson: test on '%s'...\n", inputFilename);
+	
+	objMapper = new ObjectMapper();
+	
+	// get full query as a JsonNode tree:
+	try {
+		// get full query from file as a JsonNode tree:
+		jsonNode = objMapper.readTree(inFile);
+		System.out.printf("TestJson: reading from input file: ok.\n");
+		
+		// traverse query and collect lemma subqueries in a TraversedPath object:
+		tPath = new TraversedPath(objMapper);
+		JsonTraverse.traverseJsonTree(jsonNode, tPath, JsonTraverse.COLLECT);
+		System.out.printf("Debug: TestJson: gefundene Lemmata: #: %d, list: '%s'.\n", tPath.getLemmaCount(), tPath.getLemmata());
+		} 
+	catch (IOException e) {
+		e.printStackTrace();
+		}
+
+	// Exit if no lemma subqueries found:
+	if( tPath == null || tPath.getLemmaCount() == 0 )
+		{
+		System.out.printf("TestJson: keine lemmata gefunden > Rewrite überspringen.\n");
+		return;
+		}
+
+	// Call GlemmServices to resolve lemma-Subqueries.
+	// This returns for each lemma a reference to a list of wordform instanciations
+	// (= instantiation list). 
+	// This list is submitted to traversJsonTree() with the use of the tPath object.
+	
+	// Simulates building of expansion lists, storing them in tPath:
+	if( mode == JsonTraverse.REWRITE)
+		{
+		simulateGlemmServicesConnect(tPath);
+		simulateGlemmServices(tPath);
+		}
+	else
+		simulateGlemmServicesWfs(tPath);
+	
+	// Rewrite KoralQuery: use either JsonTraverse.REWRITE or .REWRITE_EXP:
+	
+	JsonTraverse.traverseJsonTree(jsonNode, tPath, mode);
+		
+	// return rewrittn Json Tree to file:
+	try {
+		objMapper.writerWithDefaultPrettyPrinter().writeValue(outFile, jsonNode);
+		} 
+	catch (IOException e) 
+		{
+		e.printStackTrace();
+		}
+	
+	} // main
+	
+	/* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+	 * simulateWordformServices:
+	 * 
+	 * - calls same methode to parse and rewrite KoralQuery that wordformServices do.
+	 * 
+	 * 24.04.20/FB 
+	 * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
+	 */
+	
+	static void simulateWordformServices(String inputFilename, String outputFilename)  
+	
+	{
+	final String func = "simulateWordformServices";
+	String
+		query		= null,
+		newQuery	= null;
+
+    try {
+		query = Utils.readFromInputStream("./" + inputFilename);
+		} 
+    catch (IOException e1) {
+		e1.printStackTrace();
+		return;
+		}
+    
+    System.out.printf("Debug: %s: sending query to WordformServices...\n",  func);
+    
+	newQuery = WordformServices.doPOST_handleWordformQueries(query) ;
+		
+    System.out.printf("Debug: %s: receiving rewritten query from WordformServices...\n",  func);
+    
+    Utils.writeToOutputStream("./" + outputFilename, newQuery);
+
+	} // simulateWordformServices
+	
+	
+	/* simulateGlemmServicesClient
+	 * 
+	 * 14.04.20/FB
+	 * 
+	 */
+	
+	static int simulateGlemmServicesConnect(TraversedPath tPath)
+	
+	{
+	final String func = "simulateGlemmServicesConnect";
+	GlemmClient
+		glemmClient = new GlemmClient();
+	
+	glemmClient.GlemmConnect();
+	System.out.printf("Debug: %s: done.\n",  func);
+	
+	return 0;
+	} // simulateGlemmServicesClient
+	
+	/* simulateGlemmServices
+	 * 
+	 * input: list of lemmata
+	 * output: list of lemmata (same order) with references to expansion lists.
+	 * 07.04.20/FB
+	 */
+	
+	static void simulateGlemmServices(TraversedPath tPath)
+	
+	{
+		for(int i=0; i<tPath.getLemmaCount(); i++)
+			{
+			tPath.setRef("ref-" + tPath.getLemma(i) + ":" + i);
+			}
+	
+	System.out.printf("tPath.refs: #=%d list='%s'.\n", tPath.getRefsCount(), tPath.getRefsasString());
+		
+	} // simulateGlemmServices
+	
+	/* simulateGlemmServicesWfs
+	 * 
+	 * input: list of lemmata
+	 * output: list of expansion lists (= wordforms), same order.
+	 * 08.04.20/FB
+	 */
+	
+	static void simulateGlemmServicesWfs(TraversedPath tPath)
+	
+	{
+		for(int i=0; i<tPath.getLemmaCount(); i++)
+			{
+			List<String>
+				expList = new ArrayList(i+1);
+			
+			for(int j=0; j<i+1; j++)
+				{
+				expList.add(tPath.getLemma(i) + j);
+				}
+			
+			tPath.addExpList(expList); 
+			}
+	
+	System.out.printf("Debug: simulatedGlemmServicesWfs: %d expansion lists added.\n", tPath.getLemmaCount());
+		
+	} // simulateGlemmServicesWfs
+	
+		
+} // TestJson
\ No newline at end of file