a7db30320feef0c53cb725f3327b66d40c3ba82b
[iotivity.git] / cloud / interface / src / test / java / org / iotivity / cloud / ciserver / resources / proxy / rd / ResourceDirectoryTest.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.concurrent.CountDownLatch;
11
12 import org.iotivity.cloud.base.OCFConstants;
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.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 ResourceDirectoryTest {
36     private static final String TEST_RD_URI        = OCFConstants.PREFIX_OIC
37             + "/" + OCFConstants.RD_URI;
38     public static final String  DEVICE_PRS_REQ_URI = "/.well-known/ocf/prs";
39     public static final String  DEVICE_LIST_KEY    = "devices";
40     public static final String  RES_PRS_URI        = "/oic/ad";
41     // private DeviceServerSystem deviceServerSystem = new DeviceServerSystem();
42     private String              di                 = "B371C481-38E6-4D47-8320-7688D8A5B58C";
43     String                      userId             = "testuser";
44     private CoapDevice          mockDevice         = null;
45     IResponse                   res                = null;
46     IRequest                    req                = null;
47     ConnectorPool               connectorPool      = null;
48     DeviceServerSystem          deviceServerSystem = new DeviceServerSystem();
49     final CountDownLatch        latch              = new CountDownLatch(1);
50     @Mock
51     IRequestChannel             requestChannel;
52
53     @InjectMocks
54     ResourceDirectory           rdHandler          = new ResourceDirectory();
55
56     @Before
57     public void setUp() throws Exception {
58         MockitoAnnotations.initMocks(this);
59         deviceServerSystem.addResource(rdHandler);
60         mockDevice = mock(CoapDevice.class);
61         // callback requestmsg mock from (IRequestChannel) server
62         Mockito.doAnswer(new Answer<Object>() {
63             @Override
64             public CoapRequest answer(InvocationOnMock invocation)
65                     throws Throwable {
66                 Object[] args = invocation.getArguments();
67                 CoapRequest request = (CoapRequest) args[0];
68                 System.out.println(
69                         "\t----------payload : " + request.getPayloadString());
70                 System.out.println(
71                         "\t----------uripath : " + request.getUriPath());
72                 System.out.println(
73                         "\t---------uriquery : " + request.getUriQuery());
74                 req = request;
75                 latch.countDown();
76                 return request;
77             }
78         }).when(requestChannel).sendRequest(Mockito.any(IRequest.class),
79                 Mockito.any(CoapDevice.class));
80
81     }
82
83     @Test
84     public void testRDResourceOnRequestReceived() throws Exception {
85         IRequest request = makeResourcePublishRequest();
86         deviceServerSystem.onRequestReceived(mockDevice, request);
87         assertTrue(latch.await(1L, SECONDS));
88         assertTrue(req.getUriPath()
89                 .contains(Constants.PREFIX_WELL_KNOWN + "/"
90                         + Constants.PREFIX_OCF + "/" + Constants.ACL_URI + "/"
91                         + Constants.GROUP_URI + "/"));
92     }
93
94     IRequest                                rdPublishRequest      = makeResourcePublishRequest();
95
96     @InjectMocks
97     ResourceDirectory.AccountReceiveHandler accountReceiveHandler = rdHandler.new AccountReceiveHandler(
98             rdPublishRequest, mockDevice);
99
100     @Test
101     public void testRDResourceOnResponseReceived() throws Exception {
102         IRequest request = makeResourcePublishRequest();
103         deviceServerSystem.onRequestReceived(mockDevice, request);
104         // assertion : request msg to the AS is identical to the request msg
105         // from the client
106         accountReceiveHandler.onResponseReceived(
107                 MessageBuilder.createResponse(request, ResponseStatus.CHANGED));
108         assertEquals(req, rdPublishRequest);
109         assertTrue(latch.await(1L, SECONDS));
110     }
111
112     private IRequest makeResourcePublishRequest() {
113         HashMap<Object, Object> payload = new HashMap<>();
114         payload.put(Constants.DEVICE_ID, di);
115         ArrayList<HashMap<Object, Object>> publishLinks = new ArrayList<>();
116         HashMap<Object, Object> link = new HashMap<>();
117         link.put("href", "/a/light");
118         ArrayList<String> rt = new ArrayList<String>();
119         rt.add("core.light");
120         ArrayList<String> itf = new ArrayList<String>();
121         itf.add("oic.if.baseline");
122         HashMap<String, Object> policy = new HashMap<>();
123         policy.put("bm", 5);
124         link.put("rt", rt);
125         link.put("if", itf);
126         link.put("p", policy);
127         publishLinks.add(link);
128         payload.put("links", publishLinks);
129         Cbor<HashMap<Object, Object>> cbor = new Cbor<>();
130         IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
131                 TEST_RD_URI, "rt" + "=" + "oic.wk.rdpub",
132                 ContentFormat.APPLICATION_CBOR,
133                 cbor.encodingPayloadToCbor(payload));
134         return request;
135     }
136
137 }