package owltools.io;

import java.io.BufferedReader;
import java.io.FileReader;
import java.io.PrintWriter;
import java.io.StringWriter;

import org.junit.Test;
import org.obolibrary.obo2owl.Obo2Owl;
import org.obolibrary.oboformat.model.OBODoc;
import org.obolibrary.oboformat.parser.OBOFormatParser;
import org.semanticweb.owlapi.model.OWLAxiom;
import org.semanticweb.owlapi.model.OWLClass;
import org.semanticweb.owlapi.model.OWLOntology;
import org.semanticweb.owlapi.model.OWLOntologyFormat;

import owltools.OWLToolsTestBasics;
import owltools.graph.OWLGraphEdge;
import owltools.graph.OWLGraphWrapper;

public class OWLGsonRendererTest extends OWLToolsTestBasics {

	private static final boolean RENDER_FLAG = true;

	@Test
	public void testAxioms() throws Exception{
		OWLGraphWrapper  wrapper = getOBO2OWLOntologyWrapper("caro.obo");
		final StringWriter stringWriter = new StringWriter();
		OWLGsonRenderer gr = new OWLGsonRenderer(new PrintWriter(stringWriter));
		OWLOntology ont = wrapper.getSourceOntology();
		for (OWLAxiom a : ont.getAxioms()) {
			gr.render(a);
		}
		if (RENDER_FLAG) {
			System.out.println(stringWriter.toString());
		}
	}

	@Test
	public void testGEdges() throws Exception{
		OWLGraphWrapper  wrapper = getOBO2OWLOntologyWrapper("caro.obo");
		final StringWriter stringWriter = new StringWriter();
		OWLGsonRenderer gr = new OWLGsonRenderer(new PrintWriter(stringWriter));
		OWLOntology ont = wrapper.getSourceOntology();
		for (OWLClass c : ont.getClassesInSignature()) {
			for (OWLGraphEdge e : wrapper.getOutgoingEdgesClosure(c)) {
				gr.render(e);
			}
		}
		if (RENDER_FLAG) {
			System.out.println(stringWriter.toString());
		}
	}

	@Test
	public void testOnt() throws Exception{
		OWLGraphWrapper  wrapper = getOBO2OWLOntologyWrapper("caro.obo");
		final StringWriter stringWriter = new StringWriter();
		OWLGsonRenderer gr = new OWLGsonRenderer(new PrintWriter(stringWriter));
		gr.render(wrapper.getSourceOntology());
		if (RENDER_FLAG) {
			System.out.println(stringWriter.toString());
			ParserWrapper pw = new ParserWrapper();
			OWLOntologyFormat owlFormat = new OWLJSONFormat();
			pw.saveOWL(wrapper.getSourceOntology(), owlFormat , "/tmp/foo.json");
		}
	}

	
	private OWLGraphWrapper getOBO2OWLOntologyWrapper(String file) throws Exception{
		OBOFormatParser p = new OBOFormatParser();
		OBODoc obodoc = p.parse(new BufferedReader(new FileReader(getResource(file))));
		Obo2Owl bridge = new Obo2Owl();
		OWLOntology ontology = bridge.convert(obodoc);
		OWLGraphWrapper wrapper = new OWLGraphWrapper(ontology);
		return wrapper;
	}
	
}

@@ -38,32 +38,53 @@
 BufferedReader;%0A
+import java.io.File;%0A
 import java.io.F
@@ -169,20 +169,83 @@
 g.junit.
-Test
+Rule;%0Aimport org.junit.Test;%0Aimport org.junit.rules.TemporaryFolder
 ;%0Aimport
@@ -749,16 +749,80 @@
 Basics %7B
+%0A%09%0A%09@Rule%0A%09public TemporaryFolder folder= new TemporaryFolder();
 %0A%0A%09priva
@@ -2274,16 +2274,58 @@
 rmat();%0A
+%09%09%09File foo = folder.newFile(%22foo.json%22);%0A
 %09%09%09pw.sa
@@ -2375,23 +2375,29 @@
 t , 
-%22/tmp/foo.json%22
+foo.getAbsolutePath()
 );%0A%09

remove hard-coded file path for test, issue #119