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