Merge branch '1.1-rel'
[platform/upstream/iotivity.git] / cloud / resourcedirectory / src / test / java / org / iotivity / cloud / testrdserver / RDServerTest.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.testrdserver;
23
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;
40
41 import io.netty.channel.ChannelHandlerContext;
42 import io.netty.channel.SimpleChannelInboundHandler;
43
44 public class RDServerTest {
45
46     private ResourceDirectoryResource rdResource = new ResourceDirectoryResource();
47     private CoapServer                coapServer = null;
48     private CoapClient                coapClient = null;
49
50     static class CoapClientHandler
51             extends SimpleChannelInboundHandler<CoapResponse> {
52
53         ChannelHandlerContext connectCtx = null;
54
55         @Override
56         public void channelActive(ChannelHandlerContext ctx) throws Exception {
57             connectCtx = ctx;
58         }
59
60         @Override
61         protected void channelRead0(ChannelHandlerContext arg0,
62                 CoapResponse arg1) throws Exception {
63             // TODO Auto-generated method stub
64
65         }
66     }
67
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));
74     }
75
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");
83         }
84         return coapHandler.connectCtx;
85     }
86
87     public CoapRequest makePublishPayload() throws Exception {
88
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));
93
94         ArrayList<Object> payload = new ArrayList<Object>();
95
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);
100
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");
108         link.put("rt", rt);
109
110         itf.add("oic.if.baseline");
111         link.put("if", itf);
112
113         mt.add("application/json");
114         link.put("mt", mt);
115
116         link.put("ins", 1);
117
118         publishLinks.add(link);
119
120         payload.add(tags);
121         payload.add(publishLinks);
122
123         Cbor<ArrayList<Object>> cbor = new Cbor<ArrayList<Object>>();
124
125         request.setPayload(cbor.encodingPayloadToCbor(payload));
126
127         return request;
128     }
129
130     @Test
131     public void testHandlePostRequest() throws Exception {
132
133         startServer();
134         ChannelHandlerContext ctx = startClient();
135
136         rdResource.handlePostRequest(ctx, makePublishPayload());
137
138         coapClient.stopClient();
139         coapServer.stopServer();
140
141     }
142
143     @Test
144     public void testHandleGetRequest_notExistVaule() throws Exception {
145
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));
150
151         startServer();
152         ChannelHandlerContext ctx = startClient();
153
154         rdResource.handleGetRequest(ctx, request);
155
156         coapClient.stopClient();
157         coapServer.stopServer();
158     }
159
160     @Test
161     public void testHandleGetRequest_existValue() throws Exception {
162
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));
167
168         startServer();
169         ChannelHandlerContext ctx = startClient();
170
171         rdResource.handlePostRequest(ctx, makePublishPayload());
172
173         rdResource.handleGetRequest(ctx, request);
174
175         coapClient.stopClient();
176         coapServer.stopServer();
177     }
178
179     @Test
180     public void testHandleGetRequestBySt_existValue() throws Exception {
181
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));
186
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));
194         }
195         else {
196                 throw new IllegalArgumentException("payload writeJson error");
197         }
198
199         startServer();
200         ChannelHandlerContext ctx = startClient();
201
202         rdResource.handlePostRequest(ctx, makePublishPayload());
203
204         rdResource.handleGetRequest(ctx, request);
205
206         coapClient.stopClient();
207         coapServer.stopServer();
208     }
209
210     @Test
211     public void testHandleDeleteRequestByDi_notExistVaule() throws Exception {
212
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));
217
218         startServer();
219         ChannelHandlerContext ctx = startClient();
220
221         rdResource.handlePostRequest(ctx, makePublishPayload());
222
223         rdResource.handleDeleteRequest(ctx, request);
224
225         coapClient.stopClient();
226         coapServer.stopServer();
227     }
228
229     @Test
230     public void testHandleDeleteRequestByDi_existVaule() throws Exception {
231
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));
236
237         startServer();
238         ChannelHandlerContext ctx = startClient();
239
240         rdResource.handleDeleteRequest(ctx, request);
241
242         coapClient.stopClient();
243         coapServer.stopServer();
244     }
245
246     @Test
247     public void testHandleDeleteRequestByIns_notExistVaule() throws Exception {
248
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));
253
254         startServer();
255         ChannelHandlerContext ctx = startClient();
256
257         rdResource.handleDeleteRequest(ctx, request);
258
259         coapClient.stopClient();
260         coapServer.stopServer();
261     }
262
263     @Test
264     public void testHandleDeleteRequestByIns_existVaule() throws Exception {
265
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));
270
271         startServer();
272         ChannelHandlerContext ctx = startClient();
273
274         rdResource.handlePostRequest(ctx, makePublishPayload());
275
276         rdResource.handleDeleteRequest(ctx, request);
277
278         coapClient.stopClient();
279         coapServer.stopServer();
280     }
281 }