More test for improving of code coverage + test refactoring
[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.restconf.impl.test.TestUtils;
16 import org.opendaylight.controller.sal.restconf.impl.test.YangAndXmlAndDataSchemaLoader;
17 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
18
19 /**
20  * 
21  * All tests are commented now because leafref isn't supported now
22  * 
23  */
24
25 public class CnSnToJsonLeafrefType extends YangAndXmlAndDataSchemaLoader {
26
27     @BeforeClass
28     public static void initialization() {
29         dataLoad("/cnsn-to-json/leafref", 2, "main-module", "cont");
30     }
31
32     @Test
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);
36     }
37
38     @Test
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);
42     }
43
44     /**
45      * Tests case when reference to not existing element is present. In this
46      * case value from single node is printed as string.
47      */
48     @Test
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);
52     }
53
54     /**
55      * Tests case when non leaf element is referenced. In this case value from
56      * single node is printed as string.
57      */
58     @Test
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);
62     }
63
64     /**
65      * Tests case when leaflist element is refers to leaf.
66      */
67     @Test
68     public void leafrefFromLeafListToLeafTest() {
69         String json = toJson("/cnsn-to-json/leafref/xml/data_relativ_ref_from_leaflist_to_existing_leaf.xml");
70         validateJson(
71                 ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lflst1\":\\p{Blank}*.*\"345\",\\p{Space}*\"346\",\\p{Space}*\"347\".*",
72                 json);
73     }
74
75     /**
76      * Tests case when leaflist element is refers to leaf.
77      */
78     @Test
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);
82     }
83
84     private void validateJson(String regex, String value) {
85         assertNotNull(value);
86         Pattern ptrn = Pattern.compile(regex, Pattern.DOTALL);
87         Matcher mtch = ptrn.matcher(value);
88         assertTrue(mtch.matches());
89     }
90
91     private String toJson(String xmlDataPath) {
92         try {
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) {
98         }
99         return "";
100     }
101
102 }