Skip to main content
summaryrefslogtreecommitdiffstats
blob: ed547da583f2e6635047aefd975c19561c6c3841 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
/*******************************************************************************
 * Copyright (c) 2013 Boeing.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.account.rest.internal;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.mockito.Matchers.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.core.HttpHeaders;
import org.apache.commons.lang.math.RandomUtils;
import org.eclipse.osee.account.admin.AccessDetails;
import org.eclipse.osee.account.admin.Account;
import org.eclipse.osee.account.admin.AccountAdmin;
import org.eclipse.osee.account.admin.AccountLoginRequest;
import org.eclipse.osee.account.admin.AccountPreferences;
import org.eclipse.osee.account.admin.AccountSession;
import org.eclipse.osee.account.admin.CreateAccountRequest;
import org.eclipse.osee.account.rest.model.AccountActiveData;
import org.eclipse.osee.account.rest.model.AccountDetailsData;
import org.eclipse.osee.account.rest.model.AccountInfoData;
import org.eclipse.osee.account.rest.model.AccountInput;
import org.eclipse.osee.account.rest.model.AccountLoginData;
import org.eclipse.osee.account.rest.model.AccountPreferencesData;
import org.eclipse.osee.account.rest.model.AccountPreferencesInput;
import org.eclipse.osee.account.rest.model.AccountSessionData;
import org.eclipse.osee.account.rest.model.AccountSessionDetailsData;
import org.eclipse.osee.framework.jdk.core.type.Identifiable;
import org.eclipse.osee.framework.jdk.core.type.ResultSet;
import org.eclipse.osee.framework.jdk.core.type.ResultSets;
import org.eclipse.osee.framework.jdk.core.util.GUID;
import org.junit.Before;
import org.junit.Test;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.MockitoAnnotations;

/**
 * Test Case for {@link AccountOps}
 * 
 * @author Roberto E. Escobar
 */
public class AccountOpsTest {

   private static final String ACCOUNT_UID = "hello@hello.com";

   //@formatter:off
   @Mock private AccountAdmin accountAdmin;
   //@formatter:on

   private AccountOps ops;

   @Before
   public void setUp() {
      MockitoAnnotations.initMocks(this);

      ops = new AccountOps(accountAdmin);
   }

   @Test
   public void testCreateAccount() {
      String guid = GUID.create();
      String username = "aUser";

      String email = "hello@hello.com";
      String name = "myName";
      boolean active = true;

      Map<String, String> map = new HashMap<>();
      map.put("a", "1");
      map.put("b", "2");
      map.put("c", "3");

      AccountInput input = mock(AccountInput.class);
      when(input.getEmail()).thenReturn(email);
      when(input.getName()).thenReturn(name);
      when(input.isActive()).thenReturn(active);
      when(input.getPreferences()).thenReturn(map);

      ArgumentCaptor<CreateAccountRequest> captor = ArgumentCaptor.forClass(CreateAccountRequest.class);

      @SuppressWarnings("unchecked")
      Identifiable<String> id = mock(Identifiable.class);
      when(id.getGuid()).thenReturn(guid);

      when(accountAdmin.createAccount(any(CreateAccountRequest.class))).thenReturn(id);

      long accountId = 1982671L;
      Account account = mockAccount(accountId, guid, name, email, username, active);
      ResultSet<Account> result = ResultSets.singleton(account);
      when(accountAdmin.getAccountByUuid(guid)).thenReturn(result);

      AccountInfoData actual = ops.createAccount(username, input);

      verify(accountAdmin).createAccount(captor.capture());
      CreateAccountRequest request = captor.getValue();
      assertNotNull(request);
      assertAccount(actual, accountId, guid, name, email, username, active);
      assertEquals(name, request.getDisplayName());
      assertEquals(email, request.getEmail());
      assertEquals(username, request.getUserName());
      assertEquals(active, request.isActive());
      assertEquals(map, request.getPreferences());
   }

