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