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 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.
20 public class COPSClientAcceptMsgTest {
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 COPSClientAcceptMsg(0, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE, new COPSKATimer((short)1), null, null);
43 @Test(expected = IllegalArgumentException.class)
44 public void nullFlag() {
45 new COPSClientAcceptMsg(1, null, IPCMMClient.CLIENT_TYPE, new COPSKATimer((short)1), null, null);
48 @Test(expected = IllegalArgumentException.class)
49 public void nullTimer() {
50 new COPSClientAcceptMsg(1, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE, null, null, null);
53 @Test(expected = IllegalArgumentException.class)
54 public void nullHeader() {
55 final COPSHeader hdr = null;
56 new COPSClientAcceptMsg(hdr, new COPSKATimer((short)1), null, null);
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);
66 public void validMinimal() {
67 final COPSClientAcceptMsg msg = new COPSClientAcceptMsg(1, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE,
68 new COPSKATimer((short)1), null, null);
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());
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());
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());
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
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());
101 final ByteArrayOutputStream os = new ByteArrayOutputStream();
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);
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]);
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
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);
127 msg.writeData(outSocket);
129 final long start = System.currentTimeMillis();
130 while (server.copsMsgs.size() < 1) {
132 if (System.currentTimeMillis() - start > 2000) break;
135 Assert.assertEquals(1, server.copsMsgs.size());
136 Assert.assertEquals(msg, server.copsMsgs.get(0));
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
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")));
150 msg.writeData(outSocket);
152 final long start = System.currentTimeMillis();
153 while (server.copsMsgs.size() < 1) {
155 if (System.currentTimeMillis() - start > 2000) break;
158 Assert.assertEquals(1, server.copsMsgs.size());
159 Assert.assertEquals(msg, server.copsMsgs.get(0));