Merge "The second patch of an estimated 4 to complete the COPS message refactoring...
[packetcable.git] / packetcable-driver / src / test / java / org / umu / cops / stack / COPSIpv4InInterfaceTest.java
1 package org.umu.cops.stack;
2
3 import org.junit.Assert;
4 import org.junit.Test;
5 import org.umu.cops.stack.COPSObjHeader.CNum;
6 import org.umu.cops.stack.COPSObjHeader.CType;
7
8 import java.io.ByteArrayOutputStream;
9
10 /**
11  * Tests for the first constructor of the COPSIpv4InInterface class.
12  * Should any of these tests be inaccurate it is due to the fact that they have been written after COPSAcctTimer had been
13  * released and my assumptions may be incorrect.
14  */
15 public class COPSIpv4InInterfaceTest {
16
17     @Test(expected = IllegalArgumentException.class)
18     public void nullAddress() {
19         new COPSIpv4InInterface(null, 0);
20     }
21
22     @Test(expected = IllegalArgumentException.class)
23     public void nullHeader() throws Exception {
24         new COPSIpv4InInterface(null, new COPSIpv4Address("localhost"), 0);
25     }
26
27     @Test(expected = IllegalArgumentException.class)
28     public void nullAddressWithHeader() {
29         new COPSIpv4InInterface(new COPSObjHeader(CNum.ININTF, CType.DEF), null, 0);
30     }
31
32     @Test(expected = IllegalArgumentException.class)
33     public void invalidCNum() {
34         new COPSIpv4InInterface(new COPSObjHeader(CNum.HANDLE, CType.DEF), null, 0);
35     }
36
37     @Test(expected = IllegalArgumentException.class)
38     public void invalidCType() {
39         new COPSIpv4InInterface(new COPSObjHeader(CNum.ININTF, CType.STATELESS), null, 0);
40     }
41
42     @Test
43     public void valid() throws Exception {
44         final COPSIpv4Address address = new COPSIpv4Address("localhost");
45         final COPSIpv4InInterface intf = new COPSIpv4InInterface(address, 5);
46         Assert.assertEquals(new COPSObjHeader(CNum.ININTF, CType.DEF), intf.getHeader());
47         Assert.assertEquals(8, intf.getDataLength());
48         Assert.assertEquals(address, intf._addr);
49         Assert.assertEquals(5, intf._ifindex);
50
51         final ByteArrayOutputStream os = new ByteArrayOutputStream();
52         intf.dump(os);
53
54         final String out = new String(os.toByteArray());
55         System.out.println(out);
56         final String[] lines = out.split("\n");
57         Assert.assertEquals(5, lines.length);
58         Assert.assertEquals("**In-Interface**", lines[0]);
59         Assert.assertEquals("C-num: ININTF", lines[1]);
60         Assert.assertEquals("C-type: DEF", lines[2]);
61         Assert.assertEquals("Address: localhost", lines[3]);
62         Assert.assertEquals("ifindex: 5", lines[4]);
63     }
64
65     // The writeData() method will be tested implicitly via any of the COPSMsg tests
66 }