package eu.dnetlib.parthenos.publisher;

import java.io.IOException;

import eu.dnetlib.parthenos.ParthenosPublisherApplication;
import org.apache.commons.io.IOUtils;
import org.apache.jena.graph.Triple;
import org.apache.jena.util.iterator.ExtendedIterator;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.test.autoconfigure.web.servlet.AutoConfigureMockMvc;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.SpringBootTest.WebEnvironment;
import org.springframework.core.io.ClassPathResource;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.test.web.servlet.MockMvc;
import virtuoso.jena.driver.VirtGraph;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotEquals;
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.post;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;

/**
 * Created by Alessia Bardi on 29/08/2017.
 * Integration test.
 *
 * @author Alessia Bardi
 */
@RunWith(SpringRunner.class)
@SpringBootTest(
		webEnvironment = WebEnvironment.RANDOM_PORT,
		classes = ParthenosPublisherApplication.class)
@AutoConfigureMockMvc
@TestPropertySource(locations = "classpath:application-integrationtest.properties")
//Ignore test because it requires a running virtuoso server
@Ignore
public class ParthenosPublisherTest {

	private String recordPath = "eu/dnetlib/parthenos/virtuoso/test_record_plain.xml";
	@Value("${virtuoso.connectionstring}")
	private String connectionString;
	@Value("${virtuoso.usr}")
	private String testUser ;
	@Value("${virtuoso.pwd}")
	private String testPwd ;
	@Value("${virtuoso.uri.base.default}")
	private String defaultURIBaseURl;

	@Autowired
	private MockMvc mvc;


	@Test
	public void publishOK() throws Exception {
		String graphName = defaultURIBaseURl + "api_________::parthenos___::parthenos::topLevel/parthenos___::8d777f385d3dfec8815d20f7496026dc";
		VirtGraph graph = new VirtGraph(graphName, connectionString, testUser, testPwd);
		//ensure we have an empty graph
		graph.clear();
		assertEquals(0, graph.getCount());

		mvc.perform(post("/publish").param("record",  getString(recordPath)).param("parthenosTarget", "VIRTUOSO"))
				.andExpect(status().isOk());

		//need to wait some seconds to ensure the async call did its job
		Thread.sleep(10000);
		int count = graph.getCount();
		System.out.println(graphName +" has "+count+" triples:");
		assertNotEquals(0, graph.getCount());
		final ExtendedIterator<Triple> triples = graph.find();
		while(triples.hasNext()){
			System.out.println(triples.next());
		}
		//cleanup the virtuoso graph
		graph.clear();
		assertEquals(0, graph.getCount());

	}

	private String getString(final String classpath) {
		try {
			final ClassPathResource resource = new ClassPathResource(classpath);
			return IOUtils.toString(resource.getInputStream(), "UTF-8");
		}catch(IOException e){
			return null;
		}
	}

}
