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.assertTrue;
12 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadTextFile;
14 import com.google.common.base.Optional;
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.BeforeClass;
20 import org.junit.Test;
21 import org.opendaylight.yangtools.yang.common.QName;
22 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
24 import org.opendaylight.yangtools.yang.data.api.schema.ContainerNode;
25 import org.opendaylight.yangtools.yang.data.api.schema.DataContainerChild;
26 import org.opendaylight.yangtools.yang.data.api.schema.NormalizedNode;
27 import org.opendaylight.yangtools.yang.data.api.schema.stream.NormalizedNodeStreamWriter;
28 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
29 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
30 import org.opendaylight.yangtools.yang.model.api.SchemaContext;
31 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
33 public class Bug6112Test {
34 private static SchemaContext schemaContext;
37 public static void initialization() {
38 schemaContext = YangParserTestUtils.parseYangResourceDirectory("/bug-6112/yang");
41 private static NormalizedNode<?, ?> readJson(final String jsonPath) throws IOException, URISyntaxException {
42 final String inputJson = loadTextFile(jsonPath);
44 final NormalizedNodeResult result = new NormalizedNodeResult();
45 final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
46 final JsonParserStream jsonParser = JsonParserStream.create(streamWriter, schemaContext);
47 jsonParser.parse(new JsonReader(new StringReader(inputJson)));
48 return result.getResult();
52 public void testUnionIdentityrefInput() throws IOException, URISyntaxException {
53 final NormalizedNode<?, ?> transformedInput = readJson("/bug-6112/json/data-identityref.json");
54 assertTrue(transformedInput instanceof ContainerNode);
55 ContainerNode root = (ContainerNode) transformedInput;
56 Optional<DataContainerChild<? extends PathArgument, ?>> leafValue = root.getChild(NodeIdentifier.create(
57 QName.create("union:identityref:test", "2016-07-12", "leaf-value")));
59 assertTrue(leafValue.isPresent());
60 Object value = leafValue.get().getValue();
61 assertTrue(value instanceof QName);
62 QName identityref = (QName) value;
63 assertEquals(QName.create("union:identityref:test", "2016-07-12", "ident-one"), identityref);
67 public void testUnionUint8Input() throws IOException, URISyntaxException {
68 final NormalizedNode<?, ?> transformedInput = readJson("/bug-6112/json/data-uint8.json");
69 assertTrue(transformedInput instanceof ContainerNode);
70 ContainerNode root = (ContainerNode) transformedInput;
71 Optional<DataContainerChild<? extends PathArgument, ?>> leafValue = root.getChild(NodeIdentifier.create(
72 QName.create("union:identityref:test", "2016-07-12", "leaf-value")));
74 assertTrue(leafValue.isPresent());
75 Object value = leafValue.get().getValue();
76 assertTrue(value instanceof Short);
77 Short uint8Value = (Short) value;
78 assertEquals(1, uint8Value.shortValue());