Merge "Copyright"
[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     private final static int testPort = 7777;
23     TestCOPSServer server;
24     Socket outSocket;
25
26     @Before
27     public void setup() throws Exception {
28         server = new TestCOPSServer(testPort);
29         server.start();
30         outSocket = new Socket(InetAddress.getLocalHost(), testPort);
31     }
32
33     @After
34     public void tearDown() throws Exception {
35         outSocket.close();
36         server.close();
37     }
38
39     @Test(expected = IllegalArgumentException.class)
40     public void version0() {
41         new COPSKAMsg(0, Flag.SOLICITED, null);
42     }
43
44     @Test(expected = IllegalArgumentException.class)
45     public void nullFlag() {
46         new COPSKAMsg(1, null, null);
47     }
48
49     @Test(expected = IllegalArgumentException.class)
50     public void nullHeader() {
51         final COPSHeader hdr = null;
52         new COPSKAMsg(hdr, null);
53     }
54
55     @Test(expected = IllegalArgumentException.class)
56     public void invalidHeader() {
57         final COPSHeader hdr = new COPSHeader(1, Flag.UNSOLICITED, OPCode.NA, IPCMMClient.CLIENT_TYPE);
58         new COPSKAMsg(hdr, null);
59     }
60
61     @Test
62     public void validMinimal() {
63         final COPSKAMsg msg = new COPSKAMsg(1, Flag.SOLICITED, null);
64
65         Assert.assertEquals(1, msg.getHeader().getPcmmVersion());
66         Assert.assertEquals(Flag.SOLICITED, msg.getHeader().getFlag());
67         Assert.assertEquals((short)0, msg.getHeader().getClientType());
68         Assert.assertNull(msg.getIntegrity());
69     }
70
71     @Test
72     public void validAll() throws Exception {
73         final COPSKAMsg msg = new COPSKAMsg(1, Flag.SOLICITED, new COPSIntegrity());
74
75         Assert.assertEquals(1, msg.getHeader().getPcmmVersion());
76         Assert.assertEquals(Flag.SOLICITED, msg.getHeader().getFlag());
77         Assert.assertEquals((short)0, msg.getHeader().getClientType());
78         Assert.assertEquals(new COPSIntegrity(), msg.getIntegrity());
79     }
80
81     /**
82      * This test is responsible for creating a COPSKAMsg object without any nulls or empty collections
83      * and then is dumped to an OutputStream.
84      * @throws Exception - Test should fail if any exception is thrown
85      */
86     @Test
87     public void testDumpAll() throws Exception {
88         final COPSKAMsg msg = new COPSKAMsg(1, Flag.SOLICITED, new COPSIntegrity());
89
90         final ByteArrayOutputStream os = new ByteArrayOutputStream();
91         msg.dump(os);
92
93         final String out = new String(os.toByteArray());
94         System.out.println(out);
95         final String[] lines = out.split("\n");
96         Assert.assertEquals(11, lines.length);
97
98         // Only checking COPSMsg elements as the COPSObjectMsg elements have already been validated in their own tests
99         Assert.assertEquals("**MSG HEADER**", lines[0]);
100         Assert.assertEquals("Version: 1", lines[1]);
101         Assert.assertEquals("Flags: SOLICITED", lines[2]);
102         Assert.assertEquals("OpCode: KA", lines[3]);
103         Assert.assertEquals("Client-type: 0", lines[4]);
104     }
105
106     /**
107      * This test is responsible for creating a COPSKAMsg object with the minimal necessary attributes to make
108      * it valid. It is then streamed over a socket (unmarshalled) then reassembled (marshalled).
109      * @throws Exception - Test should fail if any exception is thrown
110      */
111     @Test
112     public void testWriteMinimal() throws Exception {
113         final COPSKAMsg msg = new COPSKAMsg(1, Flag.SOLICITED, null);
114
115         msg.writeData(outSocket);
116
117         final long start = System.currentTimeMillis();
118         while (server.copsMsgs.size() < 1) {
119             Thread.sleep(5);
120             if (System.currentTimeMillis() - start > 2000) break;
121         }
122
123         Assert.assertEquals(1, server.copsMsgs.size());
124         Assert.assertEquals(msg, server.copsMsgs.get(0));
125     }
126
127     /**
128      * This test is responsible for creating a COPSKAMsg object without any nulls or empty collections
129      * and then is streamed over a socket (unmarshalled) then reassembled (marshalled)
130      * @throws Exception - Test should fail if any exception is thrown
131      */
132     @Test
133     public void testWriteAll() throws Exception {
134         final COPSKAMsg msg = new COPSKAMsg(1, Flag.SOLICITED, new COPSIntegrity(8, 9, new COPSData("12345")));
135
136         msg.writeData(outSocket);
137
138         final long start = System.currentTimeMillis();
139         while (server.copsMsgs.size() < 1) {
140             Thread.sleep(5);
141             if (System.currentTimeMillis() - start > 2000) break;
142         }
143
144         Assert.assertEquals(1, server.copsMsgs.size());
145         Assert.assertEquals(msg, server.copsMsgs.get(0));
146     }
147
148 }