Implement reconnection and ping-pong between cloud services. Make
[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.connector.ConnectorPool;
34 import org.iotivity.cloud.base.device.CoapDevice;
35 import org.iotivity.cloud.base.device.IRequestChannel;
36 import org.iotivity.cloud.base.protocols.IRequest;
37 import org.iotivity.cloud.base.protocols.MessageBuilder;
38 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
39 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
40 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
41 import org.iotivity.cloud.ciserver.Constants;
42 import org.iotivity.cloud.ciserver.DeviceServerSystem;
43 import org.iotivity.cloud.util.Cbor;
44 import org.junit.Before;
45 import org.junit.Test;
46 import org.junit.runner.RunWith;
47 import org.mockito.InjectMocks;
48 import org.mockito.Mock;
49 import org.mockito.Mockito;
50 import org.mockito.MockitoAnnotations;
51 import org.mockito.invocation.InvocationOnMock;
52 import org.mockito.stubbing.Answer;
53 import org.powermock.api.mockito.PowerMockito;
54 import org.powermock.core.classloader.annotations.PrepareForTest;
55 import org.powermock.modules.junit4.PowerMockRunner;
56
57 @RunWith(PowerMockRunner.class)
58 @PrepareForTest(ConnectorPool.class)
59 public class MessageQueueTest {
60     private static final String TEST_MQ_BROKER_URI = Constants.MQ_BROKER_FULL_URI;
61     private CoapDevice           mMockDevice        = null;
62     private IRequest             mReq               = null;
63     private DeviceServerSystem   mDeviceServerSystem;
64     private final CountDownLatch mLatch             = new CountDownLatch(1);
65     @Mock
66     private IRequestChannel      mRequestChannel;
67     @InjectMocks
68     private MessageQueue         mMqHandler         = new MessageQueue();
69
70     @Before
71     public void setUp() throws Exception {
72         mReq = null;
73         MockitoAnnotations.initMocks(this);
74         mDeviceServerSystem = new DeviceServerSystem();
75         mDeviceServerSystem.addResource(mMqHandler);
76         mMockDevice = mock(CoapDevice.class);
77         // callback requestmsg mock from (IRequestChannel) server
78         Mockito.doAnswer(new Answer<Object>() {
79             @Override
80             public CoapRequest answer(InvocationOnMock invocation)
81                     throws Throwable {
82                 Object[] args = invocation.getArguments();
83                 CoapRequest request = (CoapRequest) args[0];
84                 mReq = request;
85                 mLatch.countDown();
86                 return null;
87             }
88         }).when(mRequestChannel).sendRequest(Mockito.any(IRequest.class),
89                 Mockito.any(CoapDevice.class));
90
91         PowerMockito.mockStatic(ConnectorPool.class);
92         PowerMockito.when(ConnectorPool.getConnection(Mockito.anyString())).thenReturn(mRequestChannel);
93     }
94
95     @Test
96     public void testMessageQueueResourceOnRequestReceived() throws Exception {
97         System.out.println(
98                 "\t--------------OnRequestReceived(MQ) Topic Creation Test------------");
99         IRequest request = null;
100         HashMap<Object, Object> tags = new HashMap<Object, Object>();
101         tags.put("topic", "DeviceServerSystemtestTopic");
102         Cbor<HashMap<Object, Object>> cbor = new Cbor<HashMap<Object, Object>>();
103         request = MessageBuilder.createRequest(RequestMethod.PUT,
104                 TEST_MQ_BROKER_URI, null, ContentFormat.APPLICATION_CBOR,
105                 cbor.encodingPayloadToCbor(tags));
106         mDeviceServerSystem.onRequestReceived(mMockDevice, request);
107         // assertion : request msg to the AS is identical to the request msg
108         // from the client
109         assertTrue(mLatch.await(1L, SECONDS));
110         assertEquals(request, mReq);
111     }
112
113 }