b2bb6a8b29d8e6651e8b0417bf4e76129719e68e
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / nn / to / json / test / NnJsonChoiceCaseTest.java
1 package org.opendaylight.controller.sal.restconf.impl.nn.to.json.test;
2
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;
8 import org.junit.Test;
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;
14
15 public class NnJsonChoiceCaseTest extends AbstractBodyReaderTest {
16
17     private static SchemaContext schemaContext;
18     private final NormalizedNodeJsonBodyWriter jsonBodyWriter;
19
20     public NnJsonChoiceCaseTest() throws NoSuchFieldException,
21             SecurityException {
22         super();
23         jsonBodyWriter = new NormalizedNodeJsonBodyWriter();
24     }
25
26     @BeforeClass
27     public static void initialization() {
28         schemaContext = schemaContextLoader("/nn-to-json/choice", schemaContext);
29         controllerContext.setSchemas(schemaContext);
30     }
31
32     /**
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.
36      */
37     @Test(expected = NullPointerException.class)
38     public void nodeSchemasOnVariousChoiceCasePathTest() throws Exception {
39         getJson("/nn-to-json/choice/xml/data_various_path_err.xml");
40     }
41
42     /**
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.
47      */
48     @Test(expected = NullPointerException.class)
49     public void nodeSchemasOnVariousChoiceCasePathAndMultipleChoicesTest()
50             throws Exception {
51         getJson("/nn-to-json/choice/xml/data_more_choices_same_level_various_paths_err.xml");
52     }
53
54     /**
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
57      */
58
59     @Test
60     public void nodeSchemasWithRandomOrderAccordingLevel() throws Exception {
61         final String json = getJson("/nn-to-json/choice/xml/data_random_level.xml");
62
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"));
68     }
69
70     /**
71      * Test when element from no first case is used
72      */
73     @Test
74     public void nodeSchemasNotInFirstCase() throws Exception {
75         final String json = getJson("/nn-to-json/choice/xml/data_no_first_case.xml");
76
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"));
81     }
82
83     /**
84      * Test when element in case is list
85      */
86     @Test
87     public void nodeSchemaAsList() throws Exception {
88         final String json = getJson("/nn-to-json/choice/xml/data_list.xml");
89
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\"}"));
94     }
95
96     /**
97      * Test when element in case is container
98      */
99     @Test
100     public void nodeSchemaAsContainer() throws Exception {
101         final String json = getJson("/nn-to-json/choice/xml/data_container.xml");
102
103         assertTrue(json.contains("cont"));
104         assertTrue(json.contains("\"cont1c\":{"));
105         assertTrue(json.contains("\"lf11c\":\"lf11c val\""));
106     }
107
108     /**
109      * Test when element in case is leaflist
110      */
111     @Test
112     public void nodeSchemaAsLeafList() throws Exception {
113         final String json = getJson("/nn-to-json/choice/xml/data_leaflist.xml");
114
115         assertTrue(json.contains("cont"));
116         assertTrue(json.contains("\"lflst1d\":["));
117         assertTrue(json.contains("\"lflst1d_1 val\""));
118         assertTrue(json.contains("\"lflst1d_2 val\""));
119     }
120
121     /**
122      *
123      */
124     @Test
125     public void nodeSchemasInMultipleChoicesTest() throws Exception {
126         final String json = getJson("/nn-to-json/choice/xml/data_more_choices_same_level.xml");
127
128         assertTrue(json.contains("cont"));
129         assertTrue(json.contains("\"lf2b\":\"lf2b value\""));
130         assertTrue(json.contains("\"cont1c\":{"));
131         assertTrue(json.contains("\"lf11c\":\"lf11c val\""));
132     }
133
134     /**
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)
137      */
138     @Test
139     public void nodeSchemasInCaseNotDefinedWithCaseKeyword() throws Exception {
140         final String json = getJson("/nn-to-json/choice/xml/data_case_defined_without_case.xml");
141
142         assertTrue(json.contains("cont"));
143         assertTrue(json.contains("\"lf2b\":\"lf2b val\""));
144         assertTrue(json.contains("\"e1\":45"));
145     }
146
147     /**
148      * Test of multiple use of choices
149      */
150     @Test
151     public void nodeSchemasInThreeChoicesAtSameLevel() throws Exception {
152         final String json = getJson("/nn-to-json/choice/xml/data_three_choices_same_level.xml");
153
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\""));
160     }
161
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);
166
167         final OutputStream output = new ByteArrayOutputStream();
168         jsonBodyWriter.writeTo(testNN, null, null, null, mediaType, null,
169                 output);
170
171         return output.toString();
172     }
173
174     @Override
175     protected MediaType getMediaType() {
176         return null;
177     }
178 }