Project

General

Profile

1
package eu.dnetlib.parthenos.catalogue;
2

    
3
import java.io.IOException;
4
import java.io.InputStream;
5
import java.util.List;
6

    
7
import eu.dnetlib.parthenos.CRM;
8
import eu.dnetlib.parthenos.CRMdig;
9
import eu.dnetlib.parthenos.CRMpe;
10
import eu.dnetlib.parthenos.rdf.ResourceReader;
11
import org.apache.jena.ontology.OntModel;
12
import org.apache.jena.ontology.OntModelSpec;
13
import org.apache.jena.rdf.model.InfModel;
14
import org.apache.jena.rdf.model.ModelFactory;
15
import org.apache.jena.rdf.model.ResIterator;
16
import org.apache.jena.rdf.model.Resource;
17
import org.apache.jena.vocabulary.RDF;
18
import org.assertj.core.util.Lists;
19
import org.junit.Before;
20
import org.junit.Test;
21
import org.junit.runner.RunWith;
22
import org.junit.runners.JUnit4;
23
import org.springframework.core.io.ClassPathResource;
24

    
25
import static org.junit.Assert.assertEquals;
26
import static org.junit.Assert.assertTrue;
27

    
28
/**
29
 * Created by Alessia Bardi on 11/12/2017.
30
 *
31
 * @author Alessia Bardi
32
 */
33
@RunWith(JUnit4.class)
34
public class CatalogueRegistratorTest {
35

    
36
	private CatalogueRegistrator reg;
37
	private ResourceReader reader = new ResourceReader();
38

    
39
	private String nakalaService = "eu/dnetlib/parthenos/registry/nakala.rdf";
40
	private String test = "eu/dnetlib/parthenos/registry/test.rdf";
41
	private String noRiot = "eu/dnetlib/parthenos/registry/sampleNoRiot.rdf";
42
	private String withproviders = "eu/dnetlib/parthenos/registry/withProviders.rdf";
43
	private String serviceSample = "eu/dnetlib/parthenos/registry/PE1_Service_sample.rdf";
44

    
45
	private OntModel baseModel;
46

    
47
	@Before
48
	public void setup() {
49
		reg = new CatalogueRegistrator();
50
		reg.setResourceReader(reader);
51
		baseModel = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM_TRANS_INF);
52
		baseModel.read(CRMpe.RDFS_URL);
53
		baseModel.read(CRM.RDFS_URL);
54
		baseModel.read(CRMdig.RDFS_URL);
55
	}
56

    
57
	@Test
58
	public void readModel() {
59
		InfModel model = loadBaseModel();
60
		model.read(getInputStream(noRiot), CRMpe.NS);
61
	}
62
    @Test
63
	public void testGetJsonForActivity() throws Exception{
64
		InfModel model = loadBaseModel();
65
		model.read(getInputStream(nakalaService), CRMpe.NS);
66
		ResIterator subjects = model.listSubjectsWithProperty(RDF.type, CRM.E7_Activity);
67
		while (subjects.hasNext()) {
68
			Resource subject = subjects.nextResource();
69
			String json = reg.getJsonForActivity(subject, "testCatName", "testGroup");
70
			System.out.println(json);
71
		}
72
	}
73

    
74
	@Test
75
	public void testGetJsonForMetashare() throws Exception{
76
		InfModel model = loadBaseModel();
77
		model.read(getInputStream("eu/dnetlib/parthenos/registry/metashare.rdf"), CRMpe.NS);
78
		ResIterator subjects = model.listSubjectsWithProperty(RDF.type, CRM.E7_Activity);
79
		while (subjects.hasNext()) {
80
			Resource subject = subjects.nextResource();
81
			String json = reg.getJsonForActivity(subject, "testCatName", "testGroup");
82
			System.out.println(json);
83
		}
84
	}
85

    
86
	@Test
87
	public void testGetJsonForActor() throws Exception{
88
		InfModel model = loadBaseModel();
89
		model.read(getInputStream(test), CRMpe.NS);
90
		ResIterator subjects = model.listSubjectsWithProperty(RDF.type, CRM.E39_Actor);
91
		while (subjects.hasNext()) {
92
			Resource subject = subjects.nextResource();
93
			String json = reg.getJsonForActor(subject, "testActorName", "testGroup");
94
			System.out.println(json);
95
		}
96
	}
