summaryrefslogtreecommitdiffstats
path: root/netconf/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/nn/to/json/test/NnJsonChoiceCaseTest.java
blob: 8fd5db886b2a57716563a5df33c592d8b25588af (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * Copyright (c) 2015 Cisco Systems, Inc. and others.  All rights reserved.
 *
 * This program and the accompanying materials are made available under the
 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 */
package org.opendaylight.controller.sal.restconf.impl.nn.to.json.test;

import static org.junit.Assert.assertTrue;

import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import javax.ws.rs.core.MediaType;
import org.junit.BeforeClass;
import org.junit.Test;
import org.opendaylight.controller.md.sal.rest.common.TestRestconfUtils;
import org.opendaylight.controller.sal.rest.impl.test.providers.AbstractBodyReaderTest;
import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext;
import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeJsonBodyWriter;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;

public class NnJsonChoiceCaseTest extends AbstractBodyReaderTest {

    private static EffectiveModelContext schemaContext;
    private final NormalizedNodeJsonBodyWriter jsonBodyWriter;

    public NnJsonChoiceCaseTest()  {
        super(schemaContext, null);
        jsonBodyWriter = new NormalizedNodeJsonBodyWriter();
    }

    @BeforeClass
    public static void initialization() {
        schemaContext = schemaContextLoader("/nn-to-json/choice", schemaContext);
    }

    /**
     * Test when some data are in one case node and other in another. This isn't
     * correct. Next Json validator should return error because nodes has to be
     * from one case below concrete choice.
     */
    @Test(expected = NullPointerException.class)
    public void nodeSchemasOnVariousChoiceCasePathTest() throws Exception {
        getJson("/nn-to-json/choice/xml/data_various_path_err.xml");
    }

    /**
     * Test when some data are in one case node and other in another.
     * Additionally data are loadef from various choices. This isn't correct.
     * Next Json validator should return error because nodes has to be from one
     * case below concrete choice.
     */
    @Test(expected = NullPointerException.class)
    public void nodeSchemasOnVariousChoiceCasePathAndMultipleChoicesTest()
            throws Exception {
        getJson("/nn-to-json/choice/xml/data_more_choices_same_level_various_paths_err.xml");
    }

    /**
     * Test when second level data are red first, then first and at the end
     * third level. Level represents pass through couple choice-case
     */

    @Test
    public void nodeSchemasWithRandomOrderAccordingLevel() throws Exception {
        final String json = getJson("/nn-to-json/choice/xml/data_random_level.xml");

        assertTrue(json.contains("cont"));
        assertTrue(json.contains("\"lf1\":\"lf1 val\""));
        assertTrue(json.contains("\"lf1aaa\":\"lf1aaa val\""));
        assertTrue(json.contains("\"lf1aa\":\"lf1aa val\""));
        assertTrue(json.contains("\"lf1a\":121"));
    }

    /**
     * Test when element from no first case is used.
     */
    @Test
    public void nodeSchemasNotInFirstCase() throws Exception {
        final String json = getJson("/nn-to-json/choice/xml/data_no_first_case.xml");

        assertTrue(json.contains("cont"));
        assertTrue(json.contains("\"lf1\":\"lf1 val\""));
        assertTrue(json.contains("\"lf1ab\":\"lf1ab val\""));
        assertTrue(json.contains("\"lf1a\":121"));
    }

    /**
     * Test when element in case is list.
     */
    @Test
    public void nodeSchemaAsList() throws Exception {
        final String json = getJson("/nn-to-json/choice/xml/data_list.xml");

        assertTrue(json.contains("cont"));
        assertTrue(json.contains("\"lst1b\":["));
        assertTrue(json.contains("{\"lf11b\":\"lf11b_1 val\"}"));
        assertTrue(json.contains("{\"lf11b\":\"lf11b_2 val\"}"));
    }

    /**
     * Test when element in case is container.
     */
    @Test
    public void nodeSchemaAsContainer() throws Exception {
        final String json = getJson("/nn-to-json/choice/xml/data_container.xml");

        assertTrue(json.contains("cont"));
        assertTrue(json.contains("\"cont1c\":{"));
        assertTrue(json.contains("\"lf11c\":\"lf11c val\""));
    }

    /**
     * Test when element in case is leaflist.
     */
    @Test
    public void nodeSchemaAsLeafList() throws Exception {
        final String json = getJson("/nn-to-json/choice/xml/data_leaflist.xml");

        assertTrue(json.contains("cont"));
        assertTrue(json.contains("\"lflst1d\":["));
        assertTrue(json.contains("\"lflst1d_1 val\""));
        assertTrue(json.contains("\"lflst1d_2 val\""));
    }

    @Test
    public void nodeSchemasInMultipleChoicesTest() throws Exception {
        final String json = getJson("/nn-to-json/choice/xml/data_more_choices_same_level.xml");

        assertTrue(json.contains("cont"));
        assertTrue(json.contains("\"lf2b\":\"lf2b value\""));
        assertTrue(json.contains("\"cont1c\":{"));
        assertTrue(json.contains("\"lf11c\":\"lf11c val\""));
    }

    /**
     * Test whether is possible to find data schema for node which is specified
     * as dirrect subnode of choice (case without CASE key word).
     */
    @Test
    public void nodeSchemasInCaseNotDefinedWithCaseKeyword() throws Exception {
        final String json = getJson("/nn-to-json/choice/xml/data_case_defined_without_case.xml");

        assertTrue(json.contains("cont"));
        assertTrue(json.contains("\"lf2b\":\"lf2b val\""));
        assertTrue(json.contains("\"e1\":45"));
    }

    /**
     * Test of multiple use of choices.
     */
    @Test
    public void nodeSchemasInThreeChoicesAtSameLevel() throws Exception {
        final String json = getJson("/nn-to-json/choice/xml/data_three_choices_same_level.xml");

        assertTrue(json.contains("cont"));
        assertTrue(json.contains("lf2b\":\"lf2b value"));
        assertTrue(json.contains("lst4a\":[{"));
        assertTrue(json.contains("{\"lf4ab\":33}"));
        assertTrue(json.contains("{\"lf4ab\":37}"));
        assertTrue(json.contains("\"lf1aaa\":\"lf1aaa value\""));
    }

    private String getJson(final String xmlPath) throws Exception {
        final String uri = "choice-case-test:cont";
        final NormalizedNodeContext testNN = TestRestconfUtils
                .loadNormalizedContextFromXmlFile(xmlPath, uri, controllerContext);

        final OutputStream output = new ByteArrayOutputStream();
        jsonBodyWriter.writeTo(testNN, null, null, null, mediaType, null,
                output);

        return output.toString();
    }

    @Override
    protected MediaType getMediaType() {
        return null;
    }
}