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;
25 import java.nio.charset.StandardCharsets;
27 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
28 import org.iotivity.cloud.base.protocols.coap.enums.CoapMethod;
29 import org.iotivity.cloud.util.CoapLogHandler;
30 import org.junit.Test;
32 public class CoapClientTest {
34 public void testAddHandler() throws Exception {
35 CoapServer coapServer = new CoapServer();
36 coapServer.startServer(new InetSocketAddress(5683));
37 CoapClient coapClient = new CoapClient();
38 coapClient.startClient(new InetSocketAddress("127.0.0.1", 5683));
39 coapClient.addHandler(new CoapLogHandler());
40 coapClient.stopClient();
41 coapServer.stopServer();
45 public void testAddHandlerSendRequest() throws Exception {
46 CoapServer coapServer = new CoapServer();
47 coapServer.startServer(new InetSocketAddress(5683));
49 CoapClient coapClient = new CoapClient();
50 coapClient.startClient(new InetSocketAddress("127.0.0.1", 5683));
51 coapClient.addHandler(new CoapLogHandler());
53 CoapRequest request = new CoapRequest(CoapMethod.GET);
54 coapClient.sendRequest(request);
56 CoapRequest request2 = new CoapRequest(CoapMethod.GET);
57 request2.setToken("1234".getBytes(StandardCharsets.UTF_8));
58 coapClient.sendRequest(request2);
60 CoapRequest request3 = new CoapRequest(CoapMethod.GET);
61 request3.setPayload("sample1".getBytes(StandardCharsets.UTF_8));
62 coapClient.sendRequest(request3);
64 CoapRequest request4 = new CoapRequest(CoapMethod.GET);
65 request4.setToken("5576".getBytes(StandardCharsets.UTF_8));
66 request4.setPayload("sample2".getBytes(StandardCharsets.UTF_8));
67 coapClient.sendRequest(request4);
69 CoapRequest request5 = new CoapRequest(CoapMethod.GET);
70 request5.setToken("565761".getBytes(StandardCharsets.UTF_8));
71 coapClient.sendRequest(request5);
73 CoapRequest request6 = new CoapRequest(CoapMethod.GET);
74 coapClient.sendRequest(request6);
76 CoapRequest request7 = new CoapRequest(CoapMethod.GET);
77 coapClient.sendRequest(request7);
79 coapClient.stopClient();
80 coapServer.stopServer();
84 public void testStartStopClient() throws Exception {
85 CoapServer coapServer = new CoapServer();
86 coapServer.startServer(new InetSocketAddress(5683));
87 CoapClient coapClient = new CoapClient();
88 coapClient.startClient(new InetSocketAddress("127.0.0.1", 5683));
89 coapClient.stopClient();
90 coapServer.stopServer();
94 public void testSendRequest() throws Exception {
95 CoapServer coapServer = new CoapServer();
96 coapServer.startServer(new InetSocketAddress(5683));
97 CoapClient coapClient = new CoapClient();
98 coapClient.startClient(new InetSocketAddress("127.0.0.1", 5683));
99 CoapRequest request = new CoapRequest(CoapMethod.GET);
100 coapClient.sendRequest(request);
101 coapClient.stopClient();
102 coapServer.stopServer();