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