0726aad87f2c6715b48025dfee42e27858b801d5
[iotivity.git] / cloud / interface / src / test / java / org / iotivity / cloud / ciserver / resources / proxy / account / AccountTest.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.assertFalse;
28 import static org.junit.Assert.assertTrue;
29 import static org.mockito.Mockito.mock;
30
31 import java.util.HashMap;
32 import java.util.concurrent.CountDownLatch;
33
34 import org.iotivity.cloud.base.connector.ConnectorPool;
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.base.protocols.enums.ResponseStatus;
43 import org.iotivity.cloud.ciserver.Constants;
44 import org.iotivity.cloud.ciserver.DeviceServerSystem;
45 import org.iotivity.cloud.util.Cbor;
46 import org.junit.Before;
47 import org.junit.Test;
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
55 public class AccountTest {
56     private String               mDi                 = "B371C481-38E6-4D47-8320-7688D8A5B58C";
57     public static final String   ACCOUNT_URI         = Constants.ACCOUNT_FULL_URI;
58     private String               mAuthProvider       = "github";
59     private String               mAuthCode           = "f6b55d8d8a27e0d32ab8";
60     private CoapDevice           mMockDevice         = mock(CoapDevice.class);
61     private IRequest             mReqRDServer        = null;
62     private IRequest             mReqASServer        = null;
63     ConnectorPool                connectorPool       = null;
64     private DeviceServerSystem   mDeviceServerSystem = new DeviceServerSystem();
65     private final CountDownLatch mLatch              = new CountDownLatch(1);
66     @Mock(name = "mRDServer")
67     IRequestChannel              mRequestChannelRDServer;
68     @Mock(name = "mASServer")
69     IRequestChannel              mRequestChannelASServer;
70
71     @InjectMocks
72     private Account              mAcHandler          = new Account();
73
74     @Before
75     public void setUp() throws Exception {
76         Mockito.doReturn("mockDeviceUser").when(mMockDevice).getUserId();
77         MockitoAnnotations.initMocks(this);
78         mDeviceServerSystem.addResource(mAcHandler);
79
80         Mockito.doAnswer(new Answer<Object>() {
81             @Override
82             public CoapRequest answer(InvocationOnMock invocation)
83                     throws Throwable {
84                 Object[] args = invocation.getArguments();
85                 CoapRequest request = (CoapRequest) args[0];
86                 mReqRDServer = request;
87                 mLatch.countDown();
88                 System.out.println(
89                         "\t----------requestMethod : " + request.getMethod());
90                 System.out.println(
91                         "\t----------payload : " + request.getPayloadString());
92                 System.out.println(
93                         "\t----------uripath : " + request.getUriPath());
94                 System.out.println(
95                         "\t---------uriquery : " + request.getUriQuery());
96                 return null;
97             }
98         }).when(mRequestChannelRDServer).sendRequest(
99                 Mockito.any(IRequest.class), Mockito.any(CoapDevice.class));
100
101         Mockito.doAnswer(new Answer<Object>() {
102             @Override
103             public CoapRequest answer(InvocationOnMock invocation)
104                     throws Throwable {
105                 Object[] args = invocation.getArguments();
106                 CoapRequest request = (CoapRequest) args[0];
107                 mReqASServer = request;
108                 mLatch.countDown();
109                 System.out.println(
110                         "\t----------requestMethod : " + request.getMethod());
111                 System.out.println(
112                         "\t----------payload : " + request.getPayloadString());
113                 System.out.println(
114                         "\t----------uripath : " + request.getUriPath());
115                 System.out.println(
116                         "\t---------uriquery : " + request.getUriQuery());
117                 return null;
118             }
119         }).when(mRequestChannelASServer).sendRequest(
120                 Mockito.any(IRequest.class), Mockito.any(CoapDevice.class));
121     }
122
123     @Test
124     public void testAccountDeviceDeleteOnRequestReceived() throws Exception {
125         getTestMethodName();
126         IRequest request = null;
127         request = MessageBuilder.createRequest(RequestMethod.DELETE,
128                 ACCOUNT_URI, "di=device1");
129         mDeviceServerSystem.onRequestReceived(mMockDevice, request);
130         assertTrue(mLatch.await(1L, SECONDS));
131         assertTrue(mReqASServer.getUriQueryMap().get("uid")
132                 .contains("mockDeviceUser"));
133         assertTrue(mReqASServer.getUriQueryMap().get("di").contains("device1"));
134         assertTrue(mReqASServer.getUriPath().equals(ACCOUNT_URI));
135     }
136
137     IRequest                      asRequest             = MessageBuilder
138             .createRequest(RequestMethod.DELETE, ACCOUNT_URI, "di=device1");
139
140     @InjectMocks
141     Account.AccountReceiveHandler accountReceiveHandler = mAcHandler.new AccountReceiveHandler(
142             asRequest, mMockDevice);
143
144     @Test
145     public void testAccountDeviceDeleteOnResponseReceived() throws Exception {
146         getTestMethodName();
147         IRequest request = MessageBuilder.createRequest(RequestMethod.DELETE,
148                 ACCOUNT_URI, "di=device1");
149
150         accountReceiveHandler.onResponseReceived(
151                 MessageBuilder.createResponse(request, ResponseStatus.DELETED));
152         assertTrue(mLatch.await(1L, SECONDS));
153         assertTrue(mReqRDServer.getUriQueryMap().get("di").contains("device1"));
154         assertTrue(mReqRDServer.getUriPath()
155                 .contains(Constants.PREFIX_OIC + "/" + Constants.RD_URI));
156     }
157
158     @Test
159     public void testAccountSearchSingleUserOnResponseReceived()
160             throws Exception {
161         getTestMethodName();
162         IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
163                 Constants.ACCOUNT_SEARCH_FULL_URI,
164                 "search=userid:user1,userid:user2,userid:user3");
165
166         mDeviceServerSystem.onRequestReceived(mMockDevice, request);
167         assertTrue(mReqASServer.getUriPath()
168                 .equals(Constants.ACCOUNT_SEARCH_FULL_URI));
169         assertFalse(
170                 mReqASServer.getUriQueryMap().containsKey(Constants.USER_ID));
171     }
172
173     @Test
174     public void testAccountSearchNoQueryOnResponseReceived() throws Exception {
175         getTestMethodName();
176         IRequest request = MessageBuilder.createRequest(RequestMethod.GET,
177                 Constants.ACCOUNT_SEARCH_FULL_URI, null);
178
179         mDeviceServerSystem.onRequestReceived(mMockDevice, request);
180         assertTrue(mReqASServer.getUriPath()
181                 .equals(Constants.ACCOUNT_SEARCH_FULL_URI));
182         assertTrue(
183                 mReqASServer.getUriQueryMap().containsKey(Constants.USER_ID));
184     }
185
186     @Test
187     public void testAccountResourceOnRequestReceived() throws Exception {
188         getTestMethodName();
189         // sign up request from the client
190         IRequest request = null;
191         HashMap<String, String> payloadData = new HashMap<String, String>();
192         payloadData.put(Constants.AUTH_CODE, mAuthCode);
193         payloadData.put(Constants.AUTH_PROVIDER, mAuthProvider);
194         payloadData.put(Constants.DEVICE_ID, mDi);
195         Cbor<HashMap<String, String>> cbor = new Cbor<HashMap<String, String>>();
196         request = MessageBuilder.createRequest(RequestMethod.POST, ACCOUNT_URI,
197                 null, ContentFormat.APPLICATION_CBOR,
198                 cbor.encodingPayloadToCbor(payloadData));
199         mDeviceServerSystem.onRequestReceived(mMockDevice, request);
200         // assertion : request msg to the AS is identical to the request msg
201         // from the client
202         assertTrue(mLatch.await(1L, SECONDS));
203         assertEquals(request, mReqASServer);
204     }
205
206     private void getTestMethodName() {
207         StackTraceElement[] stacks = new Throwable().getStackTrace();
208         StackTraceElement currentStack = stacks[1];
209         System.out.println("\t---Test Name : " + currentStack.getMethodName());
210     }
211 }