package eu.dnetlib.ariadneplus.catalogue;

import java.io.IOException;
import java.io.InputStream;

import eu.dnetlib.ariadneplus.CRM;
import eu.dnetlib.ariadneplus.CRMdig;
import eu.dnetlib.ariadneplus.CRMpe;
import eu.dnetlib.ariadneplus.rdf.ResourceReader;
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.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.springframework.core.io.ClassPathResource;

import static org.junit.Assert.assertEquals;

/**
 * Created by Alessia Bardi on 11/12/2017.
 *
 * @author Alessia Bardi
 */
@RunWith(JUnit4.class)
public class CatalogueRegistratorTest {

	private CatalogueRegistrator reg;
	private ResourceReader reader = new ResourceReader();

	private String nakalaService = "eu/dnetlib/ariadneplus/registry/nakala.rdf";
	private String test = "eu/dnetlib/ariadneplus/registry/test.rdf";
	private String noRiot = "eu/dnetlib/ariadneplus/registry/sampleNoRiot.rdf";
	private String withproviders = "eu/dnetlib/ariadneplus/registry/withProviders.rdf";
	private String serviceSample = "eu/dnetlib/ariadneplus/registry/PE1_Service_sample.rdf";

	private OntModel baseModel;

	@Before
	public void setup() {
		reg = new CatalogueRegistrator();
		reg.setResourceReader(reader);
		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 readModel() {
		InfModel model = loadBaseModel();
		model.read(getInputStream(noRiot), CRMpe.NS);
	}
    @Test
	public void testGetJsonForActivity() throws Exception{
		InfModel model = loadBaseModel();
		model.read(getInputStream(nakalaService), CRMpe.NS);
		ResIterator subjects = model.listSubjectsWithProperty(RDF.type, CRM.E7_Activity);
		while (subjects.hasNext()) {
			Resource subject = subjects.nextResource();
			String json = reg.getJsonForService(subject, "testCatName", "Huma-Num - Nakala");
			System.out.println(json);
		}
	}

	@Test
	public void testGetJsonForWP8Actor() throws Exception{
		InfModel model = loadBaseModel();
		model.read(getInputStream("/eu/dnetlib/ariadneplus/registry/testwp8.rdf"), CRMpe.NS);
		ResIterator subjects = model.listSubjectsWithProperty(RDF.type, CRM.E39_Actor);
		while (subjects.hasNext()) {
			Resource subject = subjects.nextResource();
			String json = reg.getJsonForActor(subject, "xx", "ARIADNEPLUS");
			System.out.println(json);
		}
	}

	@Test
	public void testGetJsonForMetashare() throws Exception{
		InfModel model = loadBaseModel();
		model.read(getInputStream("eu/dnetlib/ariadneplus/registry/metashare.rdf"), CRMpe.NS);
		ResIterator subjects = model.listSubjectsWithProperty(RDF.type, CRM.E7_Activity);
		while (subjects.hasNext()) {
			Resource subject = subjects.nextResource();
			String json = reg.getJsonForService(subject, "testCatName", "METASHARE");
			System.out.println(json);
		}
	}

	@Test
	public void testGetJsonForActor() throws Exception{
		InfModel model = loadBaseModel();
		model.read(getInputStream(test), CRMpe.NS);
		ResIterator subjects = model.listSubjectsWithProperty(RDF.type, CRM.E39_Actor);
		while (subjects.hasNext()) {
			Resource subject = subjects.nextResource();
			String json = reg.getJsonForActor(subject, "testActorName", "CLARIN");
			System.out.println(json);
		}
	}

	@Test
	public void testGetJsonForProject() throws Exception{
		InfModel model = loadBaseModel();
		model.read(getInputStream("eu/dnetlib/ariadneplus/registry/sample1.rdf"), CRMpe.NS);
		ResIterator subjects = model.listSubjectsWithProperty(RDF.type, CRMpe.PE35_Project);
		while (subjects.hasNext()) {
			Resource subject = subjects.nextResource();
			String json = reg.getJsonForProject(subject, "testProjectName", "ARIADNEPLUS");
			System.out.println(json);
		}
	}

	@Test
	public void testGetJsonForDataset() throws Exception{
		InfModel model = loadBaseModel();
		model.read(getInputStream("eu/dnetlib/ariadneplus/registry/sample1.rdf"), CRMpe.NS);
		ResIterator subjects = model.listSubjectsWithProperty(RDF.type, CRMpe.PE18_Dataset);
		while (subjects.hasNext()) {
			Resource subject = subjects.nextResource();
			String json = reg.getJsonForDataset(subject, "testDatasetName", "ARIADNEPLUS");
			System.out.println(json);
		}
	}


	@Test
	public void availibilityTest(){
		InfModel model = loadBaseModel();
		model.read(getInputStream(serviceSample), CRMpe.NS);
		Resource subject = model.getResource("http://ariadneplus.d4science.org/handle/CP/Service/TEST");
		assertEquals("Yes", reader.getAvailability(subject));

	}

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




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

}
