Revision 49189
Added by Alessia Bardi over 6 years ago
GCubeResourceGeneratorTest.java | ||
---|---|---|
2 | 2 |
|
3 | 3 |
import java.io.IOException; |
4 | 4 |
import java.io.InputStream; |
5 |
import java.io.StringWriter; |
|
6 | 5 |
|
7 |
import com.fasterxml.jackson.core.JsonFactory; |
|
8 |
import com.fasterxml.jackson.core.JsonGenerator; |
|
9 | 6 |
import com.google.common.collect.Iterables; |
10 | 7 |
import eu.dnetlib.parthenos.CRMpe; |
11 | 8 |
import org.apache.commons.io.IOUtils; |
... | ... | |
22 | 19 |
*/ |
23 | 20 |
public class GCubeResourceGeneratorTest { |
24 | 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 GCubeResourceGenerator generator = new GCubeResourceGenerator(); |
|
25 |
private static InfModel baseModel; |
|
25 | 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 |
} |
|
26 | 37 |
|
27 |
private String recordPath = "eu/dnetlib/parthenos/registry/sample1.rdf"; |
|
28 |
private static String baseURI = "http://parthenos.d4science.org/CRMext/CRMpe.rdfs/"; |
|
29 |
|
|
30 | 38 |
@Test |
31 | 39 |
public void testLoadBaseModel() { |
32 |
GCubeResourceGenerator generator = new GCubeResourceGenerator(); |
|
33 |
InfModel model = generator.loadBaseModel(); |
|
40 |
GCubeResourceGenerator generator1 = new GCubeResourceGenerator();
|
|
41 |
InfModel model = generator1.loadBaseModel();
|
|
34 | 42 |
//let's snapshot to a normal model to write all statements |
35 | 43 |
Model snapshot = ModelFactory.createDefaultModel(); |
36 | 44 |
snapshot.add(model); |
... | ... | |
38 | 46 |
|
39 | 47 |
@Test |
40 | 48 |
public void testLoadRDFFileWithModel() throws IOException { |
41 |
GCubeResourceGenerator generator = new GCubeResourceGenerator(); |
|
42 |
InfModel model = generator.loadBaseModel(); |
|
43 | 49 |
//let's snapshot to a normal model to write all statements |
44 |
model.read(getStream(recordPath), baseURI); |
|
45 | 50 |
Model snapshot = ModelFactory.createDefaultModel(); |
46 |
snapshot.add(model);
|
|
51 |
snapshot.add(baseModel);
|
|
47 | 52 |
snapshot.write(System.out); |
48 | 53 |
} |
49 | 54 |
|
50 |
/** |
|
51 |
* |
|
52 |
* Method: getGCubeER(final String rdfRecord) |
|
53 |
* |
|
54 |
*/ |
|
55 |
@Test |
|
56 |
public void testGetGCubeER() throws Exception { |
|
57 |
//TODO: Test goes here... |
|
58 |
} |
|
59 | 55 |
|
60 |
/** |
|
61 |
* |
|
62 |
* Method: writeIdentifierFacet(final JsonGenerator jg, final String identifier) |
|
63 |
* |
|
64 |
*/ |
|
65 | 56 |
@Test |
66 |
public void testWriteIdentifierFacet() throws Exception { |
|
67 |
GCubeResourceGenerator generator = new GCubeResourceGenerator(); |
|
68 |
JsonFactory jsonFactory = new JsonFactory(); |
|
69 |
StringWriter sw = new StringWriter(); |
|
70 |
JsonGenerator jg = jsonFactory.createGenerator(sw); |
|
71 |
generator.writeIdentifierFacet(jg, "thisIsAnIDentifier"); |
|
72 |
jg.close(); |
|
73 |
System.out.println(sw.toString()); |
|
74 |
Assert.assertEquals("{\"@class\":\"P1_is_identified_by\",\"target\":{\"@class\":\"IdentifierFacet\",\"value\":\"thisIsAnIDentifier\",\"type\":\"URI\"}}", sw.toString()); |
|
75 |
} |
|
76 |
|
|
77 |
|
|
78 |
|
|
79 |
/** |
|
80 |
* |
|
81 |
* Method: writeInfoFacet(final JsonGenerator jg, final String title, final String description, final String competence, final String availability) |
|
82 |
* |
|
83 |
*/ |
|
84 |
@Test |
|
85 |
public void testWriteInfoFacet() throws Exception { |
|
86 |
GCubeResourceGenerator generator = new GCubeResourceGenerator(); |
|
87 |
JsonFactory jsonFactory = new JsonFactory(); |
|
88 |
StringWriter sw = new StringWriter(); |
|
89 |
JsonGenerator jg = jsonFactory.createGenerator(sw); |
|
90 |
generator.writeInfoFacet(jg, "x", "y", "z", ""); |
|
91 |
jg.close(); |
|
92 |
System.out.println(sw.toString()); |
|
93 |
} |
|
94 |
|
|
95 |
/** |
|
96 |
* |
|
97 |
* Method: writeEventFacet(final JsonGenerator jg) |
|
98 |
* |
|
99 |
*/ |
|
100 |
@Test |
|
101 |
public void testWriteEventFacet() throws Exception { |
|
102 |
//TODO: Test goes here... |
|
103 |
} |
|
104 |
|
|
105 |
/** |
|
106 |
* |
|
107 |
* Method: writeRightsFacet(final JsonGenerator jg, final Resource res) |
|
108 |
* |
|
109 |
*/ |
|
110 |
@Test |
|
111 |
public void testWriteRightsFacet() throws Exception { |
|
112 |
//TODO: Test goes here... |
|
113 |
} |
|
114 |
|
|
115 |
/** |
|
116 |
* |
|
117 |
* Method: writeContactReferenceFacet(final JsonGenerator jg, final Resource resource) |
|
118 |
* |
|
119 |
*/ |
|
120 |
@Test |
|
121 |
public void testWriteContactReferenceFacet() throws Exception { |
|
122 |
GCubeResourceGenerator generator = new GCubeResourceGenerator(); |
|
123 |
JsonFactory jsonFactory = new JsonFactory(); |
|
124 |
StringWriter sw = new StringWriter(); |
|
125 |
JsonGenerator jg = jsonFactory.createGenerator(sw); |
|
126 |
Model model = generator.loadBaseModel(); |
|
127 |
model.read(getStream("eu/dnetlib/parthenos/registry/test.rdf"), baseURI); |
|
128 |
ResIterator iter = model.listResourcesWithProperty(RDF.type, CRMpe.PE1_Service); |
|
129 |
if(iter.hasNext()){ |
|
130 |
Resource r = iter.nextResource(); |
|
131 |
System.out.println("Found service "+r.getURI()); |
|
132 |
generator.writeContactReferenceFacet(jg, r); |
|
133 |
jg.close(); |
|
134 |
System.out.println(sw.toString()); |
|
135 |
} |
|
136 |
else { |
|
137 |
System.out.println("Could not find any service"); |
|
138 |
throw new Exception("I was expceting some PE1_Service or subclass instances!"); |
|
139 |
} |
|
140 |
} |
|
141 |
|
|
142 |
@Test |
|
143 | 57 |
public void testRules() throws IOException { |
144 |
GCubeResourceGenerator generator = new GCubeResourceGenerator(); |
|
145 |
InfModel model = generator.loadBaseModel(); |
|
146 |
model.read(getStream("eu/dnetlib/parthenos/registry/test.rdf"), baseURI); |
|
147 |
StmtIterator it = model.listStatements(new SimpleSelector(ResourceFactory.createResource("uuid:AAAH"), RDF.type, (Resource) null)); |
|
58 |
StmtIterator it = baseModel.listStatements(new SimpleSelector(ResourceFactory.createResource("uuid:AAAH"), RDF.type, (Resource) null)); |
|
148 | 59 |
while(it.hasNext()){ |
149 | 60 |
System.out.println(it.nextStatement().toString()); |
150 | 61 |
} |
151 | 62 |
Resource classHS = ResourceFactory.createResource(CRMpe.NS+"PE1_Service"); |
152 |
StmtIterator it2 = model.listStatements(classHS, RDF.type, (Resource) null);
|
|
63 |
StmtIterator it2 = baseModel.listStatements(classHS, RDF.type, (Resource) null);
|
|
153 | 64 |
while(it2.hasNext()){ |
154 | 65 |
System.out.println(it2.nextStatement().toString()); |
155 | 66 |
} |
156 | 67 |
} |
157 | 68 |
|
158 |
|
|
159 | 69 |
@Test |
160 | 70 |
public void testAmbiguousSpecificType() throws IOException { |
161 |
GCubeResourceGenerator generator = new GCubeResourceGenerator(); |
|
162 |
InfModel model = generator.loadBaseModel(); |
|
163 |
model.read(getStream("eu/dnetlib/parthenos/registry/test.rdf"), baseURI); |
|
164 |
Resource res = model.getResource("uuid:AAAH"); |
|
71 |
Resource res = baseModel.getResource("uuid:AAAH"); |
|
165 | 72 |
Resource specificType = generator.findSpecificType(res, CRMpe.PE1_Service); |
166 | 73 |
System.out.println(specificType.getURI()); |
167 | 74 |
Assert.assertEquals(CRMpe.PE1_Service.getURI(), specificType.getURI()); |
... | ... | |
175 | 82 |
@Test |
176 | 83 |
public void testJsonGeneration2() throws IOException { |
177 | 84 |
testJsonGeneration(recordPath, 0); |
178 |
|
|
179 |
|
|
180 | 85 |
} |
181 | 86 |
|
182 | 87 |
private void testJsonGeneration(final String recordClasspath, final int minCount) throws IOException { |
183 |
GCubeResourceGenerator generator = new GCubeResourceGenerator(); |
|
184 | 88 |
Iterable<String> it = generator.getGCubeER(getString(recordClasspath)); |
185 | 89 |
int count = Iterables.size(it); |
186 | 90 |
System.out.println(it); |
187 | 91 |
System.out.println(count); |
188 | 92 |
Assert.assertTrue(count > minCount); |
189 | 93 |
} |
190 |
/** |
|
191 |
* |
|
192 |
* Method: getTitleFromRDFResource(final Resource resource) |
|
193 |
* |
|
194 |
*/ |
|
195 |
@Test |
|
196 |
public void testGetTitleFromRDFResource() throws Exception { |
|
197 |
//TODO: Test goes here... |
|
198 |
} |
|
199 | 94 |
|
200 |
/** |
|
201 |
* |
|
202 |
* Method: getDescriptionFromRDFResource(final Resource resource) |
|
203 |
* |
|
204 |
*/ |
|
205 |
@Test |
|
206 |
public void testGetDescriptionFromRDFResource() throws Exception { |
|
207 |
//TODO: Test goes here... |
|
208 |
} |
|
209 |
|
|
210 |
/** |
|
211 |
* |
|
212 |
* Method: getCompetenceFromRDFResource(final Resource resource) |
|
213 |
* |
|
214 |
*/ |
|
215 |
@Test |
|
216 |
public void testGetCompetenceFromRDFResource() throws Exception { |
|
217 |
//TODO: Test goes here... |
|
218 |
} |
|
219 |
|
|
220 |
/** |
|
221 |
* |
|
222 |
* Method: getAvailabilityFromRDFResource(final Resource resource) |
|
223 |
* |
|
224 |
*/ |
|
225 |
@Test |
|
226 |
public void testGetAvailabilityFromRDFResource() throws Exception { |
|
227 |
//TODO: Test goes here... |
|
228 |
} |
|
229 |
|
|
230 |
@Test |
|
231 |
public void testJackson() throws IOException { |
|
232 |
JsonFactory jsonFactory = new JsonFactory(); |
|
233 |
StringWriter sw = new StringWriter(); |
|
234 |
JsonGenerator jgen = jsonFactory.createGenerator(sw); |
|
235 |
jgen.writeStartObject(); |
|
236 |
jgen.writeNumberField("id", 1); |
|
237 |
jgen.writeArrayFieldStart("array"); |
|
238 |
jgen.writeStartObject(); |
|
239 |
jgen.writeStringField("itemName", "theName"); |
|
240 |
jgen.writeNumberField("owner", 3); |
|
241 |
jgen.writeEndObject(); |
|
242 |
jgen.writeStartObject(); |
|
243 |
jgen.writeStringField("itemName", "theName2"); |
|
244 |
jgen.writeNumberField("owner", 8); |
|
245 |
jgen.writeEndObject(); |
|
246 |
jgen.writeEndArray(); |
|
247 |
jgen.writeEndObject(); |
|
248 |
|
|
249 |
jgen.close(); |
|
250 |
//sw.flush(); |
|
251 |
System.out.println(sw.getBuffer().toString()); |
|
252 |
} |
|
253 |
|
|
254 |
@Test |
|
255 |
public void testJacksonEmbeddedObject() throws IOException { |
|
256 |
JsonFactory jsonFactory = new JsonFactory(); |
|
257 |
StringWriter sw = new StringWriter(); |
|
258 |
JsonGenerator jgen = jsonFactory.createGenerator(sw); |
|
259 |
jgen.writeStartObject(); |
|
260 |
jgen.writeNumberField("id", 1); |
|
261 |
jgen.writeObjectFieldStart("embeddedObj"); |
|
262 |
jgen.writeStringField("itemName", "theName"); |
|
263 |
jgen.writeNumberField("owner", 3); |
|
264 |
jgen.writeEndObject(); |
|
265 |
jgen.writeEndObject(); |
|
266 |
|
|
267 |
jgen.close(); |
|
268 |
System.out.println(sw.getBuffer().toString()); |
|
269 |
} |
|
270 |
|
|
271 | 95 |
private String getString(final String classpath) { |
272 | 96 |
try { |
273 | 97 |
final ClassPathResource resource = new ClassPathResource(classpath); |
... | ... | |
277 | 101 |
} |
278 | 102 |
} |
279 | 103 |
|
280 |
private InputStream getStream(final String classpath) throws IOException { |
|
104 |
private static InputStream getStream(final String classpath) throws IOException {
|
|
281 | 105 |
return new ClassPathResource(classpath).getInputStream(); |
282 | 106 |
} |
283 | 107 |
|
Also available in: Unified diff
Refactoring