Merge changes If0630105,I9d2d5e61,I1cea2a32,Icc05b6a7,Ic57eb4f8, ...
[packetcable.git] / packetcable-driver / src / test / java / org / umu / cops / stack / COPSClientSITest.java
1 package org.umu.cops.stack;
2
3 import org.junit.Assert;
4 import org.junit.Test;
5 import org.umu.cops.stack.COPSClientSI.CSIType;
6 import org.umu.cops.stack.COPSObjHeader.CNum;
7 import org.umu.cops.stack.COPSObjHeader.CType;
8
9 import java.io.ByteArrayOutputStream;
10
11 /**
12  * Tests for the first constructor of the COPSClientSI class.
13  * Should any of these tests be inaccurate it is due to the fact that they have been written after COPSAcctTimer had been
14  * released and my assumptions may be incorrect.
15  */
16 public class COPSClientSITest {
17
18     @Test(expected = IllegalArgumentException.class)
19     public void nullCNumConstructor1() {
20         new COPSClientSI(null, CType.CSI, new COPSData());
21     }
22
23     @Test(expected = IllegalArgumentException.class)
24     public void invalidCNumConstructor1() {
25         new COPSClientSI(CNum.ACCT_TIMER, CType.CSI, new COPSData());
26     }
27
28     @Test(expected = IllegalArgumentException.class)
29     public void nullCTypeConstructor1() {
30         new COPSClientSI(CNum.DEC, null, new COPSData());
31     }
32
33     @Test(expected = IllegalArgumentException.class)
34     public void invalidCTypeConstructor1() {
35         new COPSClientSI(CNum.DEC, CType.NA, new COPSData());
36     }
37
38     @Test(expected = IllegalArgumentException.class)
39     public void nullDataConstructor1() {
40         new COPSClientSI(CNum.DEC, CType.CSI, null);
41     }
42
43     @Test(expected = IllegalArgumentException.class)
44     public void invalidCSItype() {
45         new COPSClientSI(CSIType.NA, new COPSData());
46     }
47
48     @Test(expected = IllegalArgumentException.class)
49     public void nullData() {
50         new COPSClientSI(CSIType.SIGNALED, null);
51     }
52
53     @Test(expected = IllegalArgumentException.class)
54     public void invalidCNum() {
55         new COPSClientSI(new COPSObjHeader(CNum.ACCT_TIMER, CType.DEF), new COPSData());
56     }
57
58     @Test(expected = IllegalArgumentException.class)
59     public void invalidCType() {
60         new COPSClientSI(new COPSObjHeader(CNum.ACCT_TIMER, CType.CSI), new COPSData());
61     }
62
63     @Test
64     public void validConstructor1() {
65         final COPSClientSI clientSI = new COPSClientSI(CNum.DEC, CType.CSI, new COPSData());
66         Assert.assertEquals(CSIType.NAMED, clientSI.getCsiType());
67         Assert.assertEquals(CNum.DEC, clientSI.getHeader().getCNum());
68         Assert.assertEquals(CType.CSI, clientSI.getHeader().getCType());
69         Assert.assertEquals(new COPSData(), clientSI.getData());
70     }
71
72     @Test
73     public void csiSignaledEmptyData() {
74         final COPSClientSI clientSI = new COPSClientSI(CSIType.SIGNALED, new COPSData());
75         Assert.assertEquals(CSIType.SIGNALED, clientSI.getCsiType());
76         Assert.assertEquals(CNum.CSI, clientSI.getHeader().getCNum());
77         Assert.assertEquals(CSIType.SIGNALED.ordinal(), clientSI.getHeader().getCType().ordinal());
78         Assert.assertEquals(new COPSData(), clientSI.getData());
79         Assert.assertEquals(0, clientSI.getDataLength());
80     }
81
82     @Test
83     public void csiSignaledUnPaddedData() {
84         final COPSClientSI clientSI = new COPSClientSI(CSIType.SIGNALED, new COPSData("1234"));
85         Assert.assertEquals(CSIType.SIGNALED, clientSI.getCsiType());
86         Assert.assertEquals(CNum.CSI, clientSI.getHeader().getCNum());
87         Assert.assertEquals(CSIType.SIGNALED.ordinal(), clientSI.getHeader().getCType().ordinal());
88         Assert.assertEquals(new COPSData("1234"), clientSI.getData());
89         Assert.assertEquals(4, clientSI.getDataLength());
90     }
91
92     @Test
93     public void csiSignaledPaddedData() {
94         final COPSClientSI clientSI = new COPSClientSI(CSIType.SIGNALED, new COPSData("12345"));
95         Assert.assertEquals(CSIType.SIGNALED, clientSI.getCsiType());
96         Assert.assertEquals(CNum.CSI, clientSI.getHeader().getCNum());
97         Assert.assertEquals(CSIType.SIGNALED.ordinal(), clientSI.getHeader().getCType().ordinal());
98         Assert.assertEquals(new COPSData("12345"), clientSI.getData());
99         Assert.assertEquals(8, clientSI.getDataLength());
100     }
101
102     @Test
103     public void csiNamedPaddedData() {
104         final COPSClientSI clientSI = new COPSClientSI(CSIType.NAMED, new COPSData("12345"));
105         Assert.assertEquals(CSIType.NAMED, clientSI.getCsiType());
106         Assert.assertEquals(CNum.CSI, clientSI.getHeader().getCNum());
107         Assert.assertEquals(CSIType.NAMED.ordinal(), clientSI.getHeader().getCType().ordinal());
108         Assert.assertEquals(new COPSData("12345"), clientSI.getData());
109         Assert.assertEquals(8, clientSI.getDataLength());
110     }
111
112     @Test
113     public void csiNamedDumpPadded() throws Exception {
114         final COPSClientSI clientSI = new COPSClientSI(CSIType.NAMED, new COPSData("12345"));
115         final ByteArrayOutputStream os = new ByteArrayOutputStream();
116         clientSI.dump(os);
117
118         final String out = new String(os.toByteArray());
119         System.out.println(out);
120         final String[] lines = out.split("\n");
121         Assert.assertEquals(5, lines.length);
122         Assert.assertEquals("**Client-SI**", lines[0]);
123         Assert.assertEquals("C-num: CSI", lines[1]);
124         Assert.assertEquals("C-type: STATELESS", lines[2]);
125         Assert.assertEquals("CSI-type: NAMED", lines[3]);
126         Assert.assertEquals("client-SI: 12345", lines[4]);
127     }
128
129     @Test
130     public void csiSignaledDumpUnpadded() throws Exception {
131         final COPSClientSI clientSI = new COPSClientSI(CSIType.SIGNALED, new COPSData("1234"));
132         final ByteArrayOutputStream os = new ByteArrayOutputStream();
133         clientSI.dump(os);
134
135         final String out = new String(os.toByteArray());
136         System.out.println(out);
137         final String[] lines = out.split("\n");
138         Assert.assertEquals(5, lines.length);
139         Assert.assertEquals("**Client-SI**", lines[0]);
140         Assert.assertEquals("C-num: CSI", lines[1]);
141         Assert.assertEquals("C-type: DEF", lines[2]);
142         Assert.assertEquals("CSI-type: SIGNALED", lines[3]);
143         Assert.assertEquals("client-SI: 1234", lines[4]);
144     }
145
146     // The writeData() method will be tested implicitly via any of the COPSMsg tests
147 }