   @Test
   public void testDoLogin() {
      String ipAddress = "192.168.100.199";
      String userAgent = "my agent";

      String scheme = "myscheme";
      String username = "myuser";
      String password = "mypass";

      RequestInfo reqInfo = mock(RequestInfo.class);
      when(reqInfo.getDetails()).thenReturn(userAgent);
      when(reqInfo.getRemoteIpAddress()).thenReturn(ipAddress);

      AccountLoginData login = mock(AccountLoginData.class);
      when(login.getScheme()).thenReturn(scheme);
      when(login.getUsername()).thenReturn(username);
      when(login.getPassword()).thenReturn(password);

      Date d1 = newRandomDate();
      Date d2 = newRandomDate();
      AccountSession access = mockAccess(789L, "t3", d1, d2, ipAddress, userAgent);
      ArgumentCaptor<AccountLoginRequest> captor = ArgumentCaptor.forClass(AccountLoginRequest.class);

      when(accountAdmin.login(any(AccountLoginRequest.class))).thenReturn(access);

      AccountSessionData actual = ops.doLogin(reqInfo, login);

      verify(accountAdmin).login(captor.capture());
      AccountLoginRequest request = captor.getValue();
      AccessDetails details = request.getDetails();
      assertNotNull(details);
      assertEquals(789L, actual.getAccountId());
      assertEquals("t3", actual.getToken());

      assertEquals(scheme, request.getScheme());
      assertEquals(username, request.getUserName());
      assertEquals(password, request.getPassword());

      assertEquals(userAgent, details.getAccessDetails());
      assertEquals(ipAddress, details.getRemoteAddress());
   }

   @Test
   public void testDoLogout() {
      String token = "asdasa";

      when(accountAdmin.logout(token)).thenReturn(true);

      boolean actual = ops.doLogout(token);

      assertEquals(true, actual);
      verify(accountAdmin).logout(token);
   }

   @Test
   public void testDeleteAccount() {
      when(accountAdmin.deleteAccount(ACCOUNT_UID)).thenReturn(true);

      boolean actual = ops.deleteAccount(ACCOUNT_UID);

      assertEquals(true, actual);
      verify(accountAdmin).deleteAccount(ACCOUNT_UID);
   }

   @Test
   public void testAsRequestInfo() {
      String ipAddress = "192.168.100.199";
      String userAgent = "my agent";

      HttpServletRequest request = mock(HttpServletRequest.class);
      when(request.getHeader(HttpHeaders.USER_AGENT)).thenReturn(userAgent);
      when(request.getHeader("HTTP_X_FORWARDED_FOR")).thenReturn(ipAddress);

      RequestInfo actual = ops.asRequestInfo(request);

      assertEquals(userAgent, actual.getDetails());
      assertEquals(ipAddress, actual.getRemoteIpAddress());
   }

   @Test
   public void testGetAccountAccessById() {
      Date d1 = newRandomDate();
      Date d2 = newRandomDate();
      Date d3 = newRandomDate();
      Date d4 = newRandomDate();
      Date d5 = newRandomDate();
      Date d6 = newRandomDate();

      AccountSession access1 = mockAccess(123L, "t1", d1, d2, "f1", "d1");
      AccountSession access2 = mockAccess(456L, "t2", d3, d4, "f2", "d2");
      AccountSession access3 = mockAccess(789L, "t3", d5, d6, "f3", "d3");

      ResultSet<AccountSession> result = ResultSets.newResultSet(access1, access2, access3);
      when(accountAdmin.getAccountSessionByUniqueField(ACCOUNT_UID)).thenReturn(result);

      List<AccountSessionDetailsData> actual = ops.getAccountSessionById(ACCOUNT_UID);

      assertEquals(3, actual.size());
      Iterator<AccountSessionDetailsData> iterator = actual.iterator();
      assertAccess(iterator.next(), 123L, d1, d2, "f1", "d1");
      assertAccess(iterator.next(), 456L, d3, d4, "f2", "d2");
      assertAccess(iterator.next(), 789L, d5, d6, "f3", "d3");

      verify(accountAdmin).getAccountSessionByUniqueField(ACCOUNT_UID);
   }

