Populate data/ hierarchy
[yangtools.git] / data / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / codec / Int16CodecStringTest.java
1 /*
2  * Copyright (c) 2016 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 package org.opendaylight.yangtools.yang.data.impl.codec;
9
10 import static org.junit.Assert.assertEquals;
11 import static org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodecTestHelper.deserializeWithExpectedIllegalArgEx;
12 import static org.opendaylight.yangtools.yang.data.impl.codec.TypeDefinitionAwareCodecTestHelper.getCodec;
13
14 import org.junit.Test;
15 import org.opendaylight.yangtools.yang.data.api.codec.Int16Codec;
16 import org.opendaylight.yangtools.yang.model.ri.type.BaseTypes;
17
18 /**
19  * Unit tests for Int8CodecString.
20  *
21  * @author Thomas Pantelis
22  */
23 public class Int16CodecStringTest {
24     @SuppressWarnings("unchecked")
25     @Test
26     public void testSerialize() {
27         Int16Codec<String> codec = getCodec(BaseTypes.int16Type(), Int16Codec.class);
28         assertEquals("serialize", "10", codec.serialize(Short.valueOf((short) 10)));
29     }
30
31     @SuppressWarnings("unchecked")
32     @Test
33     public void testDeserialize() {
34         final String hexa = "+0X45c";
35         final String negHexa = "-0X45c";
36         final String octal = "02134";
37         final String negOctal = "-02134";
38         final String integer = "+1116";
39         final String negInteger = "-1116";
40
41         Int16Codec<String> codec = getCodec(BaseTypes.int16Type(), Int16Codec.class);
42
43         assertEquals("deserialize", codec.deserialize(hexa), Short.valueOf("+045c", 16));
44         assertEquals("deserialize", codec.deserialize(negHexa), Short.valueOf("-045c", 16));
45         assertEquals("deserialize", codec.deserialize(octal), Short.valueOf(octal, 8));
46         assertEquals("deserialize", codec.deserialize(negOctal), Short.valueOf(negOctal, 8));
47         assertEquals("deserialize", codec.deserialize(integer), Short.valueOf(integer, 10));
48         assertEquals("deserialize", codec.deserialize(negInteger), Short.valueOf(negInteger, 10));
49
50         deserializeWithExpectedIllegalArgEx(codec, "1o");
51         deserializeWithExpectedIllegalArgEx(codec, "");
52     }
53 }