Merge changes from topic 'rem_web_xml'
[netconf.git] / restconf / restconf-nb-bierman02 / src / test / java / org / opendaylight / controller / sal / restconf / impl / nn / to / json / test / NnJsonChoiceCaseTest.java
1 /*
2  * Copyright (c) 2015 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
9 package org.opendaylight.controller.sal.restconf.impl.nn.to.json.test;
10
11 import static org.junit.Assert.assertTrue;
12
13 import java.io.ByteArrayOutputStream;
14 import java.io.OutputStream;
15 import javax.ws.rs.core.MediaType;
16 import org.junit.BeforeClass;
17 import org.junit.Test;
18 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
19 import org.opendaylight.controller.sal.rest.impl.test.providers.AbstractBodyReaderTest;
20 import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
21 import org.opendaylight.restconf.common.context.NormalizedNodeContext;
22 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
23
24 public class NnJsonChoiceCaseTest extends AbstractBodyReaderTest {
25
26     private static SchemaContext schemaContext;
27     private final NormalizedNodeJsonBodyWriter jsonBodyWriter;
28
29     public NnJsonChoiceCaseTest()  {
30         super(schemaContext, null);
31         jsonBodyWriter = new NormalizedNodeJsonBodyWriter();
32     }
33
34     @BeforeClass
35     public static void initialization() {
36         schemaContext = schemaContextLoader("/nn-to-json/choice", schemaContext);
37     }
38
39     /**
40      * Test when some data are in one case node and other in another. This isn't
41      * correct. Next Json validator should return error because nodes has to be
42      * from one case below concrete choice.
43      */
44     @Test(expected = NullPointerException.class)
45     public void nodeSchemasOnVariousChoiceCasePathTest() throws Exception {
46         getJson("/nn-to-json/choice/xml/data_various_path_err.xml");
47     }
48
49     /**
50      * Test when some data are in one case node and other in another.
51      * Additionally data are loadef from various choices. This isn't correct.
52      * Next Json validator should return error because nodes has to be from one
53      * case below concrete choice.
54      */
55     @Test(expected = NullPointerException.class)
56     public void nodeSchemasOnVariousChoiceCasePathAndMultipleChoicesTest()
57             throws Exception {
58         getJson("/nn-to-json/choice/xml/data_more_choices_same_level_various_paths_err.xml");
59     }
60
61     /**
62      * Test when second level data are red first, then first and at the end
63      * third level. Level represents pass through couple choice-case
64      */
65
66     @Test
67     public void nodeSchemasWithRandomOrderAccordingLevel() throws Exception {
68         final String json = getJson("/nn-to-json/choice/xml/data_random_level.xml");
69
70         assertTrue(json.contains("cont"));
71         assertTrue(json.contains("\"lf1\":\"lf1 val\""));
72         assertTrue(json.contains("\"lf1aaa\":\"lf1aaa val\""));
73         assertTrue(json.contains("\"lf1aa\":\"lf1aa val\""));
74         assertTrue(json.contains("\"lf1a\":121"));
75     }
76
77     /**
78      * Test when element from no first case is used.
79      */
80     @Test
81     public void nodeSchemasNotInFirstCase() throws Exception {
82         final String json = getJson("/nn-to-json/choice/xml/data_no_first_case.xml");
83
84         assertTrue(json.contains("cont"));
85         assertTrue(json.contains("\"lf1\":\"lf1 val\""));
86         assertTrue(json.contains("\"lf1ab\":\"lf1ab val\""));
87         assertTrue(json.contains("\"lf1a\":121"));
88     }
89
90     /**
91      * Test when element in case is list.
92      */
93     @Test
94     public void nodeSchemaAsList() throws Exception {
95         final String json = getJson("/nn-to-json/choice/xml/data_list.xml");
96
97         assertTrue(json.contains("cont"));
98         assertTrue(json.contains("\"lst1b\":["));
99         assertTrue(json.contains("{\"lf11b\":\"lf11b_1 val\"}"));
100         assertTrue(json.contains("{\"lf11b\":\"lf11b_2 val\"}"));
101     }
102
103     /**
104      * Test when element in case is container.
105      */
106     @Test
107     public void nodeSchemaAsContainer() throws Exception {
108         final String json = getJson("/nn-to-json/choice/xml/data_container.xml");
109
110         assertTrue(json.contains("cont"));
111         assertTrue(json.contains("\"cont1c\":{"));
112         assertTrue(json.contains("\"lf11c\":\"lf11c val\""));
113     }
114
115     /**
116      * Test when element in case is leaflist.
117      */
118     @Test
119     public void nodeSchemaAsLeafList() throws Exception {
120         final String json = getJson("/nn-to-json/choice/xml/data_leaflist.xml");
121
122         assertTrue(json.contains("cont"));
123         assertTrue(json.contains("\"lflst1d\":["));
124         assertTrue(json.contains("\"lflst1d_1 val\""));
125         assertTrue(json.contains("\"lflst1d_2 val\""));
126     }
127
128     @Test
129     public void nodeSchemasInMultipleChoicesTest() throws Exception {
130         final String json = getJson("/nn-to-json/choice/xml/data_more_choices_same_level.xml");
131
132         assertTrue(json.contains("cont"));
133         assertTrue(json.contains("\"lf2b\":\"lf2b value\""));
134         assertTrue(json.contains("\"cont1c\":{"));
135         assertTrue(json.contains("\"lf11c\":\"lf11c val\""));
136     }
137
138     /**
139      * Test whether is possible to find data schema for node which is specified
140      * as dirrect subnode of choice (case without CASE key word).
141      */
142     @Test
143     public void nodeSchemasInCaseNotDefinedWithCaseKeyword() throws Exception {
144         final String json = getJson("/nn-to-json/choice/xml/data_case_defined_without_case.xml");
145
146         assertTrue(json.contains("cont"));
147         assertTrue(json.contains("\"lf2b\":\"lf2b val\""));
148         assertTrue(json.contains("\"e1\":45"));
149     }
150
151     /**
152      * Test of multiple use of choices.
153      */
154     @Test
155     public void nodeSchemasInThreeChoicesAtSameLevel() throws Exception {
156         final String json = getJson("/nn-to-json/choice/xml/data_three_choices_same_level.xml");
157
158         assertTrue(json.contains("cont"));
159         assertTrue(json.contains("lf2b\":\"lf2b value"));
160         assertTrue(json.contains("lst4a\":[{"));
161         assertTrue(json.contains("{\"lf4ab\":33}"));
162         assertTrue(json.contains("{\"lf4ab\":37}"));
163         assertTrue(json.contains("\"lf1aaa\":\"lf1aaa value\""));
164     }
165
166     private String getJson(final String xmlPath) throws Exception {
167         final String uri = "choice-case-test:cont";
168         final NormalizedNodeContext testNN = TestRestconfUtils
169                 .loadNormalizedContextFromXmlFile(xmlPath, uri, controllerContext);
170
171         final OutputStream output = new ByteArrayOutputStream();
172         jsonBodyWriter.writeTo(testNN, null, null, null, mediaType, null,
173                 output);
174
175         return output.toString();
176     }
177
178     @Override
179     protected MediaType getMediaType() {
180         return null;
181     }
182 }