2 * Copyright (c) 2016 Intel Corporation and others. All rights reserved.
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
8 package org.opendaylight.yangtools.yang.data.codec.gson;
10 import static org.junit.Assert.assertEquals;
11 import static org.junit.Assert.assertNotNull;
12 import static org.junit.Assert.assertTrue;
13 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadTextFile;
15 import com.google.gson.stream.JsonReader;
16 import java.io.IOException;
17 import java.io.StringReader;
18 import java.net.URISyntaxException;
19 import org.junit.AfterClass;
20 import org.junit.BeforeClass;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.common.Uint8;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
25 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
26 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
27 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
28 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
29 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
30 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
31 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
32 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
34 public class Bug6112Test {
35 private static EffectiveModelContext schemaContext;
38 public static void initialization() {
39 schemaContext = YangParserTestUtils.parseYangResourceDirectory("/bug-6112/yang");
43 public static void cleanup() {
47 private static NormalizedNode readJson(final String jsonPath) throws IOException, URISyntaxException {
48 final String inputJson = loadTextFile(jsonPath);
50 final NormalizedNodeResult result = new NormalizedNodeResult();
51 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
52 final JsonParserStream jsonParser = JsonParserStream.create(streamWriter,
53 JSONCodecFactorySupplier.DRAFT_LHOTKA_NETMOD_YANG_JSON_02.getShared(schemaContext));
54 jsonParser.parse(new JsonReader(new StringReader(inputJson)));
55 return result.getResult();
59 public void testUnionIdentityrefInput() throws IOException, URISyntaxException {
60 final NormalizedNode transformedInput = readJson("/bug-6112/json/data-identityref.json");
61 assertTrue(transformedInput instanceof ContainerNode);
62 ContainerNode root = (ContainerNode) transformedInput;
63 DataContainerChild leafValue = root.childByArg(NodeIdentifier.create(
64 QName.create("union:identityref:test", "2016-07-12", "leaf-value")));
66 assertNotNull(leafValue);
67 Object value = leafValue.body();
68 assertTrue(value instanceof QName);
69 QName identityref = (QName) value;
70 assertEquals(QName.create("union:identityref:test", "2016-07-12", "ident-one"), identityref);
74 public void testUnionUint8Input() throws IOException, URISyntaxException {
75 final NormalizedNode transformedInput = readJson("/bug-6112/json/data-uint8.json");
76 assertTrue(transformedInput instanceof ContainerNode);
77 ContainerNode root = (ContainerNode) transformedInput;
78 DataContainerChild leafValue = root.childByArg(NodeIdentifier.create(
79 QName.create("union:identityref:test", "2016-07-12", "leaf-value")));
81 assertNotNull(leafValue);
82 assertEquals(Uint8.valueOf(1), leafValue.body());