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