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
8 package org.opendaylight.controller.sal.restconf.impl.nn.to.json.test;
10 import static org.junit.Assert.assertTrue;
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;
23 public class NnJsonChoiceCaseTest extends AbstractBodyReaderTest {
25 private static EffectiveModelContext schemaContext;
26 private final NormalizedNodeJsonBodyWriter jsonBodyWriter;
28 public NnJsonChoiceCaseTest() {
29 super(schemaContext, null);
30 jsonBodyWriter = new NormalizedNodeJsonBodyWriter();
34 public static void initialization() {
35 schemaContext = schemaContextLoader("/nn-to-json/choice", schemaContext);
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.
43 @Test(expected = NullPointerException.class)
44 public void nodeSchemasOnVariousChoiceCasePathTest() throws Exception {
45 getJson("/nn-to-json/choice/xml/data_various_path_err.xml");
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.
54 @Test(expected = NullPointerException.class)
55 public void nodeSchemasOnVariousChoiceCasePathAndMultipleChoicesTest()
57 getJson("/nn-to-json/choice/xml/data_more_choices_same_level_various_paths_err.xml");
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
66 public void nodeSchemasWithRandomOrderAccordingLevel() throws Exception {
67 final String json = getJson("/nn-to-json/choice/xml/data_random_level.xml");
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"));
77 * Test when element from no first case is used.
80 public void nodeSchemasNotInFirstCase() throws Exception {
81 final String json = getJson("/nn-to-json/choice/xml/data_no_first_case.xml");
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"));
90 * Test when element in case is list.
93 public void nodeSchemaAsList() throws Exception {
94 final String json = getJson("/nn-to-json/choice/xml/data_list.xml");
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\"}"));
103 * Test when element in case is container.
106 public void nodeSchemaAsContainer() throws Exception {
107 final String json = getJson("/nn-to-json/choice/xml/data_container.xml");
109 assertTrue(json.contains("cont"));
110 assertTrue(json.contains("\"cont1c\":{"));
111 assertTrue(json.contains("\"lf11c\":\"lf11c val\""));
115 * Test when element in case is leaflist.
118 public void nodeSchemaAsLeafList() throws Exception {
119 final String json = getJson("/nn-to-json/choice/xml/data_leaflist.xml");
121 assertTrue(json.contains("cont"));
122 assertTrue(json.contains("\"lflst1d\":["));
123 assertTrue(json.contains("\"lflst1d_1 val\""));
124 assertTrue(json.contains("\"lflst1d_2 val\""));
128 public void nodeSchemasInMultipleChoicesTest() throws Exception {
129 final String json = getJson("/nn-to-json/choice/xml/data_more_choices_same_level.xml");
131 assertTrue(json.contains("cont"));
132 assertTrue(json.contains("\"lf2b\":\"lf2b value\""));
133 assertTrue(json.contains("\"cont1c\":{"));
134 assertTrue(json.contains("\"lf11c\":\"lf11c val\""));
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).
142 public void nodeSchemasInCaseNotDefinedWithCaseKeyword() throws Exception {
143 final String json = getJson("/nn-to-json/choice/xml/data_case_defined_without_case.xml");
145 assertTrue(json.contains("cont"));
146 assertTrue(json.contains("\"lf2b\":\"lf2b val\""));
147 assertTrue(json.contains("\"e1\":45"));
151 * Test of multiple use of choices.
154 public void nodeSchemasInThreeChoicesAtSameLevel() throws Exception {
155 final String json = getJson("/nn-to-json/choice/xml/data_three_choices_same_level.xml");
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\""));
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);
170 final OutputStream output = new ByteArrayOutputStream();
171 jsonBodyWriter.writeTo(testNN, null, null, null, mediaType, null,
174 return output.toString();
178 protected MediaType getMediaType() {