342e89911ac05ea6472b1358dc48e36db331182b
[yangtools.git] / yang / yang-data-codec-gson / src / test / java / org / opendaylight / yangtools / yang / data / codec / gson / Bug6112Test.java
1 /*
2  * Copyright (c) 2016 Intel Corporation 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 package org.opendaylight.yangtools.yang.data.codec.gson;
9
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.loadModules;
13 import static org.opendaylight.yangtools.yang.data.codec.gson.TestUtils.loadTextFile;
14
15 import com.google.common.base.Optional;
16 import com.google.gson.stream.JsonReader;
17 import java.io.IOException;
18 import java.io.StringReader;
19 import java.net.URISyntaxException;
20 import org.junit.BeforeClass;
21 import org.junit.Test;
22 import org.opendaylight.yangtools.yang.common.QName;
23 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.NodeIdentifier;
24 import org.opendaylight.yangtools.yang.data.api.YangInstanceIdentifier.PathArgument;
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.SchemaContext;
32 import org.opendaylight.yangtools.yang.parser.spi.meta.ReactorException;
33
34 public class Bug6112Test {
35     private static SchemaContext schemaContext;
36
37     @BeforeClass
38     public static void initialization() throws IOException, URISyntaxException, ReactorException {
39         schemaContext = loadModules("/bug-6112/yang");
40     }
41
42     private NormalizedNode<?, ?> readJson(final String jsonPath) throws IOException, URISyntaxException {
43         final String inputJson = loadTextFile(jsonPath);
44
45         final NormalizedNodeResult result = new NormalizedNodeResult();
46         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
47         final JsonParserStream jsonParser = JsonParserStream.create(streamWriter, schemaContext);
48         jsonParser.parse(new JsonReader(new StringReader(inputJson)));
49         return result.getResult();
50     }
51
52     @Test
53     public void testUnionIdentityrefInput() throws IOException, URISyntaxException {
54         final NormalizedNode<?, ?> transformedInput = readJson("/bug-6112/json/data-identityref.json");
55         assertTrue(transformedInput instanceof ContainerNode);
56         ContainerNode root = (ContainerNode) transformedInput;
57         Optional<DataContainerChild<? extends PathArgument, ?>> leafValue = root.getChild(NodeIdentifier.create(
58             QName.create("union:identityref:test", "2016-07-12", "leaf-value")));
59
60         assertTrue(leafValue.isPresent());
61         Object value = leafValue.get().getValue();
62         assertTrue(value instanceof QName);
63         QName identityref = (QName) value;
64         assertEquals(QName.create("union:identityref:test", "2016-07-12", "ident-one"), identityref);
65     }
66
67     @Test
68     public void testUnionUint8Input() throws IOException, URISyntaxException {
69         final NormalizedNode<?, ?> transformedInput = readJson("/bug-6112/json/data-uint8.json");
70         assertTrue(transformedInput instanceof ContainerNode);
71         ContainerNode root = (ContainerNode) transformedInput;
72         Optional<DataContainerChild<? extends PathArgument, ?>> leafValue = root.getChild(NodeIdentifier.create(
73             QName.create("union:identityref:test", "2016-07-12", "leaf-value")));
74
75         assertTrue(leafValue.isPresent());
76         Object value = leafValue.get().getValue();
77         assertTrue(value instanceof Short);
78         Short uint8Value = (Short) value;
79         assertEquals(1, uint8Value.shortValue());
80     }
81 }