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.testrdserver;
24 import java.net.InetSocketAddress;
25 import java.nio.charset.StandardCharsets;
26 import java.util.ArrayList;
27 import java.util.HashMap;
28 import java.util.LinkedHashMap;
29 import org.iotivity.cloud.base.CoapClient;
30 import org.iotivity.cloud.base.CoapServer;
31 import org.iotivity.cloud.base.ResourceManager;
32 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
33 import org.iotivity.cloud.base.protocols.coap.CoapResponse;
34 import org.iotivity.cloud.base.protocols.coap.enums.CoapMethod;
35 import org.iotivity.cloud.rdserver.Constants;
36 import org.iotivity.cloud.rdserver.resources.ResourceDirectoryResource;
37 import org.iotivity.cloud.util.Cbor;
38 import org.iotivity.cloud.util.JSONUtil;
39 import org.junit.Test;
41 import io.netty.channel.ChannelHandlerContext;
42 import io.netty.channel.SimpleChannelInboundHandler;
44 public class RDServerTest {
46 private ResourceDirectoryResource rdResource = new ResourceDirectoryResource();
47 private CoapServer coapServer = null;
48 private CoapClient coapClient = null;
50 static class CoapClientHandler
51 extends SimpleChannelInboundHandler<CoapResponse> {
53 ChannelHandlerContext connectCtx = null;
56 public void channelActive(ChannelHandlerContext ctx) throws Exception {
61 protected void channelRead0(ChannelHandlerContext arg0,
62 CoapResponse arg1) throws Exception {
63 // TODO Auto-generated method stub
68 public void startServer() throws Exception {
69 coapServer = new CoapServer();
70 ResourceManager resourceManager = new ResourceManager();
71 coapServer.addHandler(resourceManager);
72 resourceManager.registerResource(new ResourceDirectoryResource());
73 coapServer.startServer(new InetSocketAddress(5683));
76 public ChannelHandlerContext startClient() throws Exception {
77 coapClient = new CoapClient();
78 CoapClientHandler coapHandler = new CoapClientHandler();
79 coapClient.addHandler(coapHandler);
80 coapClient.startClient(new InetSocketAddress("127.0.0.1", 5683));
81 if (coapHandler.connectCtx == null) {
82 throw new IllegalArgumentException("connectCtx is null");
84 return coapHandler.connectCtx;
87 public CoapRequest makePublishPayload() throws Exception {
89 CoapRequest request = new CoapRequest(CoapMethod.POST);
90 request.setUriPath(Constants.RD_URI);
91 request.setUriQuery("rt=oic.wk.rdpub");
92 request.setToken("1234".getBytes(StandardCharsets.UTF_8));
94 ArrayList<Object> payload = new ArrayList<Object>();
96 HashMap<Object, Object> tags = new HashMap<Object, Object>();
97 tags.put("di", "98f7483c-5a31-4161-ba7e-9c13e0d");
98 tags.put("bm", (int) 1);
99 tags.put("ttl", (int) 86400);
101 ArrayList<LinkedHashMap<Object, Object>> publishLinks = new ArrayList<LinkedHashMap<Object, Object>>();
102 LinkedHashMap<Object, Object> link = new LinkedHashMap<Object, Object>();
103 link.put("href", "/a/light");
104 ArrayList<String> rt = new ArrayList<String>();
105 ArrayList<String> itf = new ArrayList<String>();
106 ArrayList<String> mt = new ArrayList<String>();
107 rt.add("core.light");
110 itf.add("oic.if.baseline");
113 mt.add("application/json");
118 publishLinks.add(link);
121 payload.add(publishLinks);
123 Cbor<ArrayList<Object>> cbor = new Cbor<ArrayList<Object>>();
125 request.setPayload(cbor.encodingPayloadToCbor(payload));
131 public void testHandlePostRequest() throws Exception {
134 ChannelHandlerContext ctx = startClient();
136 rdResource.handlePostRequest(ctx, makePublishPayload());
138 coapClient.stopClient();
139 coapServer.stopServer();
144 public void testHandleGetRequest_notExistVaule() throws Exception {
146 CoapRequest request = new CoapRequest(CoapMethod.GET);
147 request.setUriPath(Constants.RD_URI);
148 request.setUriQuery("rt=core.light");
149 request.setToken("1234".getBytes(StandardCharsets.UTF_8));
152 ChannelHandlerContext ctx = startClient();
154 rdResource.handleGetRequest(ctx, request);
156 coapClient.stopClient();
157 coapServer.stopServer();
161 public void testHandleGetRequest_existValue() throws Exception {
163 CoapRequest request = new CoapRequest(CoapMethod.GET);
164 request.setUriPath(Constants.RD_URI);
165 request.setUriQuery("rt=core.light");
166 request.setToken("1234".getBytes(StandardCharsets.UTF_8));
169 ChannelHandlerContext ctx = startClient();
171 rdResource.handlePostRequest(ctx, makePublishPayload());
173 rdResource.handleGetRequest(ctx, request);
175 coapClient.stopClient();
176 coapServer.stopServer();
180 public void testHandleGetRequestBySt_existValue() throws Exception {
182 CoapRequest request = new CoapRequest(CoapMethod.GET);
183 request.setUriPath(Constants.RD_URI);
184 request.setUriQuery("rt=core.light&st=didList");
185 request.setToken("1234".getBytes(StandardCharsets.UTF_8));
187 HashMap<Object, Object> data = new HashMap<Object, Object>();
188 ArrayList<String> didList = new ArrayList<String>();
189 didList.add("98f7483c-5a31-4161-ba7e-9c13e0d");
190 data.put("devices", didList);
191 String payload = JSONUtil.writeJSON(data);
192 if (payload != null) {
193 request.setPayload(payload.getBytes(StandardCharsets.UTF_8));
196 throw new IllegalArgumentException("payload writeJson error");
200 ChannelHandlerContext ctx = startClient();
202 rdResource.handlePostRequest(ctx, makePublishPayload());
204 rdResource.handleGetRequest(ctx, request);
206 coapClient.stopClient();
207 coapServer.stopServer();
211 public void testHandleDeleteRequestByDi_notExistVaule() throws Exception {
213 CoapRequest request = new CoapRequest(CoapMethod.DELETE);
214 request.setUriPath(Constants.RD_URI);
215 request.setUriQuery("di=98f7483c-5a31-4161-ba7e-9c13e0d");
216 request.setToken("1234".getBytes(StandardCharsets.UTF_8));
219 ChannelHandlerContext ctx = startClient();
221 rdResource.handlePostRequest(ctx, makePublishPayload());
223 rdResource.handleDeleteRequest(ctx, request);
225 coapClient.stopClient();
226 coapServer.stopServer();
230 public void testHandleDeleteRequestByDi_existVaule() throws Exception {
232 CoapRequest request = new CoapRequest(CoapMethod.DELETE);
233 request.setUriPath(Constants.RD_URI);
234 request.setUriQuery("di=98f7483c-5a31-4161-ba7e-9c13e0d");
235 request.setToken("1234".getBytes(StandardCharsets.UTF_8));
238 ChannelHandlerContext ctx = startClient();
240 rdResource.handleDeleteRequest(ctx, request);
242 coapClient.stopClient();
243 coapServer.stopServer();
247 public void testHandleDeleteRequestByIns_notExistVaule() throws Exception {
249 CoapRequest request = new CoapRequest(CoapMethod.DELETE);
250 request.setUriPath(Constants.RD_URI);
251 request.setUriQuery("di=98f7483c-5a31-4161-ba7e-9c13e0d&ins=1");
252 request.setToken("1234".getBytes(StandardCharsets.UTF_8));
255 ChannelHandlerContext ctx = startClient();
257 rdResource.handleDeleteRequest(ctx, request);
259 coapClient.stopClient();
260 coapServer.stopServer();
264 public void testHandleDeleteRequestByIns_existVaule() throws Exception {
266 CoapRequest request = new CoapRequest(CoapMethod.DELETE);
267 request.setUriPath(Constants.RD_URI);
268 request.setUriQuery("di=98f7483c-5a31-4161-ba7e-9c13e0d&ins=1");
269 request.setToken("1234".getBytes(StandardCharsets.UTF_8));
272 ChannelHandlerContext ctx = startClient();
274 rdResource.handlePostRequest(ctx, makePublishPayload());
276 rdResource.handleDeleteRequest(ctx, request);
278 coapClient.stopClient();
279 coapServer.stopServer();