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