Merge "Copyright"
[packetcable.git] / packetcable-driver / src / test / java / org / umu / cops / stack / COPSReasonTest.java
1 package org.umu.cops.stack;
2
3 import org.junit.Assert;
4 import org.junit.Test;
5 import org.umu.cops.stack.COPSReason.ReasonCode;
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 COPSReason 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 COPSReasonTest {
17
18     @Test(expected = IllegalArgumentException.class)
19     public void nullReasonCode() {
20         new COPSReason(null, ReasonCode.NA);
21     }
22
23     @Test(expected = IllegalArgumentException.class)
24     public void invalidReasonCode() {
25         new COPSReason(ReasonCode.NA, ReasonCode.MALFORMED_DEC);
26     }
27
28     @Test(expected = IllegalArgumentException.class)
29     public void nullSubCode() {
30         new COPSReason(ReasonCode.MANAGEMENT, null);
31     }
32
33     @Test(expected = IllegalArgumentException.class)
34     public void invalidCNum() {
35         new COPSReason(new COPSObjHeader(CNum.ACCT_TIMER, CType.DEF), ReasonCode.ROUTE_CHANGE, ReasonCode.NA);
36     }
37
38     @Test(expected = IllegalArgumentException.class)
39     public void invalidCType() {
40         new COPSReason(new COPSObjHeader(CNum.REASON_CODE, CType.CSI), ReasonCode.PREEMPTED, ReasonCode.SYNC_HANDLE);
41     }
42
43     @Test
44     public void valid1() throws Exception {
45         final COPSReason reason = new COPSReason(ReasonCode.TRANS_HANDLE, ReasonCode.NA);
46         Assert.assertEquals(ReasonCode.TRANS_HANDLE, reason.getReasonCode());
47         Assert.assertEquals(ReasonCode.NA, reason.getReasonSubCode());
48         Assert.assertEquals(4, reason.getDataLength());
49         Assert.assertEquals("Transient handle.:", reason.getDescription());
50
51         final ByteArrayOutputStream os = new ByteArrayOutputStream();
52         reason.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("**Reason**", lines[0]);
59         Assert.assertEquals("C-num: REASON_CODE", lines[1]);
60         Assert.assertEquals("C-type: DEF", lines[2]);
61         Assert.assertEquals("Reason Code: TRANS_HANDLE", lines[3]);
62         Assert.assertEquals("Reason Sub Code: NA", lines[4]);
63     }
64
65     @Test
66     public void valid2() throws Exception {
67         final COPSReason reason = new COPSReason(ReasonCode.INSUFF_RESOURCES, ReasonCode.TEAR);
68         Assert.assertEquals(ReasonCode.INSUFF_RESOURCES, reason.getReasonCode());
69         Assert.assertEquals(ReasonCode.TEAR, reason.getReasonSubCode());
70         Assert.assertEquals(4, reason.getDataLength());
71         Assert.assertEquals("Insufficient Resources.:", reason.getDescription());
72
73         final ByteArrayOutputStream os = new ByteArrayOutputStream();
74         reason.dump(os);
75
76         final String out = new String(os.toByteArray());
77         System.out.println(out);
78         final String[] lines = out.split("\n");
79         Assert.assertEquals(5, lines.length);
80         Assert.assertEquals("**Reason**", lines[0]);
81         Assert.assertEquals("C-num: REASON_CODE", lines[1]);
82         Assert.assertEquals("C-type: DEF", lines[2]);
83         Assert.assertEquals("Reason Code: INSUFF_RESOURCES", lines[3]);
84         Assert.assertEquals("Reason Sub Code: TEAR", lines[4]);
85     }
86
87     // The writeData() method will be tested implicitly via any of the COPSMsg tests
88 }