package com.oreilly.rdf.tenuki.restlet;

import static org.junit.Assert.*;

import java.io.IOException;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.restlet.Client;
import org.restlet.Context;
import org.restlet.Restlet;
import org.restlet.Server;
import org.restlet.data.MediaType;
import org.restlet.data.Parameter;
import org.restlet.data.Protocol;
import org.restlet.data.Response;
import org.restlet.data.Status;
import org.restlet.resource.InputRepresentation;
import org.restlet.resource.Representation;
import org.restlet.resource.Resource;
import org.restlet.util.Series;
import org.springframework.core.io.ClassPathResource;

public class GraphResourceTest {

	private static final String HOST = "http://localhost:8182";
	private Client client;
	private static Server server;
	private ClassPathResource sampleGraphCPResource;

	@BeforeClass
	public static void before() throws Exception {
		Restlet restlet = new RDFModelApplication();
		Context context = new Context();
		context.getParameters().add("tdb.location", "testing_tdb");
		server = new Server(Protocol.HTTP, 8182, restlet);
		server.setContext(context);
		server.start();
	}
	
	@Before
	public void setUp() throws Exception {
		sampleGraphCPResource = new ClassPathResource("graph.xml");
		client = new Client(Protocol.HTTP);
	}
	
	@AfterClass
	public static void after() throws Exception {
		server.stop();
	}

	@Test
	public void testAcceptRepresentation() throws Exception {
		Representation entity = new InputRepresentation(sampleGraphCPResource
				.getInputStream(), MediaType.APPLICATION_RDF_XML);
		Response resp = client.post(HOST + "/graphs/test", entity);
		assertEquals(Status.SUCCESS_CREATED, resp.getStatus());
	}
	
	@Test
	public void testGetRepresentation() throws Exception {
		Representation entity = new InputRepresentation(sampleGraphCPResource
				.getInputStream(), MediaType.APPLICATION_RDF_XML);
		client.post(HOST + "/graphs/test", entity);
		Response resp = client.get(HOST + "/graphs/test");
		assertEquals(Status.SUCCESS_OK, resp.getStatus());
	}


}

@@ -96,16 +96,94 @@
 ception;
+%0Aimport java.util.ArrayList;%0Aimport java.util.Iterator;%0Aimport java.util.List;
 %0A%0Aimport
@@ -805,16 +805,94 @@
 ource;%0A%0A
+import com.hp.hpl.jena.query.Dataset;%0Aimport com.hp.hpl.jena.tdb.TDBFactory;%0A%0A
 public c
@@ -1394,18 +1394,17 @@
 t();%0A%09%7D%0A
-%09
 %0A
+
 %09@Before
@@ -1547,18 +1547,17 @@
 TP);%0A%09%7D%0A
-%09
 %0A
+
 %09@AfterC
@@ -1621,24 +1621,320 @@
 ver.stop();%0A
+%09%09Dataset tdb = TDBFactory.createDataset(%22testing_tdb%22);%0A%09%09List%3CString%3E list = new ArrayList%3CString%3E();%0A%09%09for (Iterator%3CString%3E iterator = tdb.listNames(); iterator.hasNext();) %7B%0A%09%09%09list.add(iterator.next());%0A%09%09%7D%0A%09%09for (String modelName : list) %7B%0A%09%09%09tdb.getNamedModel(modelName).removeAll();%0A%09%09%7D%0A
 %09%7D%0A%0A%09@Test%0A%09
@@ -2245,10 +2245,9 @@
 %0A%09%7D%0A
-%09
 %0A
+
 %09@Te
@@ -2588,11 +2588,10 @@
 ));%0A
+
 %09%7D%0A%0A
-%0A
 %7D%0A

Clean up after self in TDB