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