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() throws NoSuchFieldException,
31 jsonBodyWriter = new NormalizedNodeJsonBodyWriter();
35 public static void initialization() {
36 schemaContext = schemaContextLoader("/nn-to-json/choice", schemaContext);
37 CONTROLLER_CONTEXT.setSchemas(schemaContext);
41 * Test when some data are in one case node and other in another. This isn't
42 * correct. Next Json validator should return error because nodes has to be
43 * from one case below concrete choice.
45 @Test(expected = NullPointerException.class)
46 public void nodeSchemasOnVariousChoiceCasePathTest() throws Exception {
47 getJson("/nn-to-json/choice/xml/data_various_path_err.xml");
51 * Test when some data are in one case node and other in another.
52 * Additionally data are loadef from various choices. This isn't correct.
53 * Next Json validator should return error because nodes has to be from one
54 * case below concrete choice.
56 @Test(expected = NullPointerException.class)
57 public void nodeSchemasOnVariousChoiceCasePathAndMultipleChoicesTest()
59 getJson("/nn-to-json/choice/xml/data_more_choices_same_level_various_paths_err.xml");
63 * Test when second level data are red first, then first and at the end
64 * third level. Level represents pass through couple choice-case
68 public void nodeSchemasWithRandomOrderAccordingLevel() throws Exception {
69 final String json = getJson("/nn-to-json/choice/xml/data_random_level.xml");
71 assertTrue(json.contains("cont"));
72 assertTrue(json.contains("\"lf1\":\"lf1 val\""));
73 assertTrue(json.contains("\"lf1aaa\":\"lf1aaa val\""));
74 assertTrue(json.contains("\"lf1aa\":\"lf1aa val\""));
75 assertTrue(json.contains("\"lf1a\":121"));
79 * Test when element from no first case is used.
82 public void nodeSchemasNotInFirstCase() throws Exception {
83 final String json = getJson("/nn-to-json/choice/xml/data_no_first_case.xml");
85 assertTrue(json.contains("cont"));
86 assertTrue(json.contains("\"lf1\":\"lf1 val\""));
87 assertTrue(json.contains("\"lf1ab\":\"lf1ab val\""));
88 assertTrue(json.contains("\"lf1a\":121"));
92 * Test when element in case is list.
95 public void nodeSchemaAsList() throws Exception {
96 final String json = getJson("/nn-to-json/choice/xml/data_list.xml");
98 assertTrue(json.contains("cont"));
99 assertTrue(json.contains("\"lst1b\":["));
100 assertTrue(json.contains("{\"lf11b\":\"lf11b_1 val\"}"));
101 assertTrue(json.contains("{\"lf11b\":\"lf11b_2 val\"}"));
105 * Test when element in case is container.
108 public void nodeSchemaAsContainer() throws Exception {
109 final String json = getJson("/nn-to-json/choice/xml/data_container.xml");
111 assertTrue(json.contains("cont"));
112 assertTrue(json.contains("\"cont1c\":{"));
113 assertTrue(json.contains("\"lf11c\":\"lf11c val\""));
117 * Test when element in case is leaflist.
120 public void nodeSchemaAsLeafList() throws Exception {
121 final String json = getJson("/nn-to-json/choice/xml/data_leaflist.xml");
123 assertTrue(json.contains("cont"));
124 assertTrue(json.contains("\"lflst1d\":["));
125 assertTrue(json.contains("\"lflst1d_1 val\""));
126 assertTrue(json.contains("\"lflst1d_2 val\""));
130 public void nodeSchemasInMultipleChoicesTest() throws Exception {
131 final String json = getJson("/nn-to-json/choice/xml/data_more_choices_same_level.xml");
133 assertTrue(json.contains("cont"));
134 assertTrue(json.contains("\"lf2b\":\"lf2b value\""));
135 assertTrue(json.contains("\"cont1c\":{"));
136 assertTrue(json.contains("\"lf11c\":\"lf11c val\""));
140 * Test whether is possible to find data schema for node which is specified
141 * as dirrect subnode of choice (case without CASE key word).
144 public void nodeSchemasInCaseNotDefinedWithCaseKeyword() throws Exception {
145 final String json = getJson("/nn-to-json/choice/xml/data_case_defined_without_case.xml");
147 assertTrue(json.contains("cont"));
148 assertTrue(json.contains("\"lf2b\":\"lf2b val\""));
149 assertTrue(json.contains("\"e1\":45"));
153 * Test of multiple use of choices.
156 public void nodeSchemasInThreeChoicesAtSameLevel() throws Exception {
157 final String json = getJson("/nn-to-json/choice/xml/data_three_choices_same_level.xml");
159 assertTrue(json.contains("cont"));
160 assertTrue(json.contains("lf2b\":\"lf2b value"));
161 assertTrue(json.contains("lst4a\":[{"));
162 assertTrue(json.contains("{\"lf4ab\":33}"));
163 assertTrue(json.contains("{\"lf4ab\":37}"));
164 assertTrue(json.contains("\"lf1aaa\":\"lf1aaa value\""));
167 private String getJson(final String xmlPath) throws Exception {
168 final String uri = "choice-case-test:cont";
169 final NormalizedNodeContext testNN = TestRestconfUtils
170 .loadNormalizedContextFromXmlFile(xmlPath, uri);
172 final OutputStream output = new ByteArrayOutputStream();
173 jsonBodyWriter.writeTo(testNN, null, null, null, mediaType, null,
176 return output.toString();
180 protected MediaType getMediaType() {