1 package org.opendaylight.controller.sal.restconf.impl.test;
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
7 import java.io.IOException;
9 import java.util.regex.Matcher;
11 import javax.ws.rs.WebApplicationException;
14 import org.opendaylight.yangtools.yang.model.api.*;
16 public class ToJsonLeafrefType {
17 private static Set<Module> modules;
18 private static DataSchemaNode dataSchemaNode;
21 public static void initialization() {
22 modules = TestUtils.resolveModules("/yang-to-json-conversion/leafref");
23 assertEquals(2, modules.size());
24 Module module = TestUtils.resolveModule("main-module", modules);
25 assertNotNull(module);
26 dataSchemaNode = TestUtils.resolveDataSchemaNode(module, "cont");
27 assertNotNull(dataSchemaNode);
32 public void leafrefAbsolutePathToExistingLeafTest() {
35 json = TestUtils.writeCompNodeWithSchemaContextToJson(TestUtils
36 .loadCompositeNode("/yang-to-json-conversion/leafref/xml/data_absolut_ref_to_existing_leaf.xml"),
37 "/yang-to-json-conversion/leafref/xml", modules, dataSchemaNode);
38 } catch (WebApplicationException | IOException e) {
43 java.util.regex.Pattern ptrn = java.util.regex.Pattern.compile(".*\"lf3\":\\p{Blank}*true.*",
44 java.util.regex.Pattern.DOTALL);
45 Matcher mtch = ptrn.matcher(json);
46 assertTrue(mtch.matches());
50 public void leafrefRelativePathToExistingLeafTest() {
53 json = TestUtils.writeCompNodeWithSchemaContextToJson(TestUtils
54 .loadCompositeNode("/yang-to-json-conversion/leafref/xml/data_relativ_ref_to_existing_leaf.xml"),
55 "/yang-to-json-conversion/leafref/xml", modules, dataSchemaNode);
56 } catch (WebApplicationException | IOException e) {
61 java.util.regex.Pattern ptrn = java.util.regex.Pattern.compile(".*\"lf2\":\\p{Blank}*121.*",
62 java.util.regex.Pattern.DOTALL);
63 Matcher mtch = ptrn.matcher(json);
64 assertTrue(mtch.matches());
68 * Tests case when reference to not existing element is present. In this
69 * case value from single node is printed as string.
72 public void leafrefToNonExistingLeafTest() {
75 json = TestUtils.writeCompNodeWithSchemaContextToJson(TestUtils
76 .loadCompositeNode("/yang-to-json-conversion/leafref/xml/data_ref_to_non_existing_leaf.xml"),
77 "/yang-to-json-conversion/leafref/xml", modules, dataSchemaNode);
78 } catch (WebApplicationException | IOException e) {
83 java.util.regex.Pattern ptrn = java.util.regex.Pattern.compile(".*\"lf5\":\\p{Blank}*\"137\".*",
84 java.util.regex.Pattern.DOTALL);
85 Matcher mtch = ptrn.matcher(json);
86 assertTrue(mtch.matches());
90 * Tests case when non leaf element is referenced. In this case value from
91 * single node is printed as string.
94 public void leafrefToNotLeafTest() {
97 json = TestUtils.writeCompNodeWithSchemaContextToJson(
98 TestUtils.loadCompositeNode("/yang-to-json-conversion/leafref/xml/data_ref_to_not_leaf.xml"),
99 "/yang-to-json-conversion/leafref/xml", modules, dataSchemaNode);
100 } catch (WebApplicationException | IOException e) {
101 // shouldn't end here
105 java.util.regex.Pattern ptrn = java.util.regex.Pattern.compile(
106 ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf6\":\\p{Blank}*\"44.33\".*",
107 java.util.regex.Pattern.DOTALL);
108 Matcher mtch = ptrn.matcher(json);
109 assertTrue(mtch.matches());
113 * Tests case when leaflist element is refers to leaf.
116 public void leafrefFromLeafListToLeafTest() {
120 .writeCompNodeWithSchemaContextToJson(
122 .loadCompositeNode("/yang-to-json-conversion/leafref/xml/data_relativ_ref_from_leaflist_to_existing_leaf.xml"),
123 "/yang-to-json-conversion/leafref/xml", modules, dataSchemaNode);
124 } catch (WebApplicationException | IOException e) {
125 // shouldn't end here
129 java.util.regex.Pattern ptrn = java.util.regex.Pattern
131 ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lflst1\":\\p{Blank}*.*345,\\p{Space}*346,\\p{Space}*347.*",
132 java.util.regex.Pattern.DOTALL);
133 Matcher mtch = ptrn.matcher(json);
134 assertTrue(mtch.matches());
138 * Tests case when leaflist element is refers to leaf.
141 public void leafrefFromLeafrefToLeafrefTest() {
144 json = TestUtils.writeCompNodeWithSchemaContextToJson(TestUtils
145 .loadCompositeNode("/yang-to-json-conversion/leafref/xml/data_from_leafref_to_leafref.xml"),
146 "/yang-to-json-conversion/leafref/xml", modules, dataSchemaNode);
147 } catch (WebApplicationException | IOException e) {
148 // shouldn't end here
152 java.util.regex.Pattern ptrn = java.util.regex.Pattern.compile(
153 ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf7\":\\p{Blank}*200.*", java.util.regex.Pattern.DOTALL);
154 Matcher mtch = ptrn.matcher(json);
155 assertTrue(mtch.matches());