784e4ba8efc6534f9a0a0c9c019d5274e12bc922
[yangtools.git] / data / yang-data-impl / src / test / java / org / opendaylight / yangtools / yang / data / impl / codec / Int64CodecStringTest.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.Int64Codec;
16 import org.opendaylight.yangtools.yang.model.ri.type.BaseTypes;
17
18 /**
19  * Unit tests for Int64CodecString.
20  *
21  * @author Thomas Pantelis
22  */
23 public class Int64CodecStringTest {
24
25     @SuppressWarnings("unchecked")
26     @Test
27     public void testSerialize() {
28         Int64Codec<String> codec = getCodec(BaseTypes.int64Type(), Int64Codec.class);
29         assertEquals("serialize", "12345", codec.serialize(Long.valueOf(12345)));
30     }
31
32     @SuppressWarnings("unchecked")
33     @Test
34     public void testDeserialize() {
35         final String hexa = "0X75EDC78edCBA";
36         final String negHexa = "-0X75EDC78edCBA";
37         final String octal = "+03536670743556272";
38         final String negOctal = "-03536670743556272";
39         final String integer = "+129664115727546";
40         final String negInteger = "-129664115727546";
41
42         Int64Codec<String> codec = getCodec(BaseTypes.int64Type(), Int64Codec.class);
43
44         assertEquals("deserialize", codec.deserialize(hexa), Long.valueOf("075EDC78edCBA", 16));
45         assertEquals("deserialize", codec.deserialize(negHexa), Long.valueOf("-075EDC78edCBA", 16));
46         assertEquals("deserialize", codec.deserialize(octal), Long.valueOf(octal, 8));
47         assertEquals("deserialize", codec.deserialize(negOctal), Long.valueOf(negOctal, 8));
48         assertEquals("deserialize", codec.deserialize(integer), Long.valueOf(integer, 10));
49         assertEquals("deserialize", codec.deserialize(negInteger), Long.valueOf(negInteger, 10));
50
51         deserializeWithExpectedIllegalArgEx(codec, "1234o");
52         deserializeWithExpectedIllegalArgEx(codec, "");
53     }
54 }