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.rest.impl.XmlToCompositeNodeProvider;
16 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
17 import org.opendaylight.controller.sal.restconf.impl.test.YangAndXmlAndDataSchemaLoader;
18 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
22 * All tests are commented now because leafref isn't supported now
26 public class CnSnToJsonLeafrefType extends YangAndXmlAndDataSchemaLoader {
29 public static void initialization() {
30 dataLoad("/cnsn-to-json/leafref", 2, "main-module", "cont");
34 public void leafrefAbsolutePathToExistingLeafTest() {
35 String json = toJson("/cnsn-to-json/leafref/xml/data_absolut_ref_to_existing_leaf.xml");
36 validateJson(".*\"lf3\":\\p{Blank}*\"true\".*", json);
40 public void leafrefRelativePathToExistingLeafTest() {
41 String json = toJson("/cnsn-to-json/leafref/xml/data_relativ_ref_to_existing_leaf.xml");
42 validateJson(".*\"lf2\":\\p{Blank}*\"121\".*", json);
46 * Tests case when reference to not existing element is present. In this
47 * case value from single node is printed as string.
50 public void leafrefToNonExistingLeafTest() {
51 String json = toJson("/cnsn-to-json/leafref/xml/data_ref_to_non_existing_leaf.xml");
52 validateJson(".*\"lf5\":\\p{Blank}*\"137\".*", json);
56 * Tests case when non leaf element is referenced. In this case value from
57 * single node is printed as string.
60 public void leafrefToNotLeafTest() {
61 String json = toJson("/cnsn-to-json/leafref/xml/data_ref_to_not_leaf.xml");
62 validateJson(".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf6\":\\p{Blank}*\"44.33\".*", json);
66 * Tests case when leaflist element is refers to leaf.
69 public void leafrefFromLeafListToLeafTest() {
70 String json = toJson("/cnsn-to-json/leafref/xml/data_relativ_ref_from_leaflist_to_existing_leaf.xml");
72 ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lflst1\":\\p{Blank}*.*\"345\",\\p{Space}*\"346\",\\p{Space}*\"347\".*",
77 * Tests case when leaflist element is refers to leaf.
80 public void leafrefFromLeafrefToLeafrefTest() {
81 String json = toJson("/cnsn-to-json/leafref/xml/data_from_leafref_to_leafref.xml");
82 validateJson(".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf7\":\\p{Blank}*\"200\".*", json);
85 private void validateJson(String regex, String value) {
87 Pattern ptrn = Pattern.compile(regex, Pattern.DOTALL);
88 Matcher mtch = ptrn.matcher(value);
89 assertTrue(mtch.matches());
92 private String toJson(String xmlDataPath) {
94 CompositeNode compositeNode = TestUtils.readInputToCnSn(xmlDataPath, XmlToCompositeNodeProvider.INSTANCE);
95 TestUtils.normalizeCompositeNode(compositeNode, modules, searchedModuleName + ":" + searchedDataSchemaName);
96 return TestUtils.writeCompNodeWithSchemaContextToOutput(compositeNode, modules, dataSchemaNode,
97 StructuredDataToJsonProvider.INSTANCE);
98 } catch (WebApplicationException | IOException e) {