Revision 36401
Added by Jochen Schirrwagen over 9 years ago
modules/unibi-data-collective-transformation-common/trunk/src/test/java/eu/dnetlib/data/collective/transformation/engine/functions/RetrieveValueTest.java | ||
---|---|---|
25 | 25 |
public class RetrieveValueTest { |
26 | 26 |
|
27 | 27 |
private static final String profileId = "profileId-123"; |
28 |
private static final String profileIdXpath = "//someXpathProfileId"; // "concat('collection('/db/DRIVER/RepositoryServiceResources')//RESOURCE_PROFILE[.//EXTRA_FIELDS/FIELD[key="NamespacePrefix"][value="', //someXpathProfileId, '"]]')"; |
|
28 | 29 |
private static final String xpathExpr = "//someXpath"; |
29 | 30 |
private static final String profileFieldValue = "someValue"; |
30 | 31 |
private transient RetrieveValue retrieveValueFunc; |
... | ... | |
36 | 37 |
|
37 | 38 |
@Before |
38 | 39 |
public void setUp(){ |
40 |
when(resourceDao.getResourceByQuery(profileId)).thenReturn(resource); |
|
39 | 41 |
when(resourceDao.getResource(profileId)).thenReturn(resource); |
40 | 42 |
when(resource.getValue(xpathExpr)).thenReturn(profileFieldValue); |
41 | 43 |
retrieveValueFunc = new RetrieveValue(); |
... | ... | |
43 | 45 |
} |
44 | 46 |
|
45 | 47 |
@Test |
46 |
public void testValueFromProfile()throws ProcessingException, DocumentException{ |
|
47 |
String record = "<objectrecord></objectrecord>"; |
|
48 |
public void testValueFromProfileByValue()throws ProcessingException, DocumentException{
|
|
49 |
String record = "<objectrecord><someXpathProfileId>" + profileId + "</someXpathProfileId></objectrecord>";
|
|
48 | 50 |
List<Argument> paramList = new LinkedList<Argument>(); |
49 | 51 |
paramList.add(new Argument(Type.VALUE, profileId)); |
50 | 52 |
paramList.add(new Argument(Type.INPUTFIELD, xpathExpr)); |
51 | 53 |
assertEquals(profileFieldValue, retrieveValueFunc.executeSingleValue(RetrieveValue.FUNCTION.PROFILEFIELD.name(), paramList, record, new LinkedHashMap<String, String>())); |
52 | 54 |
} |
53 |
|
|
55 |
|
|
56 |
@Test |
|
57 |
public void testValueFromProfileByXpath()throws ProcessingException, DocumentException{ |
|
58 |
String record = "<objectrecord><someXpathProfileId>" + profileId + "</someXpathProfileId></objectrecord>"; |
|
59 |
List<Argument> paramList = new LinkedList<Argument>(); |
|
60 |
paramList.add(new Argument(Type.INPUTFIELD, profileIdXpath)); |
|
61 |
paramList.add(new Argument(Type.INPUTFIELD, xpathExpr)); |
|
62 |
assertEquals(profileFieldValue, retrieveValueFunc.executeSingleValue(RetrieveValue.FUNCTION.PROFILEFIELD.name(), paramList, record, new LinkedHashMap<String, String>())); |
|
63 |
} |
|
64 |
|
|
54 | 65 |
@Test(expected = IllegalArgumentException.class) |
55 | 66 |
public void testInvalidFunctionName()throws ProcessingException, DocumentException{ |
56 | 67 |
List<Argument> paramList = new LinkedList<Argument>(); |
modules/unibi-data-collective-transformation-common/trunk/src/main/java/eu/dnetlib/common/profile/IResourceDaoSupport.java | ||
---|---|---|
5 | 5 |
public interface IResourceDaoSupport { |
6 | 6 |
// TODO documentation |
7 | 7 |
public List<Resource> getResources(String xquery); |
8 |
public Resource getResourceByXquery(String xquery); |
|
8 | 9 |
public Resource getResource(String id); |
9 | 10 |
public void updateResource(String id, Resource resource); |
10 | 11 |
public void removeResource(String id, Resource resource); |
modules/unibi-data-collective-transformation-common/trunk/src/main/java/eu/dnetlib/common/profile/IResourceDao.java | ||
---|---|---|
8 | 8 |
|
9 | 9 |
public Resource getResource(String id); |
10 | 10 |
|
11 |
public Resource getResourceByQuery(String query); |
|
12 |
|
|
11 | 13 |
public void removeResource(String id, Resource resource); |
12 | 14 |
|
13 | 15 |
public void updateResource(String id, Resource resource); |
modules/unibi-data-collective-transformation-common/trunk/src/main/java/eu/dnetlib/common/profile/ResourceDaoRemoteSupport.java | ||
---|---|---|
39 | 39 |
} |
40 | 40 |
return list; |
41 | 41 |
} |
42 |
|
|
43 |
@Override |
|
44 |
public Resource getResourceByXquery(String xquery) { |
|
45 |
Resource resource = null; |
|
46 |
String profile; |
|
47 |
try{ |
|
48 |
profile = lookupLocator.getService().getResourceProfileByQuery(xquery); |
|
49 |
resource = new Resource(profile); |
|
50 |
} catch (ISLookUpDocumentNotFoundException e) { |
|
51 |
log.error(e); |
|
52 |
} catch (ISLookUpException e) { |
|
53 |
log.error(e); |
|
54 |
} catch (DocumentException e) { |
|
55 |
log.error(e); |
|
56 |
} |
|
57 |
return resource; |
|
58 |
} |
|
42 | 59 |
|
43 | 60 |
public Resource getResource(String id) { |
44 | 61 |
Resource resource = null; |
... | ... | |
91 | 108 |
|
92 | 109 |
|
93 | 110 |
|
111 |
|
|
94 | 112 |
} |
modules/unibi-data-collective-transformation-common/trunk/src/main/java/eu/dnetlib/common/profile/ResourceDao.java | ||
---|---|---|
11 | 11 |
return daoSupport.getResources(xquery); |
12 | 12 |
} |
13 | 13 |
|
14 |
@Override |
|
15 |
public Resource getResourceByQuery(String query) { |
|
16 |
// currently only Xquery is supported |
|
17 |
return daoSupport.getResourceByXquery(query); |
|
18 |
} |
|
19 |
|
|
14 | 20 |
public Resource getResource(String id) { |
15 | 21 |
return daoSupport.getResource(id); |
16 | 22 |
} |
modules/unibi-data-collective-transformation-common/trunk/src/main/java/eu/dnetlib/data/collective/transformation/engine/functions/RetrieveValue.java | ||
---|---|---|
15 | 15 |
import javax.xml.xpath.XPathExpressionException; |
16 | 16 |
import javax.xml.xpath.XPathFactory; |
17 | 17 |
|
18 |
import org.apache.commons.lang.StringEscapeUtils; |
|
18 | 19 |
import org.apache.commons.logging.Log; |
19 | 20 |
import org.apache.commons.logging.LogFactory; |
20 | 21 |
import org.xml.sax.InputSource; |
... | ... | |
57 | 58 |
if (arguments.size() != 2){ |
58 | 59 |
throw new ProcessingException("invalid number of arguments - required 2 but found :" + arguments.size()); |
59 | 60 |
} |
60 |
String profileId = ""; |
|
61 |
String arg = ""; |
|
62 |
Resource resource = null; |
|
61 | 63 |
log.debug("RETRIEVEVALUE: " + arguments.get(0).getArgument()); |
62 | 64 |
if (arguments.get(0).isValue()){ |
63 |
profileId = arguments.get(0).getArgument(); |
|
65 |
arg = arguments.get(0).getArgument(); |
|
66 |
resource = resourceDao.getResourceByQuery(arg); // query |
|
64 | 67 |
}else if (arguments.get(0).isInputField()){ |
65 |
profileId = evaluateXpath(objRecord, arguments.get(0).getArgument(), namespaceMap); |
|
68 |
arg = evaluateXpath(objRecord, arguments.get(0).getArgument(), namespaceMap); |
|
69 |
if (arg.startsWith("collection(")) { // xquery |
|
70 |
arg = StringEscapeUtils.unescapeXml(arg); |
|
71 |
resource = resourceDao.getResourceByQuery(arg); // query |
|
72 |
}else |
|
73 |
resource = resourceDao.getResource(arg); // profile id |
|
66 | 74 |
}else if (arguments.get(0).isJobConst()){ |
67 | 75 |
// TODO |
68 | 76 |
}else if (arguments.get(0).isVariable()){ |
69 |
// TODO |
|
77 |
// TODO |
|
78 |
log.warn("RETRIEVEVALUE: support for variables not yet implemented."); |
|
70 | 79 |
} |
71 |
Resource resource = resourceDao.getResource(profileId); // profile id |
|
72 | 80 |
if (resource == null){ |
73 |
throw new ProcessingException("invalid profileId: " + profileId + "; functionName: " + functionName + ", arg1: " + arguments.get(0).getArgument() + ", arg2: " + arguments.get(1).getArgument());
|
|
81 |
throw new ProcessingException("invalid profileId: " + arg + "; functionName: " + functionName + ", arg1: " + arguments.get(0).getArgument() + ", arg2: " + arguments.get(1).getArgument());
|
|
74 | 82 |
} |
75 | 83 |
result = resource.getValue(arguments.get(1).getArgument()); // xpath expr |
76 | 84 |
break; |
Also available in: Unified diff
lookup resource profile by (x)query in addition to lookup by id