4f3313d4055f27f183d0ea497459349004fd148e
[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     private final static int testPort = 7777;
23     TestCOPSServer server;
24     Socket outSocket;
25
26     @Before
27     public void setup() throws Exception {
28         server = new TestCOPSServer(testPort);
29         server.start();
30         outSocket = new Socket(InetAddress.getLocalHost(), testPort);
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 COPSSyncStateMsg(0, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE, null, null);
42     }
43
44     @Test(expected = IllegalArgumentException.class)
45     public void nullFlag() {
46         new COPSSyncStateMsg(1, null, IPCMMClient.CLIENT_TYPE, null, null);
47     }
48
49     @Test(expected = IllegalArgumentException.class)
50     public void nullHeader() {
51         final COPSHeader hdr = null;
52         new COPSSyncStateMsg(hdr, null, null);
53     }
54
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 COPSSyncStateMsg(hdr, null, null);
59     }
60
61     @Test
62     public void validMinimal() {
63         final COPSSyncStateMsg msg = new COPSSyncStateMsg(1, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE, null, null);
64
65         Assert.assertEquals(1, msg.getHeader().getPcmmVersion());
66         Assert.assertEquals(Flag.SOLICITED, msg.getHeader().getFlag());
67         Assert.assertEquals(IPCMMClient.CLIENT_TYPE, msg.getHeader().getClientType());
68         Assert.assertNull(msg.getClientHandle());
69         Assert.assertNull(msg.getIntegrity());
70     }
71
72     @Test
73     public void validAll() throws Exception {
74         final COPSSyncStateMsg msg = new COPSSyncStateMsg(1, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE,
75                 new COPSHandle(new COPSData()),
76                 new COPSIntegrity());
77
78         Assert.assertEquals(1, msg.getHeader().getPcmmVersion());
79         Assert.assertEquals(Flag.SOLICITED, msg.getHeader().getFlag());
80         Assert.assertEquals(IPCMMClient.CLIENT_TYPE, msg.getHeader().getClientType());
81         Assert.assertEquals(new COPSHandle(new COPSData()), msg.getClientHandle());
82         Assert.assertEquals(new COPSIntegrity(), msg.getIntegrity());
83     }
84
85     /**
86      * This test is responsible for creating a COPSSyncStateMsg object without any nulls or empty collections
87      * and then is dumped to an OutputStream.
88      * @throws Exception - Test should fail if any exception is thrown
89      */
90     @Test
91     public void testDumpAll() throws Exception {
92         final COPSSyncStateMsg msg = new COPSSyncStateMsg(1, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE,
93                 new COPSHandle(new COPSData()),
94                 new COPSIntegrity());
95
96         final ByteArrayOutputStream os = new ByteArrayOutputStream();
97         msg.dump(os);
98
99         final String out = new String(os.toByteArray());
100         System.out.println(out);
101         final String[] lines = out.split("\n");
102         Assert.assertEquals(15, lines.length);
103
104         // Only checking COPSMsg elements as the COPSObjectMsg elements have already been validated in their own tests
105         Assert.assertEquals("**MSG HEADER**", lines[0]);
106         Assert.assertEquals("Version: 1", lines[1]);
107         Assert.assertEquals("Flags: SOLICITED", lines[2]);
108         Assert.assertEquals("OpCode: SSQ", lines[3]);
109         Assert.assertEquals("Client-type: -32758", lines[4]);
110     }
111
112     /**
113      * This test is responsible for creating a COPSSyncStateMsg object with the minimal necessary attributes to make
114      * it valid. It is then streamed over a socket (unmarshalled) then reassembled (marshalled).
115      * @throws Exception - Test should fail if any exception is thrown
116      */
117     @Test
118     public void testWriteMinimal() throws Exception {
119         final COPSSyncStateMsg msg = new COPSSyncStateMsg(1, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE, null, null);
120
121         msg.writeData(outSocket);
122
123         final long start = System.currentTimeMillis();
124         while (server.copsMsgs.size() < 1) {
125             Thread.sleep(5);
126             if (System.currentTimeMillis() - start > 2000) break;
127         }
128
129         Assert.assertEquals(1, server.copsMsgs.size());
130         Assert.assertEquals(msg, server.copsMsgs.get(0));
131     }
132
133     /**
134      * This test is responsible for creating a COPSSyncStateMsg object without any nulls or empty collections
135      * and then is 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 testWriteAll() throws Exception {
140         final COPSSyncStateMsg msg = new COPSSyncStateMsg(1, Flag.SOLICITED, IPCMMClient.CLIENT_TYPE,
141                 new COPSHandle(new COPSData()),
142                 new COPSIntegrity(8, 9, new COPSData("12345")));
143
144         msg.writeData(outSocket);
145
146         final long start = System.currentTimeMillis();
147         while (server.copsMsgs.size() < 1) {
148             Thread.sleep(5);
149             if (System.currentTimeMillis() - start > 2000) break;
150         }
151
152         Assert.assertEquals(1, server.copsMsgs.size());
153         Assert.assertEquals(msg, server.copsMsgs.get(0));
154     }
155
156 }