fixed CI unit test.
[iotivity.git] / cloud / interface / src / test / java / org / iotivity / cloud / ciserver / resources / proxy / account / AclTest.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.concurrent.CountDownLatch;
31
32 import org.iotivity.cloud.base.OICConstants;
33 import org.iotivity.cloud.base.device.CoapDevice;
34 import org.iotivity.cloud.base.device.IRequestChannel;
35 import org.iotivity.cloud.base.protocols.IRequest;
36 import org.iotivity.cloud.base.protocols.MessageBuilder;
37 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
38 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
39 import org.iotivity.cloud.ciserver.DeviceServerSystem;
40 import org.junit.Before;
41 import org.junit.Test;
42 import org.mockito.InjectMocks;
43 import org.mockito.Mock;
44 import org.mockito.Mockito;
45 import org.mockito.MockitoAnnotations;
46 import org.mockito.invocation.InvocationOnMock;
47 import org.mockito.stubbing.Answer;
48
49 public class AclTest {
50     private static final String TEST_RESOURCE_ACI_URI = "/"
51             + OICConstants.PREFIX_OIC + "/" + OICConstants.ACL_URI;
52     private CoapDevice          mMockDevice           = mock(CoapDevice.class);
53     private IRequest            mReq                  = null;
54     private DeviceServerSystem  mDeviceServerSystem   = new DeviceServerSystem();
55     final CountDownLatch        mLatch                = new CountDownLatch(1);
56
57     @Mock
58     private IRequestChannel     mRequestChannel;
59
60     @InjectMocks
61     private Acl                 mAclHandler           = new Acl();
62
63     @Before
64     public void setUp() throws Exception {
65         mReq = null;
66         MockitoAnnotations.initMocks(this);
67         mDeviceServerSystem.addResource(mAclHandler);
68         // callback mock
69         Mockito.doAnswer(new Answer<Object>() {
70             @Override
71             public CoapRequest answer(InvocationOnMock invocation)
72                     throws Throwable {
73                 Object[] args = invocation.getArguments();
74                 CoapRequest request = (CoapRequest) args[0];
75                 System.out.println(
76                         "\t----------payload : " + request.getPayloadString());
77                 System.out.println(
78                         "\t----------uripath : " + request.getUriPath());
79                 System.out.println(
80                         "\t---------uriquery : " + request.getUriQuery());
81                 mReq = request;
82                 mLatch.countDown();
83                 return null;
84             }
85         }).when(mRequestChannel).sendRequest(Mockito.any(IRequest.class),
86                 Mockito.any(CoapDevice.class));
87     }
88
89     @Test
90     public void testOnDefaultRequestReceived() throws Exception {
91         System.out.println(
92                 "\t--------------OnRequestReceived(AS)Test------------");
93
94         IRequest request = MessageBuilder.createRequest(RequestMethod.POST,
95                 TEST_RESOURCE_ACI_URI, null, null, null);
96         mAclHandler.onRequestReceived(mMockDevice, request);
97
98         assertTrue(mLatch.await(1L, SECONDS));
99         assertEquals(mReq.getUriPath(), TEST_RESOURCE_ACI_URI);
100     }
101 }