Leafref and identityref types to Json
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / ToJsonLeafrefType.java
1 package org.opendaylight.controller.sal.restconf.impl.test;
2
3 import static org.junit.Assert.assertEquals;
4 import static org.junit.Assert.assertNotNull;
5 import static org.junit.Assert.assertTrue;
6
7 import java.io.IOException;
8 import java.util.Set;
9 import java.util.regex.Matcher;
10
11 import javax.ws.rs.WebApplicationException;
12
13 import org.junit.*;
14 import org.opendaylight.yangtools.yang.model.api.*;
15
16 public class ToJsonLeafrefType {
17     private static Set<Module> modules;
18     private static DataSchemaNode dataSchemaNode;
19
20     @BeforeClass
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);
28
29     }
30
31     @Test
32     public void leafrefAbsolutePathToExistingLeafTest() {
33         String json = null;
34         try {
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) {
39             // shouldn't end here
40             assertTrue(false);
41         }
42         assertNotNull(json);
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());
47     }
48
49     @Test
50     public void leafrefRelativePathToExistingLeafTest() {
51         String json = null;
52         try {
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) {
57             // shouldn't end here
58             assertTrue(false);
59         }
60         assertNotNull(json);
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());
65     }
66
67     /**
68      * Tests case when reference to not existing element is present. In this
69      * case value from single node is printed as string.
70      */
71     @Test
72     public void leafrefToNonExistingLeafTest() {
73         String json = null;
74         try {
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) {
79             // shouldn't end here
80             assertTrue(false);
81         }
82         assertNotNull(json);
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());
87     }
88
89     /**
90      * Tests case when non leaf element is referenced. In this case value from
91      * single node is printed as string.
92      */
93     @Test
94     public void leafrefToNotLeafTest() {
95         String json = null;
96         try {
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
102             assertTrue(false);
103         }
104         assertNotNull(json);
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());
110     }
111
112     /**
113      * Tests case when leaflist element is refers to leaf.
114      */
115     @Test
116     public void leafrefFromLeafListToLeafTest() {
117         String json = null;
118         try {
119             json = TestUtils
120                     .writeCompNodeWithSchemaContextToJson(
121                             TestUtils
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
126             assertTrue(false);
127         }
128         assertNotNull(json);
129         java.util.regex.Pattern ptrn = java.util.regex.Pattern
130                 .compile(
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());
135     }
136
137     /**
138      * Tests case when leaflist element is refers to leaf.
139      */
140     @Test
141     public void leafrefFromLeafrefToLeafrefTest() {
142         String json = null;
143         try {
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
149             assertTrue(false);
150         }
151         assertNotNull(json);
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());
156     }
157
158 }