Replaced bitwise operator usage with java lang APIs for robustness. Had found that...
[packetcable.git] / packetcable-driver / src / test / java / org / umu / cops / stack / COPSKAMsgTest.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 COPSKAMsg class.
17  * Should any of these tests be inaccurate it is due to the fact that they have been written after COPSKAMsg had been
18  * released and my assumptions may be incorrect.
19  */
20 public class COPSKAMsgTest {
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 COPSKAMsg(0, Flag.SOLICITED, null);
41     }
42
43     @Test(expected = IllegalArgumentException.class)
44     public void nullFlag() {
45         new COPSKAMsg(1, null, null);
46     }
47
48     @Test(expected = IllegalArgumentException.class)
49     public void nullHeader() {
50         final COPSHeader hdr = null;
51         new COPSKAMsg(hdr, null);
52     }
53
54     @Test(expected = IllegalArgumentException.class)
55     public void invalidHeader() {
56         final COPSHeader hdr = new COPSHeader(1, Flag.UNSOLICITED, OPCode.NA, IPCMMClient.CLIENT_TYPE);
57         new COPSKAMsg(hdr, null);
58     }
59
60     @Test
61     public void validMinimal() {
62         final COPSKAMsg msg = new COPSKAMsg(1, Flag.SOLICITED, null);
63
64         Assert.assertEquals(1, msg.getHeader().getPcmmVersion());
65         Assert.assertEquals(Flag.SOLICITED, msg.getHeader().getFlag());
66         Assert.assertEquals((short)0, msg.getHeader().getClientType());
67         Assert.assertNull(msg.getIntegrity());
68     }
69
70     @Test
71     public void validAll() throws Exception {
72         final COPSKAMsg msg = new COPSKAMsg(1, Flag.SOLICITED, new COPSIntegrity());
73
74         Assert.assertEquals(1, msg.getHeader().getPcmmVersion());
75         Assert.assertEquals(Flag.SOLICITED, msg.getHeader().getFlag());
76         Assert.assertEquals((short)0, msg.getHeader().getClientType());
77         Assert.assertEquals(new COPSIntegrity(), msg.getIntegrity());
78     }
79
80     /**
81      * This test is responsible for creating a COPSKAMsg object without any nulls or empty collections
82      * and then is dumped to an OutputStream.
83      * @throws Exception - Test should fail if any exception is thrown
84      */
85     @Test
86     public void testDumpAll() throws Exception {
87         final COPSKAMsg msg = new COPSKAMsg(1, Flag.SOLICITED, new COPSIntegrity());
88
89         final ByteArrayOutputStream os = new ByteArrayOutputStream();
90         msg.dump(os);
91
92         final String out = new String(os.toByteArray());
93         System.out.println(out);
94         final String[] lines = out.split("\n");
95         Assert.assertEquals(11, lines.length);
96
97         // Only checking COPSMsg elements as the COPSObjectMsg elements have already been validated in their own tests
98         Assert.assertEquals("**MSG HEADER**", lines[0]);
99         Assert.assertEquals("Version: 1", lines[1]);
100         Assert.assertEquals("Flags: SOLICITED", lines[2]);
101         Assert.assertEquals("OpCode: KA", lines[3]);
102         Assert.assertEquals("Client-type: 0", lines[4]);
103     }
104
105     /**
106      * This test is responsible for creating a COPSKAMsg object with the minimal necessary attributes to make
107      * it valid. It is then streamed over a socket (unmarshalled) then reassembled (marshalled).
108      * @throws Exception - Test should fail if any exception is thrown
109      */
110     @Test
111     public void testWriteMinimal() throws Exception {
112         final COPSKAMsg msg = new COPSKAMsg(1, Flag.SOLICITED, null);
113
114         msg.writeData(outSocket);
115
116         final long start = System.currentTimeMillis();
117         while (server.copsMsgs.size() < 1) {
118             Thread.sleep(5);
119             if (System.currentTimeMillis() - start > 2000) break;
120         }
121
122         Assert.assertEquals(1, server.copsMsgs.size());
123         Assert.assertEquals(msg, server.copsMsgs.get(0));
124     }
125
126     /**
127      * This test is responsible for creating a COPSKAMsg object without any nulls or empty collections
128      * and then is streamed over a socket (unmarshalled) then reassembled (marshalled)
129      * @throws Exception - Test should fail if any exception is thrown
130      */
131     @Test
132     public void testWriteAll() throws Exception {
133         final COPSKAMsg msg = new COPSKAMsg(1, Flag.SOLICITED, new COPSIntegrity(8, 9, new COPSData("12345")));
134
135         msg.writeData(outSocket);
136
137         final long start = System.currentTimeMillis();
138         while (server.copsMsgs.size() < 1) {
139             Thread.sleep(5);
140             if (System.currentTimeMillis() - start > 2000) break;
141         }
142
143         Assert.assertEquals(1, server.copsMsgs.size());
144         Assert.assertEquals(msg, server.copsMsgs.get(0));
145     }
146
147 }