f0b04f61b901ce6725f3511f688364e74fbd1a07
[iotivity.git] / cloud / account / src / test / java / org / iotivity / cloud / accountserver / resources / account / tokenrefresh / TokenRefreshResourceTest.java
1 package org.iotivity.cloud.accountserver.resources.account.tokenrefresh;
2
3 import static org.mockito.Mockito.mock;
4
5 import java.text.DateFormat;
6 import java.text.SimpleDateFormat;
7 import java.util.Date;
8 import java.util.HashMap;
9
10 import org.iotivity.cloud.accountserver.Constants;
11 import org.iotivity.cloud.accountserver.db.AccountDBManager;
12 import org.iotivity.cloud.accountserver.db.TokenTable;
13 import org.iotivity.cloud.accountserver.db.UserTable;
14 import org.iotivity.cloud.accountserver.util.TypeCastingManager;
15 import org.iotivity.cloud.base.device.CoapDevice;
16 import org.iotivity.cloud.base.exception.ServerException;
17 import org.iotivity.cloud.base.protocols.IRequest;
18 import org.iotivity.cloud.base.protocols.MessageBuilder;
19 import org.iotivity.cloud.base.protocols.coap.CoapResponse;
20 import org.iotivity.cloud.base.protocols.enums.ContentFormat;
21 import org.iotivity.cloud.base.protocols.enums.RequestMethod;
22 import org.iotivity.cloud.util.Cbor;
23 import org.junit.Before;
24 import org.junit.Test;
25 import org.mockito.Mockito;
26 import org.mockito.MockitoAnnotations;
27 import org.mockito.invocation.InvocationOnMock;
28 import org.mockito.stubbing.Answer;
29
30 public class TokenRefreshResourceTest {
31     private static final String            REFRESH_TOKEN_URI         = "/.well-known/ocf/account/tokenrefresh";
32     private static final String            DEVICE_ID                 = "B371C481-38E6-4D47-8320-7688D8A5B58C";
33     private String                         mAuthProvider             = "Samsung";
34     private String                         mRefreshToken             = "rt0001";
35     private String                         mUuid                     = "u0001";
36     private String                         mUserId                   = "userId";
37     private Cbor<HashMap<String, String>>  mCbor                     = new Cbor<HashMap<String, String>>();
38     private CoapDevice                     mockDevice                = mock(
39             CoapDevice.class);
40     private TypeCastingManager<UserTable>  mUserTableCastingManager  = new TypeCastingManager<>();
41     private TypeCastingManager<TokenTable> mTokenTableCastingManager = new TypeCastingManager<>();
42     private TokenRefreshResource           mTokenRefreshResource     = new TokenRefreshResource();
43
44     @Before
45     public void setUp() throws Exception {
46         MockitoAnnotations.initMocks(this);
47         // callback mock
48         Mockito.doAnswer(new Answer<Object>() {
49             @Override
50             public CoapResponse answer(InvocationOnMock invocation)
51                     throws Throwable {
52                 Object[] args = invocation.getArguments();
53                 CoapResponse resp = (CoapResponse) args[0];
54                 System.out
55                         .println("\t----payload : " + resp.getPayloadString());
56                 System.out
57                         .println("\t----responsestatus : " + resp.getStatus());
58                 return resp;
59             }
60         }).when(mockDevice).sendResponse(Mockito.anyObject());
61     }
62
63     @Test(expected = ServerException.NotFoundException.class)
64     public void testRefreshTokenonRequestReceivedWrongRefreshToken()
65             throws Exception {
66         String uuid = this.mUuid + "WrongRefreshTokenCase";
67         RegisterTokenInfo(uuid, mUserId, mAuthProvider, mRefreshToken);
68         TokenRefresh(RequestMethod.POST, mockDevice, uuid, DEVICE_ID,
69                 mRefreshToken + "NotExist");
70     }
71
72     @Test(expected = ServerException.BadRequestException.class)
73     public void testRefreshTokenonRequestReceivedInvalidMethod()
74             throws Exception {
75         String uuid = this.mUuid + "InvalidRequestMethod (GET)";
76         RegisterTokenInfo(uuid, mUserId, mAuthProvider, mRefreshToken);
77         TokenRefresh(RequestMethod.GET, mockDevice, uuid, DEVICE_ID,
78                 mRefreshToken + "InvalidMethod");
79     }
80
81     @Test(expected = ServerException.PreconditionFailedException.class)
82     public void testRefreshTokenonRequestReceivedNullUuid() throws Exception {
83         String uuid = this.mUuid + "NullUuid";
84         RegisterTokenInfo(uuid, mUserId, mAuthProvider, mRefreshToken);
85         TokenRefresh(RequestMethod.POST, mockDevice, null, DEVICE_ID,
86                 mRefreshToken + "InvalidMethod");
87     }
88
89     @Test(expected = ServerException.PreconditionFailedException.class)
90     public void testRefreshTokenonRequestReceivedNullDi() throws Exception {
91         String uuid = this.mUuid + "NullDi";
92         RegisterTokenInfo(uuid, mUserId, mAuthProvider, mRefreshToken);
93         TokenRefresh(RequestMethod.POST, mockDevice, uuid, null,
94                 mRefreshToken + "InvalidMethod");
95     }
96
97     @Test(expected = ServerException.PreconditionFailedException.class)
98     public void testRefreshTokenonRequestReceivedNullRefreshToken()
99             throws Exception {
100         String uuid = this.mUuid + "NullRefreshToken";
101         RegisterTokenInfo(uuid, mUserId, mAuthProvider, mRefreshToken);
102         TokenRefresh(RequestMethod.POST, mockDevice, uuid, DEVICE_ID, null);
103     }
104
105     public void TokenRefresh(RequestMethod method, CoapDevice device,
106             String uuid, String di, String refreshToken) throws Exception {
107         System.out.println(
108                 "-----Token Refresh using refreshtoken : " + refreshToken);
109         IRequest request = null;
110         request = RefreshTokenRequest(method, uuid, di, refreshToken);
111         mTokenRefreshResource.onDefaultRequestReceived(device, request);
112     }
113
114     public IRequest RefreshTokenRequest(RequestMethod method, String uuid,
115             String deviceId, String refreshToken) {
116         IRequest request = null;
117         HashMap<String, String> payloadData = new HashMap<String, String>();
118         payloadData.put("uid", uuid);
119         payloadData.put("di", deviceId);
120         payloadData.put("granttype", "refresh_token");
121         payloadData.put("refreshtoken", refreshToken);
122         request = MessageBuilder.createRequest(method, REFRESH_TOKEN_URI, null,
123                 ContentFormat.APPLICATION_CBOR,
124                 mCbor.encodingPayloadToCbor(payloadData));
125         return request;
126     }
127
128     public void RegisterTokenInfo(String uuid, String userId,
129             String accessToken, String refreshToken) {
130         HashMap<String, Object> tokenInfo = mTokenTableCastingManager
131                 .convertObjectToMap(
132                         makeTokenTable(uuid, accessToken, refreshToken));
133         HashMap<String, Object> userInfo = mUserTableCastingManager
134                 .convertObjectToMap(makeUserTable(uuid, userId));
135         AccountDBManager.getInstance()
136                 .insertAndReplaceRecord(Constants.TOKEN_TABLE, tokenInfo);
137         AccountDBManager.getInstance().insertRecord(Constants.USER_TABLE,
138                 userInfo);
139     }
140
141     private TokenTable makeTokenTable(String uuid, String accessToken,
142             String refreshToken) {
143         TokenTable tokenInfo = new TokenTable();
144         tokenInfo.setUuid(uuid);
145         tokenInfo.setDid(DEVICE_ID);
146         tokenInfo.setAccesstoken(accessToken);
147         tokenInfo.setRefreshtoken(refreshToken);
148         tokenInfo.setProvider(mAuthProvider);
149         tokenInfo.setExpiredtime(-1);
150         Date currentTime = new Date();
151         DateFormat transFormat = new SimpleDateFormat("yyyyMMddkkmm");
152         tokenInfo.setIssuedtime(transFormat.format(currentTime));
153         return tokenInfo;
154     }
155
156     private UserTable makeUserTable(String uuid, String userId) {
157         UserTable userInfo = new UserTable();
158         userInfo.setUuid(uuid);
159         userInfo.setProvider(mAuthProvider);
160         userInfo.setUserid(userId);
161         return userInfo;
162     }
163 }