8e3ff1d2ed4a998997181a897a1c8ff648099aaa
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / test / ToJsonChoiceCaseTest.java
1 package org.opendaylight.controller.sal.restconf.impl.test;
2
3 import static org.junit.Assert.*;
4
5 import java.io.IOException;
6 import java.util.Set;
7
8 import javax.activation.UnsupportedDataTypeException;
9 import javax.ws.rs.WebApplicationException;
10
11 import org.junit.*;
12 import org.opendaylight.yangtools.yang.model.api.*;
13
14 public class ToJsonChoiceCaseTest {
15
16     private static Set<Module> modules;
17     private static DataSchemaNode dataSchemaNode;
18
19     @BeforeClass
20     public static void initialization() {
21         modules = TestUtils.resolveModules("/yang-to-json-conversion/choice");
22         Module module = TestUtils.resolveModule(null, modules);
23         dataSchemaNode = TestUtils.resolveDataSchemaNode(module, null);
24
25     }
26
27     /**
28      * Test when some data are in one case node and other in another. This isn't
29      * correct. Next Json validator should return error because nodes has to be
30      * from one case below concrete choice.
31      * 
32      */
33     @Test
34     public void nodeSchemasOnVariousChoiceCasePathTest() {
35         try {
36             TestUtils.writeCompNodeWithSchemaContextToJson(
37                     TestUtils.loadCompositeNode("/yang-to-json-conversion/choice/xml/data_various_path_err.xml"),
38                     "/yang-to-json-conversion/choice/xml", modules, dataSchemaNode);
39         } catch (WebApplicationException | IOException e) {
40             // shouldn't end here
41             assertTrue(false);
42         }
43     }
44
45     /**
46      * Test when some data are in one case node and other in another.
47      * Additionally data are loadef from various choices. This isn't
48      * correct. Next Json validator should return error because nodes has to be
49      * from one case below concrete choice.
50      * 
51      */
52     @Test
53     public void nodeSchemasOnVariousChoiceCasePathAndMultipleChoicesTest() {
54         try {
55             TestUtils
56                     .writeCompNodeWithSchemaContextToJson(
57                             TestUtils
58                                     .loadCompositeNode("/yang-to-json-conversion/choice/xml/data_more_choices_same_level_various_paths_err.xml"),
59                             "/yang-to-json-conversion/choice/xml", modules, dataSchemaNode);
60         } catch (WebApplicationException | IOException e) {
61             // shouldn't end here
62             assertTrue(false);
63         }
64     }
65
66     /**
67      * Test when second level data are red first, then first and at the end
68      * third level. Level represents pass through couple choice-case
69      */
70
71     @Test
72     public void nodeSchemasWithRandomOrderAccordingLevel() {
73         try {
74             TestUtils.writeCompNodeWithSchemaContextToJson(
75                     TestUtils.loadCompositeNode("/yang-to-json-conversion/choice/xml/data_random_level.xml"),
76                     "/yang-to-json-conversion/choice/xml", modules, dataSchemaNode);
77
78         } catch (WebApplicationException | IOException e) {
79             // shouldn't end here
80             assertTrue(false);
81         }
82     }
83
84     /**
85      * Test when element from no first case is used
86      */
87     @Test
88     public void nodeSchemasNotInFirstCase() {
89         try {
90             TestUtils.writeCompNodeWithSchemaContextToJson(
91                     TestUtils.loadCompositeNode("/yang-to-json-conversion/choice/xml/data_no_first_case.xml"),
92                     "/yang-to-json-conversion/choice/xml", modules, dataSchemaNode);
93         } catch (WebApplicationException | IOException e) {
94             // shouldn't end here
95             assertTrue(false);
96         }
97     }
98
99     /**
100      * Test when element in case is list
101      */
102     @Test
103     public void nodeSchemaAsList() {
104         try {
105             TestUtils.writeCompNodeWithSchemaContextToJson(
106                     TestUtils.loadCompositeNode("/yang-to-json-conversion/choice/xml/data_list.xml"),
107                     "/yang-to-json-conversion/choice/xml", modules, dataSchemaNode);
108         } catch (WebApplicationException | IOException e) {
109             // shouldn't end here
110             assertTrue(false);
111         }
112     }
113
114     /**
115      * Test when element in case is container
116      */
117     @Test
118     public void nodeSchemaAsContainer() {
119         try {
120             TestUtils.writeCompNodeWithSchemaContextToJson(
121                     TestUtils.loadCompositeNode("/yang-to-json-conversion/choice/xml/data_container.xml"),
122                     "/yang-to-json-conversion/choice/xml", modules, dataSchemaNode);
123         } catch (WebApplicationException | IOException e) {
124             // shouldn't end here
125             assertTrue(false);
126         }
127     }
128
129     /**
130      * Test when element in case is leaflist
131      */
132     @Test
133     public void nodeSchemaAsLeafList() {
134         try {
135             TestUtils.writeCompNodeWithSchemaContextToJson(
136                     TestUtils.loadCompositeNode("/yang-to-json-conversion/choice/xml/data_leaflist.xml"),
137                     "/yang-to-json-conversion/choice/xml", modules, dataSchemaNode);
138         } catch (WebApplicationException | IOException e) {
139             // shouldn't end here
140             assertTrue(false);
141         }
142     }
143
144     /**
145      * 
146      */
147     @Test
148     public void nodeSchemasInMultipleChoicesTest() {
149         try {
150             TestUtils
151                     .writeCompNodeWithSchemaContextToJson(TestUtils
152                             .loadCompositeNode("/yang-to-json-conversion/choice/xml/data_more_choices_same_level.xml"),
153                             "/yang-to-json-conversion/choice/xml", modules, dataSchemaNode);
154         } catch (WebApplicationException | IOException e) {
155             // shouldn't end here
156             assertTrue(false);
157         }
158     }
159
160     /**
161      * Test whether is possible to find data schema for node which is specified
162      * as dirrect subnode of choice (case without CASE key word)
163      */
164     @Test
165     public void nodeSchemasInCaseNotDefinedWithCaseKeyword() {
166         try {
167             TestUtils.writeCompNodeWithSchemaContextToJson(TestUtils
168                     .loadCompositeNode("/yang-to-json-conversion/choice/xml/data_case_defined_without_case.xml"),
169                     "/yang-to-json-conversion/choice/xml", modules, dataSchemaNode);
170         } catch (WebApplicationException | IOException e) {
171             // shouldn't end here
172             assertTrue(false);
173         }
174     }
175
176     /**
177      * Test of multiple use of choices
178      */
179     @Test
180     public void nodeSchemasInThreeChoicesAtSameLevel() {
181         try {
182             TestUtils.writeCompNodeWithSchemaContextToJson(TestUtils
183                     .loadCompositeNode("/yang-to-json-conversion/choice/xml/data_three_choices_same_level.xml"),
184                     "/yang-to-json-conversion/choice/xml", modules, dataSchemaNode);
185         } catch (WebApplicationException | IOException e) {
186             // shouldn't end here
187             assertTrue(false);
188         }
189     }
190 }