Revision 53588
Added by Sandro La Bruzzo over 5 years ago
DOIBoostToActionsTest.java | ||
---|---|---|
1 | 1 |
package eu.dnetlib.data.mapreduce.actions; |
2 | 2 |
|
3 |
import com.google.common.collect.Lists; |
|
3 | 4 |
import com.google.gson.JsonObject; |
4 | 5 |
import com.google.gson.JsonParser; |
5 | 6 |
import eu.dnetlib.actionmanager.actions.ActionFactory; |
6 | 7 |
import eu.dnetlib.actionmanager.actions.AtomicAction; |
7 | 8 |
import eu.dnetlib.actionmanager.common.Agent; |
8 |
import eu.dnetlib.data.mapreduce.hbase.dataimport.CrossRefToActions; |
|
9 | 9 |
import eu.dnetlib.data.mapreduce.hbase.dataimport.DOIBoostToActions; |
10 |
import eu.dnetlib.data.mapreduce.util.XmlRecordFactory; |
|
11 |
import eu.dnetlib.data.transform.Column; |
|
12 |
import eu.dnetlib.data.transform.Row; |
|
13 |
import eu.dnetlib.data.transform.XsltRowTransformerFactoryTest; |
|
10 | 14 |
import org.apache.commons.lang3.StringUtils; |
15 |
import org.dom4j.Document; |
|
16 |
import org.dom4j.io.DocumentResult; |
|
17 |
import org.dom4j.io.DocumentSource; |
|
18 |
import org.dom4j.io.SAXReader; |
|
11 | 19 |
import org.junit.Before; |
12 | 20 |
import org.junit.Test; |
13 | 21 |
|
14 |
import java.io.BufferedReader; |
|
15 |
import java.io.IOException; |
|
16 |
import java.io.InputStream; |
|
17 |
import java.io.InputStreamReader; |
|
22 |
import javax.xml.transform.Transformer; |
|
23 |
import javax.xml.transform.TransformerFactory; |
|
24 |
import java.io.*; |
|
18 | 25 |
import java.util.List; |
26 |
import java.util.Map; |
|
19 | 27 |
|
20 |
public class DOIBoostToActionsTest { |
|
28 |
public class DOIBoostToActionsTest extends XsltRowTransformerFactoryTest {
|
|
21 | 29 |
private String setName; |
22 | 30 |
private Agent agent; |
23 | 31 |
|
32 |
private final static String xslt = |
|
33 |
"<xsl:stylesheet version=\"1.0\" xmlns:xsl=\"http://www.w3.org/1999/XSL/Transform\">\n" |
|
34 |
+ " <xsl:template match=\"@*|node()\">\n" |
|
35 |
+ " <xsl:copy>\n" |
|
36 |
+ " <xsl:apply-templates select=\"@*|node()\"/>\n" |
|
37 |
+ " </xsl:copy>\n" |
|
38 |
+ " </xsl:template>\n" |
|
39 |
+ "</xsl:stylesheet>"; |
|
40 |
|
|
41 |
private Transformer transformer; |
|
42 |
|
|
24 | 43 |
@Before |
25 | 44 |
public void setup() { |
26 | 45 |
setName = "DLI"; |
... | ... | |
29 | 48 |
|
30 | 49 |
@Test |
31 | 50 |
public void testSingleDOIBoostAction() throws IOException { |
32 |
doTestSingleDOIBoostAction("/eu/dnetlib/data/mapreduce/actions/DOIBoostAction.json");
|
|
51 |
doTestSingleDOIBoostAction("/eu/dnetlib/data/mapreduce/actions/broken");
|
|
33 | 52 |
} |
34 | 53 |
|
35 | 54 |
|
36 | 55 |
@Test |
56 |
public void testDOIBoostActionToXML() throws Exception { |
|
57 |
doTestSingleDOIBoostActionToXML("/eu/dnetlib/data/mapreduce/actions/broken"); |
|
58 |
} |
|
59 |
|
|
60 |
|
|
61 |
|
|
62 |
@Test |
|
37 | 63 |
public void testMultipleDOIBoostAction() throws IOException { |
38 | 64 |
doTestAllDOIBoostAction("/eu/dnetlib/data/mapreduce/actions/part-00070"); |
39 | 65 |
} |
40 | 66 |
|
41 | 67 |
|
68 |
private void doTestSingleDOIBoostActionToXML(final String filePath) throws Exception { |
|
69 |
final List<Row> rows = Lists.newArrayList(); |
|
70 |
final InputStream is = this.getClass().getResourceAsStream(filePath); |
|
71 |
final BufferedReader in = new BufferedReader(new InputStreamReader(is)); |
|
42 | 72 |
|
73 |
String line = in.readLine(); |
|
74 |
|
|
75 |
final JsonParser parser = new JsonParser(); |
|
76 |
JsonObject root = parser.parse(line).getAsJsonObject(); |
|
77 |
List<AtomicAction> actions = DOIBoostToActions.generatePublicationActionsFromDump(root, new ActionFactory(), setName, agent, false, false); |
|
78 |
if (actions!= null) { |
|
79 |
actions.forEach(action-> { |
|
80 |
if (action.getTargetColumn().equals("body") && action.getTargetColumnFamily().equals("result")) |
|
81 |
{ |
|
82 |
Column<String, byte[]> col = new Column<>("body" , action.getTargetValue()); |
|
83 |
rows.add(new Row("result",action.getTargetRowKey() , Lists.newArrayList(col))); |
|
84 |
} |
|
85 |
|
|
86 |
}); |
|
87 |
|
|
88 |
|
|
89 |
|
|
90 |
} |
|
91 |
|
|
92 |
Map<String, XmlRecordFactory> stringXmlRecordFactoryMap = mapAll(buildTable(rows)); |
|
93 |
transformer = TransformerFactory.newInstance().newTransformer(new DocumentSource((new SAXReader()).read(new StringReader(xslt)))); |
|
94 |
|
|
95 |
final SAXReader saxReader = new SAXReader(); |
|
96 |
stringXmlRecordFactoryMap.values().forEach(it -> { |
|
97 |
try { |
|
98 |
final DocumentResult result = new DocumentResult(); |
|
99 |
final Document document = saxReader.read(new StringReader(it.build())); |
|
100 |
transformer.transform(new DocumentSource(document), result); |
|
101 |
System.out.println(result.getDocument().asXML()); |
|
102 |
} |
|
103 |
catch (Exception e) { |
|
104 |
e.printStackTrace(); |
|
105 |
} |
|
106 |
|
|
107 |
}); |
|
108 |
|
|
109 |
} |
|
110 |
|
|
43 | 111 |
private void doTestSingleDOIBoostAction(final String filePath) throws IOException { |
44 | 112 |
final InputStream is = this.getClass().getResourceAsStream(filePath); |
45 | 113 |
final BufferedReader in = new BufferedReader(new InputStreamReader(is)); |
... | ... | |
48 | 116 |
|
49 | 117 |
final JsonParser parser = new JsonParser(); |
50 | 118 |
JsonObject root = parser.parse(line).getAsJsonObject(); |
51 |
List<AtomicAction> actions = DOIBoostToActions.generatePublicationActionsFromDump(root, new ActionFactory(), setName, agent, false, true);
|
|
119 |
List<AtomicAction> actions = DOIBoostToActions.generatePublicationActionsFromDump(root, new ActionFactory(), setName, agent, false, false);
|
|
52 | 120 |
if (actions!= null) { |
53 | 121 |
|
54 | 122 |
actions.forEach(it -> System.out.println(String.format(" RowKey:%s TargetColumnFamily:%s TargetColumn: %s", it.getTargetRowKey(), it.getTargetColumnFamily(), it.getTargetColumn()))); |
Also available in: Unified diff
refactored Action