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