CI unit test included
[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.DeviceServerSystem;
20 import org.iotivity.cloud.util.Cbor;
21 import org.junit.Before;
22 import org.junit.Test;
23 import org.mockito.InjectMocks;
24 import org.mockito.Mock;
25 import org.mockito.Mockito;
26 import org.mockito.MockitoAnnotations;
27 import org.mockito.invocation.InvocationOnMock;
28 import org.mockito.stubbing.Answer;
29
30 public class MessageQueueTest {
31     private static final String TEST_MQ_BROKER_URI = "/.well-known/ocf/ps/";
32     String                      userId             = "testuser";
33     private CoapDevice          mockDevice         = null;
34     IResponse                   res                = null;
35     IRequest                    req                = null;
36     DeviceServerSystem          deviceServerSystem;
37     final CountDownLatch        latch              = new CountDownLatch(1);
38     @Mock
39     IRequestChannel             requestChannel;
40     @InjectMocks
41     MessageQueue                mqHandler          = new MessageQueue();
42
43     @Before
44     public void setUp() throws Exception {
45         res = null;
46         req = null;
47         MockitoAnnotations.initMocks(this);
48         deviceServerSystem = new DeviceServerSystem();
49         deviceServerSystem.addResource(mqHandler);
50         mockDevice = mock(CoapDevice.class);
51         // callback requestmsg mock from (IRequestChannel) server
52         Mockito.doAnswer(new Answer<Object>() {
53             @Override
54             public CoapRequest answer(InvocationOnMock invocation)
55                     throws Throwable {
56                 Object[] args = invocation.getArguments();
57                 CoapRequest request = (CoapRequest) args[0];
58                 req = request;
59                 latch.countDown();
60                 return null;
61             }
62         }).when(requestChannel).sendRequest(Mockito.any(IRequest.class),
63                 Mockito.any(CoapDevice.class));
64     }
65
66     @Test
67     public void testMessageQueueResourceOnRequestReceived() throws Exception {
68         System.out.println(
69                 "\t--------------OnRequestReceived(MQ) Topic Creation Test------------");
70         IRequest request = null;
71         HashMap<Object, Object> tags = new HashMap<Object, Object>();
72         tags.put("topic", "DeviceServerSystemtestTopic");
73         Cbor<HashMap<Object, Object>> cbor = new Cbor<HashMap<Object, Object>>();
74         request = MessageBuilder.createRequest(RequestMethod.PUT,
75                 TEST_MQ_BROKER_URI, null, ContentFormat.APPLICATION_CBOR,
76                 cbor.encodingPayloadToCbor(tags));
77         deviceServerSystem.onRequestReceived(mockDevice, request);
78         // assertion : request msg to the AS is identical to the request msg
79         // from the client
80         assertTrue(latch.await(1L, SECONDS));
81         assertEquals(request, req);
82     }
83
84 }