a86b5605ea39ef3c12cdd74a3ba29160d7b4887b
[iotivity.git] / cloud / interface / src / test / java / org / iotivity / cloud / ciserver / resources / proxy / account / AccountSessionTest.java
1 package org.iotivity.cloud.ciserver.resources.proxy.account;
2
3 import static java.util.concurrent.TimeUnit.SECONDS;
4 import static org.junit.Assert.assertEquals;
5 import static org.junit.Assert.assertTrue;
6 import static org.mockito.Mockito.mock;
7
8 import java.util.HashMap;
9 import java.util.concurrent.CountDownLatch;
10
11 import org.iotivity.cloud.base.connector.ConnectorPool;
12 import org.iotivity.cloud.base.device.CoapDevice;
13 import org.iotivity.cloud.base.device.IRequestChannel;
14 import org.iotivity.cloud.base.protocols.IRequest;
15 import org.iotivity.cloud.base.protocols.IResponse;
16 import org.iotivity.cloud.base.protocols.MessageBuilder;
17 import org.iotivity.cloud.base.protocols.coap.CoapRequest;
18 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
19 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
20 import org.iotivity.cloud.ciserver.Constants;
21 import org.iotivity.cloud.ciserver.DeviceServerSystem;
22 import org.iotivity.cloud.util.Cbor;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.InjectMocks;
26 import org.mockito.Mock;
27 import org.mockito.Mockito;
28 import org.mockito.MockitoAnnotations;
29 import org.mockito.invocation.InvocationOnMock;
30 import org.mockito.stubbing.Answer;
31
32 public class AccountSessionTest {
33     private String             di                 = "B371C481-38E6-4D47-8320-7688D8A5B58C";
34     public static final String SESSION_URI        = Constants.ACCOUNT_SESSION_FULL_URI;
35     private CoapDevice         mockDevice         = mock(CoapDevice.class);
36     IResponse                  res                = null;
37     IRequest                   req                = null;
38     ConnectorPool              connectorPool      = null;
39     DeviceServerSystem         deviceServerSystem = new DeviceServerSystem();
40     final CountDownLatch       latch              = new CountDownLatch(1);
41     @Mock
42     IRequestChannel            requestChannel;
43
44     @InjectMocks
45     AccountSession             acSessionHandler   = new AccountSession();
46
47     @Before
48     public void setUp() throws Exception {
49         res = null;
50         req = null;
51         Mockito.doReturn(di).when(mockDevice).getDeviceId();
52         Mockito.doReturn("mockDeviceUser").when(mockDevice).getUserId();
53         Mockito.doReturn("1689c70ffa245effc563017fee36d250").when(mockDevice)
54                 .getAccessToken();
55         MockitoAnnotations.initMocks(this);
56         deviceServerSystem.addResource(acSessionHandler);
57         Mockito.doAnswer(new Answer<Object>() {
58             @Override
59             public CoapRequest answer(InvocationOnMock invocation)
60                     throws Throwable {
61                 Object[] args = invocation.getArguments();
62                 CoapRequest request = (CoapRequest) args[0];
63                 req = request;
64                 latch.countDown();
65                 System.out.println(
66                         "\t----------payload : " + request.getPayloadString());
67                 System.out.println(
68                         "\t----------uripath : " + request.getUriPath());
69                 System.out.println(
70                         "\t---------uriquery : " + request.getUriQuery());
71                 return null;
72             }
73         }).when(requestChannel).sendRequest(Mockito.any(IRequest.class),
74                 Mockito.any(CoapDevice.class));
75     }
76
77     @Test
78     public void testAccountSignInOnRequestReceived() throws Exception {
79         System.out.println(
80                 "\t--------------OnRequestReceived Sign In Test------------");
81         IRequest request = makeSignInRequest();
82         deviceServerSystem.onRequestReceived(mockDevice, request);
83         assertTrue(latch.await(1L, SECONDS));
84         assertEquals(request, request);
85     }
86
87     @Test
88     public void testAccountResourceOnRequestReceived() throws Exception {
89         System.out.println(
90                 "\t--------------OnRequestReceived Sign Out Test------------");
91         // sign up request from the client
92         IRequest request = makeSignOutRequest();
93         deviceServerSystem.onRequestReceived(mockDevice, request);
94         // assertion : request msg to the AS is identical to the request msg
95         // from the client
96         assertTrue(latch.await(1L, SECONDS));
97         assertTrue(hashmapCheck(req, "uid"));
98         assertTrue(hashmapCheck(req, "di"));
99         assertTrue(hashmapCheck(req, "accesstoken"));
100         assertTrue(hashmapCheck(req, "login"));
101     }
102
103     private IRequest makeSignInRequest() {
104         Cbor<HashMap<String, Object>> cbor = new Cbor<HashMap<String, Object>>();
105         IRequest request = null;
106         HashMap<String, Object> payloadData = new HashMap<>();
107         payloadData.put("uid", "u0001");
108         payloadData.put("di", di);
109         payloadData.put("accesstoken", "1689c70ffa245effc563017fee36d250");
110         payloadData.put("login", true);
111         request = MessageBuilder.createRequest(RequestMethod.POST, SESSION_URI,
112                 null, ContentFormat.APPLICATION_CBOR,
113                 cbor.encodingPayloadToCbor(payloadData));
114         return request;
115     }
116
117     private IRequest makeSignOutRequest() {
118         Cbor<HashMap<String, Object>> cbor = new Cbor<HashMap<String, Object>>();
119         IRequest request = null;
120         HashMap<String, Object> payloadData = new HashMap<>();
121         payloadData.put("login", false);
122         request = MessageBuilder.createRequest(RequestMethod.POST, SESSION_URI,
123                 null, ContentFormat.APPLICATION_CBOR,
124                 cbor.encodingPayloadToCbor(payloadData));
125         return request;
126     }
127
128     private boolean hashmapCheck(IRequest request, String propertyName) {
129         Cbor<HashMap<String, Object>> mCbor = new Cbor<>();
130         HashMap<String, Object> payloadData = mCbor
131                 .parsePayloadFromCbor(request.getPayload(), HashMap.class);
132         if (payloadData.get(propertyName) != null)
133             return true;
134         else
135             return false;
136     }
137 }