Added new constructor. May need to tighten down interfaces in the future.
[packetcable.git] / packetcable-driver / src / test / java / org / umu / cops / stack / COPSClientCloseMsgTest.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.COPSError.ErrorTypes;
9 import org.umu.cops.stack.COPSHeader.Flag;
10 import org.umu.cops.stack.COPSHeader.OPCode;
11
12 import java.io.ByteArrayOutputStream;
13 import java.net.InetAddress;
14 import java.net.Socket;
15
16 /**
17  * Tests for the first constructor of the COPSClientCloseMsg class.
18  * Should any of these tests be inaccurate it is due to the fact that they have been written after COPSClientCloseMsg had been
19  * released and my assumptions may be incorrect.
20  */
21 public class COPSClientCloseMsgTest {
22
23     TestCOPSServer server;
24     Socket outSocket;
25
26     @Before
27     public void setup() throws Exception {
28         server = new TestCOPSServer();
29         server.start();
30         outSocket = new Socket(InetAddress.getLocalHost(), server.getPort());
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 COPSClientCloseMsg(0, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE,
42                 new COPSError(ErrorTypes.AUTH_FAILURE, ErrorTypes.AUTH_REQUIRED), null, null);
43     }
44
45     @Test(expected = IllegalArgumentException.class)
46     public void nullFlag() {
47         new COPSClientCloseMsg(1, null, IPCMMClient.CLIENT_TYPE,
48                 new COPSError(ErrorTypes.AUTH_FAILURE, ErrorTypes.AUTH_REQUIRED), null, null);
49     }
50
51     @Test(expected = IllegalArgumentException.class)
52     public void nullError() {
53         new COPSClientCloseMsg(1, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE, null, null, null);
54     }
55
56     @Test(expected = IllegalArgumentException.class)
57     public void nullHeader() {
58         final COPSHeader hdr = null;
59         new COPSClientCloseMsg(hdr, new COPSError(ErrorTypes.AUTH_FAILURE, ErrorTypes.AUTH_REQUIRED), null, null);
60     }
61
62     @Test(expected = IllegalArgumentException.class)
63     public void invalidHeader() {
64         final COPSHeader hdr = new COPSHeader(1, Flag.UNSOLICITED, OPCode.NA, IPCMMClient.CLIENT_TYPE);
65         new COPSClientCloseMsg(hdr, new COPSError(ErrorTypes.AUTH_FAILURE, ErrorTypes.AUTH_REQUIRED), null, null);
66     }
67
68     @Test
69     public void validMinimal() {
70         final COPSClientCloseMsg msg = new COPSClientCloseMsg(1, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE,
71                 new COPSError(ErrorTypes.AUTH_FAILURE, ErrorTypes.AUTH_REQUIRED), null, null);
72
73         Assert.assertEquals(1, msg.getHeader().getPcmmVersion());
74         Assert.assertEquals(Flag.SOLICITED, msg.getHeader().getFlag());
75         Assert.assertEquals(IPCMMClient.CLIENT_TYPE, msg.getHeader().getClientType());
76         Assert.assertEquals(new COPSError(ErrorTypes.AUTH_FAILURE, ErrorTypes.AUTH_REQUIRED), msg.getError());
77         Assert.assertNull(msg.getRedirAddr());
78         Assert.assertNull(msg.getIntegrity());
79     }
80
81     @Test
82     public void validAll() throws Exception {
83         final COPSClientCloseMsg msg = new COPSClientCloseMsg(1, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE,
84                 new COPSError(ErrorTypes.AUTH_FAILURE, ErrorTypes.AUTH_REQUIRED),
85                 new COPSIpv4PdpRedirectAddress("localhost", 7777, (short)0),
86                 new COPSIntegrity());
87
88         Assert.assertEquals(1, msg.getHeader().getPcmmVersion());
89         Assert.assertEquals(Flag.SOLICITED, msg.getHeader().getFlag());
90         Assert.assertEquals(IPCMMClient.CLIENT_TYPE, msg.getHeader().getClientType());
91         Assert.assertEquals(new COPSError(ErrorTypes.AUTH_FAILURE, ErrorTypes.AUTH_REQUIRED), msg.getError());
92         Assert.assertEquals(new COPSIpv4PdpRedirectAddress("localhost", 7777, (short) 0), msg.getRedirAddr());
93         Assert.assertEquals(new COPSIntegrity(), msg.getIntegrity());
94     }
95
96     /**
97      * This test is responsible for creating a COPSClientCloseMsg object without any nulls or empty collections
98      * and then is dumped to an OutputStream.
99      * @throws Exception - Test should fail if any exception is thrown
100      */
101     @Test
102     public void testDumpAll() throws Exception {
103         final COPSClientCloseMsg msg = new COPSClientCloseMsg(1, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE,
104                 new COPSError(ErrorTypes.AUTH_FAILURE, ErrorTypes.AUTH_REQUIRED),
105                 new COPSIpv4PdpRedirectAddress("localhost", 7777, (short)0),
106                 new COPSIntegrity());
107
108         final ByteArrayOutputStream os = new ByteArrayOutputStream();
109         msg.dump(os);
110
111         final String out = new String(os.toByteArray());
112         System.out.println(out);
113         final String[] lines = out.split("\n");
114         Assert.assertEquals(22, lines.length);
115
116         // Only checking COPSMsg elements as the COPSObjectMsg elements have already been validated in their own tests
117         Assert.assertEquals("**MSG HEADER**", lines[0]);
118         Assert.assertEquals("Version: 1", lines[1]);
119         Assert.assertEquals("Flags: SOLICITED", lines[2]);
120         Assert.assertEquals("OpCode: CC", lines[3]);
121         Assert.assertEquals("Client-type: -32758", lines[4]);
122     }
123
124     /**
125      * This test is responsible for creating a COPSClientCloseMsg object with the minimal necessary attributes to make
126      * it valid. It is then streamed over a socket (unmarshalled) then reassembled (marshalled).
127      * @throws Exception - Test should fail if any exception is thrown
128      */
129     @Test
130     public void testWriteMinimal() throws Exception {
131         final COPSClientCloseMsg msg = new COPSClientCloseMsg(1, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE,
132                 new COPSError(ErrorTypes.AUTH_FAILURE, ErrorTypes.AUTH_REQUIRED), null, null);
133
134         msg.writeData(outSocket);
135
136         final long start = System.currentTimeMillis();
137         while (server.copsMsgs.size() < 1) {
138             Thread.sleep(5);
139             if (System.currentTimeMillis() - start > 2000) break;
140         }
141
142         Assert.assertEquals(1, server.copsMsgs.size());
143         Assert.assertEquals(msg, server.copsMsgs.get(0));
144     }
145
146     /**
147      * This test is responsible for creating a COPSClientCloseMsg object without any nulls or empty collections
148      * and then is streamed over a socket (unmarshalled) then reassembled (marshalled)
149      * @throws Exception - Test should fail if any exception is thrown
150      */
151     @Test
152     public void testWriteAll() throws Exception {
153         final COPSClientCloseMsg msg = new COPSClientCloseMsg(1, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE,
154                 new COPSError(ErrorTypes.BAD_HANDLE_REF, ErrorTypes.MA),
155                 new COPSIpv4PdpRedirectAddress("localhost", 7777, (short)0),
156                 new COPSIntegrity(8, 9, new COPSData("12345")));
157
158         msg.writeData(outSocket);
159
160         final long start = System.currentTimeMillis();
161         while (server.copsMsgs.size() < 1) {
162             Thread.sleep(5);
163             if (System.currentTimeMillis() - start > 2000) break;
164         }
165
166         Assert.assertEquals(1, server.copsMsgs.size());
167         Assert.assertEquals(msg, server.copsMsgs.get(0));
168     }
169
170 }