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 private final static int testPort = 7777;
23 TestCOPSServer server;
27 public void setup() throws Exception {
28 server = new TestCOPSServer(testPort);
30 outSocket = new Socket(InetAddress.getLocalHost(), testPort);
34 public void tearDown() throws Exception {
39 @Test(expected = IllegalArgumentException.class)
40 public void version0() {
41 new COPSKAMsg(0, Flag.SOLICITED, null);
44 @Test(expected = IllegalArgumentException.class)
45 public void nullFlag() {
46 new COPSKAMsg(1, null, null);
49 @Test(expected = IllegalArgumentException.class)
50 public void nullHeader() {
51 final COPSHeader hdr = null;
52 new COPSKAMsg(hdr, null);
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);
62 public void validMinimal() {
63 final COPSKAMsg msg = new COPSKAMsg(1, Flag.SOLICITED, null);
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());
72 public void validAll() throws Exception {
73 final COPSKAMsg msg = new COPSKAMsg(1, Flag.SOLICITED, new COPSIntegrity());
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());
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
87 public void testDumpAll() throws Exception {
88 final COPSKAMsg msg = new COPSKAMsg(1, Flag.SOLICITED, new COPSIntegrity());
90 final ByteArrayOutputStream os = new ByteArrayOutputStream();
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);
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]);
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
112 public void testWriteMinimal() throws Exception {
113 final COPSKAMsg msg = new COPSKAMsg(1, Flag.SOLICITED, null);
115 msg.writeData(outSocket);
117 final long start = System.currentTimeMillis();
118 while (server.copsMsgs.size() < 1) {
120 if (System.currentTimeMillis() - start > 2000) break;
123 Assert.assertEquals(1, server.copsMsgs.size());
124 Assert.assertEquals(msg, server.copsMsgs.get(0));
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
133 public void testWriteAll() throws Exception {
134 final COPSKAMsg msg = new COPSKAMsg(1, Flag.SOLICITED, new COPSIntegrity(8, 9, new COPSData("12345")));
136 msg.writeData(outSocket);
138 final long start = System.currentTimeMillis();
139 while (server.copsMsgs.size() < 1) {
141 if (System.currentTimeMillis() - start > 2000) break;
144 Assert.assertEquals(1, server.copsMsgs.size());
145 Assert.assertEquals(msg, server.copsMsgs.get(0));