faa90b1d7578a07b7081eeaa1f6c044ee840831d
[yangtools.git] / yang / yang-data-codec-gson / src / test / java / org / opendaylight / yangtools / yang / data / codec / gson / AbstractYT1027Test.java
1 /*
2  * Copyright (c) 2019 PANTHEON.tech, s.r.o. 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 com.google.common.base.Verify.verify;
11 import static org.hamcrest.MatcherAssert.assertThat;
12 import static org.hamcrest.Matchers.instanceOf;
13 import static org.junit.Assert.assertEquals;
14
15 import com.google.gson.stream.JsonReader;
16 import java.io.IOException;
17 import java.io.StringReader;
18 import java.io.StringWriter;
19 import java.io.Writer;
20 import java.math.BigDecimal;
21 import org.junit.AfterClass;
22 import org.junit.BeforeClass;
23 import org.junit.Test;
24 import org.opendaylight.yangtools.yang.common.QName;
25 import org.opendaylight.yangtools.yang.common.Uint64;
26 import org.opendaylight.yangtools.yang.data.api.schema.LeafNode;
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.api.schema.stream.NormalizedNodeWriter;
30 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNodes;
31 import org.opendaylight.yangtools.yang.data.impl.schema.ImmutableNormalizedNodeStreamWriter;
32 import org.opendaylight.yangtools.yang.data.impl.schema.NormalizedNodeResult;
33 import org.opendaylight.yangtools.yang.model.api.DataSchemaNode;
34 import org.opendaylight.yangtools.yang.model.api.EffectiveModelContext;
35 import org.opendaylight.yangtools.yang.model.api.LeafSchemaNode;
36 import org.opendaylight.yangtools.yang.model.api.SchemaPath;
37 import org.opendaylight.yangtools.yang.model.api.TypeDefinition;
38 import org.opendaylight.yangtools.yang.model.api.type.DecimalTypeDefinition;
39 import org.opendaylight.yangtools.yang.model.api.type.Int64TypeDefinition;
40 import org.opendaylight.yangtools.yang.model.api.type.Uint64TypeDefinition;
41 import org.opendaylight.yangtools.yang.test.util.YangParserTestUtils;
42
43 public abstract class AbstractYT1027Test {
44     private static final QName DECIMAL = QName.create("yt1027.test", "decimal");
45     private static final QName INT64 = QName.create(DECIMAL, "int64");
46     private static final QName UINT64 = QName.create(DECIMAL, "uint64");
47
48     static final LeafNode<?> DECIMAL_DATA = ImmutableNodes.leafNode(DECIMAL, new BigDecimal("1.1"));
49     static final LeafNode<?> INT64_DATA = ImmutableNodes.leafNode(INT64, 2L);
50     static final LeafNode<?> UINT64_DATA = ImmutableNodes.leafNode(UINT64, Uint64.ONE);
51
52     static final String UNQUOTED_DECIMAL = "{\n"
53             + "  \"yt1027:decimal\": 1.1\n"
54             + "}";
55     static final String UNQUOTED_INT64 = "{\n"
56             + "  \"yt1027:int64\": 2\n"
57             + "}";
58     static final String UNQUOTED_UINT64 = "{\n"
59             + "  \"yt1027:uint64\": 1\n"
60             + "}";
61
62     static EffectiveModelContext SCHEMA_CONTEXT;
63     private static DecimalTypeDefinition DECIMAL_TYPE;
64     private static Int64TypeDefinition INT64_TYPE;
65     private static Uint64TypeDefinition UINT64_TYPE;
66
67     @BeforeClass
68     public static void beforeClass() {
69         SCHEMA_CONTEXT = YangParserTestUtils.parseYangResourceDirectory("/yt1027");
70         DECIMAL_TYPE = (DecimalTypeDefinition) getTypeDefinition(DECIMAL);
71         INT64_TYPE = (Int64TypeDefinition) getTypeDefinition(INT64);
72         UINT64_TYPE = (Uint64TypeDefinition) getTypeDefinition(UINT64);
73     }
74
75     private static TypeDefinition<?> getTypeDefinition(final QName name) {
76         DataSchemaNode child = SCHEMA_CONTEXT.findDataTreeChild(name).get();
77         verify(child instanceof LeafSchemaNode);
78         return ((LeafSchemaNode) child).getType();
79     }
80
81     @AfterClass
82     public static void afterClass() {
83         DECIMAL_TYPE = null;
84         INT64_TYPE = null;
85         UINT64_TYPE = null;
86         SCHEMA_CONTEXT = null;
87     }
88
89     @Test
90     public void testDecimal() {
91         assertThat(codecFactory().decimalCodec(DECIMAL_TYPE), instanceOf(wrapperClass()));
92     }
93
94     @Test
95     public void testInt64() {
96         assertThat(codecFactory().int64Codec(INT64_TYPE), instanceOf(wrapperClass()));
97     }
98
99     @Test
100     public void testUint64() {
101         assertThat(codecFactory().uint64Codec(UINT64_TYPE), instanceOf(wrapperClass()));
102     }
103
104     @Test
105     public void testDecimalSerialization() throws IOException {
106         assertEquals(expectedDecimal(), toJSON(DECIMAL_DATA));
107     }
108
109     @Test
110     public void testInt64Serialization() throws IOException {
111         assertEquals(expectedInt64(), toJSON(INT64_DATA));
112     }
113
114     @Test
115     public void testUint64Serialization() throws IOException {
116         assertEquals(expectedUint64(), toJSON(UINT64_DATA));
117     }
118
119     @Test
120     public void testDecimalParsing() throws IOException {
121         assertEquals(DECIMAL_DATA, fromJSON(expectedDecimal()));
122     }
123
124     @Test
125     public void testInt64Parsing() throws IOException {
126         assertEquals(INT64_DATA, fromJSON(expectedInt64()));
127     }
128
129     @Test
130     public void testUint64Parsing() throws IOException {
131         assertEquals(UINT64_DATA, fromJSON(expectedUint64()));
132     }
133
134     abstract JSONCodecFactory codecFactory();
135
136     abstract Class<?> wrapperClass();
137
138     abstract String expectedDecimal();
139
140     abstract String expectedInt64();
141
142     abstract String expectedUint64();
143
144     final NormalizedNode<?, ?> fromJSON(final String input) throws IOException {
145         final NormalizedNodeResult result = new NormalizedNodeResult();
146         final NormalizedNodeStreamWriter streamWriter = ImmutableNormalizedNodeStreamWriter.from(result);
147         final JsonParserStream jsonParser = JsonParserStream.create(streamWriter, codecFactory());
148         jsonParser.parse(new JsonReader(new StringReader(input)));
149         return result.getResult();
150     }
151
152     private String toJSON(final NormalizedNode<?, ?> input) throws IOException {
153         final Writer writer = new StringWriter();
154         final NormalizedNodeStreamWriter jsonStream = JSONNormalizedNodeStreamWriter.createExclusiveWriter(
155             codecFactory(), SchemaPath.ROOT, null, JsonWriterFactory.createJsonWriter(writer, 2));
156         try (NormalizedNodeWriter nodeWriter = NormalizedNodeWriter.forStreamWriter(jsonStream)) {
157             nodeWriter.write(input);
158         }
159
160         return writer.toString();
161     }
162 }