97

    
98
	@Test
99
	public void testGetJsonForProject() throws Exception{
100
		InfModel model = loadBaseModel();
101
		model.read(getInputStream("eu/dnetlib/parthenos/registry/sample1.rdf"), CRMpe.NS);
102
		ResIterator subjects = model.listSubjectsWithProperty(RDF.type, CRMpe.PE35_Project);
103
		while (subjects.hasNext()) {
104
			Resource subject = subjects.nextResource();
105
			String json = reg.getJsonForActivity(subject, "testProjectName", "testGroup");
106
			System.out.println(json);
107
		}
108
	}
109

    
110
	@Test
111
	public void testGetJsonForDataset() throws Exception{
112
		InfModel model = loadBaseModel();
113
		model.read(getInputStream("eu/dnetlib/parthenos/registry/sample1.rdf"), CRMpe.NS);
114
		ResIterator subjects = model.listSubjectsWithProperty(RDF.type, CRMpe.PE18_Dataset);
115
		while (subjects.hasNext()) {
116
			Resource subject = subjects.nextResource();
117
			String json = reg.getJsonForThing(subject, "testDatasetName", "testGroup");
118
			System.out.println(json);
119
		}
120
	}
121

    
122
	@Test
123
	public void testAlphaNumeric(){
124
			String s = "0039%2002%203232%203325";
125
			System.out.println(s.replaceAll("[^A-Za-z0-9]","_").toLowerCase());
126
	}
127

    
128
	@Test
129
	public void testAlphaNumeric2(){
130
		String url = "http://parthenos.d4science.org/handle/Humanum/Isidore/Actor/Centre d'Études Supérieures de la Renaissance";
131
		System.out.println(url.replaceAll("[^A-Za-z0-9]","_").toLowerCase());
132
	}
133

    
134

    
135

    
136
	@Test
137
	public void ensureGroups(){
138
		InfModel model = loadBaseModel();
139
		model.read(getInputStream(withproviders), CRMpe.NS);
140
		Resource subject = model.getResource("http://parthenos.d4science.org/handle/Parthenos/REG/Service/Cendari%20Sparql%20Endpoint");
141
		List<String> providernames = Lists.newArrayList(reader.getProviderNames(subject));
142
		assertTrue(providernames.contains("SISMEL"));
143
		assertTrue(providernames.contains("EHRI Consortium"));
144
	}
145

    
146
	@Test
147
	public void ensureGroups2(){
148
		InfModel model = loadBaseModel();
149
		model.read(getInputStream(serviceSample), CRMpe.NS);
150
		Resource subject = model.getResource("http://parthenos.d4science.org/handle/CP/Service/TEST");
151
		List<String> providernames = Lists.newArrayList(reader.getProviderNames(subject));
152
		assertTrue(providernames.contains("The Provider of the service (title)"));
153
		for(String p : providernames){
154
			System.out.println(p);
155
		}
156
	}
157

    
158
	@Test
159
	public void ensureGroupsMetashare(){
160
		InfModel model = loadBaseModel();
161
		model.read(getInputStream("eu/dnetlib/parthenos/registry/metashare.rdf"), CRMpe.NS);
162
		Resource subject = model.getResource("http://parthenos.d4science.org/handle/Parthenos/REG/Service/Metashare");
163
		List<String> providernames = Lists.newArrayList(reader.getProviderNames(subject));
164
		for(String p : providernames){
165
			System.out.println(p);
166
		}
167

    
168
	}
169

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

    
177
	}
178

    
179
	private InputStream getInputStream(String classpath){
180
		try {
181
			final ClassPathResource resource = new ClassPathResource(classpath);
182
			return resource.getInputStream();
183
		}catch(IOException e){
184
			return null;
185
		}
186
	}
187

    
188

    
189

    
190
	protected InfModel loadBaseModel() {
191
		return ModelFactory.createRDFSModel(baseModel);
192
	}
193

    
194
}
(3-3/3)