1
|
package eu.dnetlib.data.mapreduce.hbase.broker;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.io.InputStream;
|
5
|
import java.util.List;
|
6
|
import java.util.Map;
|
7
|
import java.util.Set;
|
8
|
|
9
|
import com.google.common.collect.Maps;
|
10
|
import com.google.common.collect.Sets;
|
11
|
import com.googlecode.protobuf.format.JsonFormat;
|
12
|
import eu.dnetlib.data.proto.OafProtos;
|
13
|
import eu.dnetlib.data.proto.OafProtos.Oaf;
|
14
|
import eu.dnetlib.data.transform.Column;
|
15
|
import eu.dnetlib.data.transform.Row;
|
16
|
import eu.dnetlib.data.transform.XsltRowTransformer;
|
17
|
import eu.dnetlib.data.transform.XsltRowTransformerFactory;
|
18
|
import org.apache.commons.io.IOUtils;
|
19
|
import org.apache.commons.logging.Log;
|
20
|
import org.apache.commons.logging.LogFactory;
|
21
|
import org.apache.hadoop.mapreduce.Counter;
|
22
|
import org.apache.hadoop.mapreduce.Reducer.Context;
|
23
|
import org.dom4j.Document;
|
24
|
import org.dom4j.DocumentException;
|
25
|
import org.dom4j.io.SAXReader;
|
26
|
import org.junit.Before;
|
27
|
import org.junit.Ignore;
|
28
|
import org.junit.Test;
|
29
|
import org.junit.runner.RunWith;
|
30
|
import org.mockito.Mock;
|
31
|
import org.mockito.junit.MockitoJUnitRunner;
|
32
|
|
33
|
import static org.junit.Assert.*;
|
34
|
import static org.mockito.ArgumentMatchers.anyString;
|
35
|
import static org.mockito.Mockito.when;
|
36
|
|
37
|
/**
|
38
|
* Created by claudio on 22/07/16.
|
39
|
*/
|
40
|
@RunWith(MockitoJUnitRunner.Silent.class)
|
41
|
public class EventWrapperTest {
|
42
|
|
43
|
private static final Log log = LogFactory.getLog(EventWrapperTest.class);
|
44
|
|
45
|
private Oaf oaf1_1;
|
46
|
private Oaf oaf1_2;
|
47
|
private Oaf oaf2_1;
|
48
|
private Oaf oaf2_2;
|
49
|
private Oaf oaf3_1;
|
50
|
private Oaf oaf3_2;
|
51
|
|
52
|
private String xslt;
|
53
|
|
54
|
@Mock
|
55
|
private Context context;
|
56
|
|
57
|
@Mock
|
58
|
private Counter counter;
|
59
|
|
60
|
private Map<String, String> baseUrlMap = Maps.newHashMap();
|
61
|
|
62
|
private static final String basePathProfiles = "/eu/dnetlib/test/profiles/TransformationRuleDSResources/TransformationRuleDSResourceType/2hbase/";
|
63
|
|
64
|
@Before
|
65
|
public void setUp() throws Exception {
|
66
|
xslt = loadFromTransformationProfile("oaf2hbase.xml");
|
67
|
oaf1_1 = asOaf("recordOaf_1_1.xml");
|
68
|
oaf1_2 = asOaf("recordOaf_1_2.xml");
|
69
|
oaf2_1 = asOaf("recordOaf_2_1.xml");
|
70
|
oaf2_2 = asOaf("recordOaf_2_2.xml");
|
71
|
oaf3_1 = asOaf("recordOaf_3_1.xml");
|
72
|
oaf3_2 = asOaf("recordOaf_3_2.xml");
|
73
|
|
74
|
when(context.getCounter(anyString(), anyString())).thenReturn(counter);
|
75
|
|
76
|
baseUrlMap.put("publication", "https://explore.openaire.eu/search/publication?articleId=%s");
|
77
|
baseUrlMap.put("dataset", "https://explore.openaire.eu/search/dataset?datasetId=%s");
|
78
|
baseUrlMap.put("software", "https://explore.openaire.eu/search/software?softwareId=%s");
|
79
|
baseUrlMap.put("other", "https://explore.openaire.eu/search/other?orpId=%s");
|
80
|
}
|
81
|
|
82
|
@Test
|
83
|
public void testAbstractEvent() throws Exception {
|
84
|
final String id1 = AbstractEventFactory.process(oaf1_1, oaf1_2, 1.0f).get(0).asBrokerEvent().getEventId();
|
85
|
final String id2 = AbstractEventFactory.process(oaf2_1, oaf2_2, 1.0f).get(0).asBrokerEvent().getEventId();
|
86
|
final String id3 = AbstractEventFactory.process(oaf3_1, oaf3_2, 1.0f).get(0).asBrokerEvent().getEventId();
|
87
|
|
88
|
verifyEquals(id1, id2);
|
89
|
verifyDifferent(id1, id3);
|
90
|
}
|
91
|
|
92
|
@Test
|
93
|
public void testOAVersionEvent() throws Exception {
|
94
|
final Set<String> untrustedOaDs = Sets.newHashSet();
|
95
|
final String id1 = OAVersionEventFactory.process(oaf1_1, oaf1_2, 1.0f, untrustedOaDs).get(0).asBrokerEvent().getEventId();
|
96
|
final String id2 = OAVersionEventFactory.process(oaf2_1, oaf2_2, 1.0f, untrustedOaDs).get(0).asBrokerEvent().getEventId();
|
97
|
|
98
|
verifyEquals(id1, id2);
|
99
|
}
|
100
|
|
101
|
@Test
|
102
|
public void testPIDEvent() throws Exception {
|
103
|
final String id1 = PIDEventFactory.process(oaf1_1, oaf1_2, 1.0f).get(0).asBrokerEvent().getEventId();
|
104
|
final String id2 = PIDEventFactory.process(oaf2_1, oaf2_2, 1.0f).get(0).asBrokerEvent().getEventId();
|
105
|
final String id3 = PIDEventFactory.process(oaf3_1, oaf3_2, 1.0f).get(0).asBrokerEvent().getEventId();
|
106
|
|
107
|
verifyEquals(id1, id2);
|
108
|
verifyDifferent(id1, id3);
|
109
|
}
|
110
|
|
111
|
@Test
|
112
|
//@Ignore
|
113
|
public void testProjectEvent() throws Exception {
|
114
|
|
115
|
Oaf oaf1_1_c = enrichWithCachedOafRel(oaf1_1, "oafRel_isProducedBy_1.json", "project1.json");
|
116
|
Oaf oaf1_2_c = enrichWithCachedOafRel(oaf1_2, "oafRel_isProducedBy_2.json", "project2.json");
|
117
|
|
118
|
final String id1 = ProjectEventFactory.process(context, oaf1_1_c, oaf1_2_c, 1.0f, baseUrlMap).get(0).asBrokerEvent().getEventId();
|
119
|
/*final String id2 = ProjectEventFactory.process(context, oaf2_1, oaf2_2, 1.0f, baseUrlMap).get(0).asBrokerEvent().getEventId();
|
120
|
final String id3 = ProjectEventFactory.process(context, oaf3_1, oaf3_2, 1.0f, baseUrlMap).get(0).asBrokerEvent().getEventId();
|
121
|
|
122
|
verifyEquals(id1, id2);
|
123
|
verifyDifferent(id1, id3);
|
124
|
*/
|
125
|
}
|
126
|
|
127
|
private Oaf enrichWithCachedOafRel(final Oaf oaf, final String oafRel, final String oafProject) throws IOException {
|
128
|
final Oaf.Builder cachedRel = Oaf.newBuilder();
|
129
|
JsonFormat.merge(IOUtils.toString(getClass().getResourceAsStream(oafRel)), cachedRel);
|
130
|
|
131
|
final Oaf.Builder cachedTarget = Oaf.newBuilder();
|
132
|
JsonFormat.merge(IOUtils.toString(getClass().getResourceAsStream(oafProject)), cachedTarget);
|
133
|
|
134
|
cachedRel.getRelBuilder().setCachedOafTarget(cachedTarget);
|
135
|
|
136
|
Oaf.Builder builder = Oaf.newBuilder(oaf);
|
137
|
builder.getEntityBuilder().addCachedOafRel(cachedRel);
|
138
|
return builder.build();
|
139
|
}
|
140
|
|
141
|
@Test
|
142
|
public void testPublicationDateEvent() throws Exception {
|
143
|
final String id1 = PublicationDateEventFactory.process(oaf1_1, oaf1_2, 1.0f).get(0).asBrokerEvent().getEventId();
|
144
|
final String id2 = PublicationDateEventFactory.process(oaf2_1, oaf2_2, 1.0f).get(0).asBrokerEvent().getEventId();
|
145
|
final String id3 = PublicationDateEventFactory.process(oaf3_1, oaf3_2, 1.0f).get(0).asBrokerEvent().getEventId();
|
146
|
|
147
|
verifyEquals(id1, id2);
|
148
|
verifyDifferent(id1, id3);
|
149
|
}
|
150
|
|
151
|
@Test
|
152
|
@Ignore
|
153
|
public void testSoftwareEventFactory() throws Exception {
|
154
|
final String id1 = SoftwareEventFactory.process(context, oaf1_1, oaf1_2, 1.0f, baseUrlMap).get(0).asBrokerEvent().getEventId();
|
155
|
final String id2 = SoftwareEventFactory.process(context, oaf2_1, oaf2_2, 1.0f, baseUrlMap).get(0).asBrokerEvent().getEventId();
|
156
|
final String id3 = SoftwareEventFactory.process(context, oaf3_1, oaf3_2, 1.0f, baseUrlMap).get(0).asBrokerEvent().getEventId();
|
157
|
|
158
|
verifyEquals(id1, id2);
|
159
|
verifyDifferent(id1, id3);
|
160
|
}
|
161
|
|
162
|
@Test
|
163
|
public void testSubjectEventFactory() throws Exception {
|
164
|
final String id1 = SubjectEventFactory.process(context, oaf1_1, oaf1_2, 1.0f).get(0).asBrokerEvent().getEventId();
|
165
|
final String id2 = SubjectEventFactory.process(context, oaf2_1, oaf2_2, 1.0f).get(0).asBrokerEvent().getEventId();
|
166
|
final String id3 = SubjectEventFactory.process(context, oaf3_1, oaf3_2, 1.0f).get(0).asBrokerEvent().getEventId();
|
167
|
|
168
|
verifyEquals(id1, id2);
|
169
|
verifyDifferent(id1, id3);
|
170
|
}
|
171
|
|
172
|
private void verifyEquals(final String x, final String y) {
|
173
|
assertNotNull(x);
|
174
|
assertNotNull(y);
|
175
|
assertTrue(x.length() > 10);
|
176
|
assertTrue(y.length() > 10);
|
177
|
assertEquals(x, y);
|
178
|
log.info(String.format("%s = %s", x, y));
|
179
|
}
|
180
|
|
181
|
private void verifyDifferent(final String x, final String y) {
|
182
|
log.info(String.format("%s != %s", x, y));
|
183
|
assertNotNull(x);
|
184
|
assertNotNull(y);
|
185
|
assertTrue(x.length() > 10);
|
186
|
assertTrue(y.length() > 10);
|
187
|
assertFalse(x.equals(y));
|
188
|
|
189
|
}
|
190
|
|
191
|
private Oaf asOaf(final String xmlFile) throws Exception {
|
192
|
for (final Row row : asRows(getClass().getResourceAsStream(xmlFile))) {
|
193
|
for (final Column<String, byte[]> c : row.getColumns()) {
|
194
|
final String cf = row.getColumnFamily();
|
195
|
if (cf.equals("result") && c.getName().equals("body")) { return Oaf.parseFrom(c.getValue()); }
|
196
|
}
|
197
|
}
|
198
|
throw new RuntimeException("Oaf not found");
|
199
|
}
|
200
|
|
201
|
private String loadFromTransformationProfile(final String profilePath) {
|
202
|
log.info("Loading xslt from: " + basePathProfiles + profilePath);
|
203
|
final InputStream profile = getClass().getResourceAsStream(basePathProfiles + profilePath);
|
204
|
final SAXReader saxReader = new SAXReader();
|
205
|
try {
|
206
|
final Document doc = saxReader.read(profile);
|
207
|
return doc.selectSingleNode("//SCRIPT/CODE/*[local-name()='stylesheet']").asXML();
|
208
|
} catch (final DocumentException e) {
|
209
|
e.printStackTrace();
|
210
|
throw new RuntimeException(e);
|
211
|
}
|
212
|
}
|
213
|
|
214
|
private List<Row> asRows(final InputStream recordStream)
|
215
|
throws Exception {
|
216
|
|
217
|
final XsltRowTransformer transformer = new XsltRowTransformerFactory().getTransformer(xslt);
|
218
|
assertNotNull(transformer);
|
219
|
|
220
|
final String record = IOUtils.toString(recordStream);
|
221
|
final List<Row> rows = transformer.apply(record);
|
222
|
|
223
|
assertNotNull(rows);
|
224
|
assertFalse(rows.isEmpty());
|
225
|
|
226
|
return rows;
|
227
|
}
|
228
|
}
|