1 package org.umu.cops.stack;
3 import org.junit.After;
4 import org.junit.Assert;
5 import org.junit.Before;
7 import org.pcmm.rcd.IPCMMClient;
8 import org.umu.cops.stack.COPSHeader.Flag;
9 import org.umu.cops.stack.COPSHeader.OPCode;
11 import java.io.ByteArrayOutputStream;
12 import java.net.InetAddress;
13 import java.net.Socket;
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.
20 public class COPSKAMsgTest {
22 TestCOPSServer server;
26 public void setup() throws Exception {
27 server = new TestCOPSServer();
29 outSocket = new Socket(InetAddress.getLocalHost(), server.getPort());
33 public void tearDown() throws Exception {
38 @Test(expected = IllegalArgumentException.class)
39 public void version0() {
40 new COPSKAMsg(0, Flag.SOLICITED, null);
43 @Test(expected = IllegalArgumentException.class)
44 public void nullFlag() {
45 new COPSKAMsg(1, null, null);
48 @Test(expected = IllegalArgumentException.class)
49 public void nullHeader() {
50 final COPSHeader hdr = null;
51 new COPSKAMsg(hdr, null);
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);
61 public void validMinimal() {
62 final COPSKAMsg msg = new COPSKAMsg(1, Flag.SOLICITED, null);
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());
71 public void validAll() throws Exception {
72 final COPSKAMsg msg = new COPSKAMsg(1, Flag.SOLICITED, new COPSIntegrity());
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());
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
86 public void testDumpAll() throws Exception {
87 final COPSKAMsg msg = new COPSKAMsg(1, Flag.SOLICITED, new COPSIntegrity());
89 final ByteArrayOutputStream os = new ByteArrayOutputStream();
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);
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]);
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
111 public void testWriteMinimal() throws Exception {
112 final COPSKAMsg msg = new COPSKAMsg(1, Flag.SOLICITED, null);
114 msg.writeData(outSocket);
116 final long start = System.currentTimeMillis();
117 while (server.copsMsgs.size() < 1) {
119 if (System.currentTimeMillis() - start > 2000) break;
122 Assert.assertEquals(1, server.copsMsgs.size());
123 Assert.assertEquals(msg, server.copsMsgs.get(0));
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
132 public void testWriteAll() throws Exception {
133 final COPSKAMsg msg = new COPSKAMsg(1, Flag.SOLICITED, new COPSIntegrity(8, 9, new COPSData("12345")));
135 msg.writeData(outSocket);
137 final long start = System.currentTimeMillis();
138 while (server.copsMsgs.size() < 1) {
140 if (System.currentTimeMillis() - start > 2000) break;
143 Assert.assertEquals(1, server.copsMsgs.size());
144 Assert.assertEquals(msg, server.copsMsgs.get(0));