Replaced bitwise operator usage with java lang APIs for robustness. Had found that...
[packetcable.git] / packetcable-driver / src / test / java / org / umu / cops / stack / COPSClientAcceptMsgTest.java
1 package org.umu.cops.stack;
2
3 import org.junit.After;
4 import org.junit.Assert;
5 import org.junit.Before;
6 import org.junit.Test;
7 import org.pcmm.rcd.IPCMMClient;
8 import org.umu.cops.stack.COPSHeader.Flag;
9 import org.umu.cops.stack.COPSHeader.OPCode;
10
11 import java.io.ByteArrayOutputStream;
12 import java.net.InetAddress;
13 import java.net.Socket;
14
15 /**
16  * Tests for the first constructor of the COPSClientAcceptMsg class.
17  * Should any of these tests be inaccurate it is due to the fact that they have been written after COPSClientAcceptMsg had been
18  * released and my assumptions may be incorrect.
19  */
20 public class COPSClientAcceptMsgTest {
21
22     TestCOPSServer server;
23     Socket outSocket;
24
25     @Before
26     public void setup() throws Exception {
27         server = new TestCOPSServer();
28         server.start();
29         outSocket = new Socket(InetAddress.getLocalHost(), server.getPort());
30     }
31
32     @After
33     public void tearDown() throws Exception {
34         outSocket.close();
35         server.close();
36     }
37
38     @Test(expected = IllegalArgumentException.class)
39     public void version0() {
40         new COPSClientAcceptMsg(0, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE, new COPSKATimer((short)1), null, null);
41     }
42
43     @Test(expected = IllegalArgumentException.class)
44     public void nullFlag() {
45         new COPSClientAcceptMsg(1, null, IPCMMClient.CLIENT_TYPE, new COPSKATimer((short)1), null, null);
46     }
47
48     @Test(expected = IllegalArgumentException.class)
49     public void nullTimer() {
50         new COPSClientAcceptMsg(1, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE, null, null, null);
51     }
52
53     @Test(expected = IllegalArgumentException.class)
54     public void nullHeader() {
55         final COPSHeader hdr = null;
56         new COPSClientAcceptMsg(hdr, new COPSKATimer((short)1), null, null);
57     }
58
59     @Test(expected = IllegalArgumentException.class)
60     public void invalidHeader() {
61         final COPSHeader hdr = new COPSHeader(1, Flag.UNSOLICITED, OPCode.CC, IPCMMClient.CLIENT_TYPE);
62         new COPSClientAcceptMsg(hdr, new COPSKATimer((short)1), null, null);
63     }
64
65     @Test
66     public void validMinimal() {
67         final COPSClientAcceptMsg msg = new COPSClientAcceptMsg(1, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE,
68                 new COPSKATimer((short)1), null, null);
69
70         Assert.assertEquals(1, msg.getHeader().getPcmmVersion());
71         Assert.assertEquals(Flag.SOLICITED, msg.getHeader().getFlag());
72         Assert.assertEquals(IPCMMClient.CLIENT_TYPE, msg.getHeader().getClientType());
73         Assert.assertEquals(new COPSKATimer((short)1), msg.getKATimer());
74         Assert.assertNull(msg.getAcctTimer());
75         Assert.assertNull(msg.getIntegrity());
76     }
77
78     @Test
79     public void validAll() {
80         final COPSClientAcceptMsg msg = new COPSClientAcceptMsg(1, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE,
81                 new COPSKATimer((short)1), new COPSAcctTimer((short)1), new COPSIntegrity());
82
83         Assert.assertEquals(1, msg.getHeader().getPcmmVersion());
84         Assert.assertEquals(Flag.SOLICITED, msg.getHeader().getFlag());
85         Assert.assertEquals(IPCMMClient.CLIENT_TYPE, msg.getHeader().getClientType());
86         Assert.assertEquals(new COPSKATimer((short)1), msg.getKATimer());
87         Assert.assertEquals(new COPSAcctTimer((short)1), msg.getAcctTimer());
88         Assert.assertEquals(new COPSIntegrity(), msg.getIntegrity());
89     }
90
91     /**
92      * This test is responsible for creating a COPSClientAcceptMsg object without any nulls or empty collections
93      * and then is dumped to an OutputStream.
94      * @throws Exception - Test should fail if any exception is thrown
95      */
96     @Test
97     public void testDumpAll() throws Exception {
98         final COPSClientAcceptMsg msg = new COPSClientAcceptMsg(1, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE,
99                 new COPSKATimer((short)1), new COPSAcctTimer((short)1), new COPSIntegrity());
100
101         final ByteArrayOutputStream os = new ByteArrayOutputStream();
102         msg.dump(os);
103
104         final String out = new String(os.toByteArray());
105         System.out.println(out);
106         final String[] lines = out.split("\n");
107         Assert.assertEquals(19, lines.length);
108
109         // Only checking COPSMsg elements as the COPSObjectMsg elements have already been validated in their own tests
110         Assert.assertEquals("**MSG HEADER**", lines[0]);
111         Assert.assertEquals("Version: 1", lines[1]);
112         Assert.assertEquals("Flags: SOLICITED", lines[2]);
113         Assert.assertEquals("OpCode: CAT", lines[3]);
114         Assert.assertEquals("Client-type: -32758", lines[4]);
115     }
116
117     /**
118      * This test is responsible for creating a COPSClientAcceptMsg object with the minimal necessary attributes to make
119      * it valid. It is then streamed over a socket (unmarshalled) then reassembled (marshalled).
120      * @throws Exception - Test should fail if any exception is thrown
121      */
122     @Test
123     public void testWriteMinimal() throws Exception {
124         final COPSClientAcceptMsg msg = new COPSClientAcceptMsg(1, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE,
125                 new COPSKATimer((short)5), null, null);
126
127         msg.writeData(outSocket);
128
129         final long start = System.currentTimeMillis();
130         while (server.copsMsgs.size() < 1) {
131             Thread.sleep(5);
132             if (System.currentTimeMillis() - start > 2000) break;
133         }
134
135         Assert.assertEquals(1, server.copsMsgs.size());
136         Assert.assertEquals(msg, server.copsMsgs.get(0));
137     }
138
139     /**
140      * This test is responsible for creating a COPSClientAcceptMsg object without any nulls or empty collections
141      * and then is streamed over a socket (unmarshalled) then reassembled (marshalled)
142      * @throws Exception - Test should fail if any exception is thrown
143      */
144     @Test
145     public void testWriteAll() throws Exception {
146         final COPSClientAcceptMsg msg = new COPSClientAcceptMsg(1, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE,
147                 new COPSKATimer((short)5), new COPSAcctTimer((short)6),
148                 new COPSIntegrity(8, 9, new COPSData("12345")));
149
150         msg.writeData(outSocket);
151
152         final long start = System.currentTimeMillis();
153         while (server.copsMsgs.size() < 1) {
154             Thread.sleep(5);
155             if (System.currentTimeMillis() - start > 2000) break;
156         }
157
158         Assert.assertEquals(1, server.copsMsgs.size());
159         Assert.assertEquals(msg, server.copsMsgs.get(0));
160     }
161
162 }