IOT-3227 update unit test for rd,ci,as services
[iotivity.git] / cloud / messagequeue / src / test / java / org / iotivity / cloud / mqserver / resources / MQBrokerResourceTest.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
23 package org.iotivity.cloud.mqserver.resources;
24
25 import static com.jayway.awaitility.Awaitility.await;
26 import static java.util.concurrent.TimeUnit.SECONDS;
27 import static org.junit.Assert.assertFalse;
28 import static org.junit.Assert.assertTrue;
29 import static org.mockito.Mockito.mock;
30 import static org.mockito.Mockito.timeout;
31 import static org.mockito.Mockito.verify;
32
33 import java.util.ArrayList;
34 import java.util.HashMap;
35 import java.util.concurrent.CountDownLatch;
36 import java.util.concurrent.atomic.AtomicBoolean;
37
38 import org.I0Itec.zkclient.ZkClient;
39 import org.I0Itec.zkclient.ZkConnection;
40 import org.iotivity.cloud.base.device.CoapDevice;
41 import org.iotivity.cloud.base.exception.ServerException.ForbiddenException;
42 import org.iotivity.cloud.base.exception.ServerException.NotFoundException;
43 import org.iotivity.cloud.base.exception.ServerException.PreconditionFailedException;
44 import org.iotivity.cloud.base.protocols.IRequest;
45 import org.iotivity.cloud.base.protocols.IResponse;
46 import org.iotivity.cloud.base.protocols.MessageBuilder;
47 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
48 import org.iotivity.cloud.base.protocols.coap.CoapResponse;
49 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
50 import org.iotivity.cloud.base.protocols.enums.Observe;
51 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
52 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
53 import org.iotivity.cloud.mqserver.Constants;
54 import org.iotivity.cloud.util.Cbor;
55 import org.junit.After;
56 import org.junit.Before;
57 import org.junit.Test;
58 import org.mockito.Mockito;
59 import org.mockito.invocation.InvocationOnMock;
60 import org.mockito.stubbing.Answer;
61
62 import kafka.admin.TopicCommand;
63 import kafka.admin.TopicCommand.TopicCommandOptions;
64 import kafka.utils.ZKStringSerializer$;
65 import kafka.utils.ZkUtils;
66
67 public class MQBrokerResourceTest {
68     private final String     MQ_BROKER_URI     = Constants.MQ_BROKER_FULL_URI;
69
70     private MQBrokerResource mMqBrokerResource = null;
71     private String           mTopicPrefix      = null;
72     private CoapDevice       mMockDevice       = null;
73     IResponse                mResponse         = null;
74     CountDownLatch           mLatch            = null;
75
76     // insert user's zookeper and broker addresses
77     String                   mZookeeper        = "127.0.0.1:2181";
78     String                   mBroker           = "127.0.0.1:9092";
79
80     @Before
81     // setup for each test
82     public void setUp() throws Exception {
83         mMqBrokerResource = new MQBrokerResource();
84
85         mTopicPrefix = "mqtestTopic";
86         mMqBrokerResource.setKafkaInformation(mZookeeper, mBroker);
87         mLatch = new CountDownLatch(1);
88         mResponse = null; // initialize response packet
89         mMockDevice = mock(CoapDevice.class);
90
91         // callback mock
92         Mockito.doAnswer(new Answer<Object>() {
93             @Override
94             public CoapResponse answer(InvocationOnMock invocation)
95                     throws Throwable {
96                 Object[] args = invocation.getArguments();
97                 CoapResponse resp = (CoapResponse) args[0];
98                 mResponse = resp;
99                 mLatch.countDown();
100                 return resp;
101             }
102         }).when(mMockDevice).sendResponse(Mockito.anyObject());
103     }
104
105     @After
106     public void tearDown() throws Exception {
107         // delete topics in Kafka broker
108         ZkClient zkClient = new ZkClient(mZookeeper, 10000, 10000,
109                 ZKStringSerializer$.MODULE$);
110         ZkUtils zkUtils = new ZkUtils(zkClient, new ZkConnection(mZookeeper),
111                 false);
112
113         String topic = MQ_BROKER_URI + "/*";
114         topic = topic.replace('/', '.');
115
116         String[] arr = { "--topic", topic };
117         TopicCommandOptions opts = new TopicCommandOptions(arr);
118         try {
119             TopicCommand.deleteTopic(zkUtils, opts);
120         }catch (final Exception e){
121             e.printStackTrace();
122         }
123
124         zkClient.close();
125         zkUtils.close();
126     }
127
128     @Test
129     // test topic creation
130     public void testTopicCreationOnDefaultRequestReceived() throws Exception {
131         System.out.println("\t--------------Topic Creation Test------------");
132         CreateTopic(mMockDevice, mTopicPrefix);
133         // assertion: if the response status is "CREATED"
134         assertTrue(mLatch.await(1L, SECONDS));
135         assertTrue(methodCheck(mResponse, ResponseStatus.CREATED));
136     }
137
138     @Test
139     // test subtopic create
140     public void testSubTopicCreationOnDefaultRequestReceived()
141             throws Exception {
142         System.out
143                 .println("\t--------------SubTopic Creation Test------------");
144         String mainTopic = mTopicPrefix + "Main";
145         String subTopic = mTopicPrefix + "Sub";
146         // create main topic
147         CreateTopic(mMockDevice, mainTopic);
148         // create sub topic
149         CreateSubTopic(mMockDevice, mainTopic, subTopic);
150         // assertion: if the response status is "CREATED"
151         assertTrue(mLatch.await(1L, SECONDS));
152         assertTrue(methodCheck(mResponse, ResponseStatus.CREATED));
153     }
154
155     @Test
156     // test topic publish
157     public void testTopicPublishOnDefaultRequestReceived() throws Exception {
158         System.out.println("\t--------------Topic Publish Test------------");
159         String topic = mTopicPrefix + "ForPub";
160         // topic creation
161         CreateTopic(mMockDevice, topic);
162         // topic publish
163         PublishTopic(mMockDevice, topic);
164         // assertion: if the response status is "CHANGED"
165         assertTrue(mLatch.await(1L, SECONDS));
166         assertTrue(methodCheck(mResponse, ResponseStatus.CHANGED));
167     }
168
169     @Test
170     // test subscribe request
171     public void testSubscribeOnDefaultRequestReceived() throws Exception {
172         System.out.println("\t--------------Topic Subscribe Test------------");
173         CoapDevice mockSubscriber = mock(CoapDevice.class);
174         String topic = mTopicPrefix + "SubscribeTest";
175         // create topic
176         CreateTopic(mMockDevice, topic);
177         // publish topic
178         PublishTopic(mMockDevice, topic);
179         // callback mock for subscriber
180         Mockito.doAnswer(new Answer<Object>() {
181             @Override
182             public CoapResponse answer(InvocationOnMock invocation)
183                     throws Throwable {
184                 Object[] args = invocation.getArguments();
185                 CoapResponse resp = (CoapResponse) args[0];
186                 // assertion: if the response status is "CONTENT"
187                 // assertion: if the response payload has data
188                 assertTrue(methodCheck(resp, ResponseStatus.CONTENT));
189                 assertTrue(payloadCheck(resp));
190                 return resp;
191             }
192         }).when(mockSubscriber).sendResponse(Mockito.anyObject());
193         // subscribe topic
194         SubscribeTopic(mockSubscriber, topic, Observe.SUBSCRIBE);
195     }
196
197     @Test
198     // test unsubscribe request
199     public void testUnSubscribeOnDefaultRequestReceived() throws Exception {
200         System.out
201                 .println("\t--------------Topic Unsubscribe Test------------");
202         CountDownLatch latchSubscriber = new CountDownLatch(2);
203         CoapDevice mockSubscriber = mock(CoapDevice.class);
204         String topic = mTopicPrefix + "UnSubscribeTest";
205         // create topic
206         CreateTopic(mMockDevice, topic);
207         // publish topic
208         PublishTopic(mMockDevice, topic);
209         // callback mock for subscriber
210         Mockito.doAnswer(new Answer<Object>() {
211             @Override
212             public CoapResponse answer(InvocationOnMock invocation)
213                     throws Throwable {
214                 Object[] args = invocation.getArguments();
215                 CoapResponse resp = (CoapResponse) args[0];
216                 latchSubscriber.countDown();
217                 if (latchSubscriber.getCount() == 0) {
218                     // assertion: if the response payload has data
219                     assertTrue(methodCheck(resp, ResponseStatus.CONTENT));
220                     assertTrue(payloadCheck(resp));
221                 }
222                 return resp;
223             }
224         }).when(mockSubscriber).sendResponse(Mockito.anyObject());
225         // subscribe topic
226         SubscribeTopic(mockSubscriber, topic, Observe.SUBSCRIBE);
227         // unsubscribe topic
228         SubscribeTopic(mockSubscriber, topic, Observe.UNSUBSCRIBE);
229     }
230
231     @Test
232     // test delete request
233     public void testDeleteTopicOnDefaultRequestReceived() throws Exception {
234         System.out.println("\t--------------Topic Delete Test------------");
235         String topic = mTopicPrefix + "DeleteTest";
236         // create topic
237         CreateTopic(mMockDevice, topic);
238         // delete topic
239         DeleteTopic(mMockDevice, topic);
240         // assertion: if the response status is "DELETED"
241         assertTrue(methodCheck(mResponse, ResponseStatus.DELETED));
242         assertTrue(mLatch.await(1L, SECONDS));
243     }
244
245     @Test
246     // test delete subtopic request
247     public void testDeleteSubTopicOnDefaultRequestReceived() throws Exception {
248         System.out.println("\t--------------Subtopic Delete Test------------");
249         String topic = mTopicPrefix + "DeleteTest";
250         String subTopic = mTopicPrefix + "DeleteTestSub";
251         // create topic
252         CreateTopic(mMockDevice, topic);
253         // create subtopic
254         CreateSubTopic(mMockDevice, topic, subTopic);
255         // delete subtopic
256         DeleteSubTopic(mMockDevice, topic, subTopic);
257         // assertion: if the response status is "DELETED"
258         assertTrue(methodCheck(mResponse, ResponseStatus.DELETED));
259     }
260
261     @Test
262     // test notify
263     public void testTopicSubscribeNofityOnDefaultRequestReceived()
264             throws Exception {
265         System.out.println(
266                 "\t--------------Topic Publish Notify Test------------");
267         CoapDevice mockSubscriber = mock(CoapDevice.class);
268         CountDownLatch latchSubscriber = new CountDownLatch(2);
269         AtomicBoolean countTrue = new AtomicBoolean();
270         countTrue.set(false);
271         String topic = mTopicPrefix + "NotifyTest";
272         // callback mock for subscriber
273         Mockito.doAnswer(new Answer<Object>() {
274             @Override
275             public CoapResponse answer(InvocationOnMock invocation)
276                     throws Throwable {
277                 Object[] args = invocation.getArguments();
278                 CoapResponse resp = (CoapResponse) args[0];
279                 latchSubscriber.countDown();
280                 // assertion for subscriber
281                 if (latchSubscriber.getCount() == 0) {
282                     assertTrue(methodCheck(resp, ResponseStatus.CONTENT));
283                     assertTrue(payloadCheck(resp));
284
285                     DeleteTopic(mMockDevice, topic);
286                 }
287                 return resp;
288             }
289         }).when(mockSubscriber).sendResponse(Mockito.anyObject());
290         // create topic
291         CreateTopic(mMockDevice, topic);
292         // publish topic (publisher)
293         PublishTopic(mMockDevice, topic);
294         // subscribe topic (subscriber)
295         SubscribeTopic(mockSubscriber, topic, Observe.SUBSCRIBE);
296         await().atMost(2, SECONDS).untilFalse(countTrue);
297         PublishTopic(mMockDevice, topic);
298         // verity if subscriber receives two responses
299         assertTrue(latchSubscriber.await(2L, SECONDS));
300         verify(mockSubscriber, timeout(5000).times(2))
301                 .sendResponse(Mockito.anyObject());
302     }
303
304     @Test
305     // test discover request
306     public void testTopicDiscoverOnDefaultRequestReceived() throws Exception {
307         System.out.println("\t--------------Topic Discover Test------------");
308         String topic = mTopicPrefix + "DiscoverTest";
309         String subTopic = topic + "sub";
310         // create topic
311         CreateTopic(mMockDevice, topic);
312         // create sub topic
313         CreateSubTopic(mMockDevice, topic, subTopic);
314         // discover topic
315         DiscoverTopic();
316         // assertion 1: if the response status is "CONTENT"
317         // assertion 2: if the response payload has "topiclist" property
318         // and there is the topic created in this unit test in the array
319         boolean methodCheck = methodCheck(mResponse, ResponseStatus.CONTENT);
320         Cbor<HashMap<String, ArrayList<String>>> mArrayCbor = new Cbor<>();
321         HashMap<String, ArrayList<String>> payloadData = mArrayCbor
322                 .parsePayloadFromCbor(mResponse.getPayload(), HashMap.class);
323         ArrayList<String> topicList = payloadData.get("topiclist");
324         System.out.println("\ttopicList : " + topicList);
325         assertTrue(methodCheck);
326         assertTrue(topicList.contains(MQ_BROKER_URI + "/" + topic));
327         assertTrue(topicList
328                 .contains(MQ_BROKER_URI + "/" + topic + "/" + subTopic));
329     }
330
331     @Test
332     // topic read request
333     public void testTopicReadOnDefaultRequestReceived() throws Exception {
334         System.out.println("\t--------------Topic Read Test------------");
335         String topic = mTopicPrefix + "ReadTest";
336         // create topic
337         CreateTopic(mMockDevice, topic);
338         // publish topic
339         PublishTopic(mMockDevice, topic);
340         // read topic
341         ReadTopic(topic);
342         // assertion1 : if the response status is "CONTENT"
343         // assertion2 : if the response payload has data
344         assertTrue(methodCheck(mResponse, ResponseStatus.CONTENT));
345         assertTrue(payloadCheck(mResponse));
346     }
347
348     @Test(expected = NotFoundException.class)
349     public void testNotCreatedTopicDeleteOnDefaultRequestReceived()
350             throws Exception {
351         System.out.println(
352                 "\t--------------Not Created Topic Delete Test------------");
353         String topic = mTopicPrefix + "NotCreatedTopicDeleteTest";
354
355         DeleteTopic(mMockDevice, topic);
356     }
357
358     @Test(expected = NotFoundException.class)
359     public void testNotCreatedSubtopicDeleteOnDefaultRequestReceived()
360             throws Exception {
361         System.out.println(
362                 "\t--------------Not Created Subtopic Delete Test------------");
363         String topic = mTopicPrefix + "Maintopic";
364
365         CreateTopic(mMockDevice, topic);
366
367         topic += "/" + "NotCreatedSubtopicTest";
368
369         DeleteTopic(mMockDevice, topic);
370     }
371
372     @Test(expected = ForbiddenException.class)
373     // duplicate topic creation
374     public void testDuplicatedTopicCreateOnDefaultRequestReceived()
375             throws Exception {
376         System.out.println(
377                 "\t--------------Duplicated Topic Creation Test------------");
378         String topic = mTopicPrefix + "DuplicateTest";
379         // create topic
380         CreateTopic(mMockDevice, topic);
381         // create topic again
382         CreateTopic(mMockDevice, topic);
383     }
384
385     @Test(expected = ForbiddenException.class)
386     // duplicate subtopic creation
387     public void testDuplicatedSubtopicCreateOnDefaultRequestReceived()
388             throws Exception {
389         System.out.println(
390                 "\t--------------Duplicated Subtopic Creation Test------------");
391
392         String topic = mTopicPrefix + "DuplicateTest2";
393
394         // create topic
395         CreateTopic(mMockDevice, topic);
396
397         // create subtopic
398         topic += "/subtopic";
399         CreateTopic(mMockDevice, topic);
400
401         // create subtopic again
402         CreateTopic(mMockDevice, topic);
403     }
404
405     @Test(expected = NotFoundException.class)
406     // publish not created topic
407     public void testNotCreatedTopicPublishOnDefaultRequestReceived()
408             throws Exception {
409         System.out.println(
410                 "\t--------------Not Created Topic Publish Test------------");
411         String topic = mTopicPrefix + "NotCreatedTopicTest";
412         // publish not created topic
413         PublishTopic(mMockDevice, topic);
414     }
415
416     @Test(expected = NotFoundException.class)
417     // subscribe not created topic
418     public void testNotCreatedTopicSubscribeOnDefaultRequestReceived()
419             throws Exception {
420         System.out.println(
421                 "\t--------------Not Created Topic Subscribe Test------------");
422         String topic = mTopicPrefix + "NotCreatedTopicSubscribeTest";
423         SubscribeTopic(mMockDevice, topic, Observe.SUBSCRIBE);
424     }
425
426     @Test(expected = NotFoundException.class)
427     // unsubscribe not created topic
428     public void testNotCreatedTopicUnSubscribeOnDefaultRequestReceived()
429             throws Exception {
430         System.out.println(
431                 "\t--------------Not Created Topic Unsubscribe Test------------");
432         String topic = mTopicPrefix + "NotCreatedTopicUnSubscribeTest";
433         SubscribeTopic(mMockDevice, topic, Observe.UNSUBSCRIBE);
434     }
435
436     @Test(expected = PreconditionFailedException.class)
437     public void testTopicPublishWithoutMessage() throws Exception {
438         System.out.println(
439                 "\t--------------Topic Publish Without Message Test------------");
440         String topic = mTopicPrefix + "ForPubWithoutMessage";
441
442         // topic creation
443         CreateTopic(mMockDevice, topic);
444
445         // topic publish without message
446         String requestUri = MQ_BROKER_URI + "/" + topic;
447         IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
448                 requestUri, null);
449
450         mMqBrokerResource.onDefaultRequestReceived(mMockDevice, request);
451     }
452
453     @Test(expected = NotFoundException.class)
454     // create subtopic under not created maintopic
455     public void testSubTopicCreateUnderNotCreatedTopicOnDefaultRequestReceived()
456             throws Exception {
457         System.out.println(
458                 "\t--------------Create Subtopic under Not Created Maintopic  ------------");
459         String mainTopic = mTopicPrefix + "NotCreatedMain";
460         String subTopic = mTopicPrefix + "NotCreatedSub";
461         // create sub topic
462         CreateSubTopic(mMockDevice, mainTopic, subTopic);
463     }
464
465     @Test
466     // create topic which has 'core.light' rt
467     public void testTopicCreationWithRtOnDefaultRequestReceived()
468             throws Exception {
469         System.out.println(
470                 "\t--------------Topic Creation with RT Test------------");
471         String topicName = mTopicPrefix + "RtTest";
472         String rt = "rt=core.light";
473         CreateTopicWithRt(mMockDevice, topicName, rt);
474         // assertion: if the response status is "CREATED"
475         assertTrue(methodCheck(mResponse, ResponseStatus.CREATED));
476     }
477
478     @Test
479     // create topic which has 'core.light' rt
480     public void testSubtopicCreationWithRtOnDefaultRequestReceived()
481             throws Exception {
482         System.out.println(
483                 "\t--------------Subtopic Creation with RT Test------------");
484         String topicName = mTopicPrefix + "RtTest2";
485         String rt = "rt=core.light";
486
487         // create main topic
488         CreateTopicWithRt(mMockDevice, topicName, rt);
489
490         // create sub topic
491         topicName += "/subtopic";
492         CreateTopicWithRt(mMockDevice, topicName, rt);
493
494         assertTrue(methodCheck(mResponse, ResponseStatus.CREATED));
495     }
496
497     @Test
498     // test discover request with rt
499     public void testDiscoverTopicWithRtOnDefaultRequestReceived()
500             throws Exception {
501         System.out.println(
502                 "\t--------------Topic Discover with Rt Test------------");
503         String topicName = mTopicPrefix + "DiscoverRtTest";
504         String topicNameWithoutRt = mTopicPrefix + "DiscoverRtTestWithoutRt";
505         String rt = "rt=core.light";
506         // create topic with rt
507         CreateTopicWithRt(mMockDevice, topicName, rt);
508         // create topic
509         CreateTopic(mMockDevice, topicNameWithoutRt);
510         // discover topic
511         DiscoverTopicWithRt(rt);
512         // assertion 1: if the response status is "CONTENT"
513         // assertion 2: if topic list contains the topic with rt
514         // assertion 3: if topic list contains no topics which does not have the
515         // rt
516         Cbor<HashMap<String, ArrayList<String>>> mArrayCbor = new Cbor<>();
517         HashMap<String, ArrayList<String>> payloadData = mArrayCbor
518                 .parsePayloadFromCbor(mResponse.getPayload(), HashMap.class);
519         ArrayList<String> topicList = payloadData.get("topiclist");
520         System.out.println("\ttopicList : " + topicList);
521         assertTrue(methodCheck(mResponse, ResponseStatus.CONTENT));
522         assertTrue(topicList.contains(MQ_BROKER_URI + "/" + topicName));
523         assertFalse(
524                 topicList.contains(MQ_BROKER_URI + "/" + topicNameWithoutRt));
525     }
526
527     private IRequest PublishTopicRequest(String topicName) {
528         IRequest request = null;
529         HashMap<String, Object> message = new HashMap<String, Object>();
530         message.put("status", "on");
531         message.put("brightness", 20);
532         Cbor<HashMap<String, Object>> cbor = new Cbor<HashMap<String, Object>>();
533         String uri = MQ_BROKER_URI + "/" + topicName;
534         request = MessageBuilder.createRequest(RequestMethod.POST, uri, null,
535                 ContentFormat.APPLICATION_CBOR,
536                 cbor.encodingPayloadToCbor(message));
537         return request;
538     }
539
540     private IRequest CreateTopicRequest(String topicName) {
541         IRequest request = null;
542         request = MessageBuilder.createRequest(RequestMethod.PUT,
543                 MQ_BROKER_URI + "/" + topicName, null);
544         return request;
545     }
546
547     private IRequest CreateSubTopicRequest(String topicName,
548             String subTopicName) {
549         IRequest request = null;
550         String uri = MQ_BROKER_URI + "/" + topicName + "/" + subTopicName;
551         request = MessageBuilder.createRequest(RequestMethod.PUT, uri, null);
552         return request;
553     }
554
555     private IRequest CreateTopicWithRtRequest(String topicName, String type) {
556         IRequest request = null;
557         request = MessageBuilder.createRequest(RequestMethod.PUT,
558                 MQ_BROKER_URI + "/" + topicName, type);
559         return request;
560     }
561
562     private IRequest DeleteTopicRequest(String topicName) {
563         IRequest request = null;
564         String uri = MQ_BROKER_URI + "/" + topicName;
565         request = MessageBuilder.createRequest(RequestMethod.DELETE, uri, null);
566         return request;
567     }
568
569     private IRequest SubscribeTopicRequest(String topicName) {
570         IRequest request = null;
571         String uri = MQ_BROKER_URI + "/" + topicName;
572         request = MessageBuilder.createRequest(RequestMethod.GET, uri, null);
573         return request;
574     }
575
576     private IRequest DiscoverTopicRequest() {
577         IRequest request = null;
578         request = MessageBuilder.createRequest(RequestMethod.GET, MQ_BROKER_URI,
579                 null);
580         return request;
581     }
582
583     private IRequest DiscoverTopicWithRtRequest(String rt) {
584         IRequest request = null;
585         request = MessageBuilder.createRequest(RequestMethod.GET, MQ_BROKER_URI,
586                 rt);
587         return request;
588     }
589
590     private CoapRequest ReadTopicRequest(String topicName) {
591         IRequest request = null;
592         String uri = MQ_BROKER_URI + "/" + topicName;
593         request = MessageBuilder.createRequest(RequestMethod.GET, uri, null);
594         CoapRequest mqRequest = (CoapRequest) request;
595         return mqRequest;
596     }
597
598     private void CreateTopicWithRt(CoapDevice mockDevice, String topicName,
599             String type) throws Exception {
600         System.out.println("-----CreateTopic || topic : " + topicName);
601         IRequest request = null;
602         request = CreateTopicWithRtRequest(topicName, type);
603         mMqBrokerResource.onDefaultRequestReceived(mockDevice, request);
604     }
605
606     private void CreateTopic(CoapDevice mockDevice, String topicName)
607             throws Exception {
608         System.out.println("-----CreateTopic || topic : " + topicName);
609         IRequest request = null;
610         request = CreateTopicRequest(topicName);
611         mMqBrokerResource.onDefaultRequestReceived(mockDevice, request);
612     }
613
614     private void CreateSubTopic(CoapDevice mockDevice, String mainTopicName,
615             String subTopicName) throws Exception {
616         System.out.println("-----CreateSubTopic || topic : " + mainTopicName
617                 + " || subtopic : " + subTopicName);
618         IRequest subTopicRequest = null;
619         subTopicRequest = CreateSubTopicRequest(mainTopicName, subTopicName);
620         mMqBrokerResource.onDefaultRequestReceived(mockDevice, subTopicRequest);
621     }
622
623     private void PublishTopic(CoapDevice mockDevice, String topicName)
624             throws Exception {
625         System.out.println("-----PublishTopic : " + topicName);
626         IRequest request = null;
627         request = PublishTopicRequest(topicName);
628         mMqBrokerResource.onDefaultRequestReceived(mockDevice, request);
629     }
630
631     private void SubscribeTopic(CoapDevice mockSubscriber, String topicName,
632             Observe observe) throws Exception {
633         System.out.println("-----SubscribeTopic : " + topicName);
634         IRequest requestSubscribe = null;
635         requestSubscribe = SubscribeTopicRequest(topicName);
636         CoapRequest mqRequest = (CoapRequest) requestSubscribe;
637         mqRequest.setObserve(observe);
638         mMqBrokerResource.onDefaultRequestReceived(mockSubscriber, mqRequest);
639     }
640
641     private void DeleteTopic(CoapDevice mockDevice, String topicName)
642             throws Exception {
643         System.out.println("-----DeleteTopic : " + topicName);
644         IRequest requestToDelete = null;
645         requestToDelete = DeleteTopicRequest(topicName);
646         mMqBrokerResource.onDefaultRequestReceived(mockDevice, requestToDelete);
647     }
648
649     private void DeleteSubTopic(CoapDevice mockDevice, String topicName,
650             String subTopicName) throws Exception {
651         System.out.println("-----DeleteTopic : " + topicName);
652         String deleteUri = topicName + "/" + subTopicName;
653         IRequest requestToDelete = null;
654         requestToDelete = DeleteTopicRequest(deleteUri);
655         mMqBrokerResource.onDefaultRequestReceived(mockDevice, requestToDelete);
656     }
657
658     private void DiscoverTopic() throws Exception {
659         System.out.println("-----DiscoverTopic : ");
660         IRequest requestToDiscover = null;
661         requestToDiscover = DiscoverTopicRequest();
662         mMqBrokerResource.onDefaultRequestReceived(mMockDevice,
663                 requestToDiscover);
664     }
665
666     private void DiscoverTopicWithRt(String rt) throws Exception {
667         System.out.println("-----DiscoverTopicWithRt : ");
668         IRequest requestToDiscover = null;
669         requestToDiscover = DiscoverTopicWithRtRequest(rt);
670         mMqBrokerResource.onDefaultRequestReceived(mMockDevice,
671                 requestToDiscover);
672     }
673
674     private void ReadTopic(String topicName) throws Exception {
675         System.out.println("-----ReadTopic : " + topicName);
676         CoapRequest readRequest = null;
677         readRequest = ReadTopicRequest(topicName);
678         mMqBrokerResource.onDefaultRequestReceived(mMockDevice, readRequest);
679     }
680
681     private boolean payloadCheck(IResponse response) {
682         Cbor<HashMap<String, Object>> mCbor = new Cbor<>();
683         HashMap<String, Object> payloadData = mCbor
684                 .parsePayloadFromCbor(response.getPayload(), HashMap.class);
685         if (payloadData != null && payloadData.containsKey("status")
686                 && payloadData.containsKey("brightness")) {
687             return true;
688         } else
689             return false;
690     }
691
692     private boolean methodCheck(IResponse response,
693             ResponseStatus responseStatus) {
694         if (responseStatus == response.getStatus())
695             return true;
696         else
697             return false;
698     }
699 }