15122cfe3ad1101b1f9685bb763d8dce10e3fb4d
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / cnsn / to / json / test / CnSnJsonChoiceCaseTest.java
1 package org.opendaylight.controller.sal.restconf.impl.cnsn.to.json.test;
2
3 import static org.junit.Assert.assertTrue;
4
5 import java.io.IOException;
6
7 import javax.ws.rs.WebApplicationException;
8
9 import org.junit.BeforeClass;
10 import org.junit.Test;
11 import org.opendaylight.controller.sal.rest.impl.StructuredDataToJsonProvider;
12 import org.opendaylight.controller.sal.restconf.impl.test.TestUtils;
13 import org.opendaylight.controller.sal.restconf.impl.test.YangAndXmlAndDataSchemaLoader;
14 import org.opendaylight.yangtools.yang.data.api.CompositeNode;
15
16 public class CnSnJsonChoiceCaseTest extends YangAndXmlAndDataSchemaLoader {
17
18     @BeforeClass
19     public static void initialization() {
20         dataLoad("/cnsn-to-json/choice");
21     }
22
23     /**
24      * Test when some data are in one case node and other in another. This isn't
25      * correct. Next Json validator should return error because nodes has to be
26      * from one case below concrete choice.
27      * 
28      */
29     @Test
30     public void nodeSchemasOnVariousChoiceCasePathTest() {
31         testWrapper("/cnsn-to-json/choice/xml/data_various_path_err.xml", "choice-case-test:cont");
32     }
33
34     /**
35      * Test when some data are in one case node and other in another.
36      * Additionally data are loadef from various choices. This isn't correct.
37      * Next Json validator should return error because nodes has to be from one
38      * case below concrete choice.
39      * 
40      */
41     @Test
42     public void nodeSchemasOnVariousChoiceCasePathAndMultipleChoicesTest() {
43         testWrapper("/cnsn-to-json/choice/xml/data_more_choices_same_level_various_paths_err.xml",
44                 "choice-case-test:cont");
45     }
46
47     /**
48      * Test when second level data are red first, then first and at the end
49      * third level. Level represents pass through couple choice-case
50      */
51
52     @Test
53     public void nodeSchemasWithRandomOrderAccordingLevel() {
54         testWrapper("/cnsn-to-json/choice/xml/data_random_level.xml", "choice-case-test:cont");
55     }
56
57     /**
58      * Test when element from no first case is used
59      */
60     @Test
61     public void nodeSchemasNotInFirstCase() {
62         testWrapper("/cnsn-to-json/choice/xml/data_no_first_case.xml", "choice-case-test:cont");
63     }
64
65     /**
66      * Test when element in case is list
67      */
68     @Test
69     public void nodeSchemaAsList() {
70         testWrapper("/cnsn-to-json/choice/xml/data_list.xml", "choice-case-test:cont");
71     }
72
73     /**
74      * Test when element in case is container
75      */
76     @Test
77     public void nodeSchemaAsContainer() {
78         testWrapper("/cnsn-to-json/choice/xml/data_container.xml", "choice-case-test:cont");
79     }
80
81     /**
82      * Test when element in case is leaflist
83      */
84     @Test
85     public void nodeSchemaAsLeafList() {
86         testWrapper("/cnsn-to-json/choice/xml/data_leaflist.xml", "choice-case-test:cont");
87     }
88
89     /**
90      * 
91      */
92     @Test
93     public void nodeSchemasInMultipleChoicesTest() {
94         testWrapper("/cnsn-to-json/choice/xml/data_more_choices_same_level.xml", "choice-case-test:cont");
95     }
96
97     /**
98      * Test whether is possible to find data schema for node which is specified
99      * as dirrect subnode of choice (case without CASE key word)
100      */
101     @Test
102     public void nodeSchemasInCaseNotDefinedWithCaseKeyword() {
103         testWrapper("/cnsn-to-json/choice/xml/data_case_defined_without_case.xml", "choice-case-test:cont");
104     }
105
106     /**
107      * Test of multiple use of choices
108      */
109     @Test
110     public void nodeSchemasInThreeChoicesAtSameLevel() {
111         testWrapper("/cnsn-to-json/choice/xml/data_three_choices_same_level.xml", "choice-case-test:cont");
112     }
113
114     private void testWrapper(String xmlPath, String pathToSchemaNode) {
115         CompositeNode compNode = TestUtils.loadCompositeNode(xmlPath);
116         TestUtils.normalizeCompositeNode(compNode, modules, pathToSchemaNode);
117         try {
118             TestUtils.writeCompNodeWithSchemaContextToOutput(compNode, modules, dataSchemaNode,
119                     StructuredDataToJsonProvider.INSTANCE);
120         } catch (WebApplicationException | IOException e) {
121             // shouldn't end here
122             assertTrue(false);
123         }
124     }
125 }