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