package ingraph.driver.tests;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.Arrays;

import javax.xml.stream.XMLStreamException;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameter;
import org.junit.runners.Parameterized.Parameters;
import org.neo4j.driver.v1.Record;
import org.neo4j.driver.v1.StatementResult;
import org.neo4j.graphdb.GraphDatabaseService;
import org.neo4j.graphdb.Transaction;
import org.neo4j.shell.tools.imp.format.graphml.XmlGraphMLReader;
import org.neo4j.shell.tools.imp.util.MapNodeCache;

import com.google.common.base.Charsets;
import com.google.common.collect.Iterables;
import com.google.common.io.Files;

import neo4j.driver.testkit.EmbeddedTestkitDriver;
import neo4j.driver.testkit.EmbeddedTestkitSession;

@RunWith(Parameterized.class)
public class LdbcTest {

	@Parameters(name = "query={0}")
	public static Iterable<? extends Object> data() {
		return Arrays.asList(3, 4, 5, 6, 7, 8, 9, 12, 13, 14, 15, 16, 20, 23, 24);
	}

	@Parameter
	public int queryNumber;

	final String graphMlPath = "../graphs/snb_50.graphml";
	final EmbeddedTestkitDriver driver = new EmbeddedTestkitDriver();

	@Before
	public void load() throws FileNotFoundException, XMLStreamException {
		final GraphDatabaseService graphDb = driver.getUnderlyingDatabaseService();

		try (final Transaction tx = graphDb.beginTx()) {
			final XmlGraphMLReader xmlGraphMLReader = new XmlGraphMLReader(graphDb);
			xmlGraphMLReader.nodeLabels(true);
			xmlGraphMLReader.parseXML(new BufferedReader(new FileReader(graphMlPath)), MapNodeCache.usingHashMap());
			tx.success();
		}
	}

	@Test
	public void test() throws IOException {
		EmbeddedTestkitSession session = driver.session();
		try (org.neo4j.driver.v1.Transaction tx = session.beginTransaction()) {
			final String querySpecification = Files
					.toString(new File("../queries/ldbc-snb-bi/query-" + queryNumber + ".cypher"), Charsets.UTF_8);

			StatementResult statementResult = session.run(querySpecification);
			Iterable<Record> statementResultIterable = () -> statementResult;
			System.out.println("Query " + queryNumber + ": " + Iterables.size(statementResultIterable));
		}
	}

}

@@ -193,16 +193,39 @@
 .Arrays;
+%0Aimport java.util.List;
 %0A%0Aimport
@@ -615,46 +615,8 @@
 ce;%0A
-import org.neo4j.graphdb.Transaction;%0A
 impo
@@ -803,24 +803,20 @@
 collect.
-Iterable
+List
 s;%0Aimpor
@@ -948,16 +948,61 @@
 Session;
+%0Aimport neo4j.driver.util.GraphPrettyPrinter;
 %0A%0A@RunWi
@@ -1549,16 +1549,34 @@
  (final 
+org.neo4j.graphdb.
 Transact
@@ -1902,16 +1902,22 @@
 ion %7B%0A%09%09
+final 
 Embedded
@@ -2181,16 +2181,22 @@
 8);%0A%0A%09%09%09
+final 
 Statemen
@@ -2261,57 +2261,56 @@
 %0A%09%09%09
-Iterable%3CRecord%3E statementResultIterable = () -%3E 
+final List%3CRecord%3E results = Lists.newArrayList(
 stat
@@ -2320,17 +2320,19 @@
 ntResult
-;
+);%0A
 %0A%09%09%09Syst
@@ -2382,47 +2382,148 @@
 %22 + 
-Iterables.size(statementResultIterable)
+results.size());%0A%09%09%09for (Record record : results) %7B%0A%09%09%09%09System.out.println(GraphPrettyPrinter.toString(record));%0A%09%09%09%7D%0A%09%09%09System.out.println(
 );%0A%09

Print results in LDBC tests