package eu.dnetlib.ariadneplus.catalogue;

import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;

import eu.dnetlib.ariadneplus.CRM;
import eu.dnetlib.ariadneplus.CRMdig;
import eu.dnetlib.ariadneplus.CRMpe;
import eu.dnetlib.ariadneplus.publisher.AriadnePlusPublisherException;
import org.apache.jena.ontology.OntModel;
import org.apache.jena.ontology.OntModelSpec;
import org.apache.jena.rdf.model.InfModel;
import org.apache.jena.rdf.model.ModelFactory;
import org.apache.jena.rdf.model.ResIterator;
import org.apache.jena.rdf.model.Resource;
import org.apache.jena.vocabulary.RDF;
import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.core.io.ClassPathResource;
import org.springframework.test.context.TestPropertySource;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;

/**
 * Created by Alessia Bardi on 15/03/2018.
 *
 * @author Alessia Bardi
 */
@RunWith(SpringJUnit4ClassRunner.class)
@SpringBootTest
@TestPropertySource(
		locations = "classpath:application-integrationtest.properties")
@Ignore
public class CatalogueRegistratorIntegrationTest {

	private String nakalaService = "eu/dnetlib/ariadneplus/registry/nakala.rdf";
	private String resourceCatName = "nakalatest";
	private String error500_1 = "eu/dnetlib/ariadneplus/registry/error500_1.rdf";

	@Autowired
	private CatalogueRegistrator reg;
	private OntModel baseModel;



	@Before
	public void setup() {
		baseModel = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM_TRANS_INF);
		baseModel.read(getInputStream("eu/dnetlib/ariadneplus/rdfs/CRMpe.rdfs"), CRMpe.NS);
		baseModel.read(getInputStream("eu/dnetlib/ariadneplus/rdfs/CRMdig.rdfs"), CRMdig.NS);
		baseModel.read(getInputStream("eu/dnetlib/ariadneplus/rdfs/cidoc_crm_v6.2-draft-2015August.rdfs"), CRM.NS);
	}

	@Test
	public void testRegistration() throws Exception{
		InfModel model = loadBaseModel();
		model.read(getStream(nakalaService), CRMpe.NS);
		ResIterator subjects = model.listSubjectsWithProperty(RDF.type, CRM.E7_Activity);
		Resource nakala = subjects.nextResource();
		String uuid = reg.register(nakala, CRMpe.PE1_Service, "Huma-Num - Nakala");
		System.out.println(uuid);
		purge(uuid);
	}

	@Test
	public void testError500_1() throws Exception {
		InfModel model = loadBaseModel();
		model.read(getStream(error500_1), CRMpe.NS);
		ResIterator subjects = model.listSubjectsWithProperty(RDF.type, CRM.E29_Design_or_Procedure);
		Resource r = subjects.nextResource();
		String uuid = reg.register(r, CRM.E29_Design_or_Procedure, "ARIADNEPLUS");
		System.out.println(uuid);
		purge(uuid);
	}

	@Test
	public void testPurge() throws AriadnePlusPublisherException, URISyntaxException {
		reg.purge(resourceCatName);
	}

	private void purge(final String uuid) throws AriadnePlusPublisherException, URISyntaxException {
		reg.purge(uuid);
	}

	private static InputStream getStream(final String classpath) throws IOException {
		return new ClassPathResource(classpath).getInputStream();
	}

	protected InfModel loadBaseModel() {
		return ModelFactory.createRDFSModel(baseModel);
	}

	private InputStream getInputStream(String classpath){
		try {
			final ClassPathResource resource = new ClassPathResource(classpath);
			return resource.getInputStream();
		}catch(IOException e){
			return null;
		}
	}
}
