acdf9a44b994950d2bd814186f06378e7d350416
[iotivity.git] / cloud / interface / src / test / java / org / iotivity / cloud / ciserver / resources / proxy / rd / ResourceFindTest.java
1 package org.iotivity.cloud.ciserver.resources.proxy.rd;
2
3 import static java.util.concurrent.TimeUnit.SECONDS;
4 import static org.junit.Assert.assertEquals;
5 import static org.junit.Assert.assertFalse;
6 import static org.junit.Assert.assertTrue;
7 import static org.mockito.Mockito.mock;
8
9 import java.util.ArrayList;
10 import java.util.HashMap;
11 import java.util.List;
12 import java.util.concurrent.CountDownLatch;
13
14 import org.iotivity.cloud.base.connector.ConnectorPool;
15 import org.iotivity.cloud.base.device.CoapDevice;
16 import org.iotivity.cloud.base.device.IRequestChannel;
17 import org.iotivity.cloud.base.exception.ClientException;
18 import org.iotivity.cloud.base.protocols.IRequest;
19 import org.iotivity.cloud.base.protocols.IResponse;
20 import org.iotivity.cloud.base.protocols.MessageBuilder;
21 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
22 import org.iotivity.cloud.base.protocols.coap.CoapResponse;
23 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
24 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
25 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
26 import org.iotivity.cloud.ciserver.Constants;
27 import org.iotivity.cloud.ciserver.DeviceServerSystem;
28 import org.iotivity.cloud.util.Cbor;
29 import org.junit.Before;
30 import org.junit.Test;
31 import org.mockito.InjectMocks;
32 import org.mockito.Mock;
33 import org.mockito.Mockito;
34 import org.mockito.MockitoAnnotations;
35 import org.mockito.invocation.InvocationOnMock;
36 import org.mockito.stubbing.Answer;
37
38 public class ResourceFindTest {
39     private static final String TEST_RESOURCE_FIND_URI = Constants.WELL_KNOWN_FULL_URI;
40     private String              di                     = "B371C481-38E6-4D47-8320-7688D8A5B58C";
41     private CoapDevice          mockDevice             = mock(CoapDevice.class);
42     IResponse                   res                    = null;
43     IRequest                    req                    = null;
44     ConnectorPool               connectorPool          = null;
45     DeviceServerSystem          deviceServerSystem     = new DeviceServerSystem();
46     final CountDownLatch        latch                  = new CountDownLatch(1);
47
48     @Mock
49     IRequestChannel             requestChannel;
50
51     @InjectMocks
52     ResourceFind                resHandler             = new ResourceFind();
53
54     @Before
55     public void setUp() throws Exception {
56         res = null;
57         req = null;
58         Mockito.doReturn("mockDeviceId").when(mockDevice).getDeviceId();
59         MockitoAnnotations.initMocks(this);
60         deviceServerSystem.addResource(resHandler);
61         // callback mock
62         Mockito.doAnswer(new Answer<Object>() {
63             @Override
64             public CoapResponse answer(InvocationOnMock invocation)
65                     throws Throwable {
66                 Object[] args = invocation.getArguments();
67                 CoapResponse resp = (CoapResponse) args[0];
68                 res = resp;
69                 return resp;
70             }
71         }).when(mockDevice).sendResponse(Mockito.anyObject());
72         Mockito.doAnswer(new Answer<Object>() {
73             @Override
74             public CoapRequest answer(InvocationOnMock invocation)
75                     throws Throwable {
76                 Object[] args = invocation.getArguments();
77                 CoapRequest request = (CoapRequest) args[0];
78                 System.out.println(
79                         "\t----------payload : " + request.getPayloadString());
80                 System.out.println(
81                         "\t----------uripath : " + request.getUriPath());
82                 System.out.println(
83                         "\t---------uriquery : " + request.getUriQuery());
84                 req = request;
85                 latch.countDown();
86                 return null;
87             }
88         }).when(requestChannel).sendRequest(Mockito.any(IRequest.class),
89                 Mockito.any(CoapDevice.class));
90     }
91
92     // @InjectMocks for testSpecificDeviceonResponseReceived
93     IRequest                           requestSpecificDevice = MessageBuilder
94             .createRequest(RequestMethod.GET, TEST_RESOURCE_FIND_URI,
95                     "rt=core.light;di=" + "device1");
96     @InjectMocks
97     ResourceFind.AccountReceiveHandler specificDeviceHandler = resHandler.new AccountReceiveHandler(
98             requestSpecificDevice, mockDevice);
99
100     @Test
101     public void testSpecificDeviceonResponseReceived()
102             throws ClientException, InterruptedException {
103         System.out.println(
104                 "\t--------------onResponseReceived(RD) Resource Find (specific deivce) Test------------");
105         IResponse response = responseFromAccountServer();
106         specificDeviceHandler.onResponseReceived(response);
107         HashMap<String, List<String>> queryMap = req.getUriQueryMap();
108         assertTrue(req.getMethod() == RequestMethod.GET);
109         assertTrue(queryMap.get("rt").contains("core.light"));
110         assertTrue(queryMap.get("di").contains("device1"));
111         assertFalse(queryMap.get("di").contains("device2"));
112         assertFalse(queryMap.get("di").contains("device3"));
113     }
114
115     // @InjectMocks for testEntireDeviceonResponseReceived
116     IRequest                           requestEntireDevices = MessageBuilder
117             .createRequest(RequestMethod.GET, TEST_RESOURCE_FIND_URI,
118                     "rt=core.light");
119     @InjectMocks
120     ResourceFind.AccountReceiveHandler handler              = resHandler.new AccountReceiveHandler(
121             requestEntireDevices, mockDevice);
122
123     @Test
124     public void testEntireDeviceonResponseReceived() throws ClientException {
125         System.out.println(
126                 "\t--------------onResponseReceived(RD) Resource Find (entire deivces) Test------------");
127         IResponse response = responseFromAccountServer();
128         handler.onResponseReceived(response);
129         HashMap<String, List<String>> queryMap = req.getUriQueryMap();
130         assertTrue(req.getMethod() == RequestMethod.GET);
131         assertTrue(queryMap.get("rt").contains("core.light"));
132         assertTrue(queryMap.get("di").contains("device1"));
133         assertTrue(queryMap.get("di").contains("device2"));
134         assertTrue(queryMap.get("di").contains("device3"));
135     }
136
137     // @InjectMocks for testEntireDeviceonResponseReceived
138     IRequest                           requestEntireDevicesNoQuery = MessageBuilder
139             .createRequest(RequestMethod.GET, TEST_RESOURCE_FIND_URI, null);
140     @InjectMocks
141     ResourceFind.AccountReceiveHandler entireDevicesNoQueryHandler = resHandler.new AccountReceiveHandler(
142             requestEntireDevicesNoQuery, mockDevice);
143
144     @Test
145     public void testEntireDeviceNoQueryonResponseReceived()
146             throws ClientException {
147         System.out.println(
148                 "\t--------------onResponseReceived(RD) Resource Find (entire deivces, No Query) Test------------");
149         IResponse response = responseFromAccountServer();
150         entireDevicesNoQueryHandler.onResponseReceived(response);
151         HashMap<String, List<String>> queryMap = req.getUriQueryMap();
152         assertTrue(req.getMethod() == RequestMethod.GET);
153         assertTrue(queryMap.get("di").contains("device1"));
154         assertTrue(queryMap.get("di").contains("device2"));
155         assertTrue(queryMap.get("di").contains("device3"));
156     }
157
158     @Test
159     public void testResourceFindEntireDevicesOnRequestReceived()
160             throws Exception {
161         System.out.println(
162                 "\t--------------OnRequestReceived(RD) Resource Find (entire deivces) Test------------");
163         IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
164                 TEST_RESOURCE_FIND_URI, "rt=core.light;di=" + di);
165         resHandler.onRequestReceived(mockDevice, request);
166         HashMap<String, List<String>> queryMap = req.getUriQueryMap();
167         assertTrue(latch.await(1L, SECONDS));
168         assertTrue(queryMap.containsKey("mid"));
169         assertEquals(req.getUriPath(), Constants.GROUP_FULL_URI + "/null");
170     }
171
172     @Test
173     public void testResourceFindSpecificDeviceOnRequestReceived()
174             throws Exception {
175         System.out.println(
176                 "\t--------------OnRequestReceived(RD) Resource Find (specific deivce) Test------------");
177         IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
178                 TEST_RESOURCE_FIND_URI, "rt=core.light;di=" + di);
179         resHandler.onRequestReceived(mockDevice, request);
180         HashMap<String, List<String>> queryMap = req.getUriQueryMap();
181         // assertion: if the request packet from the CI contains the query
182         // which includes device ID and the accesstoken
183         assertTrue(latch.await(1L, SECONDS));
184         assertTrue(queryMap.containsKey("mid"));
185         assertEquals(req.getUriPath(), Constants.GROUP_FULL_URI + "/null");
186     }
187
188     private IResponse responseFromAccountServer() {
189         // make response which has "CONTENT" status
190         Cbor<HashMap<String, Object>> cbor = new Cbor<>();
191         HashMap<String, Object> responsePayload = new HashMap<String, Object>();
192         ArrayList<String> deviceList = new ArrayList<String>();
193         // assuming that there are three devices in the response msg from the AS
194         deviceList.add("device1");
195         deviceList.add("device2");
196         deviceList.add("device3");
197         responsePayload.put("dilist", deviceList);
198         responsePayload.put("gid", "g0001");
199         responsePayload.put("gmid", "u0001");
200         ArrayList<String> midList = new ArrayList<String>();
201         midList.add("u0001");
202         responsePayload.put("midlist", midList);
203         IResponse response = MessageBuilder.createResponse(requestEntireDevices,
204                 ResponseStatus.CONTENT, ContentFormat.APPLICATION_CBOR,
205                 cbor.encodingPayloadToCbor(responsePayload));
206         return response;
207     }
208 }