Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 44 additions & 49 deletions Parse/src/test/java/com/parse/ParseUserTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.junit.runner.RunWith;
import org.mockito.Matchers;
import org.robolectric.RobolectricGradleTestRunner;
import org.robolectric.annotation.Config;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.Semaphore;
Expand All @@ -34,7 +35,6 @@
import static org.junit.Assert.assertTrue;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.anyBoolean;
import static org.mockito.Matchers.anyMap;
import static org.mockito.Matchers.anyString;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doReturn;
Expand Down Expand Up @@ -86,7 +86,7 @@ public void testImmutableKeys() {
}

try {
user.removeAll("sessionToken", Arrays.asList());
user.removeAll("sessionToken", Collections.emptyList());
} catch (IllegalArgumentException e) {
assertTrue(e.getMessage().contains("Cannot modify"));
}
Expand Down Expand Up @@ -165,12 +165,12 @@ public void testSignUpAsyncWithObjectIdSetAndAuthDataSet() throws Exception {
ParseUser partialMockUser = spy(user);
doReturn(Task.<Void>forResult(null))
.when(partialMockUser)
.saveAsync(anyString(), any(Task.class));
.saveAsync(anyString(), Matchers.<Task<Void>>any());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the warning and why use Matchers will solve the warning?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Incorrect generic since saveAsync requires Task<Void> but any(Task.class) is equivalent to Task.


ParseTaskUtils.wait(partialMockUser.signUpAsync(Task.<Void>forResult(null)));

// Verify user is saved
verify(partialMockUser, times(1)).saveAsync(eq("sessionToken"), any(Task.class));
verify(partialMockUser, times(1)).saveAsync(eq("sessionToken"), Matchers.<Task<Void>>any());
}