   @Test
   public void testGetAccountData() {
      Account account = mockAccount(456L, "DEF", "acc2", "acc2@email.com", "u2", true);
      ResultSet<Account> accounts = ResultSets.singleton(account);
      when(accountAdmin.getAccountByUniqueField(ACCOUNT_UID)).thenReturn(accounts);

      AccountInfoData actual = ops.getAccountData(ACCOUNT_UID);

      assertAccount(actual, 456L, "DEF", "acc2", "acc2@email.com", "u2", true);
      verify(accountAdmin).getAccountByUniqueField(ACCOUNT_UID);
   }

   @Test
   public void testGetAccountDetailsData() {
      Account account = mockAccount(789L, "GHI", "acc3", "acc3@email.com", "u3", true);
      Map<String, String> map = new HashMap<>();
      map.put("a", "1");
      map.put("b", "2");
      map.put("c", "3");

      AccountPreferences preferences = mock(AccountPreferences.class);
      when(preferences.asMap()).thenReturn(map);
      when(preferences.getId()).thenReturn(789L);
      when(account.getPreferences()).thenReturn(preferences);

      ResultSet<Account> accounts = ResultSets.singleton(account);
      when(accountAdmin.getAccountByUniqueField(ACCOUNT_UID)).thenReturn(accounts);

      AccountDetailsData actual = ops.getAccountDetailsData(ACCOUNT_UID);

      assertAccount(actual, 789L, "GHI", "acc3", "acc3@email.com", "u3", true);
      AccountPreferencesData actualPrefs = actual.getPreferences();
      Map<String, String> actualMap = actualPrefs.getMap();

      assertEquals(789L, actualPrefs.getId());
      assertEquals(3, actualMap.size());
      assertEquals("1", actualMap.get("a"));
      assertEquals("2", actualMap.get("b"));
      assertEquals("3", actualMap.get("c"));

      verify(accountAdmin).getAccountByUniqueField(ACCOUNT_UID);
   }

   @Test
   public void testGetAccountPreferencesData() {
      Map<String, String> map = new HashMap<>();
      map.put("a", "1");
      map.put("b", "2");
      map.put("c", "3");

      AccountPreferences preferences = mock(AccountPreferences.class);
      when(preferences.asMap()).thenReturn(map);
      when(preferences.getId()).thenReturn(123L);

      Account account = mock(Account.class);
      when(account.getPreferences()).thenReturn(preferences);

      ResultSet<Account> accounts = ResultSets.singleton(account);
      when(accountAdmin.getAccountByUniqueField(ACCOUNT_UID)).thenReturn(accounts);

      AccountPreferencesData actual = ops.getAccountPreferencesData(ACCOUNT_UID);
      assertEquals(123L, actual.getId());

      Map<String, String> actualPrefs = actual.getMap();
      assertEquals(3, actualPrefs.size());
      assertEquals("1", actualPrefs.get("a"));
      assertEquals("2", actualPrefs.get("b"));
      assertEquals("3", actualPrefs.get("c"));

      verify(accountAdmin).getAccountByUniqueField(ACCOUNT_UID);
   }

   @Test
   public void testGetAllAccounts() {
      Account account1 = mockAccount(123L, "ABC", "acc1", "acc1@email.com", "u1", true);
      Account account2 = mockAccount(456L, "DEF", "acc2", "acc2@email.com", "u2", false);
      Account account3 = mockAccount(789L, "GHI", "acc3", "acc3@email.com", "u3", true);

      ResultSet<Account> accounts = ResultSets.newResultSet(account1, account2, account3);

      when(accountAdmin.getAllAccounts()).thenReturn(accounts);

      List<AccountInfoData> actual = ops.getAllAccounts();

      assertEquals(3, actual.size());
      Iterator<AccountInfoData> iterator = actual.iterator();

      assertAccount(iterator.next(), 123L, "ABC", "acc1", "acc1@email.com", "u1", true);
      assertAccount(iterator.next(), 456L, "DEF", "acc2", "acc2@email.com", "u2", false);
      assertAccount(iterator.next(), 789L, "GHI", "acc3", "acc3@email.com", "u3", true);

      verify(accountAdmin).getAllAccounts();
   }

