1 package org.opendaylight.controller.sal.restconf.impl.cnsn.to.json.test;
3 import static org.junit.Assert.assertNotNull;
4 import static org.junit.Assert.assertTrue;
6 import java.io.IOException;
7 import java.util.regex.Matcher;
8 import java.util.regex.Pattern;
10 import javax.ws.rs.WebApplicationException;
12 import org.junit.BeforeClass;
13 import org.junit.Test;
14 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
15 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
16 import org.opendaylight.controller.sal.restconf.impl.test.YangAndXmlAndDataSchemaLoader;
17 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
21 * All tests are commented now because leafref isn't supported now
25 public class CnSnToJsonLeafrefType extends YangAndXmlAndDataSchemaLoader {
28 public static void initialization() {
29 dataLoad("/cnsn-to-json/leafref", 2, "main-module", "cont");
33 public void leafrefAbsolutePathToExistingLeafTest() {
34 String json = toJson("/cnsn-to-json/leafref/xml/data_absolut_ref_to_existing_leaf.xml");
35 validateJson(".*\"lf3\":\\p{Blank}*\"true\".*", json);
39 public void leafrefRelativePathToExistingLeafTest() {
40 String json = toJson("/cnsn-to-json/leafref/xml/data_relativ_ref_to_existing_leaf.xml");
41 validateJson(".*\"lf2\":\\p{Blank}*\"121\".*", json);
45 * Tests case when reference to not existing element is present. In this
46 * case value from single node is printed as string.
49 public void leafrefToNonExistingLeafTest() {
50 String json = toJson("/cnsn-to-json/leafref/xml/data_ref_to_non_existing_leaf.xml");
51 validateJson(".*\"lf5\":\\p{Blank}*\"137\".*", json);
55 * Tests case when non leaf element is referenced. In this case value from
56 * single node is printed as string.
59 public void leafrefToNotLeafTest() {
60 String json = toJson("/cnsn-to-json/leafref/xml/data_ref_to_not_leaf.xml");
61 validateJson(".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf6\":\\p{Blank}*\"44.33\".*", json);
65 * Tests case when leaflist element is refers to leaf.
68 public void leafrefFromLeafListToLeafTest() {
69 String json = toJson("/cnsn-to-json/leafref/xml/data_relativ_ref_from_leaflist_to_existing_leaf.xml");
71 ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lflst1\":\\p{Blank}*.*\"345\",\\p{Space}*\"346\",\\p{Space}*\"347\".*",
76 * Tests case when leaflist element is refers to leaf.
79 public void leafrefFromLeafrefToLeafrefTest() {
80 String json = toJson("/cnsn-to-json/leafref/xml/data_from_leafref_to_leafref.xml");
81 validateJson(".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf7\":\\p{Blank}*\"200\".*", json);
84 private void validateJson(String regex, String value) {
86 Pattern ptrn = Pattern.compile(regex, Pattern.DOTALL);
87 Matcher mtch = ptrn.matcher(value);
88 assertTrue(mtch.matches());
91 private String toJson(String xmlDataPath) {
93 CompositeNode compositeNode = TestUtils.loadCompositeNode(xmlDataPath);
94 TestUtils.normalizeCompositeNode(compositeNode, modules, searchedModuleName + ":" + searchedDataSchemaName);
95 return TestUtils.writeCompNodeWithSchemaContextToOutput(compositeNode, modules, dataSchemaNode,
96 StructuredDataToJsonProvider.INSTANCE);
97 } catch (WebApplicationException | IOException e) {