Project

General

Profile

« Previous | Next » 

Revision 52302

Ugly test to verify why the context profile were not compliant to the schema. Error was in the schema.

View differences:

ContextUtilsTest.java
1 1
package eu.dnetlib.msro.openaireplus.workflows.nodes.contexts;
2 2

  
3 3
import java.io.StringReader;
4
import java.util.List;
4 5

  
5 6
import com.google.common.collect.Iterables;
6 7
import com.google.common.collect.Lists;
......
8 9
import org.apache.commons.logging.LogFactory;
9 10
import org.dom4j.Document;
10 11
import org.dom4j.DocumentException;
12
import org.dom4j.Element;
13
import org.dom4j.Node;
11 14
import org.dom4j.io.SAXReader;
12 15
import org.junit.Test;
13 16

  
......
21 24
	private static final Log log = LogFactory.getLog(ContextUtilsTest.class);
22 25
	private String params = "{param1:value1,param2:value2}";
23 26

  
27

  
24 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
25 92
	public void testContextFactory() throws DocumentException {
26 93

  
27 94
		Iterable<String> it = Lists.newArrayList(

Also available in: Unified diff