   @Test
   public void testIsActive() {
      String guid = GUID.create();
      long accountId = 23127916023214L;

      Account account = mock(Account.class);
      ResultSet<Account> result = ResultSets.singleton(account);

      when(account.getGuid()).thenReturn(guid);
      when(account.getId()).thenReturn(accountId);
      when(account.isActive()).thenReturn(true);
      when(accountAdmin.getAccountByUniqueField(ACCOUNT_UID)).thenReturn(result);

      AccountActiveData actual = ops.isActive(ACCOUNT_UID);

      assertEquals(accountId, actual.getAccountId());
      assertEquals(guid, actual.getGuid());
      assertEquals(true, actual.isActive());
      verify(accountAdmin).getAccountByUniqueField(ACCOUNT_UID);
   }

   @Test
   public void testSetAccountActive() {
      when(accountAdmin.setActive(ACCOUNT_UID, true)).thenReturn(true);

      boolean actual = ops.setAccountActive(ACCOUNT_UID, true);

      assertEquals(true, actual);
      verify(accountAdmin).setActive(ACCOUNT_UID, true);
   }

   @Test
   public void testSetAccountPreferences() {
      Map<String, String> map = new HashMap<>();
      map.put("a", "1");
      map.put("b", "2");
      map.put("c", "3");

      AccountPreferencesInput input = mock(AccountPreferencesInput.class);
      when(input.getMap()).thenReturn(map);
      when(accountAdmin.setAccountPreferences(ACCOUNT_UID, map)).thenReturn(true);

      boolean actual = ops.setAccountPreferences(ACCOUNT_UID, input);

      assertEquals(true, actual);
      verify(accountAdmin).setAccountPreferences(ACCOUNT_UID, map);
   }

   private static Date newRandomDate() {
      return new Date(Math.abs(System.currentTimeMillis() - RandomUtils.nextLong()));
   }

   private static AccountSession mockAccess(long id, String token, Date created, Date accessed, String accessFrom, String accessDetails) {
      AccountSession access = Mockito.mock(AccountSession.class);
      when(access.getAccountId()).thenReturn(id);
      when(access.getSessionToken()).thenReturn(token);
      when(access.getCreatedOn()).thenReturn(created);
      when(access.getLastAccessedOn()).thenReturn(accessed);
      when(access.getAccessedFrom()).thenReturn(accessFrom);
      when(access.getAccessDetails()).thenReturn(accessDetails);
      return access;
   }

   private static void assertAccess(AccountSessionDetailsData actual, long id, Date created, Date accessed, String accessFrom, String accessDetails) {
      assertEquals(accessDetails, actual.getAccessDetails());
      assertEquals(accessFrom, actual.getAccessedFrom());
      assertEquals(id, actual.getAccountId());
      assertEquals(created, actual.getCreatedOn());
      assertEquals(accessed, actual.getLastAccessedOn());
   }

   private static Account mockAccount(long id, String uuid, String name, String email, String username, boolean active) {
      Account account = Mockito.mock(Account.class);
      when(account.getId()).thenReturn(id);
      when(account.getGuid()).thenReturn(uuid);
      when(account.getName()).thenReturn(name);
      when(account.getEmail()).thenReturn(email);
      when(account.getUserName()).thenReturn(username);
      when(account.isActive()).thenReturn(active);
      return account;
   }

   private static void assertAccount(AccountInfoData data, long id, String uuid, String name, String email, String username, boolean active) {
      assertEquals(id, data.getAccountId());
      assertEquals(uuid, data.getGuid());
      assertEquals(name, data.getName());
      assertEquals(email, data.getEmail());
      assertEquals(username, data.getUserName());
      assertEquals(active, data.isActive());
   }
}

Back to the top