4ee8492f194d24b4440c94fc5665ca8798775f00
[yangtools.git] / yang / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / codecs / Int8CodecStringTest.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
9 package org.opendaylight.yangtools.yang.data.impl.codecs;
10
11 import static org.junit.Assert.*;
12
13 import org.junit.Test;
14 import org.opendaylight.yangtools.yang.data.api.codec.Int8Codec;
15 import org.opendaylight.yangtools.yang.model.util.Int8;
16
17 /**
18  * Unit tests for Int8CodecString.
19  *
20  * @author Thomas Pantelis
21  */
22 public class Int8CodecStringTest {
23
24     @SuppressWarnings("unchecked")
25     @Test
26     public void testSerialize() {
27         Int8Codec<String> codec = TypeDefinitionAwareCodecTestHelper.getCodec(Int8.getInstance(), Int8Codec.class);
28
29         assertEquals("serialize", "10", codec.serialize(Byte.valueOf( (byte) 10 )));
30         assertEquals("serialize", "", codec.serialize(null));
31     }
32
33     @SuppressWarnings("unchecked")
34     @Test
35     public void testDeserialize() {
36         final String hexa = "0x40";
37         final String negHexa = "-0x40";
38         final String octal = "+0100";
39         final String negOctal = "-0100";
40         final String integer = "64";
41         final String negInteger = "-64";
42
43         Int8Codec<String> codec = TypeDefinitionAwareCodecTestHelper.getCodec(Int8.getInstance(), Int8Codec.class);
44
45         assertEquals("deserialize", codec.deserialize(hexa), Byte.valueOf("040", 16));
46         assertEquals("deserialize", codec.deserialize(negHexa), Byte.valueOf("-040", 16));
47         assertEquals("deserialize", codec.deserialize(octal), Byte.valueOf(octal, 8));
48         assertEquals("deserialize", codec.deserialize(negOctal), Byte.valueOf(negOctal, 8));
49         assertEquals("deserialize", codec.deserialize(integer), Byte.valueOf(integer, 10));
50         assertEquals("deserialize", codec.deserialize(negInteger), Byte.valueOf(negInteger, 10));
51
52         TypeDefinitionAwareCodecTestHelper.deserializeWithExpectedIllegalArgEx(codec, "1o");
53         TypeDefinitionAwareCodecTestHelper.deserializeWithExpectedIllegalArgEx(codec, "");
54         TypeDefinitionAwareCodecTestHelper.deserializeWithExpectedIllegalArgEx(codec, null);
55     }
56 }