summaryrefslogtreecommitdiffstats
path: root/netconf/restconf/restconf-nb-bierman02/src/test/java/org/opendaylight/controller/sal/restconf/impl/json/to/nn/test/JsonToNnTest.java
blob: 51d1e647208bb2467644bd2eaae00b54ba0aa9e5 (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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
/*
 * Copyright (c) 2014 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.json.to.nn.test;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.junit.Assert.fail;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.util.Collection;
import javax.ws.rs.WebApplicationException;
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.JsonNormalizedNodeBodyReader;
import org.opendaylight.netconf.sal.rest.impl.NormalizedNodeContext;
import org.opendaylight.restconf.common.errors.RestconfDocumentedException;
import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNodes;
import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class JsonToNnTest extends AbstractBodyReaderTest {

    private static final Logger LOG = LoggerFactory.getLogger(AbstractBodyReaderTest.class);

    private final JsonNormalizedNodeBodyReader jsonBodyReader;
    private static EffectiveModelContext schemaContext;

    public JsonToNnTest() {
        super(schemaContext, null);
        this.jsonBodyReader = new JsonNormalizedNodeBodyReader(controllerContext);
    }

    @BeforeClass
    public static void initialize() throws FileNotFoundException {
        final Collection<File> testFiles = TestRestconfUtils.loadFiles("/json-to-nn/simple-list-yang/1");
        testFiles.addAll(TestRestconfUtils.loadFiles("/json-to-nn/simple-list-yang/3"));
        testFiles.addAll(TestRestconfUtils.loadFiles("/json-to-nn/simple-list-yang/4"));
        testFiles.addAll(TestRestconfUtils.loadFiles("/json-to-nn/simple-container-yang"));
        testFiles.addAll(TestRestconfUtils.loadFiles("/common/augment/yang"));
        schemaContext = YangParserTestUtils.parseYangFiles(testFiles);
    }

    @Test
    public void simpleListTest() throws Exception {
        simpleTest("/json-to-nn/simple-list.json",
                "lst", "simple-list-yang1");
    }

    @Test
    public void simpleContainerTest() throws Exception {
        simpleTest("/json-to-nn/simple-container.json",
                "cont", "simple-container-yang");
    }

    @Test
    public void multipleItemsInLeafListTest() throws Exception {
        final NormalizedNodeContext normalizedNodeContext = prepareNNC(
                "/json-to-nn/multiple-leaflist-items.json",
                "simple-list-yang1:lst");
        assertNotNull(normalizedNodeContext);

        final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
                .getData());
        assertTrue(dataTree.contains("45"));
        assertTrue(dataTree.contains("55"));
        assertTrue(dataTree.contains("66"));
    }

    @Test
    public void multipleItemsInListTest() throws Exception {
        final NormalizedNodeContext normalizedNodeContext = prepareNNC(
                "/json-to-nn/multiple-items-in-list.json",
                "multiple-items-yang:lst");
        assertNotNull(normalizedNodeContext);

        assertEquals("lst", normalizedNodeContext.getData().getIdentifier().getNodeType().getLocalName());

        verityMultipleItemsInList(normalizedNodeContext);
    }

    @Test
    public void nullArrayToSimpleNodeWithNullValueTest() throws Exception {
        final NormalizedNodeContext normalizedNodeContext = prepareNNC(
                "/json-to-nn/array-with-null.json", "array-with-null-yang:cont");
        assertNotNull(normalizedNodeContext);

        assertEquals("cont", normalizedNodeContext.getData().getIdentifier().getNodeType().getLocalName());

        final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext.getData());
        assertTrue(dataTree.contains("lf"));
        assertTrue(dataTree.contains("empty"));
    }

    @Test
    public void incorrectTopLevelElementsTest() throws Exception {
        mockBodyReader("simple-list-yang1:lst", this.jsonBodyReader, false);

        InputStream inputStream = this.getClass().getResourceAsStream(
                "/json-to-nn/wrong-top-level1.json");

        int countExceptions = 0;
        RestconfDocumentedException exception = null;

        try {
            this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
                    inputStream);
        } catch (final RestconfDocumentedException e) {
            exception = e;
            countExceptions++;
        }
        assertNotNull(exception);
        assertEquals(
                "Error parsing input: Schema node with name wrong was not found under "
                        + "(urn:ietf:params:xml:ns:netconf:base:1.0)data.",
                exception.getErrors().get(0).getErrorMessage());

        inputStream = this.getClass().getResourceAsStream(
                "/json-to-nn/wrong-top-level2.json");
        exception = null;
        try {
            this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
                    inputStream);
        } catch (final RestconfDocumentedException e) {
            exception = e;
            countExceptions++;
        }
        assertNotNull(exception);
        assertEquals(
                "Error parsing input: Schema node with name lst1 was not found under "
                        + "(urn:ietf:params:xml:ns:netconf:base:1.0)data.",
                exception.getErrors().get(0).getErrorMessage());

        inputStream = this.getClass().getResourceAsStream(
                "/json-to-nn/wrong-top-level3.json");
        exception = null;
        try {
            this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,
                    inputStream);
        } catch (final RestconfDocumentedException e) {
            exception = e;
            countExceptions++;
        }
        assertNotNull(exception);
        assertEquals(
                "Error parsing input: Schema node with name lf was not found under "
                        + "(urn:ietf:params:xml:ns:netconf:base:1.0)data.",
                exception.getErrors().get(0).getErrorMessage());
        assertEquals(3, countExceptions);
    }

    @Test
    public void emptyDataReadTest() throws Exception {
        final NormalizedNodeContext normalizedNodeContext = prepareNNC(
                "/json-to-nn/empty-data.json", "array-with-null-yang:cont");
        assertNotNull(normalizedNodeContext);

        assertEquals("cont", normalizedNodeContext.getData().getIdentifier().getNodeType().getLocalName());

        final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext.getData());

        assertTrue(dataTree.contains("lflst1"));

        assertTrue(dataTree.contains("lflst2 45"));

        RestconfDocumentedException exception = null;
        mockBodyReader("array-with-null-yang:cont", this.jsonBodyReader, false);
        final InputStream inputStream = this.getClass().getResourceAsStream("/json-to-nn/empty-data.json1");

        try {
            this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null,inputStream);
        } catch (final RestconfDocumentedException e) {
            exception = e;
        }
        assertNotNull(exception);
        assertEquals("Error parsing input: null", exception.getErrors().get(0).getErrorMessage());
    }

    @Test
    public void testJsonBlankInput() throws Exception {
        final NormalizedNodeContext normalizedNodeContext = prepareNNC("", "array-with-null-yang:cont");
        assertNull(normalizedNodeContext);
    }

    @Test
    public void notSupplyNamespaceIfAlreadySupplied()throws Exception {
        final String uri = "simple-list-yang1" + ":" + "lst";

        final NormalizedNodeContext normalizedNodeContext = prepareNNC("/json-to-nn/simple-list.json", uri);
        assertNotNull(normalizedNodeContext);

        verifyNormaluizedNodeContext(normalizedNodeContext, "lst");

        mockBodyReader("simple-list-yang2:lst", this.jsonBodyReader, false);
        final InputStream inputStream = this.getClass().getResourceAsStream("/json-to-nn/simple-list.json");

        try {
            this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null, inputStream);
            fail("NormalizedNodeContext should not be create because of different namespace");
        } catch (final RestconfDocumentedException e) {
            LOG.warn("Read from InputStream failed. Message: {}. Status: {}", e.getMessage(), e.getStatus());
        }

        verifyNormaluizedNodeContext(normalizedNodeContext, "lst");
    }

    @Test
    public void dataAugmentedTest() throws Exception {
        NormalizedNodeContext normalizedNodeContext = prepareNNC("/common/augment/json/dataa.json", "main:cont");

        assertNotNull(normalizedNodeContext);
        assertEquals("cont", normalizedNodeContext.getData().getIdentifier().getNodeType().getLocalName());

        String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext
                .getData());
        assertTrue(dataTree.contains("cont1"));
        assertTrue(dataTree.contains("lf11 lf11 value from a"));

        normalizedNodeContext = prepareNNC("/common/augment/json/datab.json", "main:cont");

        assertNotNull(normalizedNodeContext);
        assertEquals("cont", normalizedNodeContext.getData().getIdentifier().getNodeType().getLocalName());
        dataTree = NormalizedNodes.toStringTree(normalizedNodeContext.getData());
        assertTrue(dataTree.contains("cont1"));
        assertTrue(dataTree.contains("lf11 lf11 value from b"));
    }

    private void simpleTest(final String jsonPath, final String topLevelElementName,
            final String moduleName) throws Exception {
        final String uri = moduleName + ":" + topLevelElementName;

        final NormalizedNodeContext normalizedNodeContext = prepareNNC(jsonPath, uri);
        assertNotNull(normalizedNodeContext);

        verifyNormaluizedNodeContext(normalizedNodeContext, topLevelElementName);
    }

    private NormalizedNodeContext prepareNNC(final String jsonPath, final String uri) throws Exception {
        try {
            mockBodyReader(uri, this.jsonBodyReader, false);
        } catch (NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException e) {
            LOG.warn("Operation failed due to: {}", e.getMessage());
        }
        final InputStream inputStream = this.getClass().getResourceAsStream(jsonPath);

        NormalizedNodeContext normalizedNodeContext = null;

        try {
            normalizedNodeContext = this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null, inputStream);
        } catch (WebApplicationException e) {
            // TODO Auto-generated catch block
        }

        return normalizedNodeContext;
    }

    private static void verifyNormaluizedNodeContext(final NormalizedNodeContext normalizedNodeContext,
            final String topLevelElementName) {
        assertEquals(topLevelElementName, normalizedNodeContext.getData().getIdentifier().getNodeType().getLocalName());

        final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext.getData());
        assertTrue(dataTree.contains("cont1"));
        assertTrue(dataTree.contains("lst1"));
        assertTrue(dataTree.contains("lflst1"));
        assertTrue(dataTree.contains("lflst1_1"));
        assertTrue(dataTree.contains("lflst1_2"));
        assertTrue(dataTree.contains("lf1"));
    }

    private static void verityMultipleItemsInList(final NormalizedNodeContext normalizedNodeContext) {
        final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext.getData());
        assertTrue(dataTree.contains("lf11"));
        assertTrue(dataTree.contains("lf11_1"));
        assertTrue(dataTree.contains("lflst11"));
        assertTrue(dataTree.contains("45"));
        assertTrue(dataTree.contains("cont11"));
        assertTrue(dataTree.contains("lst11"));
    }

    @Test
    public void unsupportedDataFormatTest() throws Exception {
        mockBodyReader("simple-list-yang1:lst", this.jsonBodyReader, false);

        final InputStream inputStream = this.getClass().getResourceAsStream("/json-to-nn/unsupported-json-format.json");

        RestconfDocumentedException exception = null;

        try {
            this.jsonBodyReader.readFrom(null, null, null, this.mediaType, null, inputStream);
        } catch (final RestconfDocumentedException e) {
            exception = e;
        }
        LOG.info(exception.getErrors().get(0).getErrorMessage());

        assertTrue(exception.getErrors().get(0).getErrorMessage().contains("is not a simple type"));
    }

    @Test
    public void invalidUriCharacterInValue() throws Exception {
        mockBodyReader("array-with-null-yang:cont", this.jsonBodyReader, false);

        final InputStream inputStream = this.getClass().getResourceAsStream(
                "/json-to-nn/invalid-uri-character-in-value.json");

        final NormalizedNodeContext normalizedNodeContext = this.jsonBodyReader.readFrom(
                null, null, null, this.mediaType, null, inputStream);
        assertNotNull(normalizedNodeContext);

        assertEquals("cont", normalizedNodeContext.getData().getIdentifier().getNodeType().getLocalName());

        final String dataTree = NormalizedNodes.toStringTree(normalizedNodeContext.getData());
        assertTrue(dataTree.contains("lf1 module<Name:value lf1"));
        assertTrue(dataTree.contains("lf2 module>Name:value lf2"));
    }

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

}