Project

General

Profile

1
package eu.dnetlib.parthenos.registry;
2

    
3
import java.io.IOException;
4
import java.io.InputStream;
5

    
6
import com.google.common.collect.Iterables;
7
import eu.dnetlib.parthenos.CRMpe;
8
import org.apache.commons.io.IOUtils;
9
import org.apache.jena.rdf.model.*;
10
import org.apache.jena.vocabulary.RDF;
11
import org.junit.Assert;
12
import org.junit.Test;
13
import org.springframework.core.io.ClassPathResource;
14

    
15
/**
16
 * Created by Alessia Bardi on 28/09/2017.
17
 *
18
 * @author Alessia Bardi
19
 */
20
public class GCubeResourceGeneratorTest {
21

    
22
	private static String recordPath = "eu/dnetlib/parthenos/registry/sample1.rdf";
23
	private static String baseURI = "http://parthenos.d4science.org/CRMext/CRMpe.rdfs/";
24
	private static GCubeResourceRegistrator generator = new GCubeResourceRegistrator();
25
	private static InfModel baseModel;
26

    
27
	static{
28
		generator.setFacetWriter(new FacetWriter());
29
		baseModel = generator.loadBaseModel();
30
		try {
31
			baseModel.read(getStream(recordPath), baseURI);
32
			baseModel.read(getStream("eu/dnetlib/parthenos/registry/test.rdf"), baseURI);
33
		} catch (IOException e) {
34
			e.printStackTrace();
35
		}
36
	}
37

    
38
	@Test
39
	public void testLoadBaseModel() {
40
		GCubeResourceRegistrator generator1 = new GCubeResourceRegistrator();
41
		InfModel model = generator1.loadBaseModel();
42
		//let's snapshot to a normal model to write all statements
43
		Model snapshot = ModelFactory.createDefaultModel();
44
		snapshot.add(model);
45
	}
46

    
47
	@Test
48
	public void testLoadRDFFileWithModel() throws IOException {
49
		//let's snapshot to a normal model to write all statements
50
		Model snapshot = ModelFactory.createDefaultModel();
51
		snapshot.add(baseModel);
52
		snapshot.write(System.out);
53
	}
54

    
55

    
56
	@Test
57
	public void testRules() throws IOException {
58
		StmtIterator it = baseModel.listStatements(new SimpleSelector(ResourceFactory.createResource("uuid:AAAH"), RDF.type, (Resource) null));
59
		while(it.hasNext()){
60
			System.out.println(it.nextStatement().toString());
61
		}
62
		Resource classHS = ResourceFactory.createResource(CRMpe.NS+"PE1_Service");
63
		StmtIterator it2 = baseModel.listStatements(classHS, RDF.type, (Resource) null);
64
		while(it2.hasNext()){
65
			System.out.println(it2.nextStatement().toString());
66
		}
67
	}
68

    
69
	@Test
70
	public void testAmbiguousSpecificType() throws IOException {
71
		Resource res = baseModel.getResource("uuid:AAAH");
72
		Resource specificType = generator.findSpecificType(res, CRMpe.PE1_Service);
73
		System.out.println(specificType.getURI());
74
		Assert.assertEquals(CRMpe.PE1_Service.getURI(), specificType.getURI());
75
	}
76

    
77
	@Test
78
	public void testJsonGeneration1() throws IOException {
79
		testJsonGeneration("eu/dnetlib/parthenos/registry/test.rdf", 0);
80
	}
81

    
82
	@Test
83
	public void testJsonGeneration2() throws IOException {
84
		testJsonGeneration(recordPath, 0);
85
	}
86

    
87
	private void testJsonGeneration(final String recordClasspath, final int minCount) throws IOException {
88
		Iterable<String> it = generator.getGCubeER(getString(recordClasspath));
89
		int count = Iterables.size(it);
90
		System.out.println(it);
91
		System.out.println(count);
92
		Assert.assertTrue(count > minCount);
93
	}
94

    
95
	private String getString(final String classpath) {
96
		try {
97
			final ClassPathResource resource = new ClassPathResource(classpath);
98
			return IOUtils.toString(resource.getInputStream(), "UTF-8");
99
		}catch(IOException e){
100
			return null;
101
		}
102
	}
103

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

    
108

    
109
}
(2-2/5)