remove prefix .well-known uri & change from 'ocf' to 'oic' uri
[iotivity.git] / cloud / interface / src / test / java / org / iotivity / cloud / ciserver / resources / proxy / account / AclInviteTest.java
1 package org.iotivity.cloud.ciserver.resources.proxy.account;
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.Arrays;
10 import java.util.HashMap;
11 import java.util.concurrent.CountDownLatch;
12
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.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.ciserver.Constants;
22 import org.iotivity.cloud.ciserver.DeviceServerSystem;
23 import org.iotivity.cloud.util.Cbor;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.InjectMocks;
27 import org.mockito.Mock;
28 import org.mockito.Mockito;
29 import org.mockito.MockitoAnnotations;
30 import org.mockito.invocation.InvocationOnMock;
31 import org.mockito.stubbing.Answer;
32
33 public class AclInviteTest {
34     private static final String TEST_RESOURCE_INVITE_URI = Constants.INVITE_FULL_URI;
35
36     private CoapDevice          mockDevice               = mock(
37             CoapDevice.class);
38     IRequest                    req                      = null;
39     ConnectorPool               connectorPool            = null;
40     DeviceServerSystem          deviceServerSystem       = new DeviceServerSystem();
41     final CountDownLatch        latch                    = new CountDownLatch(
42             1);
43
44     @Mock
45     IRequestChannel             requestChannel;
46
47     @InjectMocks
48     AclInvite                   aclInviteHandler         = new AclInvite();
49
50     @Before
51     public void setUp() throws Exception {
52         req = null;
53         Mockito.doReturn("mockUserId").when(mockDevice).getUserId();
54         MockitoAnnotations.initMocks(this);
55         deviceServerSystem.addResource(aclInviteHandler);
56         // callback mock
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 null;
72             }
73         }).when(requestChannel).sendRequest(Mockito.any(IRequest.class),
74                 Mockito.any(CoapDevice.class));
75     }
76
77     @Test
78     public void testPostRequestReceived() throws Exception {
79         System.out.println(
80                 "\t--------------OnRequestReceived(AS) Post Message Test------------");
81
82         HashMap<String, Object> payloadData = new HashMap<>();
83         HashMap<String, Object> invite = new HashMap<>();
84         invite.put("gid", "g0001");
85         invite.put("mid", "u0001");
86         payloadData.put("invite", Arrays.asList(invite));
87         Cbor<HashMap<Object, Object>> cbor = new Cbor<>();
88         IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
89                 TEST_RESOURCE_INVITE_URI, null, ContentFormat.APPLICATION_CBOR,
90                 cbor.encodingPayloadToCbor(payloadData));
91         aclInviteHandler.onRequestReceived(mockDevice, request);
92
93         assertTrue(latch.await(1L, SECONDS));
94         assertTrue(cbor.parsePayloadFromCbor(req.getPayload(), HashMap.class)
95                 .containsKey("uid"));
96         assertTrue(cbor.parsePayloadFromCbor(req.getPayload(), HashMap.class)
97                 .containsKey("invite"));
98         ArrayList<HashMap<String, Object>> getinvite = (ArrayList<HashMap<String, Object>>) cbor
99                 .parsePayloadFromCbor(req.getPayload(), HashMap.class)
100                 .get("invite");
101         assertTrue(getinvite.get(0).containsKey("gid"));
102         assertTrue(getinvite.get(0).containsKey("mid"));
103         assertEquals(req.getUriPath(), TEST_RESOURCE_INVITE_URI);
104     }
105
106     @Test
107     public void testGetRequestReceived() throws Exception {
108         System.out.println(
109                 "\t--------------OnRequestReceived(AS) Get Message Test------------");
110
111         IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
112                 TEST_RESOURCE_INVITE_URI, null, null, null);
113         aclInviteHandler.onRequestReceived(mockDevice, request);
114
115         assertTrue(latch.await(1L, SECONDS));
116         assertTrue(req.getUriQueryMap().containsKey("uid"));
117         assertEquals(req.getUriPath(), TEST_RESOURCE_INVITE_URI);
118     }
119
120     @Test
121     public void testDeleteRequestReceived() throws Exception {
122         System.out.println(
123                 "\t--------------OnRequestReceived(AS) Delete Message Test------------");
124
125         IRequest request = MessageBuilder.createRequest(RequestMethod.DELETE,
126                 TEST_RESOURCE_INVITE_URI, "gid=g0001", null, null);
127         aclInviteHandler.onRequestReceived(mockDevice, request);
128
129         assertTrue(latch.await(1L, SECONDS));
130         assertTrue(req.getUriQueryMap().containsKey("gid"));
131         assertTrue(req.getUriQueryMap().containsKey("uid"));
132         assertEquals(req.getUriPath(), TEST_RESOURCE_INVITE_URI);
133     }
134 }