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