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.JSONUtil;
37 import org.iotivity.cloud.rdserver.resources.ResourceDirectoryResource;
38 import org.iotivity.cloud.util.Cbor;
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 return coapHandler.connectCtx;
84 public CoapRequest makePublishPayload() throws Exception {
86 CoapRequest request = new CoapRequest(CoapMethod.POST);
87 request.setUriPath(Constants.RD_URI);
88 request.setUriQuery("rt=oic.wk.rdPub");
89 request.setToken("1234".getBytes(StandardCharsets.UTF_8));
91 ArrayList<Object> payload = new ArrayList<Object>();
93 HashMap<Object, Object> tags = new HashMap<Object, Object>();
94 tags.put("di", "98f7483c-5a31-4161-ba7e-9c13e0d");
95 tags.put("bm", (int) 1);
96 tags.put("ttl", (int) 86400);
98 ArrayList<LinkedHashMap<Object, Object>> publishLinks = new ArrayList<LinkedHashMap<Object, Object>>();
99 LinkedHashMap<Object, Object> link = new LinkedHashMap<Object, Object>();
100 link.put("href", "/a/light");
101 ArrayList<String> rt = new ArrayList<String>();
102 ArrayList<String> itf = new ArrayList<String>();
103 ArrayList<String> mt = new ArrayList<String>();
104 rt.add("core.light");
107 itf.add("oic.if.baseline");
110 mt.add("application/json");
115 publishLinks.add(link);
118 payload.add(publishLinks);
120 Cbor<ArrayList<Object>> cbor = new Cbor<ArrayList<Object>>();
122 request.setPayload(cbor.encodingPayloadToCbor(payload));
128 public void testHandlePostRequest() throws Exception {
131 ChannelHandlerContext ctx = startClient();
133 rdResource.handlePostRequest(ctx, makePublishPayload());
135 coapClient.stopClient();
136 coapServer.stopServer();
141 public void testHandleGetRequest_notExistVaule() throws Exception {
143 CoapRequest request = new CoapRequest(CoapMethod.GET);
144 request.setUriPath(Constants.RD_URI);
145 request.setUriQuery("rt=core.light");
146 request.setToken("1234".getBytes(StandardCharsets.UTF_8));
149 ChannelHandlerContext ctx = startClient();
151 rdResource.handleGetRequest(ctx, request);
153 coapClient.stopClient();
154 coapServer.stopServer();
158 public void testHandleGetRequest_existValue() throws Exception {
160 CoapRequest request = new CoapRequest(CoapMethod.GET);
161 request.setUriPath(Constants.RD_URI);
162 request.setUriQuery("rt=core.light");
163 request.setToken("1234".getBytes(StandardCharsets.UTF_8));
166 ChannelHandlerContext ctx = startClient();
168 rdResource.handlePostRequest(ctx, makePublishPayload());
170 rdResource.handleGetRequest(ctx, request);
172 coapClient.stopClient();
173 coapServer.stopServer();
177 public void testHandleGetRequestBySt_existValue() throws Exception {
179 CoapRequest request = new CoapRequest(CoapMethod.GET);
180 request.setUriPath(Constants.RD_URI);
181 request.setUriQuery("rt=core.light&st=didList");
182 request.setToken("1234".getBytes(StandardCharsets.UTF_8));
184 HashMap<Object, Object> data = new HashMap<Object, Object>();
185 ArrayList<String> didList = new ArrayList<String>();
186 didList.add("98f7483c-5a31-4161-ba7e-9c13e0d");
187 data.put("devices", didList);
188 JSONUtil util = new JSONUtil();
189 byte[] payload = util.writeJSON(data);
190 request.setPayload(payload);
193 ChannelHandlerContext ctx = startClient();
195 rdResource.handlePostRequest(ctx, makePublishPayload());
197 rdResource.handleGetRequest(ctx, request);
199 coapClient.stopClient();
200 coapServer.stopServer();
204 public void testHandleDeleteRequestByDi_notExistVaule() throws Exception {
206 CoapRequest request = new CoapRequest(CoapMethod.DELETE);
207 request.setUriPath(Constants.RD_URI);
208 request.setUriQuery("di=98f7483c-5a31-4161-ba7e-9c13e0d");
209 request.setToken("1234".getBytes(StandardCharsets.UTF_8));
212 ChannelHandlerContext ctx = startClient();
214 rdResource.handlePostRequest(ctx, makePublishPayload());
216 rdResource.handleDeleteRequest(ctx, request);
218 coapClient.stopClient();
219 coapServer.stopServer();
223 public void testHandleDeleteRequestByDi_existVaule() throws Exception {
225 CoapRequest request = new CoapRequest(CoapMethod.DELETE);
226 request.setUriPath(Constants.RD_URI);
227 request.setUriQuery("di=98f7483c-5a31-4161-ba7e-9c13e0d");
228 request.setToken("1234".getBytes(StandardCharsets.UTF_8));
231 ChannelHandlerContext ctx = startClient();
233 rdResource.handleDeleteRequest(ctx, request);
235 coapClient.stopClient();
236 coapServer.stopServer();
240 public void testHandleDeleteRequestByIns_notExistVaule() throws Exception {
242 CoapRequest request = new CoapRequest(CoapMethod.DELETE);
243 request.setUriPath(Constants.RD_URI);
244 request.setUriQuery("di=98f7483c-5a31-4161-ba7e-9c13e0d&ins=1");
245 request.setToken("1234".getBytes(StandardCharsets.UTF_8));
248 ChannelHandlerContext ctx = startClient();
250 rdResource.handleDeleteRequest(ctx, request);
252 coapClient.stopClient();
253 coapServer.stopServer();
257 public void testHandleDeleteRequestByIns_existVaule() throws Exception {
259 CoapRequest request = new CoapRequest(CoapMethod.DELETE);
260 request.setUriPath(Constants.RD_URI);
261 request.setUriQuery("di=98f7483c-5a31-4161-ba7e-9c13e0d&ins=1");
262 request.setToken("1234".getBytes(StandardCharsets.UTF_8));
265 ChannelHandlerContext ctx = startClient();
267 rdResource.handlePostRequest(ctx, makePublishPayload());
269 rdResource.handleDeleteRequest(ctx, request);
271 coapClient.stopClient();
272 coapServer.stopServer();