Merge branch '1.1-rel'
[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 import java.nio.charset.StandardCharsets;
26
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;
31
32 public class CoapClientTest {
33     @Test
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();
42     }
43
44     @Test
45     public void testAddHandlerSendRequest() throws Exception {
46         CoapServer coapServer = new CoapServer();
47         coapServer.startServer(new InetSocketAddress(5683));
48
49         CoapClient coapClient = new CoapClient();
50         coapClient.startClient(new InetSocketAddress("127.0.0.1", 5683));
51         coapClient.addHandler(new CoapLogHandler());
52
53         CoapRequest request = new CoapRequest(CoapMethod.GET);
54         coapClient.sendRequest(request);
55
56         CoapRequest request2 = new CoapRequest(CoapMethod.GET);
57         request2.setToken("1234".getBytes(StandardCharsets.UTF_8));
58         coapClient.sendRequest(request2);
59
60         CoapRequest request3 = new CoapRequest(CoapMethod.GET);
61         request3.setPayload("sample1".getBytes(StandardCharsets.UTF_8));
62         coapClient.sendRequest(request3);
63
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);
68
69         CoapRequest request5 = new CoapRequest(CoapMethod.GET);
70         request5.setToken("565761".getBytes(StandardCharsets.UTF_8));
71         coapClient.sendRequest(request5);
72
73         CoapRequest request6 = new CoapRequest(CoapMethod.GET);
74         coapClient.sendRequest(request6);
75
76         CoapRequest request7 = new CoapRequest(CoapMethod.GET);
77         coapClient.sendRequest(request7);
78
79         coapClient.stopClient();
80         coapServer.stopServer();
81     }
82
83     @Test
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();
91     }
92
93     @Test
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();
103     }
104 }