6bffe55483e0463901fde14482d6b6b77b9dbb67
[iotivity.git] / cloud / interface / src / test / java / org / iotivity / cloud / ciserver / resources / DiResourceTest.java
1 package org.iotivity.cloud.ciserver.resources;
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.HashMap;
9 import java.util.concurrent.CountDownLatch;
10
11 import org.iotivity.cloud.base.device.CoapDevice;
12 import org.iotivity.cloud.base.device.IRequestChannel;
13 import org.iotivity.cloud.base.protocols.IRequest;
14 import org.iotivity.cloud.base.protocols.IResponse;
15 import org.iotivity.cloud.base.protocols.MessageBuilder;
16 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
17 import org.iotivity.cloud.base.protocols.coap.CoapResponse;
18 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
19 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
20 import org.iotivity.cloud.base.protocols.enums.ResponseStatus;
21 import org.iotivity.cloud.ciserver.DeviceServerSystem;
22 import org.iotivity.cloud.ciserver.DeviceServerSystem.CoapDevicePool;
23 import org.iotivity.cloud.ciserver.resources.DiResource.DefaultResponseHandler;
24 import org.iotivity.cloud.ciserver.resources.DiResource.LinkInterfaceHandler;
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 DiResourceTest {
36     private static final String RELAY_URI              = "/di";
37     private static final String RESOURCE_URI           = "/a/light/0";
38     private String              diServer               = "resourceServerId";
39     private CoapDevice          sourceDevice           = mock(CoapDevice.class);
40     private CoapDevice          targetDevice           = mock(CoapDevice.class);
41     IResponse                   res                    = null;
42     IRequest                    req                    = null;
43     DeviceServerSystem          deviceServerSystem     = new DeviceServerSystem();
44     final CountDownLatch        latch                  = new CountDownLatch(1);
45     IRequestChannel             targetChannel          = mock(
46             IRequestChannel.class);
47
48     @Mock
49     CoapDevicePool              coapDevicePool;
50
51     @Mock
52     IRequestChannel             requestChannel;
53
54     @InjectMocks
55     DiResource                  diHandler              = new DiResource(
56             coapDevicePool);
57
58     @InjectMocks
59     LinkInterfaceHandler        linkInterfaceHandler   = diHandler.new LinkInterfaceHandler(
60             "targetDeviceId", sourceDevice);
61
62     @InjectMocks
63     DefaultResponseHandler      defaultResponseHandler = diHandler.new DefaultResponseHandler(
64             "targetDeviceId", sourceDevice);
65
66     @Before
67     public void setUp() throws Exception {
68         res = null;
69         req = null;
70         Mockito.doReturn("sourceDeviceId").when(sourceDevice).getDeviceId();
71         Mockito.doReturn("targetDeviceId").when(targetDevice).getDeviceId();
72         MockitoAnnotations.initMocks(this);
73         deviceServerSystem.addResource(diHandler);
74         Mockito.doReturn(targetDevice).when(coapDevicePool)
75                 .queryDevice(Mockito.anyString());
76         Mockito.doReturn(targetChannel).when(targetDevice).getRequestChannel();
77
78         // callback mock
79         Mockito.doAnswer(new Answer<Object>() {
80             @Override
81             public CoapResponse answer(InvocationOnMock invocation)
82                     throws Throwable {
83                 Object[] args = invocation.getArguments();
84                 CoapResponse resp = (CoapResponse) args[0];
85                 res = resp;
86                 latch.countDown();
87                 return null;
88             }
89         }).when(sourceDevice).sendResponse(Mockito.anyObject());
90         Mockito.doAnswer(new Answer<Object>() {
91             @Override
92             public CoapRequest answer(InvocationOnMock invocation)
93                     throws Throwable {
94                 Object[] args = invocation.getArguments();
95                 CoapRequest request = (CoapRequest) args[0];
96                 System.out.println(
97                         "\t----------payload : " + request.getPayloadString());
98                 System.out.println(
99                         "\t----------uripath : " + request.getUriPath());
100                 System.out.println(
101                         "\t---------uriquery : " + request.getUriQuery());
102                 req = request;
103                 latch.countDown();
104                 return null;
105             }
106         }).when(targetChannel).sendRequest(Mockito.any(IRequest.class),
107                 Mockito.any(CoapDevice.class));
108     }
109
110     @Test
111     public void testOnDefaultRequestReceived() throws InterruptedException {
112         IRequest request = makePutRequest();
113         diHandler.onDefaultRequestReceived(sourceDevice, request);
114         assertTrue(req.getMethod().equals(RequestMethod.PUT));
115         assertTrue(req.getUriPath().equals(RESOURCE_URI));
116         assertTrue(latch.await(1L, SECONDS));
117     }
118
119     // TODO treat oic.if.ll resource response (on LinkInterfaceHandler)
120
121     @Test
122     public void testOnDefaultResponseHandleronResponseReceived()
123             throws InterruptedException {
124         IResponse response = makeContentResponse();
125         defaultResponseHandler.onResponseReceived(response);
126         assertEquals(res, response);
127         assertTrue(latch.await(1L, SECONDS));
128     }
129
130     private IRequest makePutRequest() {
131         Cbor<HashMap<String, Object>> cbor = new Cbor<>();
132         HashMap<String, Object> payloadData = new HashMap<>();
133         payloadData.put("state", true);
134         payloadData.put("power", 6);
135         IRequest request = MessageBuilder.createRequest(RequestMethod.PUT,
136                 RELAY_URI + "/" + diServer + RESOURCE_URI, null,
137                 ContentFormat.APPLICATION_CBOR,
138                 cbor.encodingPayloadToCbor(payloadData));
139         return request;
140     }
141
142     private IResponse makeContentResponse() {
143         Cbor<HashMap<String, Object>> cbor = new Cbor<>();
144         HashMap<String, Object> payloadData = new HashMap<>();
145         payloadData.put("state", true);
146         payloadData.put("power", 6);
147         IResponse response = MessageBuilder.createResponse(makeGetRequest(),
148                 ResponseStatus.CONTENT, ContentFormat.APPLICATION_CBOR,
149                 cbor.encodingPayloadToCbor(payloadData));
150         return response;
151     }
152
153     private IRequest makeGetRequest() {
154         IRequest request = MessageBuilder.createRequest(RequestMethod.PUT,
155                 RELAY_URI + "/" + diServer + RESOURCE_URI, null);
156         return request;
157     }
158
159 }