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