From 80f81314fe72ca467e838edf84107ffbcedafabf Mon Sep 17 00:00:00 2001 From: Julien Lancelot Date: Fri, 9 Jun 2017 16:53:24 +0200 Subject: [PATCH] Remove dbunit usage from UserDaoTest and UserIndexerTest --- .../java/org/sonar/db/user/UserDaoTest.java | 152 +++++++++--------- .../db/user/UserDaoTest/exists_by_email.xml | 28 ---- .../db/user/UserDaoTest/insert-result.xml | 11 -- .../UserDaoTest/selectActiveUserByLogin.xml | 35 ---- .../db/user/UserDaoTest/selectUsersByIds.xml | 30 ---- .../user/UserDaoTest/selectUsersByLogins.xml | 30 ---- .../user/UserDaoTest/selectUsersByQuery.xml | 21 --- .../db/user/UserDaoTest/selectUsersByText.xml | 21 --- .../db/user/UserDaoTest/select_by_login.xml | 28 ---- .../select_nullable_by_scm_account.xml | 28 ---- ..._when_same_email_is_used_by_many_users.xml | 28 ---- .../sonar/db/user/UserDaoTest/update_user.xml | 11 -- .../server/user/index/UserIndexerTest.java | 13 +- .../user/index/UserIndexerTest/index.xml | 14 -- 14 files changed, 86 insertions(+), 364 deletions(-) delete mode 100644 server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/exists_by_email.xml delete mode 100644 server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/insert-result.xml delete mode 100644 server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/selectActiveUserByLogin.xml delete mode 100644 server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/selectUsersByIds.xml delete mode 100644 server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/selectUsersByLogins.xml delete mode 100644 server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/selectUsersByQuery.xml delete mode 100644 server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/selectUsersByText.xml delete mode 100644 server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/select_by_login.xml delete mode 100644 server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/select_nullable_by_scm_account.xml delete mode 100644 server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/select_nullable_by_scm_account_return_many_results_when_same_email_is_used_by_many_users.xml delete mode 100644 server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/update_user.xml delete mode 100644 server/sonar-server/src/test/resources/org/sonar/server/user/index/UserIndexerTest/index.xml 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 c6f54ca0503..ef3d48316b9 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 @@ -20,7 +20,6 @@ package org.sonar.db.user; import java.util.Collection; -import java.util.Collections; import java.util.List; import org.junit.Before; import org.junit.Rule; @@ -35,6 +34,7 @@ import org.sonar.db.DbTester; import org.sonar.db.RowNotFoundException; import static java.util.Arrays.asList; +import static java.util.Collections.emptyList; import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric; import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.fail; @@ -64,55 +64,57 @@ public class UserDaoTest { @Test public void selectUsersIds() { - db.prepareDbUnit(getClass(), "selectUsersByIds.xml"); + UserDto user1 = db.users().insertUser(user -> user.setLogin("user1")); + UserDto user2 = db.users().insertUser(user -> user.setLogin("user2")); + UserDto user3 = db.users().insertUser(user -> user.setLogin("inactive_user").setActive(false)); - Collection users = underTest.selectByIds(session, asList(100, 101, 987)); - assertThat(users).hasSize(2); - assertThat(users).extracting("login").containsOnly("marius", "inactive_user"); - - assertThat(underTest.selectByIds(session, Collections.emptyList())).isEmpty(); + assertThat(underTest.selectByIds(session, asList(user1.getId(), user2.getId(), user3.getId(), 1_000))).extracting("login") + .containsExactlyInAnyOrder("user1", "user2", "inactive_user"); + assertThat(underTest.selectByIds(session, emptyList())).isEmpty(); } @Test public void selectUserByLogin_ignore_inactive() { - db.prepareDbUnit(getClass(), "selectActiveUserByLogin.xml"); + db.users().insertUser(user -> user.setLogin("user1")); + db.users().insertUser(user -> user.setLogin("user2")); + db.users().insertUser(user -> user.setLogin("inactive_user").setActive(false)); - UserDto user = underTest.selectUserById(session, 50); - assertThat(user.getLogin()).isEqualTo("inactive_user"); + UserDto user = underTest.selectActiveUserByLogin(session, "inactive_user"); - user = underTest.selectActiveUserByLogin(session, "inactive_user"); assertThat(user).isNull(); } @Test public void selectUserByLogin_not_found() { - db.prepareDbUnit(getClass(), "selectActiveUserByLogin.xml"); + db.users().insertUser(user -> user.setLogin("user")); UserDto user = underTest.selectActiveUserByLogin(session, "not_found"); + assertThat(user).isNull(); } @Test public void selectUsersByLogins() { - db.prepareDbUnit(getClass(), "selectUsersByLogins.xml"); + db.users().insertUser(user -> user.setLogin("user1")); + db.users().insertUser(user -> user.setLogin("user2")); + db.users().insertUser(user -> user.setLogin("inactive_user").setActive(false)); - Collection users = underTest.selectByLogins(session, asList("marius", "inactive_user", "other")); - assertThat(users).hasSize(2); - assertThat(users).extracting("login").containsOnly("marius", "inactive_user"); + Collection users = underTest.selectByLogins(session, asList("user1", "inactive_user", "other")); + + assertThat(users).extracting("login").containsExactlyInAnyOrder("user1", "inactive_user"); } @Test public void selectUsersByLogins_empty_logins() { // no need to access db - Collection users = underTest.selectByLogins(session, Collections.emptyList()); + Collection users = underTest.selectByLogins(session, emptyList()); assertThat(users).isEmpty(); } @Test public void selectByOrderedLogins() { - underTest.insert(session, newUserDto().setLogin("U1").setActive(true)); - underTest.insert(session, newUserDto().setLogin("U2").setActive(true)); - session.commit(); + db.users().insertUser(user -> user.setLogin("U1")); + db.users().insertUser(user -> user.setLogin("U2")); Iterable users = underTest.selectByOrderedLogins(session, asList("U1", "U2", "U3")); assertThat(users).extracting("login").containsExactly("U1", "U2"); @@ -120,61 +122,63 @@ public class UserDaoTest { users = underTest.selectByOrderedLogins(session, asList("U2", "U3", "U1")); assertThat(users).extracting("login").containsExactly("U2", "U1"); - assertThat(underTest.selectByOrderedLogins(session, Collections.emptyList())).isEmpty(); + assertThat(underTest.selectByOrderedLogins(session, emptyList())).isEmpty(); } @Test public void selectUsersByQuery_all() { - db.prepareDbUnit(getClass(), "selectUsersByQuery.xml"); + db.users().insertUser(user -> user.setLogin("user").setName("User")); + db.users().insertUser(user -> user.setLogin("inactive_user").setName("Disabled").setActive(false)); + + List users = underTest.selectUsers(session, UserQuery.builder().includeDeactivated().build()); - UserQuery query = UserQuery.builder().includeDeactivated().build(); - List users = underTest.selectUsers(session, query); assertThat(users).hasSize(2); } @Test public void selectUsersByQuery_only_actives() { - db.prepareDbUnit(getClass(), "selectUsersByQuery.xml"); + db.users().insertUser(user -> user.setLogin("user").setName("User")); + db.users().insertUser(user -> user.setLogin("inactive_user").setName("Disabled").setActive(false)); - UserQuery query = UserQuery.ALL_ACTIVES; - List users = underTest.selectUsers(session, query); - assertThat(users).hasSize(1); - assertThat(users.get(0).getName()).isEqualTo("Marius"); + List users = underTest.selectUsers(session, UserQuery.ALL_ACTIVES); + + assertThat(users).extracting(UserDto::getName).containsExactlyInAnyOrder("User"); } @Test public void selectUsersByQuery_filter_by_login() { - db.prepareDbUnit(getClass(), "selectUsersByQuery.xml"); + db.users().insertUser(user -> user.setLogin("user").setName("User")); + db.users().insertUser(user -> user.setLogin("inactive_user").setName("Disabled").setActive(false)); - UserQuery query = UserQuery.builder().logins("marius", "john").build(); - List users = underTest.selectUsers(session, query); - assertThat(users).hasSize(1); - assertThat(users.get(0).getName()).isEqualTo("Marius"); + List users = underTest.selectUsers(session, UserQuery.builder().logins("user", "john").build()); + + assertThat(users).extracting(UserDto::getName).containsExactlyInAnyOrder("User"); } @Test public void selectUsersByQuery_search_by_login_text() { - db.prepareDbUnit(getClass(), "selectUsersByText.xml"); + db.users().insertUser(user -> user.setLogin("user").setName("User")); + db.users().insertUser(user -> user.setLogin("sbrandhof").setName("Simon Brandhof")); - UserQuery query = UserQuery.builder().searchText("sbr").build(); - List users = underTest.selectUsers(session, query); - assertThat(users).hasSize(1); - assertThat(users.get(0).getLogin()).isEqualTo("sbrandhof"); + List users = underTest.selectUsers(session, UserQuery.builder().searchText("sbr").build()); + + assertThat(users).extracting(UserDto::getLogin).containsExactlyInAnyOrder("sbrandhof"); } @Test public void selectUsersByQuery_search_by_name_text() { - db.prepareDbUnit(getClass(), "selectUsersByText.xml"); + db.users().insertUser(user -> user.setLogin("user").setName("User")); + db.users().insertUser(user -> user.setLogin("sbrandhof").setName("Simon Brandhof")); - UserQuery query = UserQuery.builder().searchText("Simon").build(); - List users = underTest.selectUsers(session, query); - assertThat(users).hasSize(1); - assertThat(users.get(0).getLogin()).isEqualTo("sbrandhof"); + List users = underTest.selectUsers(session, UserQuery.builder().searchText("Simon").build()); + + assertThat(users).extracting(UserDto::getLogin).containsExactlyInAnyOrder("sbrandhof"); } @Test public void selectUsersByQuery_escape_special_characters_in_like() { - db.prepareDbUnit(getClass(), "selectUsersByText.xml"); + db.users().insertUser(user -> user.setLogin("user").setName("User")); + db.users().insertUser(user -> user.setLogin("sbrandhof").setName("Simon Brandhof")); UserQuery query = UserQuery.builder().searchText("%s%").build(); // we expect really a login or name containing the 3 characters "%s%" @@ -339,7 +343,7 @@ public class UserDaoTest { @Test public void update_user() { - UserDto existingUser = new UserDto() + UserDto existingUser = db.users().insertUser(user -> user .setLogin("john") .setName("John") .setEmail("jo@hn.com") @@ -347,9 +351,7 @@ public class UserDaoTest { .setUpdatedAt(1418215735482L) .setActive(true) .setLocal(true) - .setOnboarded(false); - db.getDbClient().userDao().insert(db.getSession(), existingUser); - db.getSession().commit(); + .setOnboarded(false)); UserDto userDto = new UserDto() .setId(1) @@ -416,10 +418,21 @@ public class UserDaoTest { @Test public void select_by_login() { - db.prepareDbUnit(getClass(), "select_by_login.xml"); + UserDto user1 = db.users().insertUser(user -> user + .setLogin("marius") + .setName("Marius") + .setEmail("marius@lesbronzes.fr") + .setActive(true) + .setScmAccounts("\nma\nmarius33\n") + .setSalt("79bd6a8e79fb8c76ac8b121cc7e8e11ad1af8365") + .setCryptedPassword("650d2261c98361e2f67f90ce5c65a95e7d8ea2fg") + .setCreatedAt(1418215735482L) + .setUpdatedAt(1418215735485L)); + UserDto user2 = db.users().insertUser(user -> user.setLogin("sbrandhof")); + underTest.setRoot(session, user2.getLogin(), true); UserDto dto = underTest.selectOrFailByLogin(session, "marius"); - assertThat(dto.getId()).isEqualTo(101); + assertThat(dto.getId()).isEqualTo(user1.getId()); assertThat(dto.getLogin()).isEqualTo("marius"); assertThat(dto.getName()).isEqualTo("Marius"); assertThat(dto.getEmail()).isEqualTo("marius@lesbronzes.fr"); @@ -437,36 +450,23 @@ public class UserDaoTest { @Test public void select_nullable_by_scm_account() { - db.prepareDbUnit(getClass(), "select_nullable_by_scm_account.xml"); - - List results = underTest.selectByScmAccountOrLoginOrEmail(session, "ma"); - assertThat(results).hasSize(1); - assertThat(results.get(0).getLogin()).isEqualTo("marius"); + db.users().insertUser(user -> user.setLogin("marius").setName("Marius").setEmail("marius@lesbronzes.fr").setScmAccounts(asList("ma", "marius33"))); + db.users().insertUser(user -> user.setLogin("sbrandhof").setName("Simon Brandhof").setEmail("sbrandhof@lesbronzes.fr").setScmAccounts((String) null)); - results = underTest.selectByScmAccountOrLoginOrEmail(session, "marius"); - assertThat(results).hasSize(1); - assertThat(results.get(0).getLogin()).isEqualTo("marius"); - - results = underTest.selectByScmAccountOrLoginOrEmail(session, "marius@lesbronzes.fr"); - assertThat(results).hasSize(1); - assertThat(results.get(0).getLogin()).isEqualTo("marius"); - - results = underTest.selectByScmAccountOrLoginOrEmail(session, "marius@lesbronzes.fr"); - assertThat(results).hasSize(1); - assertThat(results.get(0).getLogin()).isEqualTo("marius"); - - results = underTest.selectByScmAccountOrLoginOrEmail(session, "m"); - assertThat(results).isEmpty(); - - results = underTest.selectByScmAccountOrLoginOrEmail(session, "unknown"); - assertThat(results).isEmpty(); + assertThat(underTest.selectByScmAccountOrLoginOrEmail(session, "ma")).extracting(UserDto::getLogin).containsExactly("marius"); + assertThat(underTest.selectByScmAccountOrLoginOrEmail(session, "marius")).extracting(UserDto::getLogin).containsExactly("marius"); + assertThat(underTest.selectByScmAccountOrLoginOrEmail(session, "marius@lesbronzes.fr")).extracting(UserDto::getLogin).containsExactly("marius"); + assertThat(underTest.selectByScmAccountOrLoginOrEmail(session, "m")).isEmpty(); + assertThat(underTest.selectByScmAccountOrLoginOrEmail(session, "unknown")).isEmpty(); } @Test public void select_nullable_by_scm_account_return_many_results_when_same_email_is_used_by_many_users() { - db.prepareDbUnit(getClass(), "select_nullable_by_scm_account_return_many_results_when_same_email_is_used_by_many_users.xml"); + db.users().insertUser(user -> user.setLogin("marius").setName("Marius").setEmail("marius@lesbronzes.fr").setScmAccounts(asList("ma", "marius33"))); + db.users().insertUser(user -> user.setLogin("sbrandhof").setName("Simon Brandhof").setEmail("marius@lesbronzes.fr").setScmAccounts((String) null)); List results = underTest.selectByScmAccountOrLoginOrEmail(session, "marius@lesbronzes.fr"); + assertThat(results).hasSize(2); } @@ -482,10 +482,10 @@ public class UserDaoTest { @Test public void select_nullable_by_login() { - db.prepareDbUnit(getClass(), "select_by_login.xml"); + db.users().insertUser(user -> user.setLogin("marius")); + db.users().insertUser(user -> user.setLogin("sbrandhof")); assertThat(underTest.selectByLogin(session, "marius")).isNotNull(); - assertThat(underTest.selectByLogin(session, "unknown")).isNull(); } diff --git a/server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/exists_by_email.xml b/server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/exists_by_email.xml deleted file mode 100644 index 2226bb3ba34..00000000000 --- a/server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/exists_by_email.xml +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - diff --git a/server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/insert-result.xml b/server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/insert-result.xml deleted file mode 100644 index fe3a43320a9..00000000000 --- a/server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/insert-result.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - diff --git a/server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/selectActiveUserByLogin.xml b/server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/selectActiveUserByLogin.xml deleted file mode 100644 index fba9650f48b..00000000000 --- a/server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/selectActiveUserByLogin.xml +++ /dev/null @@ -1,35 +0,0 @@ - - - - - - - - - - - diff --git a/server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/selectUsersByIds.xml b/server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/selectUsersByIds.xml deleted file mode 100644 index 7340c944abb..00000000000 --- a/server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/selectUsersByIds.xml +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - diff --git a/server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/selectUsersByLogins.xml b/server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/selectUsersByLogins.xml deleted file mode 100644 index 7340c944abb..00000000000 --- a/server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/selectUsersByLogins.xml +++ /dev/null @@ -1,30 +0,0 @@ - - - - - - diff --git a/server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/selectUsersByQuery.xml b/server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/selectUsersByQuery.xml deleted file mode 100644 index 246d4caf77e..00000000000 --- a/server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/selectUsersByQuery.xml +++ /dev/null @@ -1,21 +0,0 @@ - - - - - diff --git a/server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/selectUsersByText.xml b/server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/selectUsersByText.xml deleted file mode 100644 index c4308ee82af..00000000000 --- a/server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/selectUsersByText.xml +++ /dev/null @@ -1,21 +0,0 @@ - - - - - diff --git a/server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/select_by_login.xml b/server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/select_by_login.xml deleted file mode 100644 index 2ac78040cab..00000000000 --- a/server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/select_by_login.xml +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - diff --git a/server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/select_nullable_by_scm_account.xml b/server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/select_nullable_by_scm_account.xml deleted file mode 100644 index 54b486eb3e3..00000000000 --- a/server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/select_nullable_by_scm_account.xml +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - diff --git a/server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/select_nullable_by_scm_account_return_many_results_when_same_email_is_used_by_many_users.xml b/server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/select_nullable_by_scm_account_return_many_results_when_same_email_is_used_by_many_users.xml deleted file mode 100644 index 2226bb3ba34..00000000000 --- a/server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/select_nullable_by_scm_account_return_many_results_when_same_email_is_used_by_many_users.xml +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - diff --git a/server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/update_user.xml b/server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/update_user.xml deleted file mode 100644 index fe3a43320a9..00000000000 --- a/server/sonar-db-dao/src/test/resources/org/sonar/db/user/UserDaoTest/update_user.xml +++ /dev/null @@ -1,11 +0,0 @@ - - - diff --git a/server/sonar-server/src/test/java/org/sonar/server/user/index/UserIndexerTest.java b/server/sonar-server/src/test/java/org/sonar/server/user/index/UserIndexerTest.java index 4726c05b828..d17ed19990b 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/user/index/UserIndexerTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/user/index/UserIndexerTest.java @@ -19,7 +19,6 @@ */ package org.sonar.server.user.index; -import java.util.Arrays; import java.util.List; import org.junit.Rule; import org.junit.Test; @@ -29,6 +28,7 @@ import org.sonar.db.DbTester; import org.sonar.db.user.UserDto; import org.sonar.server.es.EsTester; +import static java.util.Arrays.asList; import static org.assertj.core.api.Assertions.assertThat; public class UserIndexerTest { @@ -52,7 +52,14 @@ public class UserIndexerTest { @Test public void index_everything_on_startup() { - db.prepareDbUnit(getClass(), "index.xml"); + db.users().insertUser(user -> user + .setLogin("user1") + .setName("User1") + .setEmail("user1@mail.com") + .setActive(true) + .setScmAccounts(asList("user_1", "u1")) + .setCreatedAt(1500000000000L) + .setUpdatedAt(1500000000000L)); underTest.indexOnStartup(null); @@ -98,7 +105,7 @@ public class UserIndexerTest { UserDto user = db.users().insertUser(); UserDto anotherUser = db.users().insertUser(); - underTest.index(Arrays.asList(user.getLogin(), anotherUser.getLogin())); + underTest.index(asList(user.getLogin(), anotherUser.getLogin())); List docs = es.getDocuments(UserIndexDefinition.INDEX_TYPE_USER, UserDoc.class); assertThat(docs).hasSize(2); diff --git a/server/sonar-server/src/test/resources/org/sonar/server/user/index/UserIndexerTest/index.xml b/server/sonar-server/src/test/resources/org/sonar/server/user/index/UserIndexerTest/index.xml deleted file mode 100644 index c375007dd76..00000000000 --- a/server/sonar-server/src/test/resources/org/sonar/server/user/index/UserIndexerTest/index.xml +++ /dev/null @@ -1,14 +0,0 @@ - - - - - -- 2.39.5