Imported Upstream version 1.1.0
[platform/upstream/iotivity.git] / cloud / stack / src / test / java / org / iotivity / cloud / base / SessionManagerTest.java
1 package org.iotivity.cloud.base;
2
3 import static org.junit.Assert.assertNotNull;
4
5 import java.net.InetSocketAddress;
6
7 import org.iotivity.cloud.base.protocols.coap.CoapResponse;
8 import org.junit.Test;
9
10 import io.netty.channel.ChannelHandlerContext;
11 import io.netty.channel.SimpleChannelInboundHandler;
12
13 public class SessionManagerTest {
14
15     static class CoapClientHandler
16             extends SimpleChannelInboundHandler<CoapResponse> {
17
18         ChannelHandlerContext connectCtx = null;
19
20         @Override
21         public void channelActive(ChannelHandlerContext ctx) throws Exception {
22             connectCtx = ctx;
23         }
24
25         @Override
26         protected void channelRead0(ChannelHandlerContext arg0,
27                 CoapResponse arg1) throws Exception {
28             // TODO Auto-generated method stub
29
30         }
31     }
32
33     @Test
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));
42
43         sessionManager.addSession("sampleDid", coapClientHandler.connectCtx);
44
45         coapClient.stopClient();
46         coapServer.stopServer();
47     }
48
49     @Test
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));
58
59         sessionManager.addSession("sampleDid", coapClientHandler.connectCtx);
60         sessionManager.removeSession("sampleDid");
61
62         coapClient.stopClient();
63         coapServer.stopServer();
64     }
65
66     @Test
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));
75
76         sessionManager.addSession("sampleDid", coapClientHandler.connectCtx);
77         sessionManager.removeSessionByChannel(coapClientHandler.connectCtx);
78
79         coapClient.stopClient();
80         coapServer.stopServer();
81     }
82
83     @Test
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));
92
93         sessionManager.addSession("sampleDid", coapClientHandler.connectCtx);
94         assertNotNull(sessionManager.querySession("sampleDid"));
95
96         coapClient.stopClient();
97         coapServer.stopServer();
98     }
99 }