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