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