1
|
package eu.dnetlib.data.actionmanager.actions;
|
2
|
|
3
|
import java.io.IOException;
|
4
|
import java.io.InputStream;
|
5
|
import java.io.StringReader;
|
6
|
import java.io.StringWriter;
|
7
|
import java.util.Arrays;
|
8
|
import java.util.HashMap;
|
9
|
import java.util.List;
|
10
|
import java.util.Map;
|
11
|
import javax.xml.transform.TransformerException;
|
12
|
|
13
|
import eu.dnetlib.data.proto.DNGFProtos.DNGF;
|
14
|
import eu.dnetlib.rmi.data.hadoop.actionmanager.ActionManagerException;
|
15
|
import eu.dnetlib.rmi.data.hadoop.actionmanager.Agent;
|
16
|
import eu.dnetlib.rmi.data.hadoop.actionmanager.Operation;
|
17
|
import eu.dnetlib.rmi.data.hadoop.actionmanager.Provenance;
|
18
|
import eu.dnetlib.rmi.data.hadoop.actionmanager.actions.ActionFactory;
|
19
|
import eu.dnetlib.rmi.data.hadoop.actionmanager.actions.AtomicAction;
|
20
|
import eu.dnetlib.rmi.data.hadoop.actionmanager.actions.XsltInfoPackageAction;
|
21
|
import org.apache.commons.io.IOUtils;
|
22
|
import org.apache.commons.logging.Log;
|
23
|
import org.apache.commons.logging.LogFactory;
|
24
|
import org.dom4j.Document;
|
25
|
import org.dom4j.DocumentException;
|
26
|
import org.dom4j.io.SAXReader;
|
27
|
import org.junit.Before;
|
28
|
import org.junit.Test;
|
29
|
import org.junit.runner.RunWith;
|
30
|
import org.mockito.Mock;
|
31
|
import org.mockito.Mockito;
|
32
|
import org.mockito.runners.MockitoJUnitRunner;
|
33
|
import org.springframework.core.io.Resource;
|
34
|
|
35
|
import static org.junit.Assert.*;
|
36
|
|
37
|
@RunWith(MockitoJUnitRunner.class)
|
38
|
public class ActionFactoryTest {
|
39
|
|
40
|
private static final Log log = LogFactory.getLog(ActionFactoryTest.class);
|
41
|
private static String basePathProfiles = "/eu/dnetlib/bootstrap/profiles/TransformationRuleDSResources/TransformationRuleDSResourceType/";
|
42
|
private final static Agent agent = new Agent("agentId", "agentName", Agent.AGENT_TYPE.algo);
|
43
|
private final static String trust = "0.9";
|
44
|
private final InputStream recordOdfInputStream = getClass().getResourceAsStream("recordOdf.xml");
|
45
|
private final InputStream recordOafInputStream = getClass().getResourceAsStream("recordOaf.xml");
|
46
|
|
47
|
private ActionFactory actionFactory;
|
48
|
private Map<String, Resource> xslts = new HashMap<>();
|
49
|
|
50
|
@Mock
|
51
|
private Resource oaf2insertActions;
|
52
|
@Mock
|
53
|
private Resource oaf2updateActions;
|
54
|
@Mock
|
55
|
private Resource odf2insertActions;
|
56
|
@Mock
|
57
|
private Resource odf2updateActions;
|
58
|
@Mock
|
59
|
private Resource rels2actions;
|
60
|
|
61
|
@Before
|
62
|
public void setUp() throws Exception {
|
63
|
actionFactory = new ActionFactory();
|
64
|
|
65
|
xslts.put("oaf2insertActions", oaf2insertActions);
|
66
|
xslts.put("oaf2updateActions", oaf2updateActions);
|
67
|
xslts.put("odf2insertActions", odf2insertActions);
|
68
|
xslts.put("odf2updateActions", odf2updateActions);
|
69
|
xslts.put("rels2actions", rels2actions);
|
70
|
|
71
|
Mockito.when(oaf2insertActions.getInputStream()).thenReturn(loadFromTransformationProfile("oaf2insertActions.xml"));
|
72
|
Mockito.when(oaf2updateActions.getInputStream()).thenReturn(loadFromTransformationProfile("oaf2updateActions.xml"));
|
73
|
Mockito.when(odf2insertActions.getInputStream()).thenReturn(loadFromTransformationProfile("odf2insertActions.xml"));
|
74
|
Mockito.when(odf2updateActions.getInputStream()).thenReturn(loadFromTransformationProfile("odf2updateActions.xml"));
|
75
|
// Mockito.when(rels2actions.getInputStream()).thenReturn(loadFromTransformationProfile("rels2actions.xml"));
|
76
|
|
77
|
actionFactory.setXslts(xslts);
|
78
|
}
|
79
|
|
80
|
@Test
|
81
|
public void testBeans() {
|
82
|
assertNotNull(actionFactory);
|
83
|
assertNotNull(actionFactory.getXslts());
|
84
|
assertFalse(actionFactory.getXslts().isEmpty());
|
85
|
}
|
86
|
|
87
|
@Test
|
88
|
public void testDataciteInsert() throws ActionManagerException, IOException, TransformerException, DocumentException {
|
89
|
|
90
|
XsltInfoPackageAction action =
|
91
|
actionFactory.generateInfoPackageAction("odf2insertActions", "rawSet", agent, Operation.INSERT, asString(recordOdfInputStream),
|
92
|
Provenance.sysimport_crosswalk_datasetarchive, "datacite____", trust);
|
93
|
|
94
|
checkActions(action, action.asAtomicActions());
|
95
|
}
|
96
|
|
97
|
@Test
|
98
|
public void testDataciteUpdate() throws ActionManagerException, IOException, TransformerException, DocumentException {
|
99
|
|
100
|
XsltInfoPackageAction action =
|
101
|
actionFactory.generateInfoPackageAction("odf2updateActions", "rawSet", agent, Operation.UPDATE, asString(recordOdfInputStream),
|
102
|
Provenance.sysimport_crosswalk_datasetarchive, "datacite____", trust);
|
103
|
|
104
|
checkActions(action, action.asAtomicActions());
|
105
|
}
|
106
|
|
107
|
@Test
|
108
|
public void testRecordOafInsert() throws ActionManagerException, IOException, TransformerException, DocumentException {
|
109
|
|
110
|
XsltInfoPackageAction action =
|
111
|
actionFactory.generateInfoPackageAction("oaf2insertActions", "rawSet", agent, Operation.INSERT, asString(recordOafInputStream),
|
112
|
Provenance.sysimport_crosswalk_datasetarchive, "od_______296", trust);
|
113
|
|
114
|
checkActions(action, action.asAtomicActions());
|
115
|
}
|
116
|
|
117
|
@Test
|
118
|
public void testRecordOafUpdate() throws ActionManagerException, IOException, TransformerException, DocumentException {
|
119
|
|
120
|
XsltInfoPackageAction action =
|
121
|
actionFactory.generateInfoPackageAction("oaf2updateActions", "rawSet", agent, Operation.INSERT, asString(recordOafInputStream),
|
122
|
Provenance.sysimport_crosswalk_datasetarchive, "od_______296", trust);
|
123
|
checkActions(action, action.asAtomicActions());
|
124
|
}
|
125
|
|
126
|
private void checkActions(final XsltInfoPackageAction action, final List<AtomicAction> atomicActionList)
|
127
|
throws TransformerException, DocumentException, com.google.protobuf.InvalidProtocolBufferException {
|
128
|
for (AtomicAction a : action.calculateAtomicActions()) {
|
129
|
assertNotNull(a.getTargetValue());
|
130
|
|
131
|
String actionJSON = AtomicActionSerialiser.toJSON(a);
|
132
|
|
133
|
log.info(actionJSON);
|
134
|
|
135
|
final AtomicAction aa = AtomicActionDeserialiser.fromJSON(actionJSON);
|
136
|
|
137
|
assertEquals(a.getRawSet(), aa.getRawSet());
|
138
|
assertEquals(a.getTargetColumn(), aa.getTargetColumn());
|
139
|
assertEquals(a.getTargetColumnFamily(), aa.getTargetColumnFamily());
|
140
|
assertEquals(a.getTargetRowKey(), aa.getTargetRowKey());
|
141
|
assertEquals(a.getActionType(), aa.getActionType());
|
142
|
assertEquals(a.getRowKey(), aa.getRowKey());
|
143
|
|
144
|
assertEquals(a.getAgent().getId(), aa.getAgent().getId());
|
145
|
assertEquals(a.getAgent().getName(), aa.getAgent().getName());
|
146
|
assertEquals(a.getAgent().getType(), aa.getAgent().getType());
|
147
|
|
148
|
assertTrue(Arrays.equals(a.getTargetValue(), aa.getTargetValue()));
|
149
|
|
150
|
System.out.println(DNGF.parseFrom(aa.getTargetValue()));
|
151
|
}
|
152
|
|
153
|
for (AtomicAction aa : atomicActionList) {
|
154
|
assertNotNull(aa);
|
155
|
}
|
156
|
log.info("N. puts: " + atomicActionList.size());
|
157
|
}
|
158
|
|
159
|
private String asString(final InputStream i) throws IOException {
|
160
|
StringWriter writer = new StringWriter();
|
161
|
IOUtils.copy(i, writer);
|
162
|
return writer.toString();
|
163
|
}
|
164
|
|
165
|
private InputStream loadFromTransformationProfile(final String profilePath) {
|
166
|
log.info("Loading xslt from: " + basePathProfiles + profilePath);
|
167
|
InputStream profile = getClass().getResourceAsStream(basePathProfiles + profilePath);
|
168
|
final String s;
|
169
|
try {
|
170
|
s = IOUtils.toString(profile);
|
171
|
log.debug("profile: " + s);
|
172
|
} catch (IOException e) {
|
173
|
e.printStackTrace();
|
174
|
throw new RuntimeException(e);
|
175
|
}
|
176
|
|
177
|
final SAXReader saxReader = new SAXReader();
|
178
|
Document doc = null;
|
179
|
|
180
|
try {
|
181
|
doc = saxReader.read(new StringReader(s));
|
182
|
} catch (DocumentException e) {
|
183
|
e.printStackTrace();
|
184
|
throw new RuntimeException(e);
|
185
|
}
|
186
|
String xslt = doc.selectSingleNode("//SCRIPT/CODE/*[local-name()='stylesheet']").asXML();
|
187
|
return IOUtils.toInputStream(xslt);
|
188
|
}
|
189
|
}
|