e70303ff50ab02d35decdcd2517ac0d78af5447b
[iotivity.git] / cloud / interface / src / test / java / org / iotivity / cloud / ciserver / resources / proxy / mq / MessageQueueTest.java
1 package org.iotivity.cloud.ciserver.resources.proxy.mq;
2
3 import static java.util.concurrent.TimeUnit.SECONDS;
4 import static org.junit.Assert.assertEquals;
5 import static org.junit.Assert.assertTrue;
6 import static org.mockito.Mockito.mock;
7
8 import java.util.HashMap;
9 import java.util.concurrent.CountDownLatch;
10
11 import org.iotivity.cloud.base.device.CoapDevice;
12 import org.iotivity.cloud.base.device.IRequestChannel;
13 import org.iotivity.cloud.base.protocols.IRequest;
14 import org.iotivity.cloud.base.protocols.IResponse;
15 import org.iotivity.cloud.base.protocols.MessageBuilder;
16 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
17 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
18 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
19 import org.iotivity.cloud.ciserver.Constants;
20 import org.iotivity.cloud.ciserver.DeviceServerSystem;
21 import org.iotivity.cloud.util.Cbor;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.InjectMocks;
25 import org.mockito.Mock;
26 import org.mockito.Mockito;
27 import org.mockito.MockitoAnnotations;
28 import org.mockito.invocation.InvocationOnMock;
29 import org.mockito.stubbing.Answer;
30
31 public class MessageQueueTest {
32     private static final String TEST_MQ_BROKER_URI = Constants.MQ_BROKER_FULL_URI;
33     String                      userId             = "testuser";
34     private CoapDevice          mockDevice         = null;
35     IResponse                   res                = null;
36     IRequest                    req                = null;
37     DeviceServerSystem          deviceServerSystem;
38     final CountDownLatch        latch              = new CountDownLatch(1);
39     @Mock
40     IRequestChannel             requestChannel;
41     @InjectMocks
42     MessageQueue                mqHandler          = new MessageQueue();
43
44     @Before
45     public void setUp() throws Exception {
46         res = null;
47         req = null;
48         MockitoAnnotations.initMocks(this);
49         deviceServerSystem = new DeviceServerSystem();
50         deviceServerSystem.addResource(mqHandler);
51         mockDevice = mock(CoapDevice.class);
52         // callback requestmsg mock from (IRequestChannel) server
53         Mockito.doAnswer(new Answer<Object>() {
54             @Override
55             public CoapRequest answer(InvocationOnMock invocation)
56                     throws Throwable {
57                 Object[] args = invocation.getArguments();
58                 CoapRequest request = (CoapRequest) args[0];
59                 req = request;
60                 latch.countDown();
61                 return null;
62             }
63         }).when(requestChannel).sendRequest(Mockito.any(IRequest.class),
64                 Mockito.any(CoapDevice.class));
65     }
66
67     @Test
68     public void testMessageQueueResourceOnRequestReceived() throws Exception {
69         System.out.println(
70                 "\t--------------OnRequestReceived(MQ) Topic Creation Test------------");
71         IRequest request = null;
72         HashMap<Object, Object> tags = new HashMap<Object, Object>();
73         tags.put("topic", "DeviceServerSystemtestTopic");
74         Cbor<HashMap<Object, Object>> cbor = new Cbor<HashMap<Object, Object>>();
75         request = MessageBuilder.createRequest(RequestMethod.PUT,
76                 TEST_MQ_BROKER_URI, null, ContentFormat.APPLICATION_CBOR,
77                 cbor.encodingPayloadToCbor(tags));
78         deviceServerSystem.onRequestReceived(mockDevice, request);
79         // assertion : request msg to the AS is identical to the request msg
80         // from the client
81         assertTrue(latch.await(1L, SECONDS));
82         assertEquals(request, req);
83     }
84
85 }