[Simulator] Minor UI changes fixing the IOT-1087.
[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.JSONUtil;
37 import org.iotivity.cloud.rdserver.resources.ResourceDirectoryResource;
38 import org.iotivity.cloud.util.Cbor;
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         return coapHandler.connectCtx;
82     }
83
84     public CoapRequest makePublishPayload() throws Exception {
85
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));
90
91         ArrayList<Object> payload = new ArrayList<Object>();
92
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);
97
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");
105         link.put("rt", rt);
106
107         itf.add("oic.if.baseline");
108         link.put("if", itf);
109
110         mt.add("application/json");
111         link.put("mt", mt);
112
113         link.put("ins", 1);
114
115         publishLinks.add(link);
116
117         payload.add(tags);
118         payload.add(publishLinks);
119
120         Cbor<ArrayList<Object>> cbor = new Cbor<ArrayList<Object>>();
121
122         request.setPayload(cbor.encodingPayloadToCbor(payload));
123
124         return request;
125     }
126
127     @Test
128     public void testHandlePostRequest() throws Exception {
129
130         startServer();
131         ChannelHandlerContext ctx = startClient();
132
133         rdResource.handlePostRequest(ctx, makePublishPayload());
134
135         coapClient.stopClient();
136         coapServer.stopServer();
137
138     }
139
140     @Test
141     public void testHandleGetRequest_notExistVaule() throws Exception {
142
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));
147
148         startServer();
149         ChannelHandlerContext ctx = startClient();
150
151         rdResource.handleGetRequest(ctx, request);
152
153         coapClient.stopClient();
154         coapServer.stopServer();
155     }
156
157     @Test
158     public void testHandleGetRequest_existValue() throws Exception {
159
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));
164
165         startServer();
166         ChannelHandlerContext ctx = startClient();
167
168         rdResource.handlePostRequest(ctx, makePublishPayload());
169
170         rdResource.handleGetRequest(ctx, request);
171
172         coapClient.stopClient();
173         coapServer.stopServer();
174     }
175
176     @Test
177     public void testHandleGetRequestBySt_existValue() throws Exception {
178
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));
183
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);
191
192         startServer();
193         ChannelHandlerContext ctx = startClient();
194
195         rdResource.handlePostRequest(ctx, makePublishPayload());
196
197         rdResource.handleGetRequest(ctx, request);
198
199         coapClient.stopClient();
200         coapServer.stopServer();
201     }
202
203     @Test
204     public void testHandleDeleteRequestByDi_notExistVaule() throws Exception {
205
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));
210
211         startServer();
212         ChannelHandlerContext ctx = startClient();
213
214         rdResource.handlePostRequest(ctx, makePublishPayload());
215
216         rdResource.handleDeleteRequest(ctx, request);
217
218         coapClient.stopClient();
219         coapServer.stopServer();
220     }
221
222     @Test
223     public void testHandleDeleteRequestByDi_existVaule() throws Exception {
224
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));
229
230         startServer();
231         ChannelHandlerContext ctx = startClient();
232
233         rdResource.handleDeleteRequest(ctx, request);
234
235         coapClient.stopClient();
236         coapServer.stopServer();
237     }
238
239     @Test
240     public void testHandleDeleteRequestByIns_notExistVaule() throws Exception {
241
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));
246
247         startServer();
248         ChannelHandlerContext ctx = startClient();
249
250         rdResource.handleDeleteRequest(ctx, request);
251
252         coapClient.stopClient();
253         coapServer.stopServer();
254     }
255
256     @Test
257     public void testHandleDeleteRequestByIns_existVaule() throws Exception {
258
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));
263
264         startServer();
265         ChannelHandlerContext ctx = startClient();
266
267         rdResource.handlePostRequest(ctx, makePublishPayload());
268
269         rdResource.handleDeleteRequest(ctx, request);
270
271         coapClient.stopClient();
272         coapServer.stopServer();
273     }
274 }