1
|
package eu.dnetlib.msro.openaireplus.workflows.nodes.contexts;
|
2
|
|
3
|
import java.io.StringReader;
|
4
|
import java.util.List;
|
5
|
|
6
|
import com.google.common.collect.Iterables;
|
7
|
import com.google.common.collect.Lists;
|
8
|
import org.apache.commons.logging.Log;
|
9
|
import org.apache.commons.logging.LogFactory;
|
10
|
import org.dom4j.Document;
|
11
|
import org.dom4j.DocumentException;
|
12
|
import org.dom4j.Element;
|
13
|
import org.dom4j.Node;
|
14
|
import org.dom4j.io.SAXReader;
|
15
|
import org.junit.Test;
|
16
|
|
17
|
import static org.junit.Assert.assertTrue;
|
18
|
|
19
|
/**
|
20
|
* Created by claudio on 01/03/16.
|
21
|
*/
|
22
|
public class ContextUtilsTest {
|
23
|
|
24
|
private static final Log log = LogFactory.getLog(ContextUtilsTest.class);
|
25
|
private String params = "{param1:value1,param2:value2}";
|
26
|
|
27
|
|
28
|
@Test
|
29
|
public void testWrongInput() throws DocumentException {
|
30
|
Iterable<String> it = Lists.newArrayList("<fundingtree><funder><id>irb_hr______::HRZZ</id><shortname>HRZZ</shortname><name>Croatian Science Foundation (CSF)</name><originalname>Hrvatska zaklada za znanost</originalname><jurisdiction>HR</jurisdiction></funder></fundingtree>");
|
31
|
final ContextDesc context = ContextUtils.getContext(it, "HRZZ", "HRZZ", "funding", params, "all");
|
32
|
System.out.println(context.asDomElement().asXML());
|
33
|
}
|
34
|
|
35
|
@Test
|
36
|
public void testProfile() throws DocumentException {
|
37
|
Iterable<String> it = Lists.newArrayList("<fundingtree><funder><id>irb_hr______::HRZZ</id><shortname>HRZZ</shortname><name>Croatian Science Foundation (CSF)</name><originalname>Hrvatska zaklada za znanost</originalname><jurisdiction>HR</jurisdiction></funder></fundingtree>");
|
38
|
final ContextDesc contextDesc = ContextUtils.getContext(it, "HRZZ", "HRZZ", "funding", params, "all");
|
39
|
final String profile = "<RESOURCE_PROFILE xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">\n"
|
40
|
+ " <HEADER>\n"
|
41
|
+ " <RESOURCE_IDENTIFIER value=\"1110d179-f7a8-481e-a1d9-51d14bec6b8d_Q29udGV4dERTUmVzb3VyY2VzL0NvbnRleHREU1Jlc291cmNlVHlwZQ==\"/>\n"
|
42
|
+ " <RESOURCE_TYPE value=\"ContextDSResourceType\"/>\n"
|
43
|
+ " <RESOURCE_KIND value=\"ContextDSResources\"/>\n"
|
44
|
+ " <RESOURCE_URI value=\"\"/>\n"
|
45
|
+ " <DATE_OF_CREATION value=\"2018-05-05T07:31:08+00:00\"/>\n"
|
46
|
+ " </HEADER>\n"
|
47
|
+ " <BODY>\n"
|
48
|
+ " <CONFIGURATION>\n"
|
49
|
+ " <context id=\"HRZZ\" label=\"HRZZ\" type=\"funding\">\n"
|
50
|
+ " <param name=\"x\">valueX</param>\n"
|
51
|
+ " <category claim=\"true\" id=\"ARC::Special Research initiative (Direct stimulation of the visual cortex: a flexible strategy for restoring high-acuity pattern vision)\" label=\"Special Research initiative (Direct stimulation of the visual cortex: a flexible strategy for restoring high-acuity pattern vision)\">\n"
|
52
|
+ " <param name=\"name\">Special Research initiative (Direct stimulation of the visual cortex: a flexible strategy for restoring high-acuity pattern vision)</param>\n"
|
53
|
+ " <param name=\"openaireId\">arc_________::ARC::Special Research initiative (Direct stimulation of the visual cortex: a flexible strategy for restoring high-acuity pattern vision)</param>\n"
|
54
|
+ " <param name=\"class\">arc:fundingStream</param>\n"
|
55
|
+ " </category>"
|
56
|
+ " </context>\n"
|
57
|
+ " </CONFIGURATION>\n"
|
58
|
+ " <STATUS/>\n"
|
59
|
+ " <SECURITY_PARAMETERS/>\n"
|
60
|
+ " </BODY>\n"
|
61
|
+ "</RESOURCE_PROFILE>";
|
62
|
final SAXReader reader = new SAXReader();
|
63
|
final Document doc = reader.read(new StringReader(profile));
|
64
|
final String profId = doc.valueOf("//HEADER/RESOURCE_IDENTIFIER/@value");
|
65
|
final Element ctxElem = (Element) doc.selectSingleNode("//CONFIGURATION/context[@id='" + contextDesc.getId() + "' and @type='" + contextDesc.getType() + "']");
|
66
|
updateContextParams(ctxElem, contextDesc);
|
67
|
|
68
|
for (ContextPart cat : contextDesc.getCategories().values()) {
|
69
|
final Node catElem = ctxElem.selectSingleNode("./category[@id='" + cat.getId() + "']");
|
70
|
if (catElem != null) {
|
71
|
catElem.detach();
|
72
|
}
|
73
|
ctxElem.add(cat.asDomElement("category"));
|
74
|
}
|
75
|
log.info("registering profile context " + contextDesc.getId());
|
76
|
System.out.println(doc.asXML());
|
77
|
}
|
78
|
|
79
|
private void updateContextParams(final Element ctxElement, ContextDesc desc) {
|
80
|
//removing old PARAMs
|
81
|
List<Node> oldParams = ctxElement.selectNodes("param");
|
82
|
for (Node n : oldParams) {
|
83
|
n.detach();
|
84
|
}
|
85
|
//adding new params
|
86
|
for (Element param : desc.getParamsAsElements()) {
|
87
|
ctxElement.add(param);
|
88
|
}
|
89
|
}
|
90
|
|
91
|
@Test
|
92
|
public void testContextFactory() throws DocumentException {
|
93
|
|
94
|
Iterable<String> it = Lists.newArrayList(
|
95
|
"\t\t<fundings>\n"
|
96
|
+ "\t\t\t<fundingtree>\n"
|
97
|
+ "\t\t\t\t<funder>\n"
|
98
|
+ "\t\t\t\t\t<id>arc_________::ARC</id>\n"
|
99
|
+ "\t\t\t\t\t<shortname>ARC</shortname>\n"
|
100
|
+ "\t\t\t\t\t<name>Australian Research Council (ARC)</name>\n"
|
101
|
+ "\t\t\t\t\t<jurisdiction>AU</jurisdiction>\n"
|
102
|
+ "\t\t\t\t</funder>\n"
|
103
|
+ "\t\t\t</fundingtree>\n"
|
104
|
+ "\t\t</fundings>");
|
105
|
|
106
|
final ContextDesc context = ContextUtils.getContext(it, "ARC", "Australian Research Council (ARC)", "funding", params, "hidden");
|
107
|
|
108
|
final Iterable<String> iter = Iterables.transform(context.getDbEntries().entrySet(), ContextUtils.getContextRowTransformer());
|
109
|
|
110
|
final SAXReader r = new SAXReader();
|
111
|
|
112
|
for (String f : iter) {
|
113
|
|
114
|
final Document fd = r.read(new StringReader(f));
|
115
|
|
116
|
assertTrue(fd != null);
|
117
|
assertTrue("AU".equals(fd.valueOf("//FIELD[@name='jurisdiction']")));
|
118
|
assertTrue("arc_________::ARC".equals(fd.valueOf("//FIELD[@name='funder']")));
|
119
|
}
|
120
|
System.out.println(context.asDomElement().asXML());
|
121
|
}
|
122
|
|
123
|
@Test
|
124
|
public void testEmptyFunding() throws DocumentException {
|
125
|
|
126
|
Iterable<String> it = Lists.newArrayList(
|
127
|
"\t\t<fundings>\n"
|
128
|
+ "\t\t\t<fundingtree/>\n"
|
129
|
+ "\t\t</fundings>");
|
130
|
|
131
|
final ContextDesc context = ContextUtils.getContext(it, "HRZZ", "HRZZ", "funding", params, "hidden");
|
132
|
|
133
|
final Iterable<String> iter = Iterables.transform(context.getDbEntries().entrySet(), ContextUtils.getContextRowTransformer());
|
134
|
|
135
|
final SAXReader r = new SAXReader();
|
136
|
|
137
|
for (String f : iter) {
|
138
|
System.out.println(f);
|
139
|
|
140
|
/*final Document fd = r.read(new StringReader(f));
|
141
|
|
142
|
assertTrue(fd != null);
|
143
|
assertTrue("AU".equals(fd.valueOf("//FIELD[@name='jurisdiction']")));
|
144
|
assertTrue("arc_________::ARC".equals(fd.valueOf("//FIELD[@name='funder']")));
|
145
|
*/
|
146
|
}
|
147
|
}
|
148
|
|
149
|
}
|