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