Merge remote-tracking branch 'origin/notification-service'
[iotivity.git] / cloud / interface / src / test / java / org / iotivity / cloud / ciserver / resources / proxy / rd / ResourcePresenceTest.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.RequestMethod;
44 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
45 import org.iotivity.cloud.ciserver.Constants;
46 import org.iotivity.cloud.ciserver.DeviceServerSystem;
47 import org.iotivity.cloud.util.Cbor;
48 import org.junit.Before;
49 import org.junit.Test;
50 import org.mockito.InjectMocks;
51 import org.mockito.Mock;
52 import org.mockito.Mockito;
53 import org.mockito.MockitoAnnotations;
54 import org.mockito.invocation.InvocationOnMock;
55 import org.mockito.stubbing.Answer;
56
57 public class ResourcePresenceTest {
58     public static final String DEVICE_PRS_REQ_URI = Constants.DEVICE_PRESENCE_FULL_URI;
59     public static final String DEVICE_LIST_KEY    = "devices";
60     public static final String RES_PRS_URI        = Constants.RESOURCE_PRESENCE_FULL_URI;
61     private String             mDi                = "B371C481-38E6-4D47-8320-7688D8A5B58C";
62     private CoapDevice         mMockDevice        = mock(CoapDevice.class);
63     private IResponse          mRes               = null;
64     private IRequest           mReq               = null;
65     private DeviceServerSystem deviceServerSystem = new DeviceServerSystem();
66     final CountDownLatch       latch              = new CountDownLatch(1);
67     @Mock
68     private IRequestChannel    requestChannel;
69     @InjectMocks
70     private ResourcePresence   adHandler          = new ResourcePresence();
71
72     @Before
73     public void setUp() throws Exception {
74         mRes = null;
75         mReq = null;
76         Mockito.doReturn("mockDeviceId").when(mMockDevice).getDeviceId();
77         MockitoAnnotations.initMocks(this);
78         deviceServerSystem.addResource(adHandler);
79         Mockito.doAnswer(new Answer<Object>() {
80             @Override
81             public CoapRequest answer(InvocationOnMock invocation)
82                     throws Throwable {
83                 Object[] args = invocation.getArguments();
84                 CoapRequest request = (CoapRequest) args[0];
85                 System.out.println(
86                         "\t----------payload : " + request.getPayloadString());
87                 System.out.println(
88                         "\t----------uripath : " + request.getUriPath());
89                 System.out.println(
90                         "\t---------uriquery : " + request.getUriQuery());
91                 mReq = request;
92                 latch.countDown();
93                 return request;
94             }
95         }).when(requestChannel).sendRequest(Mockito.any(IRequest.class),
96                 Mockito.any(CoapDevice.class));
97     }
98
99     @Test
100     public void testResourcePresenceEntireResourcesOnRequestReceived()
101             throws Exception {
102         System.out.println(
103                 "\t--------------OnRequestReceived(RD) Resource Presence (entire resource) Test------------");
104         IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
105                 RES_PRS_URI, null);
106         deviceServerSystem.onRequestReceived(mMockDevice, request);
107         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
108         // assertion: if the request packet from the CI contains the query
109         // which includes the accesstoken and the di
110         assertTrue(latch.await(1L, SECONDS));
111         assertTrue(queryMap.containsKey("mid"));
112         assertEquals(mReq.getUriPath(), Constants.GROUP_FULL_URI + "/null");
113     }
114
115     @Test
116     public void testResourcePresenceSpecificResourceOnRequestReceived()
117             throws Exception {
118         System.out.println(
119                 "\t--------------OnRequestReceived(RD) Resource Presence (specific resources) Test------------");
120         IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
121                 RES_PRS_URI, "di=" + mDi);
122         deviceServerSystem.onRequestReceived(mMockDevice, request);
123         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
124         assertTrue(latch.await(1L, SECONDS));
125         assertTrue(queryMap.containsKey("mid"));
126         assertEquals(mReq.getUriPath(), Constants.GROUP_FULL_URI + "/null");
127     }
128
129     IRequest                               requestEntireDevices = MessageBuilder
130             .createRequest(RequestMethod.GET, RES_PRS_URI, null);
131     @InjectMocks
132     ResourcePresence.AccountReceiveHandler entireDeviceHandler  = adHandler.new AccountReceiveHandler(
133             requestEntireDevices, mMockDevice);
134
135     @Test
136     public void testEntireDeviceonResponseReceived() throws ClientException {
137         System.out.println(
138                 "\t--------------onResponseReceived(RD) Resource Presence (entire deivces) Test------------");
139         IResponse responseFromAccountServer = responseFromAccountServer();
140         entireDeviceHandler.onResponseReceived(responseFromAccountServer);
141         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
142         // assertion : if query has pre-requested multiple di list given from
143         // the AS
144         assertTrue(mReq.getMethod() == RequestMethod.GET);
145         assertTrue(queryMap.get("di").contains("device1"));
146         assertTrue(queryMap.get("di").contains("device2"));
147         assertTrue(queryMap.get("di").contains("device3"));
148     }
149
150     IRequest                               requestSpecificDevice = MessageBuilder
151             .createRequest(RequestMethod.GET, RES_PRS_URI, "di=" + "device1");
152     @InjectMocks
153     ResourcePresence.AccountReceiveHandler specificDeviceHandler = adHandler.new AccountReceiveHandler(
154             requestSpecificDevice, mMockDevice);
155
156     @Test
157     public void testSpecificDeviceonResponseReceived() throws ClientException {
158         System.out.println(
159                 "\t--------------onResponseReceived(RD) Resource Presence (specific deivce) Test------------");
160         IResponse response = responseFromAccountServer();
161         specificDeviceHandler.onResponseReceived(response);
162         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
163         // assertion : if query has pre-requested di
164         assertTrue(queryMap.get("di").contains("device1"));
165         assertTrue(mReq.getMethod() == RequestMethod.GET);
166     }
167
168     private IResponse responseFromAccountServer() {
169         // make response which has "CONTENT" status
170         Cbor<HashMap<String, Object>> cbor = new Cbor<>();
171         HashMap<String, Object> responsePayload = new HashMap<String, Object>();
172         ArrayList<String> deviceList = new ArrayList<String>();
173         // assuming that there are three devices in the response msg from the AS
174         deviceList.add("device1");
175         deviceList.add("device2");
176         deviceList.add("device3");
177         responsePayload.put("dilist", deviceList);
178         responsePayload.put("gid", "g0001");
179         responsePayload.put("gmid", "u0001");
180         ArrayList<String> midList = new ArrayList<String>();
181         midList.add("u0001");
182         responsePayload.put("midlist", midList);
183         IResponse response = MessageBuilder.createResponse(requestEntireDevices,
184                 ResponseStatus.CONTENT, ContentFormat.APPLICATION_CBOR,
185                 cbor.encodingPayloadToCbor(responsePayload));
186         return response;
187     }
188 }