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