Merge "Fix for Bug 3"
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / cnsn / to / json / test / CnSnToJsonLeafrefType.java
1 package org.opendaylight.controller.sal.restconf.impl.cnsn.to.json.test;
2
3 import static org.junit.Assert.assertNotNull;
4 import static org.junit.Assert.assertTrue;
5
6 import java.io.IOException;
7 import java.util.regex.Matcher;
8 import java.util.regex.Pattern;
9
10 import javax.ws.rs.WebApplicationException;
11
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;
19
20 /**
21  * 
22  * All tests are commented now because leafref isn't supported now
23  * 
24  */
25
26 public class CnSnToJsonLeafrefType extends YangAndXmlAndDataSchemaLoader {
27
28     @BeforeClass
29     public static void initialization() {
30         dataLoad("/cnsn-to-json/leafref", 2, "main-module", "cont");
31     }
32
33     @Test
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);
37     }
38
39     @Test
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);
43     }
44
45     /**
46      * Tests case when reference to not existing element is present. In this
47      * case value from single node is printed as string.
48      */
49     @Test
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);
53     }
54
55     /**
56      * Tests case when non leaf element is referenced. In this case value from
57      * single node is printed as string.
58      */
59     @Test
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);
63     }
64
65     /**
66      * Tests case when leaflist element is refers to leaf.
67      */
68     @Test
69     public void leafrefFromLeafListToLeafTest() {
70         String json = toJson("/cnsn-to-json/leafref/xml/data_relativ_ref_from_leaflist_to_existing_leaf.xml");
71         validateJson(
72                 ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lflst1\":\\p{Blank}*.*\"345\",\\p{Space}*\"346\",\\p{Space}*\"347\".*",
73                 json);
74     }
75
76     /**
77      * Tests case when leaflist element is refers to leaf.
78      */
79     @Test
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);
83     }
84
85     private void validateJson(String regex, String value) {
86         assertNotNull(value);
87         Pattern ptrn = Pattern.compile(regex, Pattern.DOTALL);
88         Matcher mtch = ptrn.matcher(value);
89         assertTrue(mtch.matches());
90     }
91
92     private String toJson(String xmlDataPath) {
93         try {
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) {
99         }
100         return "";
101     }
102
103 }