Fix license header violations in sal-rest-connector
[controller.git] / opendaylight / md-sal / sal-rest-connector / src / test / java / org / opendaylight / controller / sal / restconf / impl / nn / to / json / test / NnJsonChoiceCaseTest.java
1 /*
2  * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
3  *
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
7  */
8
9 package org.opendaylight.controller.sal.restconf.impl.nn.to.json.test;
10
11 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.NormalizedNodeJsonBodyWriter;
19 import org.opendaylight.controller.sal.rest.impl.test.providers.AbstractBodyReaderTest;
20 import org.opendaylight.controller.sal.restconf.impl.NormalizedNodeContext;
21 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
22
23 public class NnJsonChoiceCaseTest extends AbstractBodyReaderTest {
24
25     private static SchemaContext schemaContext;
26     private final NormalizedNodeJsonBodyWriter jsonBodyWriter;
27
28     public NnJsonChoiceCaseTest() throws NoSuchFieldException,
29             SecurityException {
30         super();
31         jsonBodyWriter = new NormalizedNodeJsonBodyWriter();
32     }
33
34     @BeforeClass
35     public static void initialization() {
36         schemaContext = schemaContextLoader("/nn-to-json/choice", schemaContext);
37         controllerContext.setSchemas(schemaContext);
38     }
39
40     /**
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.
44      */
45     @Test(expected = NullPointerException.class)
46     public void nodeSchemasOnVariousChoiceCasePathTest() throws Exception {
47         getJson("/nn-to-json/choice/xml/data_various_path_err.xml");
48     }
49
50     /**
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.
55      */
56     @Test(expected = NullPointerException.class)
57     public void nodeSchemasOnVariousChoiceCasePathAndMultipleChoicesTest()
58             throws Exception {
59         getJson("/nn-to-json/choice/xml/data_more_choices_same_level_various_paths_err.xml");
60     }
61
62     /**
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
65      */
66
67     @Test
68     public void nodeSchemasWithRandomOrderAccordingLevel() throws Exception {
69         final String json = getJson("/nn-to-json/choice/xml/data_random_level.xml");
70
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"));
76     }
77
78     /**
79      * Test when element from no first case is used
80      */
81     @Test
82     public void nodeSchemasNotInFirstCase() throws Exception {
83         final String json = getJson("/nn-to-json/choice/xml/data_no_first_case.xml");
84
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"));
89     }
90
91     /**
92      * Test when element in case is list
93      */
94     @Test
95     public void nodeSchemaAsList() throws Exception {
96         final String json = getJson("/nn-to-json/choice/xml/data_list.xml");
97
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\"}"));
102     }
103
104     /**
105      * Test when element in case is container
106      */
107     @Test
108     public void nodeSchemaAsContainer() throws Exception {
109         final String json = getJson("/nn-to-json/choice/xml/data_container.xml");
110
111         assertTrue(json.contains("cont"));
112         assertTrue(json.contains("\"cont1c\":{"));
113         assertTrue(json.contains("\"lf11c\":\"lf11c val\""));
114     }
115
116     /**
117      * Test when element in case is leaflist
118      */
119     @Test
120     public void nodeSchemaAsLeafList() throws Exception {
121         final String json = getJson("/nn-to-json/choice/xml/data_leaflist.xml");
122
123         assertTrue(json.contains("cont"));
124         assertTrue(json.contains("\"lflst1d\":["));
125         assertTrue(json.contains("\"lflst1d_1 val\""));
126         assertTrue(json.contains("\"lflst1d_2 val\""));
127     }
128
129     /**
130      *
131      */
132     @Test
133     public void nodeSchemasInMultipleChoicesTest() throws Exception {
134         final String json = getJson("/nn-to-json/choice/xml/data_more_choices_same_level.xml");
135
136         assertTrue(json.contains("cont"));
137         assertTrue(json.contains("\"lf2b\":\"lf2b value\""));
138         assertTrue(json.contains("\"cont1c\":{"));
139         assertTrue(json.contains("\"lf11c\":\"lf11c val\""));
140     }
141
142     /**
143      * Test whether is possible to find data schema for node which is specified
144      * as dirrect subnode of choice (case without CASE key word)
145      */
146     @Test
147     public void nodeSchemasInCaseNotDefinedWithCaseKeyword() throws Exception {
148         final String json = getJson("/nn-to-json/choice/xml/data_case_defined_without_case.xml");
149
150         assertTrue(json.contains("cont"));
151         assertTrue(json.contains("\"lf2b\":\"lf2b val\""));
152         assertTrue(json.contains("\"e1\":45"));
153     }
154
155     /**
156      * Test of multiple use of choices
157      */
158     @Test
159     public void nodeSchemasInThreeChoicesAtSameLevel() throws Exception {
160         final String json = getJson("/nn-to-json/choice/xml/data_three_choices_same_level.xml");
161
162         assertTrue(json.contains("cont"));
163         assertTrue(json.contains("lf2b\":\"lf2b value"));
164         assertTrue(json.contains("lst4a\":[{"));
165         assertTrue(json.contains("{\"lf4ab\":33}"));
166         assertTrue(json.contains("{\"lf4ab\":37}"));
167         assertTrue(json.contains("\"lf1aaa\":\"lf1aaa value\""));
168     }
169
170     private String getJson(final String xmlPath) throws Exception {
171         final String uri = "choice-case-test:cont";
172         final NormalizedNodeContext testNN = TestRestconfUtils
173                 .loadNormalizedContextFromXmlFile(xmlPath, uri);
174
175         final OutputStream output = new ByteArrayOutputStream();
176         jsonBodyWriter.writeTo(testNN, null, null, null, mediaType, null,
177                 output);
178
179         return output.toString();
180     }
181
182     @Override
183     protected MediaType getMediaType() {
184         return null;
185     }
186 }