Initial opendaylight infrastructure commit!!
[controller.git] / third-party / openflowj / src / test / java / org / openflow / io / OFMessageAsyncStreamTest.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
5  * This program and the accompanying materials are made available under the
6  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7  * and is available at http://www.eclipse.org/legal/epl-v10.html
8  */
9
10 package org.openflow.io;
11
12 import org.openflow.protocol.*;
13 import org.openflow.protocol.factory.BasicFactory;
14
15 import java.util.*;
16 import java.nio.channels.*;
17 import java.net.InetSocketAddress;
18
19 import org.junit.Assert;
20
21 import org.junit.Test;
22
23
24 /**
25  * @author Rob Sherwood (rob.sherwood@stanford.edu)
26  *
27  */
28 public class OFMessageAsyncStreamTest {
29     @Test
30     public void testMarshalling() throws Exception {
31         OFMessage h = new OFHello();
32         
33         ServerSocketChannel serverSC = ServerSocketChannel.open();
34         serverSC.socket().bind(new java.net.InetSocketAddress(0));
35         serverSC.configureBlocking(false);
36         
37         SocketChannel client = SocketChannel.open(
38                 new InetSocketAddress("localhost",
39                         serverSC.socket().getLocalPort())
40                 );
41         SocketChannel server = serverSC.accept();
42         OFMessageAsyncStream clientStream = new OFMessageAsyncStream(client, new BasicFactory());
43         OFMessageAsyncStream serverStream = new OFMessageAsyncStream(server, new BasicFactory());
44         
45         clientStream.write(h);
46         while(clientStream.needsFlush()) {
47             clientStream.flush();
48         }
49         List<OFMessage> l = serverStream.read();
50         Assert.assertEquals(l.size(), 1);
51         OFMessage m = l.get(0);
52         Assert.assertEquals(m.getLength(),h.getLength());
53         Assert.assertEquals(m.getVersion(), h.getVersion());
54         Assert.assertEquals(m.getType(), h.getType());
55         Assert.assertEquals(m.getType(), h.getType());
56     }
57 }