1
|
package eu.dnetlib.actionmanager.actions;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.io.InputStream;
|
5
|
import java.io.StringWriter;
|
6
|
import java.util.Arrays;
|
7
|
import java.util.List;
|
8
|
import javax.annotation.Resource;
|
9
|
|
10
|
import eu.dnetlib.actionmanager.common.Agent;
|
11
|
import eu.dnetlib.actionmanager.common.Agent.AGENT_TYPE;
|
12
|
import eu.dnetlib.actionmanager.common.Operation;
|
13
|
import eu.dnetlib.actionmanager.common.Provenance;
|
14
|
import eu.dnetlib.actionmanager.rmi.ActionManagerException;
|
15
|
import eu.dnetlib.data.proto.OafProtos.Oaf;
|
16
|
import org.apache.commons.io.IOUtils;
|
17
|
import org.apache.commons.logging.Log;
|
18
|
import org.apache.commons.logging.LogFactory;
|
19
|
import org.apache.hadoop.hbase.KeyValue;
|
20
|
import org.apache.hadoop.hbase.client.Put;
|
21
|
import org.junit.Test;
|
22
|
import org.junit.runner.RunWith;
|
23
|
import org.springframework.test.context.ContextConfiguration;
|
24
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
25
|
|
26
|
import static org.junit.Assert.*;
|
27
|
|
28
|
@RunWith(SpringJUnit4ClassRunner.class)
|
29
|
@ContextConfiguration(locations = { "/eu/dnetlib/actionmanager/actions/applicationContext-actionmanager-test.xml" })
|
30
|
public class ActionFactoryTest {
|
31
|
|
32
|
private static final Log log = LogFactory.getLog(ActionFactoryTest.class);
|
33
|
private final static Agent agent = new Agent("agentId", "agentName", AGENT_TYPE.algo);
|
34
|
private final static String trust = "0.9";
|
35
|
private final InputStream recordDataciteInputStream = getClass().getResourceAsStream("recordDatacite.xml");
|
36
|
private final InputStream recordOafInputStream = getClass().getResourceAsStream("record.xml");
|
37
|
@Resource
|
38
|
private ActionFactory actionFactory;
|
39
|
|
40
|
// @Before
|
41
|
// public void setUp() throws Exception {
|
42
|
//
|
43
|
// }
|
44
|
|
45
|
@Test
|
46
|
public void testBeans() {
|
47
|
assertNotNull(actionFactory);
|
48
|
assertNotNull(actionFactory.getXslts());
|
49
|
assertFalse(actionFactory.getXslts().isEmpty());
|
50
|
}
|
51
|
|
52
|
@Test
|
53
|
public void testDataciteInsert() throws ActionManagerException, IOException {
|
54
|
|
55
|
XsltInfoPackageAction action =
|
56
|
actionFactory.generateInfoPackageAction("datacite2insertActions", "rawSet", agent, Operation.INSERT, asString(recordDataciteInputStream),
|
57
|
Provenance.sysimport_crosswalk_datasetarchive, "datacite____", trust);
|
58
|
|
59
|
final List<Put> puts = action.asPutOperations();
|
60
|
|
61
|
for (AtomicAction a : action.calculateAtomicActions()) {
|
62
|
assertNotNull(a.getTargetValue());
|
63
|
Oaf oaf = Oaf.parseFrom(a.getTargetValue());
|
64
|
assertNotNull(oaf);
|
65
|
//System.out.println(oaf);
|
66
|
}
|
67
|
|
68
|
for (Put put : puts) {
|
69
|
assertNotNull(put);
|
70
|
}
|
71
|
log.info(puts.size());
|
72
|
}
|
73
|
|
74
|
@Test
|
75
|
public void testDataciteUpdate() throws ActionManagerException, IOException {
|
76
|
|
77
|
XsltInfoPackageAction action =
|
78
|
actionFactory.generateInfoPackageAction("datacite2updateActions", "rawSet", agent, Operation.UPDATE, asString(recordDataciteInputStream),
|
79
|
Provenance.sysimport_crosswalk_datasetarchive, "datacite____", trust);
|
80
|
|
81
|
final List<Put> puts = action.asPutOperations();
|
82
|
|
83
|
assertTrue(puts.size() == 1);
|
84
|
for (Put put : puts) {
|
85
|
assertNotNull(put);
|
86
|
final List<KeyValue> target = put.getFamilyMap().get("target".getBytes());
|
87
|
if (target != null) {
|
88
|
for (KeyValue kv : target) {
|
89
|
|
90
|
final String qualifier = new String(kv.getQualifier());
|
91
|
if (qualifier.equals("content")) {
|
92
|
Oaf oaf = Oaf.parseFrom(kv.getValue());
|
93
|
assertNotNull(oaf);
|
94
|
//System.out.println(oaf);
|
95
|
}
|
96
|
}
|
97
|
}
|
98
|
}
|
99
|
}
|
100
|
|
101
|
@Test
|
102
|
public void testRecordOafInsert() throws ActionManagerException, IOException {
|
103
|
|
104
|
XsltInfoPackageAction action =
|
105
|
actionFactory.generateInfoPackageAction("dmf2insertActions", "rawSet", agent, Operation.INSERT, asString(recordOafInputStream),
|
106
|
Provenance.sysimport_crosswalk_datasetarchive, "od_______296", trust);
|
107
|
|
108
|
final List<Put> puts = action.asPutOperations();
|
109
|
|
110
|
for (AtomicAction a : action.calculateAtomicActions()) {
|
111
|
assertNotNull(a.getTargetValue());
|
112
|
|
113
|
String actionJSON = a.toString();
|
114
|
|
115
|
log.info(actionJSON);
|
116
|
|
117
|
final AtomicAction aa = AtomicAction.fromJSON(actionJSON);
|
118
|
|
119
|
assertEquals(a.getRawSet(), aa.getRawSet());
|
120
|
assertEquals(a.getTargetColumn(), aa.getTargetColumn());
|
121
|
assertEquals(a.getTargetColumnFamily(), aa.getTargetColumnFamily());
|
122
|
assertEquals(a.getTargetRowKey(), aa.getTargetRowKey());
|
123
|
assertEquals(a.getActionType(), aa.getActionType());
|
124
|
assertEquals(a.getRowKey(), aa.getRowKey());
|
125
|
|
126
|
assertEquals(a.getAgent().getId(), aa.getAgent().getId());
|
127
|
assertEquals(a.getAgent().getName(), aa.getAgent().getName());
|
128
|
assertEquals(a.getAgent().getType(), aa.getAgent().getType());
|
129
|
|
130
|
assertTrue(Arrays.equals(a.getTargetValue(), aa.getTargetValue()));
|
131
|
|
132
|
//System.out.println(oaf);
|
133
|
}
|
134
|
|
135
|
for (Put put : puts) {
|
136
|
assertNotNull(put);
|
137
|
}
|
138
|
log.info("N. puts: " + puts.size());
|
139
|
}
|
140
|
|
141
|
private String asString(final InputStream i) throws IOException {
|
142
|
StringWriter writer = new StringWriter();
|
143
|
IOUtils.copy(i, writer);
|
144
|
return writer.toString();
|
145
|
}
|
146
|
}
|