fixed CI unit test.
[iotivity.git] / cloud / interface / src / test / java / org / iotivity / cloud / ciserver / resources / proxy / rd / DevicePresenceTest.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.rd;
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.List;
33 import java.util.concurrent.CountDownLatch;
34
35 import org.iotivity.cloud.base.device.CoapDevice;
36 import org.iotivity.cloud.base.device.IRequestChannel;
37 import org.iotivity.cloud.base.exception.ClientException;
38 import org.iotivity.cloud.base.protocols.IRequest;
39 import org.iotivity.cloud.base.protocols.IResponse;
40 import org.iotivity.cloud.base.protocols.MessageBuilder;
41 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
42 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
43 import org.iotivity.cloud.base.protocols.enums.Observe;
44 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
45 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
46 import org.iotivity.cloud.ciserver.Constants;
47 import org.iotivity.cloud.ciserver.DeviceServerSystem;
48 import org.iotivity.cloud.util.Cbor;
49 import org.junit.Before;
50 import org.junit.Test;
51 import org.mockito.InjectMocks;
52 import org.mockito.Mock;
53 import org.mockito.Mockito;
54 import org.mockito.MockitoAnnotations;
55 import org.mockito.invocation.InvocationOnMock;
56 import org.mockito.stubbing.Answer;
57
58 public class DevicePresenceTest {
59     public static final String DEVICE_PRS_REQ_URI  = Constants.DEVICE_PRESENCE_FULL_URI;
60     public static final String DEVICE_LIST_KEY     = "devices";
61     public static final String RES_PRS_URI         = Constants.RESOURCE_PRESENCE_FULL_URI;
62     private String             mDi                 = "B371C481-38E6-4D47-8320-7688D8A5B58C";
63     private CoapDevice         mMockDevice         = mock(CoapDevice.class);
64     private IRequest           mReq                = null;
65     private DeviceServerSystem mDeviceServerSystem = new DeviceServerSystem();
66     final CountDownLatch       mLatch              = new CountDownLatch(1);
67     @Mock
68     private IRequestChannel    mRequestChannel;
69     @InjectMocks
70     private DevicePresence     mPrsHandler         = new DevicePresence();
71
72     @Before
73     public void setUp() throws Exception {
74         MockitoAnnotations.initMocks(this);
75         mDeviceServerSystem.addResource(mPrsHandler);
76         Mockito.doReturn("mockDeviceId").when(mMockDevice).getDeviceId();
77         Mockito.doAnswer(new Answer<Object>() {
78             @Override
79             public CoapRequest answer(InvocationOnMock invocation)
80                     throws Throwable {
81                 Object[] args = invocation.getArguments();
82                 CoapRequest request = (CoapRequest) args[0];
83                 System.out.println(
84                         "\t----------payload : " + request.getPayloadString());
85                 System.out.println(
86                         "\t----------uripath : " + request.getUriPath());
87                 System.out.println(
88                         "\t---------uriquery : " + request.getUriQuery());
89                 mReq = request;
90                 mLatch.countDown();
91                 return request;
92             }
93         }).when(mRequestChannel).sendRequest(Mockito.any(IRequest.class),
94                 Mockito.any(CoapDevice.class));
95     }
96
97     // @InjectMocks for testSpecificDeviceonResponseReceived
98     IRequest                             requestSpecificDevice = makePresenceSpecificDevice(
99             "device1");
100     @InjectMocks
101     DevicePresence.AccountReceiveHandler SpecificDeviceHandler = mPrsHandler.new AccountReceiveHandler(
102             requestSpecificDevice, mMockDevice);
103
104     @Test
105     public void testSpecificDeviceonResponseReceived() throws ClientException {
106         System.out.println(
107                 "\t--------------onResponseReceived(RD) Device Presence (specific deivce) Test------------");
108         IResponse response = responseFromAccountServer();
109         SpecificDeviceHandler.onResponseReceived(response);
110         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
111         assertTrue(mReq.getObserve() == Observe.SUBSCRIBE);
112         assertTrue(queryMap.get("di").contains("device1"));
113         assertTrue(mReq.getMethod() == RequestMethod.GET);
114     }
115
116     // @InjectMocks for testEntireDeviceonResponseReceived
117     IRequest                             requestEntireDevices = makePresenceEntireDevice();
118     @InjectMocks
119     DevicePresence.AccountReceiveHandler EntireDeviceHandler  = mPrsHandler.new AccountReceiveHandler(
120             requestEntireDevices, mMockDevice);
121
122     @Test
123     public void testEntireDeviceonResponseReceived() throws ClientException {
124         System.out.println(
125                 "\t--------------onResponseReceived(RD) Device Presence (entire deivces) Test------------");
126         IResponse response = responseFromAccountServer();
127         EntireDeviceHandler.onResponseReceived(response);
128         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
129         assertTrue(mReq.getMethod() == RequestMethod.GET);
130         assertTrue(queryMap.get("di").contains("device1"));
131         assertTrue(queryMap.get("di").contains("device2"));
132         assertTrue(queryMap.get("di").contains("device3"));
133         assertTrue(mReq.getObserve() == Observe.SUBSCRIBE);
134     }
135
136     @Test
137     public void testDevicePresenceObserverSpecificDeviceOnRequestReceived()
138             throws Exception {
139         System.out.println(
140                 "\t--------------OnRequestReceived(RD) Device Presence (observe specific device) Test------------");
141         IRequest request = makePresenceSpecificDevice(mDi);
142         mDeviceServerSystem.onRequestReceived(mMockDevice, request);
143         assertTrue(mLatch.await(1L, SECONDS));
144         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
145         assertTrue(queryMap.containsKey("mid"));
146         assertTrue(mReq.getMethod().equals(RequestMethod.GET));
147         assertEquals(mReq.getUriPath(), Constants.GROUP_FULL_URI + "/null");
148     }
149
150     // @InjectMocks for testPresenceDeregisterSpecificDeviceOnRequestReceived
151     IRequest                             requestDeregisterSpecificDevice = makeDeregisterSpecificDevice(
152             "device1");
153     @InjectMocks
154     DevicePresence.AccountReceiveHandler DeregisterHandler               = mPrsHandler.new AccountReceiveHandler(
155             requestDeregisterSpecificDevice, mMockDevice);
156
157     @Test
158     public void testPresenceDeregisterSpecificDeviceOnRequestReceived()
159             throws Exception {
160         System.out.println(
161                 "\t--------------OnRequestReceived(RD) Device Presence (deregister specific device) Test------------");
162         IResponse response = responseFromAccountServer();
163         DeregisterHandler.onResponseReceived(response);
164         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
165         assertTrue(mReq.getMethod() == RequestMethod.GET);
166         assertTrue(queryMap.get("di").contains("device1"));
167         assertTrue(mReq.getObserve() == Observe.UNSUBSCRIBE);
168     }
169
170     @Test
171     public void testDevicePresenceObserverEntireDevicesOnRequestReceived()
172             throws Exception {
173         System.out.println(
174                 "\t--------------OnRequestReceived(RD) Device Presence (observe entire devices) Test------------");
175         IRequest request = makePresenceEntireDevice();
176         mDeviceServerSystem.onRequestReceived(mMockDevice, request);
177         assertTrue(mLatch.await(1L, SECONDS));
178         assertTrue(mReq.getMethod().equals(RequestMethod.GET));
179         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
180         assertTrue(queryMap.containsKey("mid"));
181         assertEquals(mReq.getUriPath(), Constants.GROUP_FULL_URI + "/null");
182     }
183
184     public IRequest makePresenceEntireDevice() {
185         IRequest request = null;
186         request = MessageBuilder.createRequest(RequestMethod.GET,
187                 DEVICE_PRS_REQ_URI, null);
188         ((CoapRequest) request).setObserve(Observe.SUBSCRIBE);
189         return request;
190     }
191
192     private IRequest makePresenceSpecificDevice(String di) {
193         String query = "di=" + di;
194         IRequest request = null;
195         request = MessageBuilder.createRequest(RequestMethod.GET,
196                 DEVICE_PRS_REQ_URI, query);
197         ((CoapRequest) request).setObserve(Observe.SUBSCRIBE);
198         return request;
199     }
200
201     private IRequest makeDeregisterSpecificDevice(String di) {
202         String query = "di=" + di;
203         IRequest request = null;
204         request = MessageBuilder.createRequest(RequestMethod.GET,
205                 DEVICE_PRS_REQ_URI, query);
206         ((CoapRequest) request).setObserve(Observe.UNSUBSCRIBE);
207         return request;
208     }
209
210     private IResponse responseFromAccountServer() {
211         // make response which has "CONTENT" status
212         Cbor<HashMap<String, Object>> cbor = new Cbor<>();
213         HashMap<String, Object> responsePayload = new HashMap<String, Object>();
214         ArrayList<String> deviceList = new ArrayList<String>();
215         // assuming that there are three devices in the response msg from
216         // the AS
217         deviceList.add("device1");
218         deviceList.add("device2");
219         deviceList.add("device3");
220         responsePayload.put("dilist", deviceList);
221         responsePayload.put("gid", "g0001");
222         responsePayload.put("gmid", "u0001");
223         ArrayList<String> midList = new ArrayList<String>();
224         midList.add("u0001");
225         responsePayload.put("midlist", midList);
226         IRequest requestFromCitoAs = MessageBuilder.createRequest(
227                 RequestMethod.GET, Constants.GROUP_FULL_URI + "/g0001",
228                 "mid=null");
229         IResponse response = MessageBuilder.createResponse(requestFromCitoAs,
230                 ResponseStatus.CONTENT, ContentFormat.APPLICATION_CBOR,
231                 cbor.encodingPayloadToCbor(responsePayload));
232         return response;
233     }
234 }