[Simulator] Minor UI changes fixing the IOT-1087.
[platform/upstream/iotivity.git] / cloud / stack / src / test / java / org / iotivity / cloud / base / CoapClientTest.java
1 /*
2  *******************************************************************
3  *
4  * Copyright 2016 Samsung Electronics All Rights Reserved.
5  *
6  *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  *
20  *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21  */
22 package org.iotivity.cloud.base;
23
24 import java.net.InetSocketAddress;
25
26 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
27 import org.iotivity.cloud.base.protocols.coap.enums.CoapMethod;
28 import org.junit.Test;
29
30 import io.netty.channel.ChannelHandlerContext;
31 import io.netty.channel.SimpleChannelInboundHandler;
32
33 public class CoapClientTest {
34
35     private static class CoapHandler
36             extends SimpleChannelInboundHandler<CoapRequest> {
37         @Override
38         protected void channelRead0(ChannelHandlerContext ctx, CoapRequest msg)
39                 throws Exception {
40             // TODO Auto-generated method stub
41         }
42     }
43
44     @Test
45     public void testAddHandler() throws Exception {
46         CoapServer coapServer = new CoapServer();
47         coapServer.startServer(new InetSocketAddress(5683));
48         CoapClient coapClient = new CoapClient();
49         coapClient.startClient(new InetSocketAddress("127.0.0.1", 5683));
50         coapClient.addHandler(new CoapHandler());
51         coapClient.stopClient();
52         coapServer.stopServer();
53     }
54
55     @Test
56     public void testAddHandlerSendRequest() throws Exception {
57         CoapServer coapServer = new CoapServer();
58         coapServer.startServer(new InetSocketAddress(5683));
59
60         CoapClient coapClient = new CoapClient();
61         coapClient.startClient(new InetSocketAddress("127.0.0.1", 5683));
62         coapClient.addHandler(new CoapHandler());
63
64         CoapRequest request = new CoapRequest(CoapMethod.GET);
65         coapClient.sendRequest(request);
66
67         CoapRequest request2 = new CoapRequest(CoapMethod.GET);
68         request2.setToken("1234".getBytes());
69         coapClient.sendRequest(request2);
70
71         CoapRequest request3 = new CoapRequest(CoapMethod.GET);
72         request3.setPayload("sample1".getBytes());
73         coapClient.sendRequest(request3);
74
75         CoapRequest request4 = new CoapRequest(CoapMethod.GET);
76         request4.setToken("5576".getBytes());
77         request4.setPayload("sample2".getBytes());
78         coapClient.sendRequest(request4);
79
80         CoapRequest request5 = new CoapRequest(CoapMethod.GET);
81         request5.setToken("565761".getBytes());
82         coapClient.sendRequest(request5);
83
84         CoapRequest request6 = new CoapRequest(CoapMethod.GET);
85         coapClient.sendRequest(request6);
86
87         CoapRequest request7 = new CoapRequest(CoapMethod.GET);
88         coapClient.sendRequest(request7);
89
90         coapClient.stopClient();
91         coapServer.stopServer();
92     }
93
94     @Test
95     public void testStartStopClient() throws Exception {
96         CoapServer coapServer = new CoapServer();
97         coapServer.startServer(new InetSocketAddress(5683));
98         CoapClient coapClient = new CoapClient();
99         coapClient.startClient(new InetSocketAddress("127.0.0.1", 5683));
100         coapClient.stopClient();
101         coapServer.stopServer();
102     }
103
104     @Test
105     public void testSendRequest() throws Exception {
106         CoapServer coapServer = new CoapServer();
107         coapServer.startServer(new InetSocketAddress(5683));
108         CoapClient coapClient = new CoapClient();
109         coapClient.startClient(new InetSocketAddress("127.0.0.1", 5683));
110         CoapRequest request = new CoapRequest(CoapMethod.GET);
111         coapClient.sendRequest(request);
112         coapClient.stopClient();
113         coapServer.stopServer();
114     }
115 }