From ce8b51d92bb43f694e7cd955653eabc818e82c68 Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Wed, 19 Apr 2017 11:22:41 +0200 Subject: [PATCH] SONAR-8957 Rename deactivateUserByLogin to deactivateUserById --- .../main/java/org/sonar/db/user/UserDao.java | 16 ++-------------- .../java/org/sonar/db/user/UserDaoTest.java | 17 +++-------------- .../sonar/server/user/ws/DeactivateAction.java | 2 +- .../server/user/ServerUserSessionTest.java | 2 +- .../sonar/server/user/ws/CreateActionTest.java | 7 ++----- 5 files changed, 9 insertions(+), 35 deletions(-) diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDao.java index 46a1dcbfdf6..f45573f7dc5 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDao.java @@ -106,20 +106,8 @@ public class UserDao implements Dao { mapper(session).setRoot(login, root, system2.now()); } - /** - * Deactivate a user and drops all his preferences. - * @return false if the user does not exist, true if the existing user has been deactivated - */ - public boolean deactivateUserByLogin(DbSession dbSession, String login) { - UserMapper mapper = mapper(dbSession); - UserDto dto = mapper.selectUserByLogin(login); - if (dto == null) { - return false; - } - - mapper.deactivateUser(dto.getId(), system2.now()); - dbSession.commit(); - return true; + public void deactivateUserById(DbSession dbSession, int userId) { + mapper(dbSession).deactivateUser(userId, system2.now()); } @CheckForNull diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserDaoTest.java index 42742612be2..3acf9a2b530 100644 --- a/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserDaoTest.java +++ b/server/sonar-db-dao/src/test/java/org/sonar/db/user/UserDaoTest.java @@ -40,8 +40,6 @@ import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; -import static org.sonar.db.user.GroupMembershipQuery.IN; -import static org.sonar.db.user.GroupMembershipQuery.builder; import static org.sonar.db.user.GroupTesting.newGroupDto; import static org.sonar.db.user.UserTesting.newUserDto; @@ -388,13 +386,10 @@ public class UserDaoTest { public void deactivate_user() throws Exception { UserDto user = newActiveUser(); insertUserGroup(user); - UserDto otherUser = newActiveUser(); - session.commit(); - boolean deactivated = underTest.deactivateUserByLogin(session, user.getLogin()); - assertThat(deactivated).isTrue(); + underTest.deactivateUserById(session, user.getId()); UserDto userReloaded = underTest.selectUserById(session, user.getId()); assertThat(userReloaded.isActive()).isFalse(); @@ -406,18 +401,12 @@ public class UserDaoTest { assertThat(userReloaded.getExternalIdentityProvider()).isNull(); assertThat(userReloaded.isRoot()).isFalse(); assertThat(userReloaded.getUpdatedAt()).isEqualTo(NOW); - assertThat(underTest.selectUserById(session, otherUser.getId())).isNotNull(); - - assertThat(dbClient.groupMembershipDao().countGroups(session, builder().organizationUuid(db.getDefaultOrganization().getUuid()).membership(IN).build(), user.getId())).isZero(); } @Test - public void deactivate_missing_user() { - String login = "does_not_exist"; - boolean deactivated = underTest.deactivateUserByLogin(session, login); - assertThat(deactivated).isFalse(); - assertThat(underTest.selectActiveUserByLogin(session, login)).isNull(); + public void does_not_fail_to_deactivate_missing_user() { + underTest.deactivateUserById(session, 123); } @Test diff --git a/server/sonar-server/src/main/java/org/sonar/server/user/ws/DeactivateAction.java b/server/sonar-server/src/main/java/org/sonar/server/user/ws/DeactivateAction.java index bc12aaf157f..24365f287a4 100644 --- a/server/sonar-server/src/main/java/org/sonar/server/user/ws/DeactivateAction.java +++ b/server/sonar-server/src/main/java/org/sonar/server/user/ws/DeactivateAction.java @@ -99,7 +99,7 @@ public class DeactivateAction implements UsersWsAction { dbClient.userPermissionDao().deleteByUserId(dbSession, user.getId()); dbClient.propertiesDao().deleteByKeyAndValue(dbSession, DEFAULT_ISSUE_ASSIGNEE, user.getLogin()); dbClient.organizationMemberDao().deleteByUserId(dbSession, user.getId()); - dbClient.userDao().deactivateUserByLogin(dbSession, login); + dbClient.userDao().deactivateUserById(dbSession, user.getId()); dbSession.commit(); } diff --git a/server/sonar-server/src/test/java/org/sonar/server/user/ServerUserSessionTest.java b/server/sonar-server/src/test/java/org/sonar/server/user/ServerUserSessionTest.java index 933297c4b38..b366b6093d6 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/user/ServerUserSessionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/user/ServerUserSessionTest.java @@ -377,7 +377,7 @@ public class ServerUserSessionTest { session.checkIsSystemAdministrator(); - db.getDbClient().userDao().deactivateUserByLogin(db.getSession(), userDto.getLogin()); + db.getDbClient().userDao().deactivateUserById(db.getSession(), userDto.getId()); db.commit(); // should fail but succeeds because flag is kept in cache diff --git a/server/sonar-server/src/test/java/org/sonar/server/user/ws/CreateActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/user/ws/CreateActionTest.java index 60fc9a9d4fb..9076e41d9d1 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/user/ws/CreateActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/user/ws/CreateActionTest.java @@ -234,11 +234,8 @@ public class CreateActionTest { public void reactivate_user() throws Exception { logInAsSystemAdministrator(); - UserDto userDto = newUserDto("john", "John", "john@email.com"); - db.users().insertUser(userDto); - db.getDbClient().userDao().deactivateUserByLogin(db.getSession(), "john"); - db.commit(); - userIndexer.index(userDto.getLogin()); + db.users().insertUser(newUserDto("john", "John", "john@email.com").setActive(false)); + userIndexer.index("john"); call(CreateRequest.builder() .setLogin("john") -- 2.39.5