9ed3e0a649605c9e66fc02a6152f895bbc08228f
[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         MockitoAnnotations.initMocks(this);
77         deviceServerSystem.addResource(adHandler);
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                 System.out.println(
85                         "\t----------payload : " + request.getPayloadString());
86                 System.out.println(
87                         "\t----------uripath : " + request.getUriPath());
88                 System.out.println(
89                         "\t---------uriquery : " + request.getUriQuery());
90                 mReq = request;
91                 latch.countDown();
92                 return request;
93             }
94         }).when(requestChannel).sendRequest(Mockito.any(IRequest.class),
95                 Mockito.any(CoapDevice.class));
96     }
97
98     @Test
99     public void testResourcePresenceEntireResourcesOnRequestReceived()
100             throws Exception {
101         System.out.println(
102                 "\t--------------OnRequestReceived(RD) Resource Presence (entire resource) Test------------");
103         IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
104                 RES_PRS_URI, null);
105         deviceServerSystem.onRequestReceived(mMockDevice, request);
106         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
107         // assertion: if the request packet from the CI contains the query
108         // which includes the accesstoken and the di
109         assertTrue(latch.await(1L, SECONDS));
110         assertTrue(queryMap.containsKey("mid"));
111         assertEquals(mReq.getUriPath(), Constants.GROUP_FULL_URI + "/null");
112     }
113
114     @Test
115     public void testResourcePresenceSpecificResourceOnRequestReceived()
116             throws Exception {
117         System.out.println(
118                 "\t--------------OnRequestReceived(RD) Resource Presence (specific resources) Test------------");
119         IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
120                 RES_PRS_URI, "di=" + mDi);
121         deviceServerSystem.onRequestReceived(mMockDevice, request);
122         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
123         assertTrue(latch.await(1L, SECONDS));
124         assertTrue(queryMap.containsKey("mid"));
125         assertEquals(mReq.getUriPath(), Constants.GROUP_FULL_URI + "/null");
126     }
127
128     IRequest                               requestEntireDevices = MessageBuilder
129             .createRequest(RequestMethod.GET, RES_PRS_URI, null);
130     @InjectMocks
131     ResourcePresence.AccountReceiveHandler entireDeviceHandler  = adHandler.new AccountReceiveHandler(
132             requestEntireDevices, mMockDevice);
133
134     @Test
135     public void testEntireDeviceonResponseReceived() throws ClientException {
136         System.out.println(
137                 "\t--------------onResponseReceived(RD) Resource Presence (entire deivces) Test------------");
138         IResponse responseFromAccountServer = responseFromAccountServer();
139         entireDeviceHandler.onResponseReceived(responseFromAccountServer);
140         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
141         // assertion : if query has pre-requested multiple di list given from
142         // the AS
143         assertTrue(mReq.getMethod() == RequestMethod.GET);
144         assertTrue(queryMap.get("di").contains("device1"));
145         assertTrue(queryMap.get("di").contains("device2"));
146         assertTrue(queryMap.get("di").contains("device3"));
147     }
148
149     IRequest                               requestSpecificDevice = MessageBuilder
150             .createRequest(RequestMethod.GET, RES_PRS_URI, "di=" + "device1");
151     @InjectMocks
152     ResourcePresence.AccountReceiveHandler specificDeviceHandler = adHandler.new AccountReceiveHandler(
153             requestSpecificDevice, mMockDevice);
154
155     @Test
156     public void testSpecificDeviceonResponseReceived() throws ClientException {
157         System.out.println(
158                 "\t--------------onResponseReceived(RD) Resource Presence (specific deivce) Test------------");
159         IResponse response = responseFromAccountServer();
160         specificDeviceHandler.onResponseReceived(response);
161         HashMap<String, List<String>> queryMap = mReq.getUriQueryMap();
162         // assertion : if query has pre-requested di
163         assertTrue(queryMap.get("di").contains("device1"));
164         assertTrue(mReq.getMethod() == RequestMethod.GET);
165     }
166
167     private IResponse responseFromAccountServer() {
168         // make response which has "CONTENT" status
169         Cbor<HashMap<String, Object>> cbor = new Cbor<>();
170         HashMap<String, Object> responsePayload = new HashMap<String, Object>();
171         ArrayList<String> deviceList = new ArrayList<String>();
172         // assuming that there are three devices in the response msg from the AS
173         deviceList.add("device1");
174         deviceList.add("device2");
175         deviceList.add("device3");
176         responsePayload.put("dilist", deviceList);
177         responsePayload.put("gid", "g0001");
178         responsePayload.put("gmid", "u0001");
179         ArrayList<String> midList = new ArrayList<String>();
180         midList.add("u0001");
181         responsePayload.put("midlist", midList);
182         IResponse response = MessageBuilder.createResponse(requestEntireDevices,
183                 ResponseStatus.CONTENT, ContentFormat.APPLICATION_CBOR,
184                 cbor.encodingPayloadToCbor(responsePayload));
185         return response;
186     }
187 }