Bump to odlparent 3.1.0 and yangtools 2.0.3
[packetcable.git] / packetcable-driver / src / test / java / org / umu / cops / stack / COPSMsgParserTest.java
1 /*
2  * (c) 2015 Cable Television Laboratories, Inc.  All rights reserved.
3  */
4
5 package org.umu.cops.stack;
6
7 import org.junit.Assert;
8 import org.junit.Test;
9 import org.pcmm.rcd.IPCMMClient;
10
11 import java.util.Random;
12
13 /**
14  * Tests the public static COPSMsgParser methods
15  */
16 public class COPSMsgParserTest {
17
18     @Test
19     public void testCombineTwoBytesToOne() {
20
21         byte byte1 = (byte)2;
22         byte byte2 = (byte)15;
23         byte combined = (byte)((byte1) * 16 + (byte2));
24
25         System.out.println("Combined value =" + combined + " Hex value = " + Integer.toHexString(combined) );
26
27     }
28
29     @Test
30     public void testCombineAndSplitForAllValidCombinations() {
31         for (byte byte1 = 0; byte1 < 16; byte1++ ) {
32             for (byte byte2 = 0; byte2 < 16; byte2++ ) {
33                 int combined = COPSMsgParser.combineNibbles(byte1, byte2);
34                 System.out.println("byte1 = " + byte1 + " byte2 = " + byte2 + " Combined value =" + combined + " Hex value = " + Integer.toHexString(combined) );
35
36                 Assert.assertTrue(combined >= 0 && combined < 256);
37                 byte[] nibbles = COPSMsgParser.splitByteToNibbles((byte) combined);
38
39                 Assert.assertEquals("Nibble 1 value = " + nibbles[0], byte1, nibbles[0]);
40                 Assert.assertEquals("Nibble 2 value = " + nibbles[1], byte2, nibbles[1]);
41             }
42         }
43     }
44
45     @Test
46     public void testBytesToShortMin() {
47         final byte byte1 = (byte)0;
48         final byte byte2 = (byte)0;
49         final short val = COPSMsgParser.bytesToShort(byte1, byte2);
50         final byte[] outBytes = COPSMsgParser.shortToBytes(val);
51         Assert.assertEquals(byte1, outBytes[0]);
52         Assert.assertEquals(byte2, outBytes[1]);
53         Assert.assertEquals(0, val);
54     }
55
56     @Test
57     public void testBytesToShortMax() {
58         final byte byte1 = (byte)255;
59         final byte byte2 = (byte)255;
60         final short val = COPSMsgParser.bytesToShort(byte1, byte2);
61         final byte[] outBytes = COPSMsgParser.shortToBytes(val);
62         Assert.assertEquals(byte1, outBytes[0]);
63         Assert.assertEquals(byte2, outBytes[1]);
64     }
65
66     @Test
67     public void bytesToShortAndBack() {
68         final Random rnd = new Random();
69         final short val = (short)rnd.nextInt();
70         final byte[] bytes = COPSMsgParser.shortToBytes(val);
71         final short parsed = COPSMsgParser.bytesToShort(bytes[0], bytes[1]);
72         Assert.assertEquals(val, parsed);
73     }
74
75     @Test
76     public void testBytesToIntMin() {
77         final byte byte1 = (byte)0;
78         final byte byte2 = (byte)0;
79         final byte byte3 = (byte)0;
80         final byte byte4 = (byte)0;
81         final int val = COPSMsgParser.bytesToInt(byte1, byte2, byte3, byte4);
82         final byte[] outBytes = COPSMsgParser.intToBytes(val);
83         Assert.assertEquals(byte1, outBytes[0]);
84         Assert.assertEquals(byte2, outBytes[1]);
85         Assert.assertEquals(byte3, outBytes[2]);
86         Assert.assertEquals(byte4, outBytes[3]);
87         Assert.assertEquals(0, val);
88     }
89
90     @Test
91     public void intToBytesAndBack() {
92         final int val = 100001;
93         final byte[] bytes = COPSMsgParser.intToBytes(val);
94         final int parsed = COPSMsgParser.bytesToInt(bytes[0], bytes[1], bytes[2], bytes[3]);
95         Assert.assertEquals(val, parsed);
96         System.out.println("Sucessfully converted value - " + val);
97     }
98
99     @Test
100     public void randomIntToBytesAndBack() {
101         for (int i = 0; i < 5; i++) {
102             final Random rnd = new Random();
103             final int val = rnd.nextInt();
104             final byte[] bytes = COPSMsgParser.intToBytes(val);
105             final int parsed = COPSMsgParser.bytesToInt(bytes[0], bytes[1], bytes[2], bytes[3]);
106             Assert.assertEquals(val, parsed);
107             System.out.println("Sucessfully converted value - " + val);
108         }
109     }
110
111     @Test
112     public void testBytesToShortPCMMClientType() {
113         final byte[] outBytes = COPSMsgParser.shortToBytes(IPCMMClient.CLIENT_TYPE);
114         final short val = COPSMsgParser.bytesToShort(outBytes[0], outBytes[1]);
115         Assert.assertEquals(IPCMMClient.CLIENT_TYPE, val);
116     }
117
118     @Test(expected = IllegalArgumentException.class)
119     public void testCombineByte1TooBig() {
120         COPSMsgParser.combineNibbles((byte)16, (byte)0);
121     }
122
123     @Test(expected = IllegalArgumentException.class)
124     public void testCombineByte2TooBig() {
125         COPSMsgParser.combineNibbles((byte)0, (byte)16);
126     }
127
128     @Test(expected = IllegalArgumentException.class)
129     public void testCombineByte1TooSmall() {
130         COPSMsgParser.combineNibbles((byte)-1, (byte)0);
131     }
132
133     @Test(expected = IllegalArgumentException.class)
134     public void testCombineByte2TooSmall() {
135         COPSMsgParser.combineNibbles((byte)0, (byte)-1);
136     }
137
138     // TODO - determine if tests for marshalling & un should be done with this class or implicitly via the COPSMsg objects
139
140 }