added new serialization factories and their tests
[openflowjava.git] / openflow-protocol-impl / src / test / java / org / opendaylight / openflowjava / protocol / impl / util / ByteBufUtilsTest.java
1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */\r
2 package org.opendaylight.openflowjava.protocol.impl.util;\r
3 \r
4 import io.netty.buffer.ByteBuf;\r
5 import io.netty.buffer.UnpooledByteBufAllocator;\r
6 \r
7 import java.util.HashMap;\r
8 import java.util.Map;\r
9 \r
10 import org.junit.Assert;\r
11 import org.junit.Test;\r
12 \r
13 /**\r
14  * @author michal.polkorab\r
15  *\r
16  */\r
17 public class ByteBufUtilsTest {\r
18 \r
19     private byte[] expected = new byte[]{0x01, 0x02, 0x03, 0x04, 0x05, (byte) 0xff};\r
20     \r
21     /**\r
22      * Test of {@link ByteBufUtils#hexStringToBytes(String)}\r
23      */\r
24     @Test\r
25     public void testHexStringToBytes() {\r
26         byte[] data = ByteBufUtils.hexStringToBytes("01 02 03 04 05 ff");\r
27 \r
28         Assert.assertArrayEquals(expected, data);\r
29     }\r
30     \r
31     /**\r
32      * Test of {@link ByteBufUtils#hexStringToBytes(String, boolean)}\r
33      */\r
34     @Test\r
35     public void testHexStringToBytes2() {\r
36         byte[] data = ByteBufUtils.hexStringToBytes("0102030405ff", false);\r
37 \r
38         Assert.assertArrayEquals(expected, data);\r
39     }\r
40     \r
41     /**\r
42      * Test of {@link ByteBufUtils#hexStringToByteBuf(String)}\r
43      */\r
44     @Test\r
45     public void testHexStringToByteBuf() {\r
46         ByteBuf bb = ByteBufUtils.hexStringToByteBuf("01 02 03 04 05 ff");\r
47         \r
48         Assert.assertArrayEquals(expected, byteBufToByteArray(bb));\r
49     }\r
50     \r
51     /**\r
52      * Test of {@link ByteBufUtils#hexStringToByteBuf(String, ByteBuf)}\r
53      */\r
54     @Test\r
55     public void testHexStringToGivenByteBuf() {\r
56         ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.buffer();\r
57         ByteBufUtils.hexStringToByteBuf("01 02 03 04 05 ff", buffer);\r
58 \r
59         Assert.assertArrayEquals(expected, byteBufToByteArray(buffer));\r
60     }\r
61     \r
62     private static byte[] byteBufToByteArray(ByteBuf bb) {\r
63         byte[] result = new byte[bb.readableBytes()];\r
64         bb.readBytes(result);\r
65         return result;\r
66     }\r
67     \r
68     /**\r
69      * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}\r
70      */\r
71     @Test\r
72     public void testFillBitmaskByEmptyMap() {\r
73         Map<Integer, Boolean> emptyMap = new HashMap<>();\r
74         String expectedBinaryString = "00000000000000000000000000000000";\r
75         String bitmaskInBinaryString = toBinaryString(emptyMap, 32);\r
76         \r
77         Assert.assertEquals("Not null string", expectedBinaryString, bitmaskInBinaryString);\r
78     }\r
79 \r
80     private String toBinaryString(Map<Integer, Boolean> emptyMap, int length) {\r
81         String binaryString = Integer.toBinaryString(ByteBufUtils.fillBitMaskFromMap(emptyMap));; \r
82         return String.format("%"+length+"s", binaryString).replaceAll(" ", "0");\r
83     }\r
84     \r
85     /**\r
86      * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}\r
87      */\r
88     @Test\r
89     public void testFillBitmaskByFullMap() {\r
90         Map<Integer, Boolean> fullMap = new HashMap<>();\r
91         String expectedBinaryString = "11111111111111111111111111111111";\r
92         String bitmaskValueInBinarySytring;\r
93         for(Integer i=0;i<=31;i++) {\r
94             fullMap.put(i, true);\r
95         }\r
96         bitmaskValueInBinarySytring = toBinaryString(fullMap, 32);\r
97         Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);\r
98     }\r
99     \r
100     /**\r
101      * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}\r
102      */\r
103     @Test\r
104     public void testFillBitmaskByZeroMap() {\r
105         Map<Integer, Boolean> zeroMap = new HashMap<>();\r
106         String expectedBinaryString = "00000000000000000000000000000000";\r
107         String bitmaskValueInBinarySytring;\r
108         for(Integer i=0;i<=31;i++) {\r
109             zeroMap.put(i, false);\r
110         }\r
111         bitmaskValueInBinarySytring = toBinaryString(zeroMap, 32);\r
112         Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);\r
113     }\r
114     \r
115     /**\r
116      * Test of {@link ByteBufUtils#fillBitMaskFromMap(java.util.Map)}\r
117      */\r
118     @Test\r
119     public void testFillBitmaskByRandomSet() {\r
120         Map<Integer, Boolean> randomMap = new HashMap<>();\r
121         String expectedBinaryString = "00000000000000000111100000000000";\r
122         String bitmaskValueInBinarySytring;\r
123         Boolean mapValue;\r
124         for(Integer i=0;i<=31;i++) {\r
125             mapValue = false;\r
126             if(i>=11 && i<=14) {\r
127                 mapValue = true;\r
128             }\r
129             randomMap.put(i, mapValue);\r
130         }\r
131         bitmaskValueInBinarySytring = toBinaryString(randomMap, 32);\r
132         Assert.assertEquals("Strings does not match", expectedBinaryString, bitmaskValueInBinarySytring);\r
133     }\r
134 \r
135 }\r