1 package org.iotivity.cloud.base;
3 import static org.junit.Assert.assertNotNull;
5 import java.net.InetSocketAddress;
7 import org.iotivity.cloud.base.protocols.coap.CoapResponse;
10 import io.netty.channel.ChannelHandlerContext;
11 import io.netty.channel.SimpleChannelInboundHandler;
13 public class SessionManagerTest {
15 static class CoapClientHandler
16 extends SimpleChannelInboundHandler<CoapResponse> {
18 ChannelHandlerContext connectCtx = null;
21 public void channelActive(ChannelHandlerContext ctx) throws Exception {
26 protected void channelRead0(ChannelHandlerContext arg0,
27 CoapResponse arg1) throws Exception {
28 // TODO Auto-generated method stub
34 public void testAddSession() throws Exception {
35 SessionManager sessionManager = new SessionManager();
36 CoapServer coapServer = new CoapServer();
37 CoapClient coapClient = new CoapClient();
38 CoapClientHandler coapClientHandler = new CoapClientHandler();
39 coapServer.startServer(new InetSocketAddress(5683));
40 coapClient.addHandler(coapClientHandler);
41 coapClient.startClient(new InetSocketAddress("127.0.0.1", 5683));
43 sessionManager.addSession("sampleDid", coapClientHandler.connectCtx);
45 coapClient.stopClient();
46 coapServer.stopServer();
50 public void testRemoveSession() throws Exception {
51 SessionManager sessionManager = new SessionManager();
52 CoapServer coapServer = new CoapServer();
53 CoapClient coapClient = new CoapClient();
54 CoapClientHandler coapClientHandler = new CoapClientHandler();
55 coapServer.startServer(new InetSocketAddress(5683));
56 coapClient.addHandler(coapClientHandler);
57 coapClient.startClient(new InetSocketAddress("127.0.0.1", 5683));
59 sessionManager.addSession("sampleDid", coapClientHandler.connectCtx);
60 sessionManager.removeSession("sampleDid");
62 coapClient.stopClient();
63 coapServer.stopServer();
67 public void testRemoveSessionByChannel() throws Exception {
68 SessionManager sessionManager = new SessionManager();
69 CoapServer coapServer = new CoapServer();
70 CoapClient coapClient = new CoapClient();
71 CoapClientHandler coapClientHandler = new CoapClientHandler();
72 coapServer.startServer(new InetSocketAddress(5683));
73 coapClient.addHandler(coapClientHandler);
74 coapClient.startClient(new InetSocketAddress("127.0.0.1", 5683));
76 sessionManager.addSession("sampleDid", coapClientHandler.connectCtx);
77 sessionManager.removeSessionByChannel(coapClientHandler.connectCtx);
79 coapClient.stopClient();
80 coapServer.stopServer();
84 public void testQuerySession() throws Exception {
85 SessionManager sessionManager = new SessionManager();
86 CoapServer coapServer = new CoapServer();
87 CoapClient coapClient = new CoapClient();
88 CoapClientHandler coapClientHandler = new CoapClientHandler();
89 coapServer.startServer(new InetSocketAddress(5683));
90 coapClient.addHandler(coapClientHandler);
91 coapClient.startClient(new InetSocketAddress("127.0.0.1", 5683));
93 sessionManager.addSession("sampleDid", coapClientHandler.connectCtx);
94 assertNotNull(sessionManager.querySession("sampleDid"));
96 coapClient.stopClient();
97 coapServer.stopServer();