Simplify method isMutualExclusive in Subnet.
[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.assertNotNull;
4 import static org.junit.Assert.assertTrue;
5
6 import java.io.IOException;
7 import java.util.regex.Matcher;
8
9 import javax.ws.rs.WebApplicationException;
10
11 import org.junit.*;
12
13 public class ToJsonLeafrefType extends YangAndXmlAndDataSchemaLoader {
14
15     @BeforeClass
16     public static void initialization() {
17         dataLoad("/yang-to-json-conversion/leafref", 2, "main-module", "cont");
18     }
19
20     @Test
21     public void leafrefAbsolutePathToExistingLeafTest() {
22         String json = null;
23         try {
24             json = TestUtils.writeCompNodeWithSchemaContextToJson(TestUtils
25                     .loadCompositeNode("/yang-to-json-conversion/leafref/xml/data_absolut_ref_to_existing_leaf.xml"),
26                     "/yang-to-json-conversion/leafref/xml", modules, dataSchemaNode);
27         } catch (WebApplicationException | IOException e) {
28             // shouldn't end here
29             assertTrue(false);
30         }
31         assertNotNull(json);
32         java.util.regex.Pattern ptrn = java.util.regex.Pattern.compile(".*\"lf3\":\\p{Blank}*true.*",
33                 java.util.regex.Pattern.DOTALL);
34         Matcher mtch = ptrn.matcher(json);
35         assertTrue(mtch.matches());
36     }
37
38     @Test
39     public void leafrefRelativePathToExistingLeafTest() {
40         String json = null;
41         try {
42             json = TestUtils.writeCompNodeWithSchemaContextToJson(TestUtils
43                     .loadCompositeNode("/yang-to-json-conversion/leafref/xml/data_relativ_ref_to_existing_leaf.xml"),
44                     "/yang-to-json-conversion/leafref/xml", modules, dataSchemaNode);
45         } catch (WebApplicationException | IOException e) {
46             // shouldn't end here
47             assertTrue(false);
48         }
49         assertNotNull(json);
50         java.util.regex.Pattern ptrn = java.util.regex.Pattern.compile(".*\"lf2\":\\p{Blank}*121.*",
51                 java.util.regex.Pattern.DOTALL);
52         Matcher mtch = ptrn.matcher(json);
53         assertTrue(mtch.matches());
54     }
55
56     /**
57      * Tests case when reference to not existing element is present. In this
58      * case value from single node is printed as string.
59      */
60     @Test
61     public void leafrefToNonExistingLeafTest() {
62         String json = null;
63         try {
64             json = TestUtils.writeCompNodeWithSchemaContextToJson(TestUtils
65                     .loadCompositeNode("/yang-to-json-conversion/leafref/xml/data_ref_to_non_existing_leaf.xml"),
66                     "/yang-to-json-conversion/leafref/xml", modules, dataSchemaNode);
67         } catch (WebApplicationException | IOException e) {
68             // shouldn't end here
69             assertTrue(false);
70         }
71         assertNotNull(json);
72         java.util.regex.Pattern ptrn = java.util.regex.Pattern.compile(".*\"lf5\":\\p{Blank}*\"137\".*",
73                 java.util.regex.Pattern.DOTALL);
74         Matcher mtch = ptrn.matcher(json);
75         assertTrue(mtch.matches());
76     }
77
78     /**
79      * Tests case when non leaf element is referenced. In this case value from
80      * single node is printed as string.
81      */
82     @Test
83     public void leafrefToNotLeafTest() {
84         String json = null;
85         try {
86             json = TestUtils.writeCompNodeWithSchemaContextToJson(
87                     TestUtils.loadCompositeNode("/yang-to-json-conversion/leafref/xml/data_ref_to_not_leaf.xml"),
88                     "/yang-to-json-conversion/leafref/xml", modules, dataSchemaNode);
89         } catch (WebApplicationException | IOException e) {
90             // shouldn't end here
91             assertTrue(false);
92         }
93         assertNotNull(json);
94         java.util.regex.Pattern ptrn = java.util.regex.Pattern.compile(
95                 ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf6\":\\p{Blank}*\"44.33\".*",
96                 java.util.regex.Pattern.DOTALL);
97         Matcher mtch = ptrn.matcher(json);
98         assertTrue(mtch.matches());
99     }
100
101     /**
102      * Tests case when leaflist element is refers to leaf.
103      */
104     @Test
105     public void leafrefFromLeafListToLeafTest() {
106         String json = null;
107         try {
108             json = TestUtils
109                     .writeCompNodeWithSchemaContextToJson(
110                             TestUtils
111                                     .loadCompositeNode("/yang-to-json-conversion/leafref/xml/data_relativ_ref_from_leaflist_to_existing_leaf.xml"),
112                             "/yang-to-json-conversion/leafref/xml", modules, dataSchemaNode);
113         } catch (WebApplicationException | IOException e) {
114             // shouldn't end here
115             assertTrue(false);
116         }
117         assertNotNull(json);
118         java.util.regex.Pattern ptrn = java.util.regex.Pattern
119                 .compile(
120                         ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lflst1\":\\p{Blank}*.*345,\\p{Space}*346,\\p{Space}*347.*",
121                         java.util.regex.Pattern.DOTALL);
122         Matcher mtch = ptrn.matcher(json);
123         assertTrue(mtch.matches());
124     }
125
126     /**
127      * Tests case when leaflist element is refers to leaf.
128      */
129     @Test
130     public void leafrefFromLeafrefToLeafrefTest() {
131         String json = null;
132         try {
133             json = TestUtils.writeCompNodeWithSchemaContextToJson(TestUtils
134                     .loadCompositeNode("/yang-to-json-conversion/leafref/xml/data_from_leafref_to_leafref.xml"),
135                     "/yang-to-json-conversion/leafref/xml", modules, dataSchemaNode);
136         } catch (WebApplicationException | IOException e) {
137             // shouldn't end here
138             assertTrue(false);
139         }
140         assertNotNull(json);
141         java.util.regex.Pattern ptrn = java.util.regex.Pattern.compile(
142                 ".*\"cont-augment-module\\p{Blank}*:\\p{Blank}*lf7\":\\p{Blank}*200.*", java.util.regex.Pattern.DOTALL);
143         Matcher mtch = ptrn.matcher(json);
144         assertTrue(mtch.matches());
145     }
146
147 }