2 * Copyright (c) 2015 Cisco Systems, Inc. and others. All rights reserved.
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
9 package org.opendaylight.controller.sal.restconf.impl.nn.to.json.test;
11 import static org.junit.Assert.assertTrue;
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;
24 public class NnJsonChoiceCaseTest extends AbstractBodyReaderTest {
26 private static SchemaContext schemaContext;
27 private final NormalizedNodeJsonBodyWriter jsonBodyWriter;
29 public NnJsonChoiceCaseTest() {
30 super(schemaContext, null);
31 jsonBodyWriter = new NormalizedNodeJsonBodyWriter();
35 public static void initialization() {
36 schemaContext = schemaContextLoader("/nn-to-json/choice", schemaContext);
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.
44 @Test(expected = NullPointerException.class)
45 public void nodeSchemasOnVariousChoiceCasePathTest() throws Exception {
46 getJson("/nn-to-json/choice/xml/data_various_path_err.xml");
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.
55 @Test(expected = NullPointerException.class)
56 public void nodeSchemasOnVariousChoiceCasePathAndMultipleChoicesTest()
58 getJson("/nn-to-json/choice/xml/data_more_choices_same_level_various_paths_err.xml");
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
67 public void nodeSchemasWithRandomOrderAccordingLevel() throws Exception {
68 final String json = getJson("/nn-to-json/choice/xml/data_random_level.xml");
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"));
78 * Test when element from no first case is used.
81 public void nodeSchemasNotInFirstCase() throws Exception {
82 final String json = getJson("/nn-to-json/choice/xml/data_no_first_case.xml");
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"));
91 * Test when element in case is list.
94 public void nodeSchemaAsList() throws Exception {
95 final String json = getJson("/nn-to-json/choice/xml/data_list.xml");
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\"}"));
104 * Test when element in case is container.
107 public void nodeSchemaAsContainer() throws Exception {
108 final String json = getJson("/nn-to-json/choice/xml/data_container.xml");
110 assertTrue(json.contains("cont"));
111 assertTrue(json.contains("\"cont1c\":{"));
112 assertTrue(json.contains("\"lf11c\":\"lf11c val\""));
116 * Test when element in case is leaflist.
119 public void nodeSchemaAsLeafList() throws Exception {
120 final String json = getJson("/nn-to-json/choice/xml/data_leaflist.xml");
122 assertTrue(json.contains("cont"));
123 assertTrue(json.contains("\"lflst1d\":["));
124 assertTrue(json.contains("\"lflst1d_1 val\""));
125 assertTrue(json.contains("\"lflst1d_2 val\""));
129 public void nodeSchemasInMultipleChoicesTest() throws Exception {
130 final String json = getJson("/nn-to-json/choice/xml/data_more_choices_same_level.xml");
132 assertTrue(json.contains("cont"));
133 assertTrue(json.contains("\"lf2b\":\"lf2b value\""));
134 assertTrue(json.contains("\"cont1c\":{"));
135 assertTrue(json.contains("\"lf11c\":\"lf11c val\""));
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).
143 public void nodeSchemasInCaseNotDefinedWithCaseKeyword() throws Exception {
144 final String json = getJson("/nn-to-json/choice/xml/data_case_defined_without_case.xml");
146 assertTrue(json.contains("cont"));
147 assertTrue(json.contains("\"lf2b\":\"lf2b val\""));
148 assertTrue(json.contains("\"e1\":45"));
152 * Test of multiple use of choices.
155 public void nodeSchemasInThreeChoicesAtSameLevel() throws Exception {
156 final String json = getJson("/nn-to-json/choice/xml/data_three_choices_same_level.xml");
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\""));
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);
171 final OutputStream output = new ByteArrayOutputStream();
172 jsonBodyWriter.writeTo(testNN, null, null, null, mediaType, null,
175 return output.toString();
179 protected MediaType getMediaType() {