fixed CI unit test.
[iotivity.git] / cloud / interface / src / test / java / org / iotivity / cloud / ciserver / resources / proxy / account / AclInviteTest.java
1 /*
2  * //******************************************************************
3  * //
4  * // Copyright 2016 Samsung Electronics All Rights Reserved.
5  * //
6  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
7  * //
8  * // Licensed under the Apache License, Version 2.0 (the "License");
9  * // you may not use this file except in compliance with the License.
10  * // You may obtain a copy of the License at
11  * //
12  * //      http://www.apache.org/licenses/LICENSE-2.0
13  * //
14  * // Unless required by applicable law or agreed to in writing, software
15  * // distributed under the License is distributed on an "AS IS" BASIS,
16  * // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * // See the License for the specific language governing permissions and
18  * // limitations under the License.
19  * //
20  * //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21  */
22
23 package org.iotivity.cloud.ciserver.resources.proxy.account;
24
25 import static java.util.concurrent.TimeUnit.SECONDS;
26 import static org.junit.Assert.assertEquals;
27 import static org.junit.Assert.assertTrue;
28 import static org.mockito.Mockito.mock;
29
30 import java.util.ArrayList;
31 import java.util.Arrays;
32 import java.util.HashMap;
33 import java.util.concurrent.CountDownLatch;
34
35 import org.iotivity.cloud.base.device.CoapDevice;
36 import org.iotivity.cloud.base.device.IRequestChannel;
37 import org.iotivity.cloud.base.protocols.IRequest;
38 import org.iotivity.cloud.base.protocols.MessageBuilder;
39 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
40 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
41 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
42 import org.iotivity.cloud.ciserver.Constants;
43 import org.iotivity.cloud.ciserver.DeviceServerSystem;
44 import org.iotivity.cloud.util.Cbor;
45 import org.junit.Before;
46 import org.junit.Test;
47 import org.mockito.InjectMocks;
48 import org.mockito.Mock;
49 import org.mockito.Mockito;
50 import org.mockito.MockitoAnnotations;
51 import org.mockito.invocation.InvocationOnMock;
52 import org.mockito.stubbing.Answer;
53
54 public class AclInviteTest {
55     private static final String  TEST_RESOURCE_INVITE_URI = Constants.INVITE_FULL_URI;
56
57     private CoapDevice           mMockDevice              = mock(
58             CoapDevice.class);
59     private IRequest             mReq                     = null;
60     private DeviceServerSystem   mDeviceServerSystem      = new DeviceServerSystem();
61     private final CountDownLatch mLatch                   = new CountDownLatch(
62             1);
63
64     @Mock
65     private IRequestChannel      mRequestChannel;
66
67     @InjectMocks
68     private AclInvite            mAclInviteHandler        = new AclInvite();
69
70     @Before
71     public void setUp() throws Exception {
72         mReq = null;
73         Mockito.doReturn("mockUserId").when(mMockDevice).getUserId();
74         MockitoAnnotations.initMocks(this);
75         mDeviceServerSystem.addResource(mAclInviteHandler);
76         // callback mock
77         Mockito.doAnswer(new Answer<Object>() {
78             @Override
79             public CoapRequest answer(InvocationOnMock invocation)
80                     throws Throwable {
81                 Object[] args = invocation.getArguments();
82                 CoapRequest request = (CoapRequest) args[0];
83                 System.out.println(
84                         "\t----------payload : " + request.getPayloadString());
85                 System.out.println(
86                         "\t----------uripath : " + request.getUriPath());
87                 System.out.println(
88                         "\t---------uriquery : " + request.getUriQuery());
89                 mReq = request;
90                 mLatch.countDown();
91                 return null;
92             }
93         }).when(mRequestChannel).sendRequest(Mockito.any(IRequest.class),
94                 Mockito.any(CoapDevice.class));
95     }
96
97     @Test
98     public void testPostRequestReceived() throws Exception {
99         System.out.println(
100                 "\t--------------OnRequestReceived(AS) Post Message Test------------");
101
102         HashMap<String, Object> payloadData = new HashMap<>();
103         HashMap<String, Object> invite = new HashMap<>();
104         invite.put("gid", "g0001");
105         invite.put("mid", "u0001");
106         payloadData.put("invite", Arrays.asList(invite));
107         Cbor<HashMap<Object, Object>> cbor = new Cbor<>();
108         IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
109                 TEST_RESOURCE_INVITE_URI, null, ContentFormat.APPLICATION_CBOR,
110                 cbor.encodingPayloadToCbor(payloadData));
111         mAclInviteHandler.onRequestReceived(mMockDevice, request);
112
113         assertTrue(mLatch.await(1L, SECONDS));
114         assertTrue(cbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class)
115                 .containsKey("uid"));
116         assertTrue(cbor.parsePayloadFromCbor(mReq.getPayload(), HashMap.class)
117                 .containsKey("invite"));
118         ArrayList<HashMap<String, Object>> getinvite = (ArrayList<HashMap<String, Object>>) cbor
119                 .parsePayloadFromCbor(mReq.getPayload(), HashMap.class)
120                 .get("invite");
121         assertTrue(getinvite.get(0).containsKey("gid"));
122         assertTrue(getinvite.get(0).containsKey("mid"));
123         assertEquals(mReq.getUriPath(), TEST_RESOURCE_INVITE_URI);
124     }
125
126     @Test
127     public void testGetRequestReceived() throws Exception {
128         System.out.println(
129                 "\t--------------OnRequestReceived(AS) Get Message Test------------");
130
131         IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
132                 TEST_RESOURCE_INVITE_URI, null, null, null);
133         mAclInviteHandler.onRequestReceived(mMockDevice, request);
134
135         assertTrue(mLatch.await(1L, SECONDS));
136         assertTrue(mReq.getUriQueryMap().containsKey("uid"));
137         assertEquals(mReq.getUriPath(), TEST_RESOURCE_INVITE_URI);
138     }
139
140     @Test
141     public void testDeleteRequestReceived() throws Exception {
142         System.out.println(
143                 "\t--------------OnRequestReceived(AS) Delete Message Test------------");
144
145         IRequest request = MessageBuilder.createRequest(RequestMethod.DELETE,
146                 TEST_RESOURCE_INVITE_URI, "gid=g0001", null, null);
147         mAclInviteHandler.onRequestReceived(mMockDevice, request);
148
149         assertTrue(mLatch.await(1L, SECONDS));
150         assertTrue(mReq.getUriQueryMap().containsKey("gid"));
151         assertTrue(mReq.getUriQueryMap().containsKey("uid"));
152         assertEquals(mReq.getUriPath(), TEST_RESOURCE_INVITE_URI);
153     }
154 }