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