2 *******************************************************************
4 * Copyright 2016 Samsung Electronics All Rights Reserved.
6 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
12 * http://www.apache.org/licenses/LICENSE-2.0
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.
20 *-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
22 package org.iotivity.cloud.base;
24 import java.net.InetSocketAddress;
26 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
27 import org.iotivity.cloud.base.protocols.coap.enums.CoapMethod;
28 import org.junit.Test;
30 import io.netty.channel.ChannelHandlerContext;
31 import io.netty.channel.SimpleChannelInboundHandler;
33 public class CoapClientTest {
35 private static class CoapHandler
36 extends SimpleChannelInboundHandler<CoapRequest> {
38 protected void channelRead0(ChannelHandlerContext ctx, CoapRequest msg)
40 // TODO Auto-generated method stub
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();
56 public void testAddHandlerSendRequest() throws Exception {
57 CoapServer coapServer = new CoapServer();
58 coapServer.startServer(new InetSocketAddress(5683));
60 CoapClient coapClient = new CoapClient();
61 coapClient.startClient(new InetSocketAddress("127.0.0.1", 5683));
62 coapClient.addHandler(new CoapHandler());
64 CoapRequest request = new CoapRequest(CoapMethod.GET);
65 coapClient.sendRequest(request);
67 CoapRequest request2 = new CoapRequest(CoapMethod.GET);
68 request2.setToken("1234".getBytes());
69 coapClient.sendRequest(request2);
71 CoapRequest request3 = new CoapRequest(CoapMethod.GET);
72 request3.setPayload("sample1".getBytes());
73 coapClient.sendRequest(request3);
75 CoapRequest request4 = new CoapRequest(CoapMethod.GET);
76 request4.setToken("5576".getBytes());
77 request4.setPayload("sample2".getBytes());
78 coapClient.sendRequest(request4);
80 CoapRequest request5 = new CoapRequest(CoapMethod.GET);
81 request5.setToken("565761".getBytes());
82 coapClient.sendRequest(request5);
84 CoapRequest request6 = new CoapRequest(CoapMethod.GET);
85 coapClient.sendRequest(request6);
87 CoapRequest request7 = new CoapRequest(CoapMethod.GET);
88 coapClient.sendRequest(request7);
90 coapClient.stopClient();
91 coapServer.stopServer();
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();
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();