9072d42fca6c0ac2fd3b6a56d1befd6163fa52eb
[iotivity.git] / cloud / interface / src / test / java / org / iotivity / cloud / ciserver / resources / proxy / rd / ResourcePresenceTest.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.device.CoapDevice;
14 import org.iotivity.cloud.base.device.IRequestChannel;
15 import org.iotivity.cloud.base.exception.ClientException;
16 import org.iotivity.cloud.base.protocols.IRequest;
17 import org.iotivity.cloud.base.protocols.IResponse;
18 import org.iotivity.cloud.base.protocols.MessageBuilder;
19 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
20 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
21 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
22 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
23 import org.iotivity.cloud.ciserver.Constants;
24 import org.iotivity.cloud.ciserver.DeviceServerSystem;
25 import org.iotivity.cloud.util.Cbor;
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.mockito.InjectMocks;
29 import org.mockito.Mock;
30 import org.mockito.Mockito;
31 import org.mockito.MockitoAnnotations;
32 import org.mockito.invocation.InvocationOnMock;
33 import org.mockito.stubbing.Answer;
34
35 public class ResourcePresenceTest {
36     public static final String DEVICE_PRS_REQ_URI = Constants.DEVICE_PRESENCE_FULL_URI;
37     public static final String DEVICE_LIST_KEY    = "devices";
38     public static final String RES_PRS_URI        = Constants.RESOURCE_PRESENCE_FULL_URI;
39     private String             di                 = "B371C481-38E6-4D47-8320-7688D8A5B58C";
40     private CoapDevice         mockDevice         = mock(CoapDevice.class);
41     IResponse                  res                = null;
42     IRequest                   req                = null;
43     DeviceServerSystem         deviceServerSystem = new DeviceServerSystem();
44     final CountDownLatch       latch              = new CountDownLatch(1);
45     @Mock
46     IRequestChannel            requestChannel;
47     @InjectMocks
48     ResourcePresence           adHandler          = new ResourcePresence();
49
50     @Before
51     public void setUp() throws Exception {
52         res = null;
53         req = null;
54         Mockito.doReturn("mockDeviceId").when(mockDevice).getDeviceId();
55         MockitoAnnotations.initMocks(this);
56         deviceServerSystem.addResource(adHandler);
57         Mockito.doAnswer(new Answer<Object>() {
58             @Override
59             public CoapRequest answer(InvocationOnMock invocation)
60                     throws Throwable {
61                 Object[] args = invocation.getArguments();
62                 CoapRequest request = (CoapRequest) args[0];
63                 System.out.println(
64                         "\t----------payload : " + request.getPayloadString());
65                 System.out.println(
66                         "\t----------uripath : " + request.getUriPath());
67                 System.out.println(
68                         "\t---------uriquery : " + request.getUriQuery());
69                 req = request;
70                 latch.countDown();
71                 return request;
72             }
73         }).when(requestChannel).sendRequest(Mockito.any(IRequest.class),
74                 Mockito.any(CoapDevice.class));
75     }
76
77     @Test
78     public void testResourcePresenceEntireResourcesOnRequestReceived()
79             throws Exception {
80         System.out.println(
81                 "\t--------------OnRequestReceived(RD) Resource Presence (entire resource) Test------------");
82         IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
83                 RES_PRS_URI, null);
84         deviceServerSystem.onRequestReceived(mockDevice, request);
85         HashMap<String, List<String>> queryMap = req.getUriQueryMap();
86         // assertion: if the request packet from the CI contains the query
87         // which includes the accesstoken and the di
88         assertTrue(latch.await(1L, SECONDS));
89         assertTrue(queryMap.containsKey("mid"));
90         assertEquals(req.getUriPath(), Constants.GROUP_FULL_URI + "/null");
91     }
92
93     @Test
94     public void testResourcePresenceSpecificResourceOnRequestReceived()
95             throws Exception {
96         System.out.println(
97                 "\t--------------OnRequestReceived(RD) Resource Presence (specific resources) Test------------");
98         IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
99                 RES_PRS_URI, "di=" + di);
100         deviceServerSystem.onRequestReceived(mockDevice, request);
101         HashMap<String, List<String>> queryMap = req.getUriQueryMap();
102         assertTrue(latch.await(1L, SECONDS));
103         assertTrue(queryMap.containsKey("mid"));
104         assertEquals(req.getUriPath(), Constants.GROUP_FULL_URI + "/null");
105     }
106
107     IRequest                               requestEntireDevices = MessageBuilder
108             .createRequest(RequestMethod.GET, RES_PRS_URI, null);
109     @InjectMocks
110     ResourcePresence.AccountReceiveHandler entireDeviceHandler  = adHandler.new AccountReceiveHandler(
111             requestEntireDevices, mockDevice);
112
113     @Test
114     public void testEntireDeviceonResponseReceived() throws ClientException {
115         System.out.println(
116                 "\t--------------onResponseReceived(RD) Resource Presence (entire deivces) Test------------");
117         IResponse responseFromAccountServer = responseFromAccountServer();
118         entireDeviceHandler.onResponseReceived(responseFromAccountServer);
119         HashMap<String, List<String>> queryMap = req.getUriQueryMap();
120         // assertion : if query has pre-requested multiple di list given from
121         // the AS
122         assertTrue(req.getMethod() == RequestMethod.GET);
123         assertTrue(queryMap.get("di").contains("device1"));
124         assertTrue(queryMap.get("di").contains("device2"));
125         assertTrue(queryMap.get("di").contains("device3"));
126     }
127
128     IRequest                               requestSpecificDevice = MessageBuilder
129             .createRequest(RequestMethod.GET, RES_PRS_URI, "di=" + "device1");
130     @InjectMocks
131     ResourcePresence.AccountReceiveHandler specificDeviceHandler = adHandler.new AccountReceiveHandler(
132             requestSpecificDevice, mockDevice);
133
134     @Test
135     public void testSpecificDeviceonResponseReceived() throws ClientException {
136         System.out.println(
137                 "\t--------------onResponseReceived(RD) Resource Presence (specific deivce) Test------------");
138         IResponse response = responseFromAccountServer();
139         specificDeviceHandler.onResponseReceived(response);
140         HashMap<String, List<String>> queryMap = req.getUriQueryMap();
141         // assertion : if query has pre-requested di
142         assertTrue(queryMap.get("di").contains("device1"));
143         assertTrue(req.getMethod() == RequestMethod.GET);
144     }
145
146     private IResponse responseFromAccountServer() {
147         // make response which has "CONTENT" status
148         Cbor<HashMap<String, Object>> cbor = new Cbor<>();
149         HashMap<String, Object> responsePayload = new HashMap<String, Object>();
150         ArrayList<String> deviceList = new ArrayList<String>();
151         // assuming that there are three devices in the response msg from the AS
152         deviceList.add("device1");
153         deviceList.add("device2");
154         deviceList.add("device3");
155         responsePayload.put("dilist", deviceList);
156         responsePayload.put("gid", "g0001");
157         responsePayload.put("gmid", "u0001");
158         ArrayList<String> midList = new ArrayList<String>();
159         midList.add("u0001");
160         responsePayload.put("midlist", midList);
161         IResponse response = MessageBuilder.createResponse(requestEntireDevices,
162                 ResponseStatus.CONTENT, ContentFormat.APPLICATION_CBOR,
163                 cbor.encodingPayloadToCbor(responsePayload));
164         return response;
165     }
166 }