1 package org.opendaylight.controller.sal.restconf.impl.nn.to.json.test;
3 import static org.junit.Assert.assertTrue;
4 import java.io.ByteArrayOutputStream;
5 import java.io.OutputStream;
6 import javax.ws.rs.core.MediaType;
7 import org.junit.BeforeClass;
9 import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
10 import org.opendaylight.controller.sal.rest.impl.NormalizedNodeJsonBodyWriter;
11 import org.opendaylight.controller.sal.rest.impl.test.providers.AbstractBodyReaderTest;
12 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
13 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
15 public class NnJsonChoiceCaseTest extends AbstractBodyReaderTest {
17 private static SchemaContext schemaContext;
18 private final NormalizedNodeJsonBodyWriter jsonBodyWriter;
20 public NnJsonChoiceCaseTest() throws NoSuchFieldException,
23 jsonBodyWriter = new NormalizedNodeJsonBodyWriter();
27 public static void initialization() {
28 schemaContext = schemaContextLoader("/nn-to-json/choice", schemaContext);
29 controllerContext.setSchemas(schemaContext);
33 * Test when some data are in one case node and other in another. This isn't
34 * correct. Next Json validator should return error because nodes has to be
35 * from one case below concrete choice.
37 @Test(expected = NullPointerException.class)
38 public void nodeSchemasOnVariousChoiceCasePathTest() throws Exception {
39 getJson("/nn-to-json/choice/xml/data_various_path_err.xml");
43 * Test when some data are in one case node and other in another.
44 * Additionally data are loadef from various choices. This isn't correct.
45 * Next Json validator should return error because nodes has to be from one
46 * case below concrete choice.
48 @Test(expected = NullPointerException.class)
49 public void nodeSchemasOnVariousChoiceCasePathAndMultipleChoicesTest()
51 getJson("/nn-to-json/choice/xml/data_more_choices_same_level_various_paths_err.xml");
55 * Test when second level data are red first, then first and at the end
56 * third level. Level represents pass through couple choice-case
60 public void nodeSchemasWithRandomOrderAccordingLevel() throws Exception {
61 final String json = getJson("/nn-to-json/choice/xml/data_random_level.xml");
63 assertTrue(json.contains("cont"));
64 assertTrue(json.contains("\"lf1\":\"lf1 val\""));
65 assertTrue(json.contains("\"lf1aaa\":\"lf1aaa val\""));
66 assertTrue(json.contains("\"lf1aa\":\"lf1aa val\""));
67 assertTrue(json.contains("\"lf1a\":121"));
71 * Test when element from no first case is used
74 public void nodeSchemasNotInFirstCase() throws Exception {
75 final String json = getJson("/nn-to-json/choice/xml/data_no_first_case.xml");
77 assertTrue(json.contains("cont"));
78 assertTrue(json.contains("\"lf1\":\"lf1 val\""));
79 assertTrue(json.contains("\"lf1ab\":\"lf1ab val\""));
80 assertTrue(json.contains("\"lf1a\":121"));
84 * Test when element in case is list
87 public void nodeSchemaAsList() throws Exception {
88 final String json = getJson("/nn-to-json/choice/xml/data_list.xml");
90 assertTrue(json.contains("cont"));
91 assertTrue(json.contains("\"lst1b\":["));
92 assertTrue(json.contains("{\"lf11b\":\"lf11b_1 val\"}"));
93 assertTrue(json.contains("{\"lf11b\":\"lf11b_2 val\"}"));
97 * Test when element in case is container
100 public void nodeSchemaAsContainer() throws Exception {
101 final String json = getJson("/nn-to-json/choice/xml/data_container.xml");
103 assertTrue(json.contains("cont"));
104 assertTrue(json.contains("\"cont1c\":{"));
105 assertTrue(json.contains("\"lf11c\":\"lf11c val\""));
109 * Test when element in case is leaflist
112 public void nodeSchemaAsLeafList() throws Exception {
113 final String json = getJson("/nn-to-json/choice/xml/data_leaflist.xml");
115 assertTrue(json.contains("cont"));
116 assertTrue(json.contains("\"lflst1d\":["));
117 assertTrue(json.contains("\"lflst1d_1 val\""));
118 assertTrue(json.contains("\"lflst1d_2 val\""));
125 public void nodeSchemasInMultipleChoicesTest() throws Exception {
126 final String json = getJson("/nn-to-json/choice/xml/data_more_choices_same_level.xml");
128 assertTrue(json.contains("cont"));
129 assertTrue(json.contains("\"lf2b\":\"lf2b value\""));
130 assertTrue(json.contains("\"cont1c\":{"));
131 assertTrue(json.contains("\"lf11c\":\"lf11c val\""));
135 * Test whether is possible to find data schema for node which is specified
136 * as dirrect subnode of choice (case without CASE key word)
139 public void nodeSchemasInCaseNotDefinedWithCaseKeyword() throws Exception {
140 final String json = getJson("/nn-to-json/choice/xml/data_case_defined_without_case.xml");
142 assertTrue(json.contains("cont"));
143 assertTrue(json.contains("\"lf2b\":\"lf2b val\""));
144 assertTrue(json.contains("\"e1\":45"));
148 * Test of multiple use of choices
151 public void nodeSchemasInThreeChoicesAtSameLevel() throws Exception {
152 final String json = getJson("/nn-to-json/choice/xml/data_three_choices_same_level.xml");
154 assertTrue(json.contains("cont"));
155 assertTrue(json.contains("lf2b\":\"lf2b value"));
156 assertTrue(json.contains("lst4a\":[{"));
157 assertTrue(json.contains("{\"lf4ab\":33}"));
158 assertTrue(json.contains("{\"lf4ab\":37}"));
159 assertTrue(json.contains("\"lf1aaa\":\"lf1aaa value\""));
162 private String getJson(final String xmlPath) throws Exception {
163 final String uri = "choice-case-test:cont";
164 final NormalizedNodeContext testNN = TestRestconfUtils
165 .loadNormalizedContextFromXmlFile(xmlPath, uri);
167 final OutputStream output = new ByteArrayOutputStream();
168 jsonBodyWriter.writeTo(testNN, null, null, null, mediaType, null,
171 return output.toString();
175 protected MediaType getMediaType() {