fixed CI unit test.
[iotivity.git] / cloud / interface / src / test / java / org / iotivity / cloud / ciserver / resources / DiResourceTest.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;
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.ArrayList;
31 import java.util.HashMap;
32 import java.util.concurrent.CountDownLatch;
33
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.IResponse;
38 import org.iotivity.cloud.base.protocols.MessageBuilder;
39 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
40 import org.iotivity.cloud.base.protocols.coap.CoapResponse;
41 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
42 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
43 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
44 import org.iotivity.cloud.ciserver.Constants;
45 import org.iotivity.cloud.ciserver.DeviceServerSystem;
46 import org.iotivity.cloud.ciserver.DeviceServerSystem.CoapDevicePool;
47 import org.iotivity.cloud.ciserver.resources.DiResource.DefaultResponseHandler;
48 import org.iotivity.cloud.ciserver.resources.DiResource.LinkInterfaceHandler;
49 import org.iotivity.cloud.util.Cbor;
50 import org.junit.Before;
51 import org.junit.Test;
52 import org.mockito.InjectMocks;
53 import org.mockito.Mock;
54 import org.mockito.Mockito;
55 import org.mockito.MockitoAnnotations;
56 import org.mockito.invocation.InvocationOnMock;
57 import org.mockito.stubbing.Answer;
58
59 public class DiResourceTest {
60     private static final String           RELAY_URI              = "/di";
61     private static final String           RESOURCE_URI           = "/a/light/0";
62     private String                        mDiServer              = "resourceServerId";
63     private CoapDevice                    mSourceDevice          = mock(
64             CoapDevice.class);
65     private CoapDevice                    mTargetDevice          = mock(
66             CoapDevice.class);
67     private IResponse                     mRes                   = null;
68     private IRequest                      mReq                   = null;
69     private DeviceServerSystem            mDeviceServerSystem    = new DeviceServerSystem();
70     private final CountDownLatch          mLatch                 = new CountDownLatch(
71             1);
72     private Cbor<HashMap<String, Object>> mCbor                  = new Cbor<>();
73     private IRequestChannel               mTargetChannel         = mock(
74             IRequestChannel.class);
75
76     @Mock
77     CoapDevicePool                        coapDevicePool;
78
79     @Mock
80     IRequestChannel                       requestChannel;
81
82     @InjectMocks
83     DiResource                            diHandler              = new DiResource(
84             coapDevicePool);
85
86     @InjectMocks
87     LinkInterfaceHandler                  linkInterfaceHandler   = diHandler.new LinkInterfaceHandler(
88             "targetDeviceId", mSourceDevice);
89
90     @InjectMocks
91     DefaultResponseHandler                defaultResponseHandler = diHandler.new DefaultResponseHandler(
92             "targetDeviceId", mSourceDevice);
93
94     @Before
95     public void setUp() throws Exception {
96         mRes = null;
97         mReq = null;
98         Mockito.doReturn("sourceDeviceId").when(mSourceDevice).getDeviceId();
99         Mockito.doReturn("targetDeviceId").when(mTargetDevice).getDeviceId();
100         MockitoAnnotations.initMocks(this);
101         mDeviceServerSystem.addResource(diHandler);
102         Mockito.doReturn(mTargetDevice).when(coapDevicePool)
103                 .queryDevice(Mockito.anyString());
104         Mockito.doReturn(mTargetChannel).when(mTargetDevice)
105                 .getRequestChannel();
106
107         // callback mock
108         Mockito.doAnswer(new Answer<Object>() {
109             @Override
110             public CoapResponse answer(InvocationOnMock invocation)
111                     throws Throwable {
112                 Object[] args = invocation.getArguments();
113                 CoapResponse resp = (CoapResponse) args[0];
114                 mRes = resp;
115                 mLatch.countDown();
116                 return null;
117             }
118         }).when(mSourceDevice).sendResponse(Mockito.anyObject());
119         Mockito.doAnswer(new Answer<Object>() {
120             @Override
121             public CoapRequest answer(InvocationOnMock invocation)
122                     throws Throwable {
123                 Object[] args = invocation.getArguments();
124                 CoapRequest request = (CoapRequest) args[0];
125                 System.out.println(
126                         "\t----------payload : " + request.getPayloadString());
127                 System.out.println(
128                         "\t----------uripath : " + request.getUriPath());
129                 System.out.println(
130                         "\t---------uriquery : " + request.getUriQuery());
131                 mReq = request;
132                 mLatch.countDown();
133                 return null;
134             }
135         }).when(mTargetChannel).sendRequest(Mockito.any(IRequest.class),
136                 Mockito.any(CoapDevice.class));
137     }
138
139     @Test
140     public void testOnDefaultRequestReceived() throws InterruptedException {
141         IRequest request = makePutRequest();
142         diHandler.onDefaultRequestReceived(mSourceDevice, request);
143         assertTrue(mReq.getMethod().equals(RequestMethod.PUT));
144         assertTrue(mReq.getUriPath().equals(RESOURCE_URI));
145         assertTrue(mLatch.await(1L, SECONDS));
146     }
147
148     @Test
149     public void testOnLinkInterfaceRequestReceived()
150             throws InterruptedException {
151         IRequest request = makePutLinkInterfaceRequest();
152         diHandler.onLinkInterfaceRequestReceived(mSourceDevice, request);
153         assertTrue(mReq.getMethod().equals(RequestMethod.PUT));
154         assertTrue(mReq.getUriPath().equals(RESOURCE_URI));
155         assertTrue(mLatch.await(1L, SECONDS));
156     }
157
158     @Test
159     public void testOnDefaultResponseHandleronResponseReceived()
160             throws InterruptedException {
161         IResponse response = makeContentResponse();
162         defaultResponseHandler.onResponseReceived(response);
163         assertEquals(mRes, response);
164         assertTrue(mLatch.await(1L, SECONDS));
165     }
166
167     @Test
168     public void testOnLinkInterfaceResponseHandleronResponseReceived()
169             throws InterruptedException {
170         IResponse response = makeContentLinkResponse();
171         linkInterfaceHandler.onResponseReceived(response);
172         assertEquals(mRes, response);
173         assertTrue(mLatch.await(1L, SECONDS));
174     }
175
176     private IRequest makePutRequest() {
177         HashMap<String, Object> payloadData = new HashMap<>();
178         payloadData.put("state", true);
179         payloadData.put("power", 6);
180         IRequest request = MessageBuilder.createRequest(RequestMethod.PUT,
181                 RELAY_URI + "/" + mDiServer + RESOURCE_URI, null,
182                 ContentFormat.APPLICATION_CBOR,
183                 mCbor.encodingPayloadToCbor(payloadData));
184         return request;
185     }
186
187     private IRequest makePutLinkInterfaceRequest() {
188         HashMap<String, Object> payloadData = new HashMap<>();
189         payloadData.put("state", true);
190         payloadData.put("power", 6);
191         IRequest request = MessageBuilder.createRequest(RequestMethod.PUT,
192                 RELAY_URI + "/" + mDiServer + RESOURCE_URI,
193                 Constants.RS_INTERFACE + "=" + Constants.LINK_INTERFACE,
194                 ContentFormat.APPLICATION_CBOR,
195                 mCbor.encodingPayloadToCbor(payloadData));
196         return request;
197     }
198
199     private IResponse makeContentResponse() {
200
201         HashMap<String, Object> payloadData = new HashMap<>();
202         payloadData.put("state", true);
203         payloadData.put("power", 6);
204         IResponse response = MessageBuilder.createResponse(makeGetRequest(),
205                 ResponseStatus.CONTENT, ContentFormat.APPLICATION_CBOR,
206                 mCbor.encodingPayloadToCbor(payloadData));
207         return response;
208     }
209
210     private IResponse makeContentLinkResponse() {
211         HashMap<String, Object> payloadData = new HashMap<>();
212         ArrayList<HashMap<String, Object>> linkPayload = new ArrayList<>();
213
214         payloadData.put("href", "hrefsample1");
215         linkPayload.add(payloadData);
216         linkPayload.add(payloadData);
217         linkPayload.add(payloadData);
218
219         IResponse response = MessageBuilder.createResponse(makeGetRequest(),
220                 ResponseStatus.CONTENT, ContentFormat.APPLICATION_CBOR,
221                 mCbor.encodingPayloadToCbor(linkPayload));
222         return response;
223     }
224
225     private IRequest makeGetRequest() {
226         IRequest request = MessageBuilder.createRequest(RequestMethod.PUT,
227                 RELAY_URI + "/" + mDiServer + RESOURCE_URI, null);
228         return request;
229     }
230
231 }