1
|
package eu.dnetlib.parthenos.registry;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.io.InputStream;
|
5
|
import java.util.List;
|
6
|
|
7
|
import com.fasterxml.jackson.core.JsonFactory;
|
8
|
import eu.dnetlib.parthenos.CRM;
|
9
|
import eu.dnetlib.parthenos.CRMdig;
|
10
|
import eu.dnetlib.parthenos.CRMpe;
|
11
|
import eu.dnetlib.parthenos.jrr.ParthenosRegistryRel;
|
12
|
import eu.dnetlib.parthenos.jrr.ParthenosRegistryResource;
|
13
|
import eu.dnetlib.parthenos.publisher.ParthenosPublisherException;
|
14
|
import eu.dnetlib.parthenos.rdf.ResourceReader;
|
15
|
import org.apache.commons.io.IOUtils;
|
16
|
import org.apache.commons.lang3.StringUtils;
|
17
|
import org.apache.jena.ontology.OntModel;
|
18
|
import org.apache.jena.ontology.OntModelSpec;
|
19
|
import org.apache.jena.rdf.model.*;
|
20
|
import org.apache.jena.vocabulary.RDF;
|
21
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
22
|
import org.gcube.informationsystem.resourceregistry.client.ResourceRegistryClient;
|
23
|
import org.junit.Before;
|
24
|
import org.junit.Ignore;
|
25
|
import org.junit.Test;
|
26
|
import org.mockito.Mock;
|
27
|
import org.mockito.Mockito;
|
28
|
import org.springframework.boot.test.mock.mockito.MockBean;
|
29
|
import org.springframework.core.io.ClassPathResource;
|
30
|
|
31
|
import static org.junit.Assert.assertEquals;
|
32
|
import static org.junit.Assert.assertNotNull;
|
33
|
|
34
|
/**
|
35
|
* Created by Alessia Bardi on 28/09/2017.
|
36
|
*
|
37
|
* //TODO: Avoid loading remote resources (see setup())
|
38
|
*
|
39
|
* @author Alessia Bardi
|
40
|
*/
|
41
|
@Ignore
|
42
|
public class GCubeResourceRegistratorTest {
|
43
|
|
44
|
private String nakalaService = "eu/dnetlib/parthenos/registry/nakala.rdf";
|
45
|
private String test = "eu/dnetlib/parthenos/registry/test.rdf";
|
46
|
|
47
|
private GCubeResourceRegistrator reg;
|
48
|
private OntModel baseModel;
|
49
|
|
50
|
private ResourceRegistryClient regClient = Mockito.mock(ResourceRegistryClient.class);
|
51
|
|
52
|
|
53
|
@Before
|
54
|
public void setup(){
|
55
|
reg = new GCubeResourceRegistrator();
|
56
|
FacetWriter facetWriter = new FacetWriter();
|
57
|
facetWriter.setResourceReader(new ResourceReader());
|
58
|
reg.setFacetWriter(facetWriter);
|
59
|
reg.setResourceReader(new ResourceReader());
|
60
|
baseModel = ModelFactory.createOntologyModel(OntModelSpec.RDFS_MEM_TRANS_INF);
|
61
|
baseModel.read(CRMpe.RDFS_URL);
|
62
|
baseModel.read(CRM.RDFS_URL);
|
63
|
baseModel.read(CRMdig.RDFS_URL);
|
64
|
Mockito.when(regClient.query(Mockito.anyString(), Mockito.anyInt(), Mockito.anyString())).thenReturn(null);
|
65
|
reg.setResourceRegistryClient(regClient);
|
66
|
}
|
67
|
|
68
|
@Test
|
69
|
public void testProcessService() throws Exception{
|
70
|
String resource = getString(nakalaService);
|
71
|
InfModel model = loadBaseModel();
|
72
|
model.read(IOUtils.toInputStream(resource, "UTF-8"), CRMpe.NS);
|
73
|
ResIterator subjects = model.listSubjectsWithProperty(RDF.type, CRM.E7_Activity);
|
74
|
while (subjects.hasNext()) {
|
75
|
Resource subject = subjects.nextResource();
|
76
|
ParthenosRegistryResource prr = reg.processService(subject, new JsonFactory(), "FAKEUUID");
|
77
|
System.out.println(prr.getJson());
|
78
|
final List<ParthenosRegistryRel> resourceRels = reg.getResourceRels(subject, prr);
|
79
|
for(ParthenosRegistryRel r : resourceRels){
|
80
|
System.out.println(r.getJson());
|
81
|
}
|
82
|
}
|
83
|
}
|
84
|
|
85
|
@Test
|
86
|
public void testLoadBaseModel() {
|
87
|
//let's snapshot to a normal model to write all statements
|
88
|
Model snapshot = ModelFactory.createDefaultModel();
|
89
|
snapshot.add(baseModel);
|
90
|
}
|
91
|
|
92
|
@Test
|
93
|
public void testLoadRDFFileWithModel() throws IOException {
|
94
|
//let's snapshot to a normal model to write all statements
|
95
|
Model snapshot = ModelFactory.createDefaultModel();
|
96
|
snapshot.add(baseModel);
|
97
|
snapshot.write(System.out);
|
98
|
}
|
99
|
|
100
|
|
101
|
@Test
|
102
|
public void testRules() throws IOException {
|
103
|
StmtIterator it = baseModel.listStatements(new SimpleSelector(ResourceFactory.createResource("uuid:AAAH"), RDF.type, (Resource) null));
|
104
|
while(it.hasNext()){
|
105
|
System.out.println(it.nextStatement().toString());
|
106
|
}
|
107
|
Resource classHS = ResourceFactory.createResource(CRMpe.NS+"PE1_Service");
|
108
|
StmtIterator it2 = baseModel.listStatements(classHS, RDF.type, (Resource) null);
|
109
|
while(it2.hasNext()){
|
110
|
System.out.println(it2.nextStatement().toString());
|
111
|
}
|
112
|
}
|
113
|
|
114
|
@Test
|
115
|
public void testAmbiguousSpecificType() throws IOException {
|
116
|
Resource res = baseModel.getResource("uuid:AAAH");
|
117
|
Resource specificType = reg.getResourceReader().findSpecificType(res, CRMpe.PE1_Service);
|
118
|
System.out.println(specificType.getURI());
|
119
|
assertEquals(CRMpe.PE1_Service.getURI(), specificType.getURI());
|
120
|
}
|
121
|
|
122
|
@Test
|
123
|
public void testDashes() throws IOException {
|
124
|
System.out.println(StringUtils.remove(CRMpe.NS+"PE1_Service-HOLA", "-"));
|
125
|
}
|
126
|
|
127
|
@Test
|
128
|
public void testJsonGeneration1() throws IOException, ResourceRegistryException, ParthenosPublisherException {
|
129
|
testJsonGeneration(test);
|
130
|
}
|
131
|
|
132
|
@Test
|
133
|
public void testJsonGeneration2() throws IOException, ResourceRegistryException, ParthenosPublisherException {
|
134
|
//testJsonGeneration(recordPath);
|
135
|
}
|
136
|
|
137
|
@Test
|
138
|
public void testJsonGeneration3() throws IOException, ResourceRegistryException, ParthenosPublisherException {
|
139
|
testJsonGeneration(nakalaService);
|
140
|
}
|
141
|
|
142
|
private void testJsonGeneration(final String recordClasspath) throws IOException, ResourceRegistryException,
|
143
|
ParthenosPublisherException {
|
144
|
try {
|
145
|
//reg.register(getString(recordClasspath), objIdentifier);
|
146
|
}catch(NullPointerException npe){
|
147
|
//ok: it is because of the registryPublisher
|
148
|
}
|
149
|
}
|
150
|
|
151
|
private String getString(final String classpath) {
|
152
|
try {
|
153
|
final ClassPathResource resource = new ClassPathResource(classpath);
|
154
|
return IOUtils.toString(resource.getInputStream(), "UTF-8");
|
155
|
}catch(IOException e){
|
156
|
return null;
|
157
|
}
|
158
|
}
|
159
|
|
160
|
private static InputStream getStream(final String classpath) throws IOException {
|
161
|
return new ClassPathResource(classpath).getInputStream();
|
162
|
}
|
163
|
|
164
|
protected InfModel loadBaseModel() {
|
165
|
return ModelFactory.createRDFSModel(baseModel);
|
166
|
}
|
167
|
|
168
|
}
|