fixed CI unit test.
[iotivity.git] / cloud / interface / src / test / java / org / iotivity / cloud / ciserver / resources / proxy / account / AccountSessionTest.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.HashMap;
31 import java.util.concurrent.CountDownLatch;
32
33 import org.iotivity.cloud.base.connector.ConnectorPool;
34 import org.iotivity.cloud.base.device.CoapDevice;
35 import org.iotivity.cloud.base.device.IRequestChannel;
36 import org.iotivity.cloud.base.protocols.IRequest;
37 import org.iotivity.cloud.base.protocols.IResponse;
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 AccountSessionTest {
55     private String               mDi                 = "B371C481-38E6-4D47-8320-7688D8A5B58C";
56     public static final String   SESSION_URI         = Constants.ACCOUNT_SESSION_FULL_URI;
57     private CoapDevice           mMockDevice         = mock(CoapDevice.class);
58     private IResponse            mRes                = null;
59     private IRequest             mReq                = null;
60     private ConnectorPool        mConnectorPool      = null;
61     private DeviceServerSystem   mDeviceServerSystem = new DeviceServerSystem();
62     private final CountDownLatch mLatch              = new CountDownLatch(1);
63     @Mock
64     private IRequestChannel      mRequestChannel;
65
66     @InjectMocks
67     private AccountSession       mAcSessionHandler   = new AccountSession();
68
69     @Before
70     public void setUp() throws Exception {
71         mRes = null;
72         mReq = null;
73         Mockito.doReturn(mDi).when(mMockDevice).getDeviceId();
74         Mockito.doReturn("mockDeviceUser").when(mMockDevice).getUserId();
75         Mockito.doReturn("1689c70ffa245effc563017fee36d250").when(mMockDevice)
76                 .getAccessToken();
77         MockitoAnnotations.initMocks(this);
78         mDeviceServerSystem.addResource(mAcSessionHandler);
79         Mockito.doAnswer(new Answer<Object>() {
80             @Override
81             public CoapRequest answer(InvocationOnMock invocation)
82                     throws Throwable {
83                 Object[] args = invocation.getArguments();
84                 CoapRequest request = (CoapRequest) args[0];
85                 mReq = request;
86                 mLatch.countDown();
87                 System.out.println(
88                         "\t----------payload : " + request.getPayloadString());
89                 System.out.println(
90                         "\t----------uripath : " + request.getUriPath());
91                 System.out.println(
92                         "\t---------uriquery : " + request.getUriQuery());
93                 return null;
94             }
95         }).when(mRequestChannel).sendRequest(Mockito.any(IRequest.class),
96                 Mockito.any(CoapDevice.class));
97     }
98
99     @Test
100     public void testAccountSignInOnRequestReceived() throws Exception {
101         System.out.println(
102                 "\t--------------OnRequestReceived Sign In Test------------");
103         IRequest request = makeSignInRequest();
104         mDeviceServerSystem.onRequestReceived(mMockDevice, request);
105         assertTrue(mLatch.await(1L, SECONDS));
106         assertEquals(request, request);
107     }
108
109     @Test
110     public void testAccountResourceOnRequestReceived() throws Exception {
111         System.out.println(
112                 "\t--------------OnRequestReceived Sign Out Test------------");
113         // sign up request from the client
114         IRequest request = makeSignOutRequest();
115         mDeviceServerSystem.onRequestReceived(mMockDevice, request);
116         // assertion : request msg to the AS is identical to the request msg
117         // from the client
118         assertTrue(mLatch.await(1L, SECONDS));
119         assertTrue(hashmapCheck(mReq, Constants.USER_ID));
120         assertTrue(hashmapCheck(mReq, Constants.DEVICE_ID));
121         assertTrue(hashmapCheck(mReq, Constants.ACCESS_TOKEN));
122         assertTrue(hashmapCheck(mReq, Constants.REQ_LOGIN));
123     }
124
125     private IRequest makeSignInRequest() {
126         Cbor<HashMap<String, Object>> cbor = new Cbor<HashMap<String, Object>>();
127         IRequest request = null;
128         HashMap<String, Object> payloadData = new HashMap<>();
129         payloadData.put(Constants.USER_ID, "u0001");
130         payloadData.put(Constants.DEVICE_ID, mDi);
131         payloadData.put(Constants.ACCESS_TOKEN,
132                 "1689c70ffa245effc563017fee36d250");
133         payloadData.put(Constants.REQ_LOGIN, true);
134         request = MessageBuilder.createRequest(RequestMethod.POST, SESSION_URI,
135                 null, ContentFormat.APPLICATION_CBOR,
136                 cbor.encodingPayloadToCbor(payloadData));
137         return request;
138     }
139
140     private IRequest makeSignOutRequest() {
141         Cbor<HashMap<String, Object>> cbor = new Cbor<HashMap<String, Object>>();
142         IRequest request = null;
143         HashMap<String, Object> payloadData = new HashMap<>();
144         payloadData.put(Constants.REQ_LOGIN, false);
145         request = MessageBuilder.createRequest(RequestMethod.POST, SESSION_URI,
146                 null, ContentFormat.APPLICATION_CBOR,
147                 cbor.encodingPayloadToCbor(payloadData));
148         return request;
149     }
150
151     private boolean hashmapCheck(IRequest request, String propertyName) {
152         Cbor<HashMap<String, Object>> mCbor = new Cbor<>();
153         HashMap<String, Object> payloadData = mCbor
154                 .parsePayloadFromCbor(request.getPayload(), HashMap.class);
155         if (payloadData.get(propertyName) != null)
156             return true;
157         else
158             return false;
159     }
160 }