Implement reconnection and ping-pong between cloud services. Make
[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.junit.runner.RunWith;
48 import org.mockito.InjectMocks;
49 import org.mockito.Mock;
50 import org.mockito.Mockito;
51 import org.mockito.MockitoAnnotations;
52 import org.mockito.invocation.InvocationOnMock;
53 import org.mockito.stubbing.Answer;
54 import org.powermock.api.mockito.PowerMockito;
55 import org.powermock.core.classloader.annotations.PrepareForTest;
56 import org.powermock.modules.junit4.PowerMockRunner;
57
58 @RunWith(PowerMockRunner.class)
59 @PrepareForTest(ConnectorPool.class)
60 public class AccountSessionTest {
61     private String               mDi                 = "B371C481-38E6-4D47-8320-7688D8A5B58C";
62     public static final String   SESSION_URI         = Constants.ACCOUNT_SESSION_FULL_URI;
63     private CoapDevice           mMockDevice         = mock(CoapDevice.class);
64     private IResponse            mRes                = null;
65     private IRequest             mReq                = null;
66     private ConnectorPool        mConnectorPool      = null;
67     private DeviceServerSystem   mDeviceServerSystem = new DeviceServerSystem();
68     private final CountDownLatch mLatch              = new CountDownLatch(1);
69     @Mock
70     private IRequestChannel      mRequestChannel;
71
72     @InjectMocks
73     private AccountSession       mAcSessionHandler   = new AccountSession();
74
75     @Before
76     public void setUp() throws Exception {
77         mRes = null;
78         mReq = null;
79         Mockito.doReturn(mDi).when(mMockDevice).getDeviceId();
80         Mockito.doReturn("mockDeviceUser").when(mMockDevice).getUserId();
81         Mockito.doReturn("1689c70ffa245effc563017fee36d250").when(mMockDevice)
82                 .getAccessToken();
83         MockitoAnnotations.initMocks(this);
84         mDeviceServerSystem.addResource(mAcSessionHandler);
85         Mockito.doAnswer(new Answer<Object>() {
86             @Override
87             public CoapRequest answer(InvocationOnMock invocation)
88                     throws Throwable {
89                 Object[] args = invocation.getArguments();
90                 CoapRequest request = (CoapRequest) args[0];
91                 mReq = request;
92                 mLatch.countDown();
93                 System.out.println(
94                         "\t----------payload : " + request.getPayloadString());
95                 System.out.println(
96                         "\t----------uripath : " + request.getUriPath());
97                 System.out.println(
98                         "\t---------uriquery : " + request.getUriQuery());
99                 return null;
100             }
101         }).when(mRequestChannel).sendRequest(Mockito.any(IRequest.class),
102                 Mockito.any(CoapDevice.class));
103
104         PowerMockito.mockStatic(ConnectorPool.class);
105         PowerMockito.when(ConnectorPool.getConnection(Mockito.anyString())).thenReturn(mRequestChannel);
106     }
107
108     @Test
109     public void testAccountSignInOnRequestReceived() throws Exception {
110         System.out.println(
111                 "\t--------------OnRequestReceived Sign In Test------------");
112         IRequest request = makeSignInRequest();
113         mDeviceServerSystem.onRequestReceived(mMockDevice, request);
114         assertTrue(mLatch.await(1L, SECONDS));
115         assertEquals(request, request);
116     }
117
118     @Test
119     public void testAccountResourceOnRequestReceived() throws Exception {
120         System.out.println(
121                 "\t--------------OnRequestReceived Sign Out Test------------");
122         // sign up request from the client
123         IRequest request = makeSignOutRequest();
124         mDeviceServerSystem.onRequestReceived(mMockDevice, request);
125         // assertion : request msg to the AS is identical to the request msg
126         // from the client
127         assertTrue(mLatch.await(1L, SECONDS));
128         assertTrue(queryCheck(mReq, Constants.USER_ID));
129         assertTrue(payloadCheck(mReq, Constants.DEVICE_ID));
130         assertTrue(payloadCheck(mReq, Constants.ACCESS_TOKEN));
131         assertTrue(payloadCheck(mReq, Constants.REQ_LOGIN));
132     }
133
134     private IRequest makeSignInRequest() {
135         Cbor<HashMap<String, Object>> cbor = new Cbor<HashMap<String, Object>>();
136         IRequest request = null;
137         HashMap<String, Object> payloadData = new HashMap<>();
138         payloadData.put(Constants.USER_ID, "u0001");
139         payloadData.put(Constants.DEVICE_ID, mDi);
140         payloadData.put(Constants.ACCESS_TOKEN,
141                 "1689c70ffa245effc563017fee36d250");
142         payloadData.put(Constants.REQ_LOGIN, true);
143         request = MessageBuilder.createRequest(RequestMethod.POST, SESSION_URI,
144                 null, ContentFormat.APPLICATION_CBOR,
145                 cbor.encodingPayloadToCbor(payloadData));
146         return request;
147     }
148
149     private IRequest makeSignOutRequest() {
150         Cbor<HashMap<String, Object>> cbor = new Cbor<HashMap<String, Object>>();
151         IRequest request = null;
152         HashMap<String, Object> payloadData = new HashMap<>();
153         payloadData.put(Constants.REQ_LOGIN, false);
154         payloadData.put(Constants.DEVICE_ID, mDi);
155         payloadData.put(Constants.ACCESS_TOKEN,
156                 "1689c70ffa245effc563017fee36d250");
157         request = MessageBuilder.createRequest(RequestMethod.POST, SESSION_URI,
158                 null, ContentFormat.APPLICATION_CBOR,
159                 cbor.encodingPayloadToCbor(payloadData));
160         return request;
161     }
162
163     private boolean payloadCheck(IRequest request, String propertyName) {
164         Cbor<HashMap<String, Object>> mCbor = new Cbor<>();
165         HashMap<String, Object> payloadData = mCbor
166                 .parsePayloadFromCbor(request.getPayload(), HashMap.class);
167         if (payloadData.get(propertyName) != null)
168             return true;
169         else
170             return false;
171     }
172
173     private boolean queryCheck(IRequest request, String propertyName) {
174         if (request.getUriQueryMap().get(propertyName) != null)
175             return true;
176         else
177             return false;
178     }
179 }