fixed CI unit test.
[iotivity.git] / cloud / interface / src / test / java / org / iotivity / cloud / ciserver / resources / proxy / mq / MessageQueueTest.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.ciserver.resources.proxy.mq;
24
25 import static java.util.concurrent.TimeUnit.SECONDS;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.Mockito.mock;
29
30 import java.util.HashMap;
31 import java.util.concurrent.CountDownLatch;
32
33 import org.iotivity.cloud.base.device.CoapDevice;
34 import org.iotivity.cloud.base.device.IRequestChannel;
35 import org.iotivity.cloud.base.protocols.IRequest;
36 import org.iotivity.cloud.base.protocols.MessageBuilder;
37 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
38 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
39 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
40 import org.iotivity.cloud.ciserver.Constants;
41 import org.iotivity.cloud.ciserver.DeviceServerSystem;
42 import org.iotivity.cloud.util.Cbor;
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.mockito.InjectMocks;
46 import org.mockito.Mock;
47 import org.mockito.Mockito;
48 import org.mockito.MockitoAnnotations;
49 import org.mockito.invocation.InvocationOnMock;
50 import org.mockito.stubbing.Answer;
51
52 public class MessageQueueTest {
53     private static final String TEST_MQ_BROKER_URI = Constants.MQ_BROKER_FULL_URI;
54     private CoapDevice           mMockDevice        = null;
55     private IRequest             mReq               = null;
56     private DeviceServerSystem   mDeviceServerSystem;
57     private final CountDownLatch mLatch             = new CountDownLatch(1);
58     @Mock
59     private IRequestChannel      mRequestChannel;
60     @InjectMocks
61     private MessageQueue         mMqHandler         = new MessageQueue();
62
63     @Before
64     public void setUp() throws Exception {
65         mReq = null;
66         MockitoAnnotations.initMocks(this);
67         mDeviceServerSystem = new DeviceServerSystem();
68         mDeviceServerSystem.addResource(mMqHandler);
69         mMockDevice = mock(CoapDevice.class);
70         // callback requestmsg mock from (IRequestChannel) server
71         Mockito.doAnswer(new Answer<Object>() {
72             @Override
73             public CoapRequest answer(InvocationOnMock invocation)
74                     throws Throwable {
75                 Object[] args = invocation.getArguments();
76                 CoapRequest request = (CoapRequest) args[0];
77                 mReq = request;
78                 mLatch.countDown();
79                 return null;
80             }
81         }).when(mRequestChannel).sendRequest(Mockito.any(IRequest.class),
82                 Mockito.any(CoapDevice.class));
83     }
84
85     @Test
86     public void testMessageQueueResourceOnRequestReceived() throws Exception {
87         System.out.println(
88                 "\t--------------OnRequestReceived(MQ) Topic Creation Test------------");
89         IRequest request = null;
90         HashMap<Object, Object> tags = new HashMap<Object, Object>();
91         tags.put("topic", "DeviceServerSystemtestTopic");
92         Cbor<HashMap<Object, Object>> cbor = new Cbor<HashMap<Object, Object>>();
93         request = MessageBuilder.createRequest(RequestMethod.PUT,
94                 TEST_MQ_BROKER_URI, null, ContentFormat.APPLICATION_CBOR,
95                 cbor.encodingPayloadToCbor(tags));
96         mDeviceServerSystem.onRequestReceived(mMockDevice, request);
97         // assertion : request msg to the AS is identical to the request msg
98         // from the client
99         assertTrue(mLatch.await(1L, SECONDS));
100         assertEquals(request, mReq);
101     }
102
103 }