@Test
Expand Down Expand Up @@ -220,7 +220,7 @@ public void testSignUpAsyncWithMergeInDiskAnonymousUser() throws Exception {
when(currentUser.isLinked(ParseAnonymousUtils.AUTH_TYPE)).thenReturn(true);
when(currentUser.getSessionToken()).thenReturn("oldSessionToken");
when(currentUser.getAuthData()).thenReturn(new HashMap<String, Map<String, String>>());
when(currentUser.saveAsync(anyString(), any(Task.class)))
when(currentUser.saveAsync(anyString(), Matchers.<Task<Void>>any()))
.thenReturn(Task.<Void>forResult(null));
ParseUser.State state = new ParseUser.State.Builder()
.put("oldKey", "oldValue")
Expand Down Expand Up @@ -248,7 +248,7 @@ public void testSignUpAsyncWithMergeInDiskAnonymousUser() throws Exception {
verify(currentUser, times(1)).setUsername("userName");
verify(currentUser, times(1)).setPassword("password");
// Make sure we save currentUser
verify(currentUser, times(1)).saveAsync(eq("oldSessionToken"), any(Task.class));
verify(currentUser, times(1)).saveAsync(eq("oldSessionToken"), Matchers.<Task<Void>>any());
// Make sure we merge currentUser with user after save
assertEquals("oldValue", user.get("oldKey"));
// Make sure set currentUser
Expand All @@ -269,7 +269,7 @@ public void testSignUpAsyncWithMergeInDiskAnonymousUserSaveFailure() throws Exce
ParseException saveException = new ParseException(ParseException.OTHER_CAUSE, "");
doReturn(Task.<Void>forError(saveException))
.when(partialMockCurrentUser)
.saveAsync(anyString(), any(Task.class));
.saveAsync(anyString(), Matchers.<Task<Void>>any());
ParseCurrentUserController currentUserController = mock(ParseCurrentUserController.class);
when(currentUserController.getAsync(anyBoolean()))
.thenReturn(Task.forResult(partialMockCurrentUser));
Expand All @@ -293,7 +293,8 @@ public void testSignUpAsyncWithMergeInDiskAnonymousUserSaveFailure() throws Exce
// Make sure we sync user with currentUser
verify(partialMockCurrentUser, times(1)).copyChangesFrom(eq(user));
// Make sure we save currentUser
verify(partialMockCurrentUser, times(1)).saveAsync(eq("oldSessionToken"), any(Task.class));
verify(partialMockCurrentUser, times(1))
.saveAsync(eq("oldSessionToken"), Matchers.<Task<Void>>any());
// Make sure we restore old username and password after save fails
verify(partialMockCurrentUser, times(1)).setUsername("oldUserName");
verify(partialMockCurrentUser, times(1)).setPassword("oldPassword");
Expand Down Expand Up @@ -388,23 +389,22 @@ public void testLoginWithAsyncWithLinkedLazyUser() throws Exception {
when(partialMockCurrentUser.getSessionToken()).thenReturn("oldSessionToken");
doReturn(Task.<ParseUser>forResult(null))
.when(partialMockCurrentUser)
.resolveLazinessAsync(any(Task.class));
.resolveLazinessAsync(Matchers.<Task<Void>>any());
ParseCurrentUserController currentUserController = mock(ParseCurrentUserController.class);
when(currentUserController.getAsync()).thenReturn(Task.forResult(partialMockCurrentUser));
ParseCorePlugins.getInstance().registerCurrentUserController(currentUserController);

ParseUser user = new ParseUser();
String authType = "facebook";
Map<String, String> authData = new HashMap<>();
authData.put("token", "123");
ParseUser userAfterLogin = ParseTaskUtils.wait(user.logInWithAsync(authType, authData));
ParseUser userAfterLogin = ParseTaskUtils.wait(ParseUser.logInWithAsync(authType, authData));

// Make sure we stripAnonymity
assertNull(userAfterLogin.getAuthData().get(ParseAnonymousUtils.AUTH_TYPE));
// Make sure we update authData
assertEquals(authData, userAfterLogin.getAuthData().get("facebook"));
// Make sure we resolveLaziness
verify(partialMockCurrentUser, times(1)).resolveLazinessAsync(any(Task.class));
verify(partialMockCurrentUser, times(1)).resolveLazinessAsync(Matchers.<Task<Void>>any());
}

@Test
Expand All @@ -418,21 +418,20 @@ public void testLoginWithAsyncWithLinkedLazyUseAndResolveLazinessFailure() throw
when(partialMockCurrentUser.getSessionToken()).thenReturn("oldSessionToken");
doReturn(Task.<ParseUser>forError(new Exception()))
.when(partialMockCurrentUser)
.resolveLazinessAsync(any(Task.class));
.resolveLazinessAsync(Matchers.<Task<Void>>any());
ParseCurrentUserController currentUserController = mock(ParseCurrentUserController.class);
when(currentUserController.getAsync()).thenReturn(Task.forResult(partialMockCurrentUser));
ParseCorePlugins.getInstance().registerCurrentUserController(currentUserController);

ParseUser user = new ParseUser();
String authType = "facebook";
Map<String, String> authData = new HashMap<>();
authData.put("token", "123");

Task<ParseUser> loginTask = user.logInWithAsync(authType, authData);
Task<ParseUser> loginTask = ParseUser.logInWithAsync(authType, authData);
loginTask.waitForCompletion();

// Make sure we try to resolveLaziness
verify(partialMockCurrentUser, times(1)).resolveLazinessAsync(any(Task.class));
verify(partialMockCurrentUser, times(1)).resolveLazinessAsync(Matchers.<Task<Void>>any());
// Make sure we do not save new authData
assertNull(partialMockCurrentUser.getAuthData().get("facebook"));
// Make sure we restore anonymity after resolve laziness failure
Expand All @@ -452,17 +451,16 @@ public void testLoginWithAsyncWithLinkedNotLazyUser() throws Exception {
when(partialMockCurrentUser.getSessionToken()).thenReturn("sessionToken");
doReturn(Task.<Void>forResult(null))
.when(partialMockCurrentUser)
.linkWithAsync(anyString(), anyMap(), anyString());
.linkWithAsync(anyString(), Matchers.<Map<String, String>>any(), anyString());
ParseCurrentUserController currentUserController = mock(ParseCurrentUserController.class);
when(currentUserController.getAsync()).thenReturn(Task.forResult(partialMockCurrentUser));
ParseCorePlugins.getInstance().registerCurrentUserController(currentUserController);

ParseUser user = new ParseUser();
String authType = "facebook";
Map<String, String> authData = new HashMap<>();
authData.put("token", "123");

ParseUser userAfterLogin = ParseTaskUtils.wait(user.logInWithAsync(authType, authData));
ParseUser userAfterLogin = ParseTaskUtils.wait(ParseUser.logInWithAsync(authType, authData));

// Make sure we link authData
verify(partialMockCurrentUser, times(1)).linkWithAsync(
Expand All @@ -478,7 +476,8 @@ public void testLoginWithAsyncWithLinkedNotLazyUserLinkFailure() throws Exceptio
.put("newKey", "newValue")
.sessionToken("newSessionToken")
.build();
when(userController.logInAsync(anyString(), anyMap())).thenReturn(Task.forResult(newUserState));
when(userController.logInAsync(anyString(), Matchers.<Map<String, String>>any()))
.thenReturn(Task.forResult(newUserState));
ParseCorePlugins.getInstance().registerUserController(userController);
// Register a mock currentUserController to make getCurrentUser work
ParseUser currentUser = new ParseUser();
Expand All @@ -490,19 +489,18 @@ public void testLoginWithAsyncWithLinkedNotLazyUserLinkFailure() throws Exceptio
new ParseException(ParseException.ACCOUNT_ALREADY_LINKED, "Account already linked");
doReturn(Task.<Void>forError(linkException))
.when(partialMockCurrentUser)
.linkWithAsync(anyString(), anyMap(), anyString());
.linkWithAsync(anyString(), Matchers.<Map<String, String>>any(), anyString());
ParseCurrentUserController currentUserController = mock(ParseCurrentUserController.class);
when(currentUserController.getAsync()).thenReturn(Task.forResult(partialMockCurrentUser));
when(currentUserController.setAsync(any(ParseUser.class)))
.thenReturn(Task.<Void>forResult(null));
ParseCorePlugins.getInstance().registerCurrentUserController(currentUserController);


ParseUser user = new ParseUser();
String authType = "facebook";
Map<String, String> authData = new HashMap<>();
authData.put("token", "123");
ParseUser userAfterLogin = ParseTaskUtils.wait(user.logInWithAsync(authType, authData));
ParseUser userAfterLogin = ParseTaskUtils.wait(ParseUser.logInWithAsync(authType, authData));

// Make sure we link authData
verify(partialMockCurrentUser, times(1)).linkWithAsync(
Expand All @@ -524,22 +522,21 @@ public void testLoginWithAsyncWithNoCurrentUser() throws Exception {
.put("newKey", "newValue")
.sessionToken("newSessionToken")
.build();
when(userController.logInAsync(anyString(), anyMap())).thenReturn(Task.forResult(newUserState));
when(userController.logInAsync(anyString(), Matchers.<Map<String, String>>any()))
.thenReturn(Task.forResult(newUserState));
ParseCorePlugins.getInstance().registerUserController(userController);
// Register a mock currentUserController to make getCurrentUser work
ParseUser currentUser = new ParseUser();
ParseCurrentUserController currentUserController = mock(ParseCurrentUserController.class);
when(currentUserController.getAsync()).thenReturn(Task.<ParseUser>forResult(null));
when(currentUserController.setAsync(any(ParseUser.class)))
.thenReturn(Task.<Void>forResult(null));
ParseCorePlugins.getInstance().registerCurrentUserController(currentUserController);

ParseUser user = new ParseUser();
String authType = "facebook";
Map<String, String> authData = new HashMap<>();
authData.put("token", "123");

ParseUser userAfterLogin = ParseTaskUtils.wait(user.logInWithAsync(authType, authData));
ParseUser userAfterLogin = ParseTaskUtils.wait(ParseUser.logInWithAsync(authType, authData));

// Make sure we login authData
verify(userController, times(1)).logInAsync("facebook", authData);
Expand All @@ -565,7 +562,7 @@ public void testLinkWithAsyncWithSaveAsyncSuccess() throws Exception {
// Register a mock authenticationProvider
ParseAuthenticationProvider provider = mock(ParseAuthenticationProvider.class);
when(provider.getAuthType()).thenReturn("facebook");
when(provider.restoreAuthentication(anyMap())).thenReturn(true);
when(provider.restoreAuthentication(Matchers.<Map<String, String>>any())).thenReturn(true);
ParseUser.registerAuthenticationProvider(provider);

ParseUser user = new ParseUser();
Expand All @@ -577,7 +574,7 @@ public void testLinkWithAsyncWithSaveAsyncSuccess() throws Exception {
ParseUser partialMockUser = spy(user);
doReturn(Task.<Void>forResult(null))
.when(partialMockUser)
.saveAsync(anyString(), any(Task.class));
.saveAsync(anyString(), Matchers.<Task<Void>>any());
String authType = "facebook";
Map<String, String> authData = new HashMap<>();
authData.put("token", "test");
Expand All @@ -589,7 +586,7 @@ public void testLinkWithAsyncWithSaveAsyncSuccess() throws Exception {
// Make sure new authData is added
assertSame(authData, partialMockUser.getAuthData().get("facebook"));
// Make sure we save the user
verify(partialMockUser, times(1)).saveAsync(eq("sessionTokenAgain"), any(Task.class));
verify(partialMockUser, times(1)).saveAsync(eq("sessionTokenAgain"), Matchers.<Task<Void>>any());
// Make sure synchronizeAuthData() is called
verify(provider, times(1)).restoreAuthentication(authData);
}
Expand All @@ -612,7 +609,7 @@ public void testLinkWithAsyncWithSaveAsyncFailure() throws Exception {
Exception saveException = new Exception();
doReturn(Task.<Void>forError(saveException))
.when(partialMockUser)
.saveAsync(anyString(), any(Task.class));
.saveAsync(anyString(), Matchers.<Task<Void>>any());
String facebookAuthType = "facebook";
Map<String, String> facebookAuthData = new HashMap<>();
facebookAuthData.put("facebookToken", "facebookTest");
Expand All @@ -624,7 +621,7 @@ public void testLinkWithAsyncWithSaveAsyncFailure() throws Exception {
// Make sure new authData is added
assertSame(facebookAuthData, partialMockUser.getAuthData().get("facebook"));
// Make sure we save the user
verify(partialMockUser, times(1)).saveAsync(eq("sessionTokenAgain"), any(Task.class));
verify(partialMockUser, times(1)).saveAsync(eq("sessionTokenAgain"), Matchers.<Task<Void>>any());
// Make sure old authData is restored
assertSame(anonymousAuthData, partialMockUser.getAuthData().get(ParseAnonymousUtils.AUTH_TYPE));
// Verify exception
Expand Down Expand Up @@ -843,7 +840,7 @@ public void testSaveAsyncWithLazyAndCurrentUser() throws Exception {
ParseUser partialMockUser = spy(user);
doReturn(Task.<Void>forResult(null))
.when(partialMockUser)
.resolveLazinessAsync(any(Task.class));
.resolveLazinessAsync(Matchers.<Task<Void>>any());

ParseTaskUtils.wait(partialMockUser.saveAsync("sessionToken", Task.<Void>forResult(null)));

Expand Down Expand Up @@ -871,7 +868,7 @@ public void testSaveAsyncWithLazyAndNotCurrentUser() throws Exception {
ParseUser partialMockUser = spy(user);
doReturn(Task.<Void>forResult(null))
.when(partialMockUser)
.resolveLazinessAsync(any(Task.class));
.resolveLazinessAsync(Matchers.<Task<Void>>any());

ParseTaskUtils.wait(partialMockUser.saveAsync("sessionToken", Task.<Void>forResult(null)));

Expand Down Expand Up @@ -899,7 +896,7 @@ public void testLogoutInternal() throws Exception {
// Register a mock authenticationProvider
ParseAuthenticationProvider provider = mock(ParseAuthenticationProvider.class);
when(provider.getAuthType()).thenReturn("facebook");
when(provider.restoreAuthentication(anyMap())).thenReturn(true);
when(provider.restoreAuthentication(Matchers.<Map<String, String>>any())).thenReturn(true);
ParseUser.registerAuthenticationProvider(provider);

// Set user initial state
Expand Down Expand Up @@ -965,8 +962,7 @@ public void testEnableRevocableSessionInBackgroundWithCurrentUser() throws Excep
when(currentUserController.getAsync(anyBoolean())).thenReturn(Task.forResult(mockUser));
ParseCorePlugins.getInstance().registerCurrentUserController(currentUserController);

ParseUser user = new ParseUser();
ParseTaskUtils.wait(user.enableRevocableSessionInBackground());
ParseTaskUtils.wait(ParseUser.enableRevocableSessionInBackground());

verify(currentUserController, times(1)).getAsync(false);
verify(mockUser, times(1)).upgradeToRevocableSessionAsync();
Expand All @@ -983,8 +979,7 @@ public void testEnableRevocableSessionInBackgroundWithNoCurrentUser() throws Exc
when(currentUserController.getAsync(anyBoolean())).thenReturn(Task.<ParseUser>forResult(null));
ParseCorePlugins.getInstance().registerCurrentUserController(currentUserController);

ParseUser user = new ParseUser();
ParseTaskUtils.wait(user.enableRevocableSessionInBackground());
ParseTaskUtils.wait(ParseUser.enableRevocableSessionInBackground());

verify(currentUserController, times(1)).getAsync(false);
}
Expand Down Expand Up @@ -1078,14 +1073,14 @@ public void testUnlinkFromAsyncWithAuthType() throws Exception {
ParseUser partialMockUser = spy(user);
doReturn(Task.<Void>forResult(null))
.when(partialMockUser)
.saveAsync(anyString(), any(Task.class));
.saveAsync(anyString(), Matchers.<Task<Void>>any());

ParseTaskUtils.wait(partialMockUser.unlinkFromAsync("facebook"));

// Verify we delete authData
assertNull(user.getAuthData().get("facebook"));
// Verify we save the user
verify(partialMockUser, times(1)).saveAsync(eq("sessionToken"), any(Task.class));
verify(partialMockUser, times(1)).saveAsync(eq("sessionToken"), Matchers.<Task<Void>>any());
}

@Test
Expand Down Expand Up @@ -1342,7 +1337,7 @@ public void testSynchronizeAuthData() throws Exception {
// Register a mock authenticationProvider
ParseAuthenticationProvider provider = mock(ParseAuthenticationProvider.class);
when(provider.getAuthType()).thenReturn("facebook");
when(provider.restoreAuthentication(anyMap())).thenReturn(true);
when(provider.restoreAuthentication(Matchers.<Map<String, String>>any())).thenReturn(true);
ParseUser.registerAuthenticationProvider(provider);

// Set user initial state
Expand Down Expand Up @@ -1370,7 +1365,7 @@ public void testSynchronizeAllAuthData() throws Exception {
// Register a mock authenticationProvider
ParseAuthenticationProvider provider = mock(ParseAuthenticationProvider.class);
when(provider.getAuthType()).thenReturn("facebook");
when(provider.restoreAuthentication(anyMap())).thenReturn(true);
when(provider.restoreAuthentication(Matchers.<Map<String, String>>any())).thenReturn(true);
ParseUser.registerAuthenticationProvider(provider);

// Set user initial state
Expand All @@ -1395,13 +1390,13 @@ public void testSynchronizeAllAuthData() throws Exception {

@Test
public void testAutomaticUser() throws Exception {
ParseUser user = new ParseUser();
new ParseUser();

user.disableAutomaticUser();
assertFalse(user.isAutomaticUserEnabled());
ParseUser.disableAutomaticUser();
assertFalse(ParseUser.isAutomaticUserEnabled());

user.enableAutomaticUser();
assertTrue(user.isAutomaticUserEnabled());
ParseUser.enableAutomaticUser();
assertTrue(ParseUser.isAutomaticUserEnabled());
}

//endregion
Expand Down