1
|
package eu.dnetlib.data.transform;
|
2
|
|
3
|
import java.io.*;
|
4
|
import java.util.*;
|
5
|
import java.util.Map.Entry;
|
6
|
import java.util.zip.GZIPInputStream;
|
7
|
import javax.xml.transform.TransformerConfigurationException;
|
8
|
import javax.xml.transform.TransformerFactoryConfigurationError;
|
9
|
|
10
|
import com.google.common.base.Function;
|
11
|
import com.google.common.collect.Iterables;
|
12
|
import com.google.common.collect.Lists;
|
13
|
import com.google.common.collect.Maps;
|
14
|
import com.google.common.collect.Sets;
|
15
|
import com.google.protobuf.InvalidProtocolBufferException;
|
16
|
import com.googlecode.protobuf.format.JsonFormat;
|
17
|
import com.googlecode.protobuf.format.JsonFormat.ParseException;
|
18
|
import eu.dnetlib.actionmanager.actions.ActionFactory;
|
19
|
import eu.dnetlib.actionmanager.actions.XsltInfoPackageAction;
|
20
|
import eu.dnetlib.actionmanager.common.Agent;
|
21
|
import eu.dnetlib.actionmanager.common.Operation;
|
22
|
import eu.dnetlib.actionmanager.common.Provenance;
|
23
|
import eu.dnetlib.data.mapreduce.hbase.index.config.*;
|
24
|
import eu.dnetlib.data.mapreduce.util.*;
|
25
|
import eu.dnetlib.data.proto.KindProtos.Kind;
|
26
|
import eu.dnetlib.data.proto.OafProtos.Oaf;
|
27
|
import eu.dnetlib.data.proto.TypeProtos.Type;
|
28
|
import eu.dnetlib.miscutils.functional.xml.IndentXmlString;
|
29
|
import org.apache.commons.io.IOUtils;
|
30
|
import org.apache.commons.lang.StringUtils;
|
31
|
import org.apache.commons.logging.Log;
|
32
|
import org.apache.commons.logging.LogFactory;
|
33
|
import org.dom4j.Document;
|
34
|
import org.dom4j.DocumentException;
|
35
|
import org.dom4j.io.SAXReader;
|
36
|
import org.json.JSONObject;
|
37
|
import org.junit.Before;
|
38
|
import org.junit.Ignore;
|
39
|
import org.junit.Test;
|
40
|
import org.springframework.core.io.ByteArrayResource;
|
41
|
import org.springframework.core.io.Resource;
|
42
|
|
43
|
import static org.junit.Assert.*;
|
44
|
|
45
|
public class XsltRowTransformerFactoryTest {
|
46
|
|
47
|
private static final Log log = LogFactory.getLog(XsltRowTransformerFactoryTest.class);
|
48
|
private static String basePathProfiles = "/eu/dnetlib/test/profiles/TransformationRuleDSResources/TransformationRuleDSResourceType/2hbase/";
|
49
|
private XsltRowTransformerFactory factory;
|
50
|
private EntityConfigTable entityConfigTable;
|
51
|
|
52
|
@Before
|
53
|
public void setUp() throws Exception {
|
54
|
factory = new XsltRowTransformerFactory();
|
55
|
entityConfigTable = IndexConfig.load(IndexConfigTest.config).getConfigMap();
|
56
|
}
|
57
|
|
58
|
@Test
|
59
|
@Ignore // need to reimplement because claimUpdates_2_hbase.xsl was removed
|
60
|
public void testParseOafClaimUpdate() throws Exception {
|
61
|
doTest(loadFromTransformationProfile("claimUpdates_2_hbase.xsl"), load("recordClaimUpdate.xml"));
|
62
|
}
|
63
|
|
64
|
@Test
|
65
|
@Ignore // need to reimplement because claimUpdates_2_hbase.xsl was removed
|
66
|
public void testParseClaimUpdate() throws Exception {
|
67
|
|
68
|
final List<Row> rows = Lists.newArrayList();
|
69
|
rows.addAll(asRows(loadFromTransformationProfile("oaf2hbase.xml"), load("recordClaim.xml")));
|
70
|
rows.addAll(asRows(loadFromTransformationProfile("claimUpdates_2_hbase.xsl"), load("recordClaimUpdate.xml")));
|
71
|
|
72
|
printAll(mapAll(buildTable(rows)));
|
73
|
}
|
74
|
|
75
|
@Test
|
76
|
public void testParseClaimRel() throws Exception {
|
77
|
|
78
|
doTest(loadFromTransformationProfile("claimRels_2_hbase.xml"), load("recordClaimRel.xml"));
|
79
|
}
|
80
|
|
81
|
|
82
|
@Test
|
83
|
public void testParseFp7IctPUB() throws Exception {
|
84
|
|
85
|
doTest(loadFromTransformationProfile("oaf2hbase.xml"), load("ec_fp7_ict.xml"));
|
86
|
}
|
87
|
|
88
|
@Test
|
89
|
public void testParseRecordCrossref() throws Exception {
|
90
|
|
91
|
doTest(loadFromTransformationProfile("oaf2hbase.xml"), load("recordCrossref.xml"));
|
92
|
}
|
93
|
|
94
|
@Test
|
95
|
public void testParseDatasetPUB() throws Exception {
|
96
|
|
97
|
doTest(loadFromTransformationProfile("odf2hbase.xml"), load("recordDatasetPUB.xml"));
|
98
|
}
|
99
|
|
100
|
@Test
|
101
|
public void testParseSoftwareEgiApp() throws Exception {
|
102
|
|
103
|
doTest(loadFromTransformationProfile("odf2hbase.xml"), load("softwareEgiApp.xml"));
|
104
|
}
|
105
|
|
106
|
@Test
|
107
|
public void testParseSoftwareEgiApp2() throws Exception {
|
108
|
|
109
|
doTest(loadFromTransformationProfile("odf2hbase.xml"), load("softwareEgiApp2.xml"));
|
110
|
}
|
111
|
|
112
|
@Test
|
113
|
public void testParseOrpEgiApp() throws Exception {
|
114
|
|
115
|
doTest(loadFromTransformationProfile("odf2hbase.xml"), load("orpEgiApp.xml"));
|
116
|
}
|
117
|
|
118
|
@Test
|
119
|
public void testParseSoftwareDOECODE() throws Exception {
|
120
|
|
121
|
doTest(loadFromTransformationProfile("odf2hbase.xml"), load("doecode.xml"));
|
122
|
}
|
123
|
|
124
|
|
125
|
@Test
|
126
|
public void testParseDatasetLindat() throws Exception {
|
127
|
|
128
|
doTest(loadFromTransformationProfile("odf2hbase.xml"), load("datasetLindat.xml"));
|
129
|
}
|
130
|
|
131
|
@Test
|
132
|
public void testParseDatasetNeuroVault() throws Exception {
|
133
|
|
134
|
doTest(loadFromTransformationProfile("odf2hbase.xml"), load("recordNeuroVault.xml"));
|
135
|
}
|
136
|
|
137
|
@Test
|
138
|
public void testParseDatasetNeuroVault2() throws Exception {
|
139
|
|
140
|
doTest(loadFromTransformationProfile("odf2hbase.xml"), load("recordNeuroVault2.xml"));
|
141
|
}
|
142
|
|
143
|
@Test
|
144
|
public void testParseClaim() throws Exception {
|
145
|
|
146
|
doTest(loadFromTransformationProfile("oaf2hbase.xml"), load("recordClaim.xml"));
|
147
|
}
|
148
|
|
149
|
@Test
|
150
|
public void testParseClaimDedup() throws Exception {
|
151
|
|
152
|
doTest(loadFromTransformationProfile("oaf2hbase.xml"), load("recordClaimedDedup.xml"));
|
153
|
}
|
154
|
|
155
|
|
156
|
@Test
|
157
|
public void testParseClaimDataset() throws Exception {
|
158
|
|
159
|
doTest(loadFromTransformationProfile("odf2hbase.xml"), load("recordClaimDataset.xml"));
|
160
|
}
|
161
|
|
162
|
|
163
|
@Test
|
164
|
public void testParseACM() throws Exception {
|
165
|
|
166
|
doTest(loadFromTransformationProfile("oaf2hbase.xml"), load("recordACM.xml"));
|
167
|
}
|
168
|
|
169
|
@Test
|
170
|
public void testParseASB() throws Exception {
|
171
|
|
172
|
doTest(loadFromTransformationProfile("oaf2hbase.xml"), load("recordASB.xml"));
|
173
|
}
|
174
|
|
175
|
@Test
|
176
|
public void testParseProjectCorda() throws Exception {
|
177
|
|
178
|
doTest(loadFromTransformationProfile("projects_2_hbase.xsl"), load("projectRecordCorda.xml"));
|
179
|
}
|
180
|
|
181
|
@Test
|
182
|
public void testParseProjectFCT() throws Exception {
|
183
|
|
184
|
doTest(loadFromTransformationProfile("projects_2_hbase.xsl"), load("projectRecordFCT.xml"));
|
185
|
}
|
186
|
|
187
|
|
188
|
@Test
|
189
|
public void testParseOaf() throws Exception {
|
190
|
|
191
|
doTest(loadFromTransformationProfile("oaf2hbase.xml"), load("record.xml"));
|
192
|
}
|
193
|
|
194
|
@Test
|
195
|
public void testParseOafPublication() throws Exception {
|
196
|
|
197
|
doTest(loadFromTransformationProfile("oaf_entity2hbase.xml"), load("record.xml"));
|
198
|
}
|
199
|
|
200
|
@Test
|
201
|
public void testParseLindat() throws Exception {
|
202
|
|
203
|
doTest(loadFromTransformationProfile("odf2hbase.xml"), load("recordLindat.xml"));
|
204
|
}
|
205
|
|
206
|
@Test
|
207
|
public void testParseDatacite() throws Exception {
|
208
|
|
209
|
doTest(loadFromTransformationProfile("odf2hbase.xml"), load("recordDatacite.xml"));
|
210
|
}
|
211
|
|
212
|
@Test
|
213
|
public void testParseDatacite2() throws Exception {
|
214
|
|
215
|
doTest(loadFromTransformationProfile("odf2hbase.xml"), load("recordDatacite2.xml"));
|
216
|
}
|
217
|
|
218
|
@Test
|
219
|
public void testParseDataciteNewES() throws Exception {
|
220
|
|
221
|
doTest(loadFromTransformationProfile("odf2hbase.xml"), load("dataciteNew.xml"));
|
222
|
}
|
223
|
|
224
|
@Test
|
225
|
public void testParseOpenTrials() throws Exception {
|
226
|
|
227
|
doTest(loadFromTransformationProfile("odf2hbase.xml"), load("opentrials_datacite1.xml"));
|
228
|
}
|
229
|
|
230
|
@Test
|
231
|
public void testLinkPangaea() throws Exception {
|
232
|
|
233
|
final List<Row> rows = Lists.newArrayList();
|
234
|
rows.addAll(asRows(loadFromTransformationProfile("odf2hbase.xml"), load("pangaeODF1.xml")));
|
235
|
rows.addAll(asRows(loadFromTransformationProfile("odf2hbase.xml"), load("pangaeODF2.xml")));
|
236
|
rows.addAll(asRows(loadFromTransformationProfile("oaf2hbase.xml"), load("pangaeOAF.xml")));
|
237
|
rows.addAll(asRows(loadFromTransformationProfile("projects_2_hbase.xsl"), load("projectRecordCordaPangaea.xml")));
|
238
|
|
239
|
printAll(mapAll(buildTable(rows)));
|
240
|
}
|
241
|
|
242
|
@Test
|
243
|
public void testODF() throws Exception {
|
244
|
|
245
|
final List<Row> rows = Lists.newArrayList();
|
246
|
rows.addAll(asRows(loadFromTransformationProfile("oaf2hbase.xml"), load("odf.xml")));
|
247
|
//printAll(mapAll(buildTable(rows)));
|
248
|
}
|
249
|
|
250
|
@Test
|
251
|
public void testPangaea() throws Exception {
|
252
|
|
253
|
final List<Row> rows = Lists.newArrayList();
|
254
|
rows.addAll(asRows(loadFromTransformationProfile("oaf2hbase.xml"), load("pangaeOAF2.xml")));
|
255
|
printAll(mapAll(buildTable(rows)));
|
256
|
}
|
257
|
@Test
|
258
|
public void testZenodo() throws Exception {
|
259
|
|
260
|
final List<Row> rows = Lists.newArrayList();
|
261
|
rows.addAll(asRows(loadFromTransformationProfile("odf2hbase.xml"), load("zenodoData.xml")));
|
262
|
printAll(mapAll(buildTable(rows)));
|
263
|
}
|
264
|
|
265
|
@Test
|
266
|
public void testZenodoSoftware() throws Exception {
|
267
|
|
268
|
final List<Row> rows = Lists.newArrayList();
|
269
|
rows.addAll(asRows(loadFromTransformationProfile("odf2hbase.xml"), load("softwareZenodo_odf.xml")));
|
270
|
printAll(mapAll(buildTable(rows)));
|
271
|
}
|
272
|
|
273
|
@Test
|
274
|
public void testLinkCorda() throws Exception {
|
275
|
|
276
|
final List<Row> rows = Lists.newArrayList();
|
277
|
rows.addAll(asRows(loadFromTransformationProfile("projects_2_hbase.xsl"), load("projectRecordCorda.xml")));
|
278
|
rows.addAll(asRows(loadFromTransformationProfile("oaf2hbase.xml"), load("recordCorda.xml")));
|
279
|
|
280
|
printAll(mapAll(buildTable(rows)));
|
281
|
}
|
282
|
|
283
|
@Test
|
284
|
public void testLinkFCT() throws Exception {
|
285
|
|
286
|
final List<Row> rows = Lists.newArrayList();
|
287
|
rows.addAll(asRows(loadFromTransformationProfile("projects_2_hbase.xsl"), load("projectRecordFCT.xml")));
|
288
|
rows.addAll(asRows(loadFromTransformationProfile("oaf2hbase.xml"), load("recordFCT.xml")));
|
289
|
|
290
|
printAll(mapAll(buildTable(rows)));
|
291
|
}
|
292
|
|
293
|
@Test
|
294
|
public void testLinkARC() throws Exception {
|
295
|
|
296
|
final List<Row> rows = Lists.newArrayList();
|
297
|
rows.addAll(asRows(loadFromTransformationProfile("projects_2_hbase.xsl"), load("projectRecordARC.xml")));
|
298
|
rows.addAll(asRows(loadFromTransformationProfile("oaf2hbase.xml"), load("recordARC.xml")));
|
299
|
|
300
|
printAll(mapAll(buildTable(rows)));
|
301
|
}
|
302
|
|
303
|
@Test
|
304
|
public void testLinkWT() throws Exception {
|
305
|
|
306
|
final List<Row> rows = Lists.newArrayList();
|
307
|
rows.addAll(asRows(loadFromTransformationProfile("projects_2_hbase.xsl"), load("projectRecordWT.xml")));
|
308
|
rows.addAll(asRows(loadFromTransformationProfile("oaf2hbase.xml"), load("recordWT.xml")));
|
309
|
|
310
|
printAll(mapAll(buildTable(rows)));
|
311
|
}
|
312
|
|
313
|
|
314
|
|
315
|
@Test
|
316
|
public void testLinkOrganization() throws Exception {
|
317
|
|
318
|
final List<Row> rows = Lists.newArrayList();
|
319
|
rows.addAll(asRows(loadFromTransformationProfile("organizations_2_hbase.xsl"), load("organization.xml")));
|
320
|
rows.addAll(asRows(loadFromTransformationProfile("projectorganization_2_hbase.xsl"), load("project_organization.xml")));
|
321
|
rows.addAll(asRows(loadFromTransformationProfile("projects_2_hbase.xsl"), load("projectRecordCorda.xml")));
|
322
|
|
323
|
printAll(mapAll(buildTable(rows)));
|
324
|
}
|
325
|
|
326
|
@Test
|
327
|
public void testLinkOrganizationAffiliation() throws Exception {
|
328
|
|
329
|
final List<Row> rows = Lists.newArrayList();
|
330
|
rows.addAll(asRows(loadFromTransformationProfile("organizations_2_hbase.xsl"), load("organization.xml")));
|
331
|
rows.addAll(asRows(loadFromTransformationProfile("resultorganization_2_hbase.xsl"), load("result_organization.xml")));
|
332
|
rows.addAll(asRows(loadFromTransformationProfile("oaf2hbase.xml"), load("record.xml")));
|
333
|
|
334
|
printAll(mapAll(buildTable(rows)));
|
335
|
}
|
336
|
|
337
|
@Test
|
338
|
public void testDuplicates() throws Exception {
|
339
|
final String mergeId = "50|dedup_wf_001::08ed625d07e5738b794ff14d6773fd9f";
|
340
|
final List<Row> rows = Lists.newArrayList();
|
341
|
|
342
|
final Function<Row, Row> f = rowIn -> {
|
343
|
|
344
|
final List<Column<String,byte[]>> cols = Lists.newArrayList();
|
345
|
for(Column<String,byte[]> col : rowIn.getColumns()) {
|
346
|
if (col.getName().equals("body")) {
|
347
|
cols.add(new Column(col.getName(), col.getValue()));
|
348
|
|
349
|
}
|
350
|
}
|
351
|
return new Row("result", rowIn.getKey(), cols);
|
352
|
};
|
353
|
|
354
|
final List<Row> puma1 = asRows(loadFromTransformationProfile("oaf2hbase.xml"), load("recordPuma1.xml"), f);
|
355
|
puma1.add(new Row("resultResult_dedup_isMergedIn", mergeId));
|
356
|
|
357
|
final List<Row> puma2 = asRows(loadFromTransformationProfile("oaf2hbase.xml"), load("recordPuma2.xml"), f);
|
358
|
puma2.add(new Row("resultResult_dedup_isMergedIn", mergeId));
|
359
|
|
360
|
rows.addAll(puma1);
|
361
|
rows.addAll(puma2);
|
362
|
|
363
|
List<Oaf> duplicates = Lists.newArrayList();
|
364
|
duplicates.add(getOafBody(puma1));
|
365
|
duplicates.add(getOafBody(puma2));
|
366
|
final Oaf.Builder oafMerge = OafEntityMerger.merge(mergeId, duplicates);
|
367
|
|
368
|
final Row mergeRow = new Row("result", mergeId, Lists.newArrayList(new Column("body", oafMerge.build().toByteArray())));
|
369
|
|
370
|
rows.add(mergeRow);
|
371
|
|
372
|
printAll(mapAll(buildTable(rows)));
|
373
|
}
|
374
|
|
375
|
private Oaf getOafBody(final List<Row> rows) throws InvalidProtocolBufferException {
|
376
|
for(Row row : rows) {
|
377
|
if(StringUtils.startsWith(row.getKey(), "50")) {
|
378
|
return Oaf.parseFrom(row.getColumn("body").getValue());
|
379
|
|
380
|
}
|
381
|
}
|
382
|
return null;
|
383
|
}
|
384
|
|
385
|
@Test
|
386
|
public void testParseDoajOAF() throws Exception {
|
387
|
|
388
|
doTest(loadFromTransformationProfile("oaf2hbase.xml"), load("doajUniversityRecord.xml"));
|
389
|
}
|
390
|
|
391
|
@Test
|
392
|
public void testParseDatasource() throws Exception {
|
393
|
|
394
|
doTest(loadFromTransformationProfile("datasources_2_hbase.xsl"), load("datasourceNative.xml"));
|
395
|
}
|
396
|
@Test
|
397
|
public void testParseDatasourcePiwik() throws Exception {
|
398
|
|
399
|
doTest(loadFromTransformationProfile("datasources_2_hbase.xsl"), load("datasourcePiwik.xml"));
|
400
|
}
|
401
|
|
402
|
@Test
|
403
|
public void testParseDataDatasource() throws Exception {
|
404
|
|
405
|
doTest(loadFromTransformationProfile("datasources_2_hbase.xsl"), load("dataDatasource.xml"));
|
406
|
}
|
407
|
|
408
|
@Test
|
409
|
public void testFromMongodbCompressedDump() throws Exception {
|
410
|
doTestJsonGz(loadFromTransformationProfile("oaf2hbase.xml"), load("mdstore_cleaned.json.gz"));
|
411
|
}
|
412
|
|
413
|
|
414
|
@Test
|
415
|
public void testLoadFromTransformationProfile() throws IOException {
|
416
|
InputStream in = loadFromTransformationProfile("oaf2hbase.xml");
|
417
|
log.info(IOUtils.toString(in));
|
418
|
}
|
419
|
|
420
|
@Test
|
421
|
public void test_template() throws Exception {
|
422
|
final String xslt = IOUtils.toString(loadFromTransformationProfile("oaf2hbase.xml"));
|
423
|
final XsltRowTransformer transformer = factory.getTransformer(xslt);
|
424
|
assertNotNull(transformer);
|
425
|
|
426
|
final String record = IOUtils.toString(load("record.xml"));
|
427
|
final List<Row> rows = transformer.apply(record);
|
428
|
|
429
|
System.out.println(rows);
|
430
|
}
|
431
|
|
432
|
@Test
|
433
|
public void testWrongCharsOrganization() throws Exception {
|
434
|
final List<Row> rows = Lists.newArrayList();
|
435
|
rows.addAll(asRows(loadFromTransformationProfile("organizations_2_hbase.xsl"), load("organizationWrongChars.xml")));
|
436
|
printAll(mapAll(buildTable(rows)));
|
437
|
}
|
438
|
|
439
|
@Test
|
440
|
public void testParseProjectWithFunderOriginalName() throws Exception {
|
441
|
|
442
|
doTest(loadFromTransformationProfile("projects_2_hbase.xsl"), load("projectWithFunderOriginalName.xml"));
|
443
|
}
|
444
|
@Test
|
445
|
public void testLinkFunderOriginalName() throws Exception {
|
446
|
|
447
|
final List<Row> rows = Lists.newArrayList();
|
448
|
rows.addAll(asRows(loadFromTransformationProfile("projects_2_hbase.xsl"), load("projectWithFunderOriginalName.xml")));
|
449
|
rows.addAll(asRows(loadFromTransformationProfile("oaf2hbase.xml"), load("recordFunderOriginalName.xml")));
|
450
|
|
451
|
printAll(mapAll(buildTable(rows)));
|
452
|
}
|
453
|
|
454
|
@Test
|
455
|
public void testProjectExtraInfo() throws Exception {
|
456
|
final List<Row> rows = Lists.newArrayList();
|
457
|
rows.addAll(asRows(loadFromTransformationProfile("projects_2_hbase.xsl"), load("projectRecordExtraInfo.xml")));
|
458
|
printAll(mapAll(buildTable(rows)));
|
459
|
}
|
460
|
|
461
|
@Test
|
462
|
public void testParseSoftwareFromODF() throws Exception {
|
463
|
final List<Row> rows = Lists.newArrayList();
|
464
|
rows.addAll(asRows(loadFromTransformationProfile("odf2hbase.xml"), load("softwareODF.xml")));
|
465
|
rows.addAll(asRows(loadFromTransformationProfile("projects_2_hbase.xsl"), load("projectRecordCorda.xml")));
|
466
|
printAll(mapAll(buildTable(rows)));
|
467
|
}
|
468
|
|
469
|
@Test
|
470
|
public void testParseSoftwareFromOAF() throws Exception {
|
471
|
final List<Row> rows = Lists.newArrayList();
|
472
|
rows.addAll(asRows(loadFromTransformationProfile("oaf2hbase.xml"), load("recordOAFsoftwareCLOSED.xml")));
|
473
|
printAll(mapAll(buildTable(rows)));
|
474
|
}
|
475
|
|
476
|
@Test
|
477
|
public void testParsePubFromODF() throws Exception {
|
478
|
final List<Row> rows = Lists.newArrayList();
|
479
|
rows.addAll(asRows(loadFromTransformationProfile("odf2hbase.xml"), load("recordODFPub.xml")));
|
480
|
printAll(mapAll(buildTable(rows)));
|
481
|
}
|
482
|
|
483
|
@Test
|
484
|
public void testParseSoftwareFromOAFOpen() throws Exception {
|
485
|
final List<Row> rows = Lists.newArrayList();
|
486
|
rows.addAll(asRows(loadFromTransformationProfile("oaf2hbase.xml"), load("recordOAFsoftwareOPEN.xml")));
|
487
|
printAll(mapAll(buildTable(rows)));
|
488
|
}
|
489
|
|
490
|
@Test
|
491
|
public void testParseSoftwareBiotool() throws Exception {
|
492
|
final List<Row> rows = Lists.newArrayList();
|
493
|
rows.addAll(asRows(loadFromTransformationProfile("odf2hbase.xml"), load("biotoolSw.xml")));
|
494
|
printAll(mapAll(buildTable(rows)));
|
495
|
}
|
496
|
|
497
|
@Test
|
498
|
public void testParseOafWithExternalRef() throws Exception {
|
499
|
final List<Row> rows = Lists.newArrayList();
|
500
|
rows.addAll(asRows(loadFromTransformationProfile("oaf2hbase.xml"), load("oafWithExternalReference.xml")));
|
501
|
printAll(mapAll(buildTable(rows)));
|
502
|
}
|
503
|
|
504
|
@Test
|
505
|
public void testParseOafWithCommunity() throws Exception {
|
506
|
final List<Row> rows = Lists.newArrayList();
|
507
|
rows.addAll(asRows(loadFromTransformationProfile("oaf2hbase.xml"), load("oafWithCommunity.xml")));
|
508
|
printAll(mapAll(buildTable(rows)));
|
509
|
}
|
510
|
|
511
|
@Test
|
512
|
public void testParseOafWithUpdates() throws Exception {
|
513
|
final List<Row> rows = Lists.newArrayList();
|
514
|
rows.addAll(asRows(loadFromTransformationProfile("oaf2hbase.xml"), load("oafWithCommunity.xml")));
|
515
|
|
516
|
ActionFactory actionFactory = new ActionFactory();
|
517
|
|
518
|
Map<String, Resource> xslts = Maps.newHashMap();
|
519
|
|
520
|
xslts.put("oaf2hbase", new ByteArrayResource(IOUtils.toString(loadFromTransformationProfile("oaf2hbase.xml")).getBytes()));
|
521
|
actionFactory.setXslts(xslts);
|
522
|
|
523
|
XsltInfoPackageAction pa = actionFactory.generateInfoPackageAction(
|
524
|
"oaf2hbase",
|
525
|
"rawset-id",
|
526
|
new Agent("agent-id", "agent-name", Agent.AGENT_TYPE.algo),
|
527
|
Operation.UPDATE,
|
528
|
IOUtils.toString(load("oafUpdateWithCommunity.xml")),
|
529
|
Provenance.sysimport_mining_aggregator,
|
530
|
"0.9");
|
531
|
|
532
|
final String qualifier = "update_" + System.nanoTime();
|
533
|
|
534
|
IOUtils.readLines(load("country_updates.json")).forEach(line -> {
|
535
|
|
536
|
Oaf.Builder oaf = Oaf.newBuilder();
|
537
|
|
538
|
try {
|
539
|
JsonFormat.merge(line, oaf);
|
540
|
} catch (JsonFormat.ParseException e) {
|
541
|
throw new IllegalArgumentException(e);
|
542
|
}
|
543
|
|
544
|
Column<String, byte[]> col = new Column<>("update_" + System.nanoTime(), oaf.build().toByteArray());
|
545
|
rows.add(new Row("result", oaf.getEntity().getId(), Lists.newArrayList(col)));
|
546
|
});
|
547
|
|
548
|
pa.asAtomicActions().forEach(a -> {
|
549
|
Column<String, byte[]> col = new Column<>("update_" + System.nanoTime(), a.getTargetValue());
|
550
|
rows.add(new Row(a.getTargetColumnFamily(), a.getTargetRowKey(), Lists.newArrayList(col)));
|
551
|
});
|
552
|
|
553
|
|
554
|
/*
|
555
|
rows.forEach(r -> {
|
556
|
log.info(r);
|
557
|
});
|
558
|
*/
|
559
|
|
560
|
mapAll(buildTable(rows)).entrySet().forEach(b -> {
|
561
|
log.info(b.getKey());
|
562
|
log.info(b.getValue());
|
563
|
});
|
564
|
}
|
565
|
|
566
|
@Test
|
567
|
public void testParseCrisPub() throws Exception {
|
568
|
doTest(loadFromTransformationProfile("odf2hbase.xml"), load("cris_pub1.xml"));
|
569
|
}
|
570
|
|
571
|
@Test
|
572
|
public void testBioToolSwRowJson() throws Exception {
|
573
|
doTestJsonRow(IOUtils.toString(load("biotoolSwRow.json")));
|
574
|
}
|
575
|
|
576
|
@Test
|
577
|
public void testParseVirta() throws Exception {
|
578
|
doTest(loadFromTransformationProfile("datasources_2_hbase.xsl"), load("virta.xml"));
|
579
|
}
|
580
|
|
581
|
@Test
|
582
|
public void testParseJournal() throws Exception {
|
583
|
doTest(loadFromTransformationProfile("datasources_2_hbase.xsl"), load("datasourceWithISSN.xml"));
|
584
|
}
|
585
|
|
586
|
@Test
|
587
|
public void testGuidelines4Qeios() throws Exception {
|
588
|
|
589
|
doTest(loadFromTransformationProfile("odf2hbase.xml"), load("guidelines4_qeios1.xml"));
|
590
|
}
|
591
|
|
592
|
@Test
|
593
|
public void testGuidelines4Aria() throws Exception {
|
594
|
|
595
|
doTest(loadFromTransformationProfile("odf2hbase.xml"), load("guidelines4_aria.xml"));
|
596
|
}
|
597
|
|
598
|
@Test
|
599
|
public void testJournalRecord() throws Exception {
|
600
|
doTest(loadFromTransformationProfile("oaf2hbase.xml"), load("journalRecord.xml"));
|
601
|
}
|
602
|
|
603
|
private void doTestJsonRow(final String json) throws Exception {
|
604
|
Row row = asRowFromJson(json);
|
605
|
log.info(row);
|
606
|
List<Row> rows = new ArrayList<>();
|
607
|
rows.add(row);
|
608
|
final Map<String, Map<String, Map<String, byte[]>>> table = buildTable(rows);
|
609
|
final Map<String, XmlRecordFactory> builders = mapAll(table);
|
610
|
printAll(builders);
|
611
|
|
612
|
}
|
613
|
|
614
|
private void doTest(final InputStream xsltStream, final InputStream recordStream) throws Exception {
|
615
|
try {
|
616
|
final List<Row> rows = asRows(xsltStream, recordStream);
|
617
|
|
618
|
log.info(rows);
|
619
|
|
620
|
final Map<String, Map<String, Map<String, byte[]>>> table = buildTable(rows);
|
621
|
|
622
|
// System.out.println("\n" + table.toString());
|
623
|
|
624
|
final Map<String, XmlRecordFactory> builders = mapAll(table);
|
625
|
|
626
|
printAll(builders);
|
627
|
} catch (final InvalidProtocolBufferException e) {
|
628
|
throw new Exception(e);
|
629
|
} catch (final TransformerConfigurationException e) {
|
630
|
throw new Exception(e);
|
631
|
} catch (final TransformerFactoryConfigurationError e) {
|
632
|
throw new Exception(e);
|
633
|
} catch (final DocumentException e) {
|
634
|
throw new Exception(e);
|
635
|
}
|
636
|
}
|
637
|
|
638
|
private void doTestJsonGz(final InputStream xsltStream, final InputStream recordStream) throws Exception {
|
639
|
|
640
|
final Iterator<List<Row>> rowsIterator = asRowsJsonGzip(xsltStream, recordStream);
|
641
|
|
642
|
int i = 0;
|
643
|
while (rowsIterator.hasNext()) {
|
644
|
final List<Row> rows = rowsIterator.next();
|
645
|
i++;
|
646
|
|
647
|
if ((i % 10000) == 0) {
|
648
|
System.out.println(i);
|
649
|
}
|
650
|
|
651
|
final Map<String, Map<String, Map<String, byte[]>>> table = buildTableDoaj(rows);
|
652
|
|
653
|
for (final Map<String, Map<String, byte[]>> m : table.values()) {
|
654
|
for (final Map<String, byte[]> mv : m.values()) {
|
655
|
for (final byte[] v : mv.values()) {
|
656
|
final OafDecoder d = OafDecoder.decode(v);
|
657
|
assertNotNull(d);
|
658
|
assertNotNull(d.getOaf());
|
659
|
|
660
|
switch (d.getKind()) {
|
661
|
case entity:
|
662
|
assertNotNull(d.getMetadata());
|
663
|
if (d.getOaf().getEntity().getType().equals(Type.result)) {
|
664
|
System.out.println(d.getOaf());
|
665
|
}
|
666
|
break;
|
667
|
case relation:
|
668
|
assertNotNull(d.getRel());
|
669
|
break;
|
670
|
default:
|
671
|
break;
|
672
|
}
|
673
|
}
|
674
|
}
|
675
|
}
|
676
|
}
|
677
|
}
|
678
|
|
679
|
private List<Row> asRows(final InputStream xsltStream, final InputStream recordStream, final Function<Row, Row> p) throws Exception {
|
680
|
return asRows(xsltStream, new HashMap<>(), recordStream, p);
|
681
|
}
|
682
|
|
683
|
private List<Row> asRows(final InputStream xsltStream, final InputStream recordStream) throws Exception {
|
684
|
return asRows(xsltStream, new HashMap<>(), recordStream);
|
685
|
}
|
686
|
|
687
|
private List<Row> asRows(final InputStream xsltStream, final Map<String, Object> params, final InputStream recordStream) throws Exception {
|
688
|
return asRows(xsltStream, params, recordStream, null);
|
689
|
}
|
690
|
|
691
|
private List<Row> asRows(final InputStream xsltStream, final Map<String, Object> params, final InputStream recordStream, final Function<Row, Row> p) throws Exception {
|
692
|
final String xslt = IOUtils.toString(xsltStream);
|
693
|
final XsltRowTransformer transformer = factory.getTransformer(xslt, params);
|
694
|
assertNotNull(transformer);
|
695
|
|
696
|
final String record = IOUtils.toString(recordStream);
|
697
|
final List<Row> rows = transformer.apply(record);
|
698
|
|
699
|
assertNotNull(rows);
|
700
|
assertFalse(rows.isEmpty());
|
701
|
return p == null ? rows : Lists.newArrayList(Iterables.transform(rows, p));
|
702
|
}
|
703
|
|
704
|
private Iterator<List<Row>> asRowsJsonGzip(final InputStream xsltStream, final InputStream recordStreamJsonGzip) throws Exception {
|
705
|
final String xslt = IOUtils.toString(xsltStream);
|
706
|
final XsltRowTransformer transformer = factory.getTransformer(xslt);
|
707
|
assertNotNull(transformer);
|
708
|
assertNotNull(recordStreamJsonGzip);
|
709
|
|
710
|
final GZIPInputStream stream = new GZIPInputStream(recordStreamJsonGzip);
|
711
|
assertNotNull(stream);
|
712
|
final BufferedReader inStream = new BufferedReader(new InputStreamReader(stream));
|
713
|
assertNotNull(inStream);
|
714
|
return new Iterator<List<Row>>() {
|
715
|
|
716
|
String jsonRecord = null;
|
717
|
|
718
|
@Override
|
719
|
public boolean hasNext() {
|
720
|
try {
|
721
|
return (jsonRecord = inStream.readLine()) != null;
|
722
|
} catch (final IOException e) {
|
723
|
throw new RuntimeException(e);
|
724
|
}
|
725
|
}
|
726
|
|
727
|
@Override
|
728
|
public List<Row> next() {
|
729
|
|
730
|
final JSONObject jsonObj = new JSONObject(jsonRecord);
|
731
|
final String body = jsonObj.getString("body");
|
732
|
try {
|
733
|
assertTrue(StringUtils.isNotBlank(body));
|
734
|
// System.out.println(body);
|
735
|
final List<Row> rows = transformer.apply(body);
|
736
|
assertNotNull(rows);
|
737
|
assertFalse(rows.isEmpty());
|
738
|
return rows;
|
739
|
} catch (final Throwable e) {
|
740
|
System.err.println("error transforming document: " + body);
|
741
|
throw new RuntimeException(e);
|
742
|
}
|
743
|
}
|
744
|
|
745
|
@Override
|
746
|
public void remove() {
|
747
|
throw new UnsupportedOperationException();
|
748
|
}
|
749
|
|
750
|
};
|
751
|
|
752
|
}
|
753
|
|
754
|
private Map<String, Map<String, Map<String, byte[]>>> buildTableDoaj(final List<Row> rows) throws UnsupportedEncodingException {
|
755
|
final Map<String, Map<String, Map<String, byte[]>>> table = Maps.newHashMap();
|
756
|
|
757
|
for (final Row row : rows) {
|
758
|
final String rowKey = row.getKey();
|
759
|
final String cf = row.getColumnFamily();
|
760
|
if (!table.containsKey(rowKey)) {
|
761
|
table.put(rowKey, new HashMap<>());
|
762
|
}
|
763
|
if (!table.get(rowKey).containsKey(cf)) {
|
764
|
table.get(rowKey).put(row.getColumnFamily(), new HashMap<>());
|
765
|
}
|
766
|
for (final Column<String, byte[]> c : row.getColumns()) {
|
767
|
// System.out.println(String.format("ADDING K:%s CF:%s Q:%s", rowKey, cf, c.getName()));
|
768
|
table.get(rowKey).get(cf).put(c.getName(), c.getValue());
|
769
|
if (cf.equals("result") && c.getName().equals("body")) {
|
770
|
// System.out.println(String.format("ADDING K:%s CF:%s Q:%s", rowKey, cf, c.getName()));
|
771
|
assertTrue(StringUtils.isNotBlank(new String(c.getValue(), "UTF-8")));
|
772
|
}
|
773
|
}
|
774
|
}
|
775
|
return table;
|
776
|
|
777
|
}
|
778
|
|
779
|
protected Map<String, Map<String, Map<String, byte[]>>> buildTable(final List<Row> rows) throws UnsupportedEncodingException {
|
780
|
final Map<String, Map<String, Map<String, byte[]>>> table = Maps.newHashMap();
|
781
|
|
782
|
for (final Row row : rows) {
|
783
|
final String rowKey = row.getKey();
|
784
|
final String cf = row.getColumnFamily();
|
785
|
if (!table.containsKey(rowKey)) {
|
786
|
table.put(rowKey, new HashMap<>());
|
787
|
}
|
788
|
if (!table.get(rowKey).containsKey(cf)) {
|
789
|
table.get(rowKey).put(row.getColumnFamily(), new HashMap<>());
|
790
|
}
|
791
|
for (final Column<String, byte[]> c : row.getColumns()) {
|
792
|
System.out.println(String.format("ADDING K:%s CF:%s Q:%s", rowKey, cf, c.getName()));
|
793
|
table.get(rowKey).get(cf).put(c.getName(), c.getValue());
|
794
|
if (c.getName().equals("body")) {
|
795
|
final String theBody = new String(c.getValue(), "UTF-8");
|
796
|
assertTrue(StringUtils.isNotBlank(theBody));
|
797
|
//System.out.println(theBody);
|
798
|
}
|
799
|
}
|
800
|
}
|
801
|
return table;
|
802
|
|
803
|
}
|
804
|
|
805
|
protected Map<String, XmlRecordFactory> mapAll(final Map<String, Map<String, Map<String, byte[]>>> table) throws Exception {
|
806
|
|
807
|
final Map<String, XmlRecordFactory> builders = Maps.newHashMap();
|
808
|
for (final Entry<String, Map<String, Map<String, byte[]>>> e : table.entrySet()) {
|
809
|
map(builders, e.getKey(), e.getValue());
|
810
|
}
|
811
|
return builders;
|
812
|
}
|
813
|
|
814
|
// private Map<String, XmlRecordFactory> mapResultsOnly(final Map<String, Map<String, Map<String, byte[]>>> table) throws Exception {
|
815
|
//
|
816
|
// final Map<String, XmlRecordFactory> builders = Maps.newHashMap();
|
817
|
// for (final Entry<String, Map<String, Map<String, byte[]>>> e : table.entrySet()) {
|
818
|
// final Type type = OafRowKeyDecoder.decode(e.getKey()).getType();
|
819
|
// if (type == Type.result) {
|
820
|
// map(builders, e.getKey(), e.getValue());
|
821
|
// }
|
822
|
// }
|
823
|
// return builders;
|
824
|
// }
|
825
|
|
826
|
private void map(final Map<String, XmlRecordFactory> builders, final String rowKey, final Map<String, Map<String, byte[]>> row) throws Exception {
|
827
|
|
828
|
final Type type = OafRowKeyDecoder.decode(rowKey).getType();
|
829
|
|
830
|
final Map<String, byte[]> familyMap = row.get(type.toString());
|
831
|
|
832
|
if (familyMap == null) return;
|
833
|
|
834
|
final byte[] bodyB = familyMap.get("body");
|
835
|
|
836
|
if (bodyB != null) {
|
837
|
ensureBuilder(builders, rowKey);
|
838
|
|
839
|
final Oaf oaf = UpdateMerger.mergeBodyUpdates(familyMap);
|
840
|
|
841
|
final OafDecoder mainEntity = OafDecoder.decode(oaf);
|
842
|
|
843
|
builders.get(rowKey).setMainEntity(mainEntity);
|
844
|
|
845
|
for (final LinkDescriptor ld : entityConfigTable.getDescriptors(type)) {
|
846
|
|
847
|
final String it = ld.getRelDescriptor().getIt();
|
848
|
final Map<String, byte[]> cols = row.get(it);
|
849
|
|
850
|
if ((cols != null) && !cols.isEmpty()) {
|
851
|
|
852
|
for (final byte[] oafB : cols.values()) {
|
853
|
|
854
|
final Oaf.Builder relBuilder = Oaf.newBuilder(Oaf.parseFrom(oafB));
|
855
|
|
856
|
if (ld.isSymmetric()) {
|
857
|
final RelDescriptor rd = ld.getRelDescriptor();
|
858
|
|
859
|
relBuilder.getRelBuilder().setCachedTarget(mainEntity.getEntity()).setRelType(rd.getRelType()).setSubRelType(rd.getSubRelType());
|
860
|
}
|
861
|
|
862
|
relBuilder.getRelBuilder().setChild(ld.isChild());
|
863
|
|
864
|
final Oaf.Builder oafBuilder = Oaf.newBuilder().setKind(Kind.relation).setLastupdatetimestamp(System.currentTimeMillis());
|
865
|
oafBuilder.mergeFrom(relBuilder.build());
|
866
|
|
867
|
final String targetId = ld.isSymmetric() ? oafBuilder.getRel().getTarget() : oafBuilder.getRel().getSource();
|
868
|
ensureBuilder(builders, targetId);
|
869
|
final OafDecoder decoder = OafDecoder.decode(oafBuilder.build());
|
870
|
|
871
|
if (ld.isChild()) {
|
872
|
builders.get(targetId).addChild(type, decoder);
|
873
|
} else {
|
874
|
builders.get(targetId).addRelation(type, decoder);
|
875
|
}
|
876
|
}
|
877
|
|
878
|
}
|
879
|
}
|
880
|
}
|
881
|
|
882
|
}
|
883
|
|
884
|
private void printAll(final Map<String, XmlRecordFactory> builders) throws DocumentException {
|
885
|
print(Sets.newHashSet(Type.values()), builders, null);
|
886
|
}
|
887
|
|
888
|
private void print(final Set<Type> types, final Map<String, XmlRecordFactory> builders, final Map<Type, Set<String>> xpaths) throws DocumentException {
|
889
|
final SAXReader r = new SAXReader();
|
890
|
|
891
|
for (final Entry<String, XmlRecordFactory> e : builders.entrySet()) {
|
892
|
final OafRowKeyDecoder kd = OafRowKeyDecoder.decode(e.getKey());
|
893
|
|
894
|
if (!e.getValue().isValid()) throw new IllegalArgumentException("invalid builder: " + e.getKey());
|
895
|
if (types.contains(kd.getType())) {
|
896
|
final String val = IndentXmlString.apply(e.getValue().build());
|
897
|
|
898
|
if ((xpaths != null) && !xpaths.isEmpty() && (xpaths.get(kd.getType()) != null)) {
|
899
|
final Document doc = r.read(new StringReader(val));
|
900
|
|
901
|
log.debug("\n" + e.getKey());
|
902
|
for (final String xpath : xpaths.get(kd.getType())) {
|
903
|
log.debug(doc.valueOf(xpath));
|
904
|
}
|
905
|
} else {
|
906
|
log.info(val);
|
907
|
}
|
908
|
}
|
909
|
}
|
910
|
}
|
911
|
|
912
|
private void printNoIndent(final Map<String, XmlRecordFactory> builders) {
|
913
|
for (final Entry<String, XmlRecordFactory> e : builders.entrySet()) {
|
914
|
if (e.getValue().isValid()) {
|
915
|
log.debug(e.getValue().build());
|
916
|
} else {
|
917
|
log.debug("invalid builder: " + e.getKey());
|
918
|
}
|
919
|
}
|
920
|
}
|
921
|
|
922
|
private void ensureBuilder(final Map<String, XmlRecordFactory> builders, final String rowKey) throws Exception {
|
923
|
if (!builders.containsKey(rowKey)) {
|
924
|
builders.put(rowKey, newBuilder());
|
925
|
}
|
926
|
}
|
927
|
|
928
|
private XmlRecordFactory newBuilder() throws TransformerConfigurationException, TransformerFactoryConfigurationError, DocumentException {
|
929
|
return new XmlRecordFactory(entityConfigTable, ContextMapper.fromXml(Context.xml),
|
930
|
RelClasses.fromJSon(RelClassesTest.relClassesJson), XmlRecordFactoryTest.SCHEMA_LOCATION, true, false, false, XmlRecordFactoryTest.specialDatasourceTypes);
|
931
|
}
|
932
|
|
933
|
private InputStream load(final String fileName) {
|
934
|
return getClass().getResourceAsStream(fileName);
|
935
|
}
|
936
|
|
937
|
private InputStream loadFromTransformationProfile(final String profilePath) {
|
938
|
log.info("Loading xslt from: " + basePathProfiles + profilePath);
|
939
|
InputStream profile = getClass().getResourceAsStream(basePathProfiles + profilePath);
|
940
|
SAXReader saxReader = new SAXReader();
|
941
|
Document doc = null;
|
942
|
try {
|
943
|
doc = saxReader.read(profile);
|
944
|
} catch (DocumentException e) {
|
945
|
e.printStackTrace();
|
946
|
throw new RuntimeException(e);
|
947
|
}
|
948
|
String xslt = doc.selectSingleNode("//SCRIPT/CODE/*[local-name()='stylesheet']").asXML();
|
949
|
//log.info(xslt);
|
950
|
return IOUtils.toInputStream(xslt);
|
951
|
}
|
952
|
|
953
|
|
954
|
private Row asRowFromJson(String json) throws ParseException {
|
955
|
Oaf.Builder oafBuilder = Oaf.newBuilder();
|
956
|
JsonFormat.merge(json, oafBuilder);
|
957
|
final Oaf oaf = oafBuilder.build();
|
958
|
Row row = new Row("result", oaf.getEntity().getId());
|
959
|
Column<String, byte[]> c = new Column<>("body", oaf.toByteArray());
|
960
|
row.setColumn("body", c);
|
961
|
return row;
|
962
|
|
963
|
}
|
964
|
|
965
|
}
|