[IOT-1556] Changes in cloud according to the stateless in CloudInterface and AccountS...
[platform/upstream/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 RES_PRS_URI        = Constants.RESOURCE_PRESENCE_FULL_URI;
60     private String             mDi                = "B371C481-38E6-4D47-8320-7688D8A5B58C";
61     private CoapDevice         mMockDevice        = mock(CoapDevice.class);
62     private IResponse          mRes               = null;
63     private IRequest           mReq               = null;
64     private DeviceServerSystem deviceServerSystem = new DeviceServerSystem();
65     final CountDownLatch       latch              = new CountDownLatch(1);
66     @Mock
67     private IRequestChannel    requestChannel;
68     @InjectMocks
69     private ResourcePresence   adHandler          = new ResourcePresence();
70
71     @Before
72     public void setUp() throws Exception {
73         mRes = null;
74         mReq = null;
75         Mockito.doReturn("mockDeviceId").when(mMockDevice).getDeviceId();
76         Mockito.doReturn("mockUserId").when(mMockDevice).getUserId();
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("uid"));
112         assertTrue(queryMap.containsKey("members"));
113         assertEquals(mReq.getUriPath(),
114                 Constants.GROUP_FULL_URI + "/mockUserId");
115     }
116
117     @Test
118     public void testResourcePresenceSpecificResourceOnRequestReceived()
119             throws Exception {
120         System.out.println(
121                 "\t--------------OnRequestReceived(RD) Resource Presence (specific resources) Test------------");
122         IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
123                 RES_PRS_URI, "di=" + mDi);
124         deviceServerSystem.onRequestReceived(mMockDevice, request);
125         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
126         assertTrue(latch.await(1L, SECONDS));
127         assertTrue(queryMap.containsKey("uid"));
128         assertTrue(queryMap.containsKey("members"));
129         assertEquals(mReq.getUriPath(),
130                 Constants.GROUP_FULL_URI + "/mockUserId");
131     }
132
133     IRequest                               requestEntireDevices = MessageBuilder
134             .createRequest(RequestMethod.GET, RES_PRS_URI, null);
135     @InjectMocks
136     ResourcePresence.AccountReceiveHandler entireDeviceHandler  = adHandler.new AccountReceiveHandler(
137             requestEntireDevices, mMockDevice);
138
139     @Test
140     public void testEntireDeviceonResponseReceived() throws ClientException {
141         System.out.println(
142                 "\t--------------onResponseReceived(RD) Resource Presence (entire deivces) Test------------");
143         IResponse responseFromAccountServer = responseFromAccountServer();
144         entireDeviceHandler.onResponseReceived(responseFromAccountServer);
145         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
146         // assertion : if query has pre-requested multiple di list given from
147         // the AS
148         assertTrue(mReq.getMethod() == RequestMethod.GET);
149         assertTrue(queryMap.get("di").contains("device1"));
150         assertTrue(queryMap.get("di").contains("device2"));
151         assertTrue(queryMap.get("di").contains("device3"));
152     }
153
154     IRequest                               requestSpecificDevice = MessageBuilder
155             .createRequest(RequestMethod.GET, RES_PRS_URI, "di=" + "device1");
156     @InjectMocks
157     ResourcePresence.AccountReceiveHandler specificDeviceHandler = adHandler.new AccountReceiveHandler(
158             requestSpecificDevice, mMockDevice);
159
160     @Test
161     public void testSpecificDeviceonResponseReceived() throws ClientException {
162         System.out.println(
163                 "\t--------------onResponseReceived(RD) Resource Presence (specific deivce) Test------------");
164         IResponse response = responseFromAccountServer();
165         specificDeviceHandler.onResponseReceived(response);
166         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
167         // assertion : if query has pre-requested di
168         assertTrue(queryMap.get("di").contains("device1"));
169         assertTrue(mReq.getMethod() == RequestMethod.GET);
170     }
171
172     private IResponse responseFromAccountServer() {
173         // make response which has "CONTENT" status
174         Cbor<HashMap<String, Object>> cbor = new Cbor<>();
175         HashMap<String, Object> responsePayload = new HashMap<String, Object>();
176         ArrayList<String> deviceList = new ArrayList<String>();
177         // assuming that there are three devices in the response msg from the AS
178         deviceList.add("device1");
179         deviceList.add("device2");
180         deviceList.add("device3");
181         responsePayload.put("devices", deviceList);
182         responsePayload.put("gid", "g0001");
183         ArrayList<String> midList = new ArrayList<String>();
184         midList.add("u0001");
185         responsePayload.put("members", midList);
186         IResponse response = MessageBuilder.createResponse(requestEntireDevices,
187                 ResponseStatus.CONTENT, ContentFormat.APPLICATION_CBOR,
188                 cbor.encodingPayloadToCbor(responsePayload));
189         return response;
190     }
191 }