BUG-770: NumberFormatException for datapathId
[openflowplugin.git] / openflowplugin / src / test / java / org / opendaylight / openflowplugin / openflow / md / util / InventoryDataServiceUtilTest.java
1 /**
2  * Copyright (c) 2013 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.openflowplugin.openflow.md.util;
9
10 import java.math.BigInteger;
11
12 import org.junit.Assert;
13 import org.junit.Test;
14 import org.opendaylight.yang.gen.v1.urn.opendaylight.inventory.rev130819.NodeId;
15
16 /**
17  * 
18  */
19 public class InventoryDataServiceUtilTest {
20
21     /**
22      * Test method for {@link InventoryDataServiceUtil#dataPathIdFromNodeId(NodeId)}.
23      */
24     @Test
25     public void testDataPathIdFromNodeId() {
26         String string = "openflow:";
27         NodeId[] nodeIds = new NodeId[] {
28                 // 0x00000000 000004d2
29                 new NodeId(string + "1234"),
30                 // 0x8db2089e 01391a86
31                 new NodeId(string + "10210232779920710278"),
32                 // 0xffffffff ffffffff
33                 new NodeId(string + "18446744073709551615"),
34         };
35         
36         long[] expectedDPIDs = new long[] {
37                 1234L,
38                 -8236511293788841338L,
39                 -1L
40         };
41         
42         for (int i = 0; i < nodeIds.length; i++) {
43             BigInteger datapathId = InventoryDataServiceUtil.dataPathIdFromNodeId(nodeIds[i] );
44             Assert.assertEquals(expectedDPIDs[i], datapathId.longValue());
45         }
46     }
47     
48     /**
49      * Test method for {@link InventoryDataServiceUtil#bigIntegerToPaddedHex(BigInteger)}.
50      */
51     @Test
52     public void testLongToPaddedHex() {
53         BigInteger[] dpids = new BigInteger[] {
54                 // 0x00000000 000004d2
55                 new BigInteger("1234"),
56                 // 0x8db2089e 01391a86
57                 new BigInteger("10210232779920710278"),
58                 // 0xffffffff ffffffff
59                 new BigInteger("18446744073709551615"),
60         };
61         
62         String[] expectedPaddedHexes = new String[] {
63                 "00000000000004d2",
64                 "8db2089e01391a86",
65                 "ffffffffffffffff"
66         };
67         
68         for (int i = 0; i < dpids.length; i++) {
69             String datapathIdHex = InventoryDataServiceUtil.bigIntegerToPaddedHex(dpids[i]);
70             Assert.assertEquals(expectedPaddedHexes[i], datapathIdHex);
71         }
72     }
73
74 }