YANGTOOLS-766: introduce JSONCodecFactorySupplier
[yangtools.git] / yang / yang-data-codec-gson / src / test / java / org / opendaylight / yangtools / yang / data / codec / gson / Bug8083Test.java
1 /*
2  * Copyright (c) 2017 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.yangtools.yang.data.codec.gson;
10
11 import static org.junit.Assert.assertNotNull;
12 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadTextFile;
13
14 import com.google.gson.stream.JsonReader;
15 import java.io.IOException;
16 import java.io.StringReader;
17 import java.net.URISyntaxException;
18 import org.junit.Ignore;
19 import org.junit.Test;
20 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
21 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
22 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
23 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
24 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
25 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
26
27 public class Bug8083Test {
28
29     @Ignore("Instance-identifier codec has to be modified according to the RFC7951 to correctly parse this.")
30     @Test
31     public void testRFC7951InstanceIdentifierPath() throws IOException, URISyntaxException {
32         final SchemaContext schemaContext = YangParserTestUtils.parseYangResourceDirectory("/bug8083/yang/");
33         final String inputJson = loadTextFile("/bug8083/json/foo.json");
34
35         // deserialization
36         final NormalizedNodeResult result = new NormalizedNodeResult();
37         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
38         final JsonParserStream jsonParser = JsonParserStream.create(streamWriter,
39             JSONCodecFactorySupplier.RFC7951.getShared(schemaContext));
40         jsonParser.parse(new JsonReader(new StringReader(inputJson)));
41         final NormalizedNode<?, ?> transformedInput = result.getResult();
42         assertNotNull(transformedInput);
43     }
44
45     @Test
46     public void testInstanceIdentifierPathWithEmptyListKey() throws IOException, URISyntaxException {
47         final SchemaContext schemaContext = YangParserTestUtils.parseYangResource("/bug8083/yang/baz.yang");
48         final String inputJson = loadTextFile("/bug8083/json/baz.json");
49
50         // deserialization
51         final NormalizedNodeResult result = new NormalizedNodeResult();
52         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
53         final JsonParserStream jsonParser = JsonParserStream.create(streamWriter,
54             JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext));
55         jsonParser.parse(new JsonReader(new StringReader(inputJson)));
56         final NormalizedNode<?, ?> transformedInput = result.getResult();
57         assertNotNull(transformedInput);
58     }
59
60     @Test
61     public void testInstanceIdentifierPathWithIdentityrefListKey() throws IOException, URISyntaxException {
62         final SchemaContext schemaContext = YangParserTestUtils.parseYangResource("/bug8083/yang/zab.yang");
63         final String inputJson = loadTextFile("/bug8083/json/zab.json");
64
65         // deserialization
66         final NormalizedNodeResult result = new NormalizedNodeResult();
67         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
68         final JsonParserStream jsonParser = JsonParserStream.create(streamWriter,
69             JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext));
70         jsonParser.parse(new JsonReader(new StringReader(inputJson)));
71         final NormalizedNode<?, ?> transformedInput = result.getResult();
72         assertNotNull(transformedInput);
73     }
74
75     @Test
76     public void testInstanceIdentifierPathWithInstanceIdentifierListKey() throws IOException, URISyntaxException {
77         final SchemaContext schemaContext = YangParserTestUtils.parseYangResource("/bug8083/yang/foobar.yang");
78         final String inputJson = loadTextFile("/bug8083/json/foobar.json");
79
80         // deserialization
81         final NormalizedNodeResult result = new NormalizedNodeResult();
82         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
83         final JsonParserStream jsonParser = JsonParserStream.create(streamWriter,
84             JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext));
85         jsonParser.parse(new JsonReader(new StringReader(inputJson)));
86         final NormalizedNode<?, ?> transformedInput = result.getResult();
87         assertNotNull(transformedInput);
88     }
89 }