]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-10597 Introduce UUID in table USERS
authorGuillaume Jambet <guillaume.jambet@gmail.com>
Thu, 26 Apr 2018 08:26:37 +0000 (10:26 +0200)
committerSonarTech <sonartech@sonarsource.com>
Wed, 23 May 2018 18:20:46 +0000 (20:20 +0200)
27 files changed:
server/sonar-db-core/src/main/resources/org/sonar/db/version/rows-h2.sql
server/sonar-db-core/src/main/resources/org/sonar/db/version/schema-h2.ddl
server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDao.java
server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDto.java
server/sonar-db-dao/src/main/java/org/sonar/db/user/UserMapper.java
server/sonar-db-dao/src/main/resources/org/sonar/db/user/UserMapper.xml
server/sonar-db-dao/src/test/java/org/sonar/db/property/PropertiesDaoTest.java
server/sonar-db-dao/src/test/java/org/sonar/db/user/UserDaoTest.java
server/sonar-db-dao/src/test/resources/org/sonar/db/permission/template/UserWithPermissionTemplateDaoTest/select_only_enable_users.xml
server/sonar-db-dao/src/test/resources/org/sonar/db/permission/template/UserWithPermissionTemplateDaoTest/users_with_permissions.xml
server/sonar-db-dao/src/test/resources/org/sonar/db/permission/template/UserWithPermissionTemplateDaoTest/users_with_permissions_should_be_sorted_by_user_name.xml
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v72/AddUUIDtoUsers.java [new file with mode: 0644]
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v72/DbVersion72.java
server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v72/PopulateUUIDOnUsers.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v72/AddUUIDtoUsersTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v72/DbVersion72Test.java
server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v72/PopulateUUIDOnUsersTest.java [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v72/AddUUIDtoUsers/users.sql [new file with mode: 0644]
server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v72/PopulateUUIDOnUsersTest/users.sql [new file with mode: 0644]
server/sonar-server/src/test/java/org/sonar/server/authentication/LocalAuthenticationTest.java
server/sonar-server/src/test/java/org/sonar/server/issue/ws/SearchActionTest.java
server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/CreateActionTest.java
server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/SearchActionTest.java
server/sonar-server/src/test/java/org/sonar/server/measure/custom/ws/UpdateActionTest.java
server/sonar-server/src/test/java/org/sonar/server/user/DeprecatedUserFinderTest.java
server/sonar-server/src/test/java/org/sonar/server/user/ws/SearchActionTest.java
server/sonar-server/src/test/resources/org/sonar/server/user/DeprecatedUserFinderTest/fixture.xml [deleted file]

index b82e764641bc832169fa84db521452103556518b..95b8cf51d7b3b81328c5b0d71cbefe568ef66d14 100644 (file)
@@ -1,4 +1,4 @@
-INSERT INTO USERS(ID, LOGIN, NAME, EMAIL, EXTERNAL_IDENTITY, EXTERNAL_IDENTITY_PROVIDER, USER_LOCAL, CRYPTED_PASSWORD, SALT, HASH_METHOD, IS_ROOT, ONBOARDED, CREATED_AT, UPDATED_AT) VALUES (1, 'admin', 'Administrator', '', 'admin', 'sonarqube', true, '$2a$12$uCkkXmhW5ThVK8mpBvnXOOJRLd64LJeHTeCkSuB3lfaR2N0AYBaSi', null, 'BCRYPT', false, false, '1418215735482', '1418215735482');
+INSERT INTO USERS(ID, UUID, LOGIN, NAME, EMAIL, EXTERNAL_IDENTITY, EXTERNAL_IDENTITY_PROVIDER, USER_LOCAL, CRYPTED_PASSWORD, SALT, HASH_METHOD, IS_ROOT, ONBOARDED, CREATED_AT, UPDATED_AT) VALUES (1, 'UuidnciQUUs7Zd3KPvFD', 'admin', 'Administrator', '', 'admin', 'sonarqube', true, '$2a$12$uCkkXmhW5ThVK8mpBvnXOOJRLd64LJeHTeCkSuB3lfaR2N0AYBaSi', null, 'BCRYPT', false, false, '1418215735482', '1418215735482');
 ALTER TABLE USERS ALTER COLUMN ID RESTART WITH 2;
 
 INSERT INTO GROUPS(ID, ORGANIZATION_UUID, NAME, DESCRIPTION, CREATED_AT, UPDATED_AT) VALUES (1, 'AVdqnciQUUs7Zd3KPvFD', 'sonar-administrators', 'System administrators', '2011-09-26 22:27:51.0', '2011-09-26 22:27:51.0');
index dd2e3a3317e9dab4eeee4ecc4e35bd785f0fdf91..46b8deff215dad7655f9fa058b1a363e9b3c30d8 100644 (file)
@@ -454,6 +454,7 @@ CREATE INDEX "IX_ARP_ON_ACTIVE_RULE_ID" ON "ACTIVE_RULE_PARAMETERS" ("ACTIVE_RUL
 
 CREATE TABLE "USERS" (
   "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+  "UUID" VARCHAR(40),
   "LOGIN" VARCHAR(255),
   "NAME" VARCHAR(200),
   "EMAIL" VARCHAR(100),
index e58945b21aa39a1d2f2b9cdb8c0e00d7d8dcced9..73c04c6e179559bfdc8b7d7f53a057ccec92f769 100644 (file)
@@ -32,6 +32,7 @@ import javax.annotation.CheckForNull;
 import javax.annotation.Nonnull;
 import org.sonar.api.user.UserQuery;
 import org.sonar.api.utils.System2;
+import org.sonar.core.util.UuidFactory;
 import org.sonar.db.Dao;
 import org.sonar.db.DbSession;
 import org.sonar.db.RowNotFoundException;
@@ -44,9 +45,12 @@ import static org.sonar.db.DatabaseUtils.executeLargeInputsWithoutOutput;
 public class UserDao implements Dao {
 
   private final System2 system2;
+  private final UuidFactory uuidFactory;
 
-  public UserDao(System2 system2) {
+
+  public UserDao(System2 system2, UuidFactory uuidFactory) {
     this.system2 = system2;
+    this.uuidFactory = uuidFactory;
   }
 
   @CheckForNull
@@ -102,16 +106,12 @@ public class UserDao implements Dao {
 
   public UserDto insert(DbSession session, UserDto dto) {
     long now = system2.now();
-    mapper(session).insert(dto, now);
-    dto.setCreatedAt(now);
-    dto.setUpdatedAt(now);
+    mapper(session).insert(dto.setUuid(uuidFactory.create()).setCreatedAt(now).setUpdatedAt(now));
     return dto;
   }
 
   public UserDto update(DbSession session, UserDto dto) {
-    long now = system2.now();
-    mapper(session).update(dto, now);
-    dto.setUpdatedAt(now);
+    mapper(session).update(dto.setUpdatedAt(system2.now()));
     return dto;
   }
 
index 32b5e69e83569f9b10fda67739b63ea681cbc09b..5c2cd93a2f36342f2e389bb65dee6a3aa289a21d 100644 (file)
@@ -33,6 +33,8 @@ import org.sonar.core.user.DefaultUser;
 public class UserDto {
   public static final char SCM_ACCOUNTS_SEPARATOR = '\n';
 
+  /** Technical unique identifier, can't be null */
+  private String uuid;
   private Integer id;
   private String login;
   private String name;
@@ -55,6 +57,15 @@ public class UserDto {
   private boolean root = false;
   private boolean onboarded = false;
 
+  public String getUuid() {
+    return uuid;
+  }
+
+  UserDto setUuid(String uuid) {
+    this.uuid = uuid;
+    return this;
+  }
+
   public Integer getId() {
     return id;
   }
index fa80aba9ac8584499669b8de13f76cc02c44bb03..e02486dce16088d68280ebf1e49706a8fdc12ea5 100644 (file)
@@ -62,9 +62,9 @@ public interface UserMapper {
    */
   long countRootUsersButLogin(@Param("login") String login);
 
-  void insert(@Param("user") UserDto userDto, @Param("now") long now);
+  void insert(@Param("user") UserDto userDto);
 
-  void update(@Param("user") UserDto userDto, @Param("now") long now);
+  void update(@Param("user") UserDto userDto);
 
   void setRoot(@Param("login") String login, @Param("root") boolean root, @Param("now") long now);
 
index 97909a403071895a11d52fad5abd96f4de6d3a84..5626e1c4c689d975d19ec5df86a7127b54dc5f42 100644 (file)
@@ -5,6 +5,7 @@
 
     <sql id="userColumns">
         u.id as id,
+        u.uuid as uuid,
         u.login as login,
         u.name as name,
         u.email as email,
 
     <insert id="insert" parameterType="map" keyColumn="id" useGeneratedKeys="true" keyProperty="user.id">
         insert into users (
+        uuid,
         login,
         name,
         email,
         homepage_type,
         homepage_parameter
         ) values (
+        #{user.uuid,jdbcType=VARCHAR},
         #{user.login,jdbcType=VARCHAR},
         #{user.name,jdbcType=VARCHAR},
         #{user.email,jdbcType=VARCHAR},
         #{user.hashMethod,jdbcType=VARCHAR},
         #{user.root,jdbcType=BOOLEAN},
         #{user.onboarded,jdbcType=BOOLEAN},
-        #{now,jdbcType=BIGINT},
-        #{now,jdbcType=BIGINT},
+        #{user.createdAt,jdbcType=BIGINT},
+        #{user.updatedAt,jdbcType=BIGINT},
         #{user.homepageType,jdbcType=VARCHAR},
         #{user.homepageParameter,jdbcType=VARCHAR}
         )
         salt = #{user.salt, jdbcType=VARCHAR},
         crypted_password = #{user.cryptedPassword, jdbcType=BIGINT},
         hash_method = #{user.hashMethod, jdbcType=VARCHAR},
-        updated_at = #{now, jdbcType=BIGINT},
+        updated_at = #{user.updatedAt,jdbcType=BIGINT},
         homepage_type = #{user.homepageType, jdbcType=VARCHAR},
         homepage_parameter = #{user.homepageParameter, jdbcType=VARCHAR}
         where
index 4d0616529c1207623622f41fc9f8e61d0b4634bb..a01c5d52058a8fc4adc7cdf5a378bd9b05db6b3e 100644 (file)
@@ -44,7 +44,6 @@ import org.sonar.db.component.ComponentTesting;
 import org.sonar.db.organization.OrganizationDto;
 import org.sonar.db.organization.OrganizationTesting;
 import org.sonar.db.user.UserDto;
-import org.sonar.db.user.UserTesting;
 
 import static com.google.common.collect.Lists.newArrayList;
 import static com.google.common.collect.Sets.newHashSet;
@@ -56,6 +55,7 @@ import static org.mockito.Mockito.when;
 import static org.sonar.db.property.PropertyTesting.newComponentPropertyDto;
 import static org.sonar.db.property.PropertyTesting.newGlobalPropertyDto;
 import static org.sonar.db.property.PropertyTesting.newUserPropertyDto;
+import static org.sonar.db.user.UserTesting.newUserDto;
 
 @RunWith(DataProviderRunner.class)
 public class PropertiesDaoTest {
@@ -83,9 +83,9 @@ public class PropertiesDaoTest {
   public void shouldFindUsersForNotification() throws SQLException {
     ComponentDto project1 = insertPrivateProject("uuid_45");
     ComponentDto project2 = insertPrivateProject("uuid_56");
-    UserDto user1 = insertUser("user1");
-    UserDto user2 = insertUser("user2");
-    UserDto user3 = insertUser("user3");
+    UserDto user1 = dbTester.users().insertUser(u -> u.setLogin("user1"));
+    UserDto user2 = dbTester.users().insertUser(u -> u.setLogin("user2"));
+    UserDto user3 = dbTester.users().insertUser(u -> u.setLogin("user3"));
     insertProperty("notification.NewViolations.Email", "true", project1.getId(), user2.getId());
     insertProperty("notification.NewViolations.Twitter", "true", null, user3.getId());
     insertProperty("notification.NewViolations.Twitter", "true", project2.getId(), user1.getId());
@@ -122,8 +122,8 @@ public class PropertiesDaoTest {
 
   @Test
   public void hasNotificationSubscribers() throws SQLException {
-    int userId1 = insertUser("user1").getId();
-    int userId2 = insertUser("user2").getId();
+    int userId1 = dbTester.users().insertUser(u -> u.setLogin("user1")).getId();
+    int userId2 = dbTester.users().insertUser(u -> u.setLogin("user2")).getId();
     Long projectId = insertPrivateProject("PROJECT_A").getId();
     // global subscription
     insertProperty("notification.DispatcherWithGlobalSubscribers.Email", "true", null, userId2);
@@ -352,7 +352,7 @@ public class PropertiesDaoTest {
   @Test
   public void select_global_properties_by_keys() throws Exception {
     insertPrivateProject("A");
-    int userId = insertUser("B").getId();
+    int userId = dbTester.users().insertUser(u -> u.setLogin("B")).getId();
 
     String key = "key";
     String anotherKey = "anotherKey";
@@ -378,9 +378,7 @@ public class PropertiesDaoTest {
   public void select_component_properties_by_ids() {
     ComponentDto project = dbTester.components().insertPrivateProject();
     ComponentDto project2 = dbTester.components().insertPrivateProject();
-
-    UserDto user = UserTesting.newUserDto();
-    dbClient.userDao().insert(session, user);
+    UserDto user = dbTester.users().insertUser();
 
     String key = "key";
     String anotherKey = "anotherKey";
@@ -406,9 +404,7 @@ public class PropertiesDaoTest {
   public void select_properties_by_keys_and_component_ids() {
     ComponentDto project = dbTester.components().insertPrivateProject();
     ComponentDto project2 = dbTester.components().insertPrivateProject();
-
-    UserDto user = UserTesting.newUserDto();
-    dbClient.userDao().insert(session, user);
+    UserDto user = dbTester.users().insertUser();
 
     String key = "key";
     String anotherKey = "anotherKey";
@@ -1029,14 +1025,6 @@ public class PropertiesDaoTest {
     return project;
   }
 
-  private UserDto insertUser(String login) {
-    UserDto dto = new UserDto().setLogin(login);
-    DbSession session = dbTester.getSession();
-    dbClient.userDao().insert(session, dto);
-    session.commit();
-    return dto;
-  }
-
   private static PropertyDtoAssert assertThatDto(@Nullable PropertyDto dto) {
     return new PropertyDtoAssert(dto);
   }
index e5899b9d08a4e4d166c7a66de8aa98aaa03f0aab..781451868252455325f42dc228f848825e323427 100644 (file)
@@ -360,7 +360,7 @@ public class UserDaoTest {
       .setLocal(true)
       .setOnboarded(false));
 
-    UserDto userUpdate = new UserDto()
+    UserDto userUpdate = newUserDto()
       .setId(1)
       .setLogin("john")
       .setName("John Doo")
@@ -488,7 +488,7 @@ public class UserDaoTest {
 
   @Test
   public void does_not_fail_to_deactivate_missing_user() {
-    underTest.deactivateUser(session, UserTesting.newUserDto());
+    underTest.deactivateUser(session, newUserDto());
   }
 
   @Test
index c3a899ddf49db51295486378f701012ede962530..159c76bec0045b6903551a1c043bedc929c1371a 100644 (file)
@@ -1,24 +1,28 @@
 <dataset>
 
   <users id="200"
+         uuid="user1uuid"
          login="user1"
          name="User1"
          active="[true]"
          is_root="[false]"
          onboarded="[true]"/>
   <users id="201"
+         uuid="user2uuid"
          login="user2"
          name="User2"
          active="[true]"
          is_root="[false]"
          onboarded="[true]"/>
   <users id="202"
+         uuid="user3uuid"
          login="user3"
          name="User3"
          active="[true]"
          is_root="[false]"
          onboarded="[true]"/>
   <users id="999"
+         uuid="user999uuid"
          login="disabledUser"
          name="disabledUser"
          active="[false]"
index 8382a060d2779bd238bfc996d17b51c3b7a833ac..23599c693879eafdc5dff1242219fbea14938f74 100644 (file)
@@ -1,18 +1,21 @@
 <dataset>
 
   <users id="200"
+         uuid="user1uuid"
          login="user1"
          name="User1"
          active="[true]"
          is_root="[false]"
          onboarded="[true]"/>
   <users id="201"
+         uuid="user2uuid"
          login="user2"
          name="User2"
          active="[true]"
          is_root="[false]"
          onboarded="[true]"/>
   <users id="202"
+         uuid="user3uuid"
          login="user3"
          name="User3"
          active="[true]"
index 56e76fad3f90241f9156419cb75cd43c02cc05e3..d8f866c81ab5ae84fe359eb59ab00b562a88d2dc 100644 (file)
@@ -1,18 +1,21 @@
 <dataset>
 
   <users id="200"
+         uuid="user3uuid"
          login="user3"
          name="User3"
          active="[true]"
          is_root="[false]"
          onboarded="[true]"/>
   <users id="201"
+         uuid="user1uuid"
          login="user1"
          name="User1"
          active="[true]"
          is_root="[false]"
          onboarded="[true]"/>
   <users id="202"
+         uuid="user2uuid"
          login="user2"
          name="User2"
          active="[true]"
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v72/AddUUIDtoUsers.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v72/AddUUIDtoUsers.java
new file mode 100644 (file)
index 0000000..d05abca
--- /dev/null
@@ -0,0 +1,48 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2018 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.platform.db.migration.version.v72;
+
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.def.VarcharColumnDef;
+import org.sonar.server.platform.db.migration.sql.AddColumnsBuilder;
+import org.sonar.server.platform.db.migration.step.DdlChange;
+
+import java.sql.SQLException;
+
+public class AddUUIDtoUsers extends DdlChange {
+
+  private static final String TABLE_NAME = "users";
+  private static final String UUID_COLUMN_NAME = "uuid";
+  private static final int UUID_LENGTH = 40;
+
+  public AddUUIDtoUsers(Database db) {
+    super(db);
+  }
+
+  @Override public void execute(Context context) throws SQLException {
+    context.execute(new AddColumnsBuilder(getDialect(), TABLE_NAME)
+      .addColumn(VarcharColumnDef.newVarcharColumnDefBuilder()
+        .setColumnName(UUID_COLUMN_NAME)
+        .setIsNullable(true)
+        .setLimit(UUID_LENGTH)
+        .build())
+      .build());
+  }
+}
index 8400eb1a14e4b9fda69c7c54782d9d10154615e5..dae4cf27704df33030d368e3b62729b7de7d99bb 100644 (file)
@@ -34,6 +34,8 @@ public class DbVersion72 implements DbVersion {
       .add(2104, "Create ALM_APP_INSTALLS table", CreateAlmAppInstallsTable.class)
       .add(2105, "Add LINE_HASHES_VERSION to table FILE_SOURCES", AddLineHashesVersionToFileSources.class)
       .add(2106, "Create PROJECT_MAPPINGS table", CreateProjectMappingsTable.class)
+      .add(2107, "Add UUID on table USERS", AddUUIDtoUsers.class)
+      .add(2108, "Populate USERS.UUID with USERS.LOGIN", PopulateUUIDOnUsers.class)
     ;
   }
 }
diff --git a/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v72/PopulateUUIDOnUsers.java b/server/sonar-db-migration/src/main/java/org/sonar/server/platform/db/migration/version/v72/PopulateUUIDOnUsers.java
new file mode 100644 (file)
index 0000000..54f018b
--- /dev/null
@@ -0,0 +1,57 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2018 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.platform.db.migration.version.v72;
+
+import java.sql.SQLException;
+import org.sonar.api.utils.System2;
+import org.sonar.core.util.UuidFactory;
+import org.sonar.db.Database;
+import org.sonar.server.platform.db.migration.step.DataChange;
+import org.sonar.server.platform.db.migration.step.MassUpdate;
+
+public class PopulateUUIDOnUsers extends DataChange {
+
+  private final System2 system2;
+  private final UuidFactory uuidFactory;
+
+  public PopulateUUIDOnUsers(Database db, System2 system2, UuidFactory uuidFactory) {
+    super(db);
+    this.system2 = system2;
+    this.uuidFactory = uuidFactory;
+  }
+
+  @Override
+  public void execute(Context context) throws SQLException {
+    MassUpdate massUpdate = context.prepareMassUpdate();
+    massUpdate.select("select id, login from users where uuid is null");
+    massUpdate.rowPluralName("users");
+    massUpdate.update("update users set uuid=?, updated_at=? where id=?");
+    massUpdate.execute((row, update) -> {
+      String login = row.getString(2);
+      if (login == null) {
+        login = uuidFactory.create();
+      }
+      update.setString(1, login); // login -> uuid
+      update.setLong(2, system2.now());
+      update.setLong(3, row.getLong(1));
+      return true;
+    });
+  }
+}
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v72/AddUUIDtoUsersTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v72/AddUUIDtoUsersTest.java
new file mode 100644 (file)
index 0000000..29165a3
--- /dev/null
@@ -0,0 +1,59 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2018 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.platform.db.migration.version.v72;
+
+import java.sql.SQLException;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.db.CoreDbTester;
+
+import static java.sql.Types.VARCHAR;
+import static org.junit.rules.ExpectedException.none;
+import static org.sonar.db.CoreDbTester.createForSchema;
+
+public class AddUUIDtoUsersTest {
+
+  @Rule
+  public final CoreDbTester dbTester = createForSchema(AddUUIDtoUsers.class, "users.sql");
+
+  @Rule
+  public ExpectedException expectedException = none();
+
+  private AddUUIDtoUsers underTest = new AddUUIDtoUsers(dbTester.database());
+
+  @Test
+  public void column_is_added_to_table() throws SQLException {
+    underTest.execute();
+
+    dbTester.assertColumnDefinition("users", "uuid", VARCHAR, 40, true);
+  }
+
+  @Test
+  public void migration_is_not_reentrant() throws SQLException {
+    underTest.execute();
+
+    expectedException.expect(IllegalStateException.class);
+
+    underTest.execute();
+  }
+
+
+}
index 1cb135035acc51bc971b1299221581731d52f69a..fa799924abaf8f5f67ae241aa897d4e8d24fe424 100644 (file)
@@ -34,7 +34,7 @@ public class DbVersion72Test {
 
   @Test
   public void verify_migration_count() {
-    verifyMigrationCount(underTest, 7);
+    verifyMigrationCount(underTest, 9);
   }
 
 }
diff --git a/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v72/PopulateUUIDOnUsersTest.java b/server/sonar-db-migration/src/test/java/org/sonar/server/platform/db/migration/version/v72/PopulateUUIDOnUsersTest.java
new file mode 100644 (file)
index 0000000..948a36c
--- /dev/null
@@ -0,0 +1,129 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2018 SonarSource SA
+ * mailto:info AT sonarsource DOT com
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 3 of the License, or (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public License
+ * along with this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
+ */
+package org.sonar.server.platform.db.migration.version.v72;
+
+import java.sql.SQLException;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.stream.Collectors;
+
+import org.assertj.core.groups.Tuple;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+import org.sonar.api.utils.System2;
+import org.sonar.api.utils.internal.TestSystem2;
+import org.sonar.core.util.UuidFactory;
+import org.sonar.core.util.UuidFactoryFast;
+import org.sonar.db.CoreDbTester;
+
+import static java.util.stream.Collectors.toList;
+import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.assertj.core.api.Assertions.tuple;
+import static org.junit.rules.ExpectedException.none;
+import static org.sonar.db.CoreDbTester.createForSchema;
+
+public class PopulateUUIDOnUsersTest {
+
+  private final static long PAST = 10_000_000_000L;
+  private static final long NOW = 50_000_000_000L;
+  private System2 system2 = new TestSystem2().setNow(NOW);
+
+  private final static String NO_LOGIN = null;
+  private final static String NO_UUID = null;
+
+  @Rule
+  public ExpectedException expectedException = none();
+
+  @Rule
+  public CoreDbTester db = createForSchema(PopulateUUIDOnUsersTest.class, "users.sql");
+
+  private UuidFactory uuidFactory = UuidFactoryFast.getInstance();
+
+  private PopulateUUIDOnUsers underTest = new PopulateUUIDOnUsers(db.database(), system2, uuidFactory);
+
+  @Test
+  public void update_uuid_when_login_is_present() throws SQLException {
+
+    String login1 = insertUser(NO_UUID, randomAlphanumeric(10));
+    String login2 = insertUser(NO_UUID, randomAlphanumeric(10));
+    String login3 = insertUser(NO_UUID, randomAlphanumeric(10));
+
+    underTest.execute();
+
+    assertUser(
+      tuple(login1, login1, PAST, NOW),
+      tuple(login2, login2, PAST, NOW),
+      tuple(login3, login3, PAST, NOW)
+    );
+  }
+
+  @Test
+  public void generate_random_uuid_when_login_is_null() throws SQLException {
+    insertUser(NO_UUID, NO_LOGIN);
+    insertUser(NO_UUID, NO_LOGIN);
+    insertUser(NO_UUID, NO_LOGIN);
+
+    underTest.execute();
+
+    assertThat(new ArrayList<>(db.select("SELECT distinct UUID FROM USERS"))).hasSize(3);
+  }
+
+  @Test
+  public void _do_nothing_when_uuid_is_already_present() throws SQLException {
+    String login1 = insertUser(NO_UUID, randomAlphanumeric(10));
+    String login2 = insertUser("existing-uuid", randomAlphanumeric(10));
+
+    underTest.execute();
+
+    assertUser(
+      tuple(login1, login1, PAST, NOW),
+      tuple("existing-uuid", login2, PAST, PAST)
+    );
+  }
+
+  @Test
+  public void is_reentrant() throws SQLException {
+    String login1 = insertUser(NO_UUID, randomAlphanumeric(10));
+    String login2 = insertUser("existing-uuid", randomAlphanumeric(10));
+
+    underTest.execute();
+    underTest.execute();
+
+    assertUser(
+      tuple(login1, login1, PAST, NOW),
+      tuple("existing-uuid", login2, PAST, PAST)
+    );
+  }
+
+  private String insertUser(String uuid, String login) {
+    db.executeInsert("USERS", "UUID", uuid, "LOGIN", login, "IS_ROOT", false, "ONBOARDED", false, "CREATED_AT", PAST, "UPDATED_AT", PAST);
+    return login;
+  }
+
+  private void assertUser(Tuple... expectedTuples) {
+    assertThat(db.select("SELECT LOGIN, UUID, CREATED_AT, UPDATED_AT FROM USERS")
+      .stream()
+      .map(map -> new Tuple(map.get("UUID"), map.get("LOGIN"), map.get("CREATED_AT"), map.get("UPDATED_AT")))
+      .collect(toList()))
+      .containsExactlyInAnyOrder(expectedTuples);
+  }
+}
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v72/AddUUIDtoUsers/users.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v72/AddUUIDtoUsers/users.sql
new file mode 100644 (file)
index 0000000..7788824
--- /dev/null
@@ -0,0 +1,22 @@
+CREATE TABLE "USERS" (
+  "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+  "LOGIN" VARCHAR(255),
+  "NAME" VARCHAR(200),
+  "EMAIL" VARCHAR(100),
+  "CRYPTED_PASSWORD" VARCHAR(100),
+  "SALT" VARCHAR(40),
+  "HASH_METHOD" VARCHAR(10),
+  "ACTIVE" BOOLEAN DEFAULT TRUE,
+  "SCM_ACCOUNTS" VARCHAR(4000),
+  "EXTERNAL_IDENTITY" VARCHAR(255),
+  "EXTERNAL_IDENTITY_PROVIDER" VARCHAR(100),
+  "IS_ROOT" BOOLEAN NOT NULL,
+  "USER_LOCAL" BOOLEAN,
+  "ONBOARDED" BOOLEAN NOT NULL,
+  "CREATED_AT" BIGINT,
+  "UPDATED_AT" BIGINT,
+  "HOMEPAGE_TYPE" VARCHAR(40),
+  "HOMEPAGE_PARAMETER" VARCHAR(40)
+);
+CREATE UNIQUE INDEX "USERS_LOGIN" ON "USERS" ("LOGIN");
+CREATE INDEX "USERS_UPDATED_AT" ON "USERS" ("UPDATED_AT");
diff --git a/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v72/PopulateUUIDOnUsersTest/users.sql b/server/sonar-db-migration/src/test/resources/org/sonar/server/platform/db/migration/version/v72/PopulateUUIDOnUsersTest/users.sql
new file mode 100644 (file)
index 0000000..9f03d34
--- /dev/null
@@ -0,0 +1,23 @@
+CREATE TABLE "USERS" (
+  "ID" INTEGER NOT NULL GENERATED BY DEFAULT AS IDENTITY (START WITH 1, INCREMENT BY 1),
+  "UUID" VARCHAR(40),
+  "LOGIN" VARCHAR(255),
+  "NAME" VARCHAR(200),
+  "EMAIL" VARCHAR(100),
+  "CRYPTED_PASSWORD" VARCHAR(100),
+  "SALT" VARCHAR(40),
+  "HASH_METHOD" VARCHAR(10),
+  "ACTIVE" BOOLEAN DEFAULT TRUE,
+  "SCM_ACCOUNTS" VARCHAR(4000),
+  "EXTERNAL_IDENTITY" VARCHAR(255),
+  "EXTERNAL_IDENTITY_PROVIDER" VARCHAR(100),
+  "IS_ROOT" BOOLEAN NOT NULL,
+  "USER_LOCAL" BOOLEAN,
+  "ONBOARDED" BOOLEAN NOT NULL,
+  "CREATED_AT" BIGINT,
+  "UPDATED_AT" BIGINT,
+  "HOMEPAGE_TYPE" VARCHAR(40),
+  "HOMEPAGE_PARAMETER" VARCHAR(40)
+);
+CREATE UNIQUE INDEX "USERS_LOGIN" ON "USERS" ("LOGIN");
+CREATE INDEX "USERS_UPDATED_AT" ON "USERS" ("UPDATED_AT");
index 6ac43702ab6735ae6cf56438e2321216f42bb05a..eed583f2333946b1d294b0e2fef64572f8a838fd 100644 (file)
@@ -29,11 +29,13 @@ import org.junit.rules.ExpectedException;
 import org.mindrot.jbcrypt.BCrypt;
 import org.sonar.db.DbTester;
 import org.sonar.db.user.UserDto;
+import org.sonar.db.user.UserTesting;
 import org.sonar.server.authentication.event.AuthenticationEvent;
 import org.sonar.server.authentication.event.AuthenticationException;
 
 import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
 import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.db.user.UserTesting.newUserDto;
 import static org.sonar.server.authentication.LocalAuthentication.HashMethod.BCRYPT;
 import static org.sonar.server.authentication.LocalAuthentication.HashMethod.SHA1;
 
@@ -49,7 +51,7 @@ public class LocalAuthenticationTest {
 
   @Test
   public void incorrect_hash_should_throw_AuthenticationException() {
-    UserDto user = new UserDto()
+    UserDto user = newUserDto()
       .setHashMethod("ALGON2");
 
     expectedException.expect(AuthenticationException.class);
@@ -60,7 +62,7 @@ public class LocalAuthenticationTest {
 
   @Test
   public void null_hash_should_throw_AuthenticationException() {
-    UserDto user = new UserDto();
+    UserDto user = newUserDto();
 
     expectedException.expect(AuthenticationException.class);
     expectedException.expectMessage("null hash method");
@@ -72,7 +74,7 @@ public class LocalAuthenticationTest {
   public void authentication_with_bcrypt_with_correct_password_should_work() {
     String password = randomAlphanumeric(60);
 
-    UserDto user = new UserDto()
+    UserDto user = newUserDto()
       .setHashMethod(BCRYPT.name())
       .setCryptedPassword(BCrypt.hashpw(password, BCrypt.gensalt(12)));
 
@@ -87,7 +89,7 @@ public class LocalAuthenticationTest {
     RANDOM.nextBytes(saltRandom);
     String salt = DigestUtils.sha1Hex(saltRandom);
 
-    UserDto user = new UserDto()
+    UserDto user = newUserDto()
       .setHashMethod(SHA1.name())
       .setCryptedPassword(DigestUtils.sha1Hex("--" + salt + "--" + password + "--"))
       .setSalt(salt);
@@ -103,7 +105,7 @@ public class LocalAuthenticationTest {
     RANDOM.nextBytes(saltRandom);
     String salt = DigestUtils.sha1Hex(saltRandom);
 
-    UserDto user = new UserDto()
+    UserDto user = newUserDto()
       .setHashMethod(SHA1.name())
       .setCryptedPassword(DigestUtils.sha1Hex("--" + salt + "--" + password + "--"))
       .setSalt(salt);
@@ -120,8 +122,9 @@ public class LocalAuthenticationTest {
     RANDOM.nextBytes(saltRandom);
     String salt = DigestUtils.sha1Hex(saltRandom);
 
-    UserDto user = new UserDto()
+    UserDto user = newUserDto()
       .setHashMethod(SHA1.name())
+      .setCryptedPassword(null)
       .setSalt(salt);
 
     expectedException.expect(AuthenticationException.class);
@@ -134,7 +137,8 @@ public class LocalAuthenticationTest {
   public void authentication_with_sha1_with_empty_salt_should_throw_AuthenticationException() {
     String password = randomAlphanumeric(60);
 
-    UserDto user = new UserDto()
+    UserDto user = newUserDto()
+      .setSalt(null)
       .setHashMethod(SHA1.name())
       .setCryptedPassword(DigestUtils.sha1Hex("--0242b0b4c0a93ddfe09dd886de50bc25ba000b51--" + password + "--"));
 
@@ -148,7 +152,7 @@ public class LocalAuthenticationTest {
   public void authentication_with_bcrypt_with_incorrect_password_should_throw_AuthenticationException() {
     String password = randomAlphanumeric(60);
 
-    UserDto user = new UserDto()
+    UserDto user = newUserDto()
       .setHashMethod(BCRYPT.name())
       .setCryptedPassword(BCrypt.hashpw(password, BCrypt.gensalt(12)));
 
@@ -166,7 +170,7 @@ public class LocalAuthenticationTest {
     RANDOM.nextBytes(saltRandom);
     String salt = DigestUtils.sha1Hex(saltRandom);
 
-    UserDto user = new UserDto()
+    UserDto user = newUserDto()
       .setLogin("myself")
       .setHashMethod(SHA1.name())
       .setCryptedPassword(DigestUtils.sha1Hex("--" + salt + "--" + password + "--"))
index 8bd4ed073ea8800fa512b737b5db8835e9954716..ccc3aff971a0268131d0c29f825e1ef93f7bf664 100644 (file)
@@ -51,6 +51,7 @@ import org.sonar.db.protobuf.DbIssues;
 import org.sonar.db.rule.RuleDto;
 import org.sonar.db.rule.RuleTesting;
 import org.sonar.db.user.UserDto;
+import org.sonar.db.user.UserTesting;
 import org.sonar.server.es.EsTester;
 import org.sonar.server.es.SearchOptions;
 import org.sonar.server.es.StartupIndexer;
@@ -76,6 +77,7 @@ import static org.assertj.core.api.Assertions.assertThat;
 import static org.assertj.core.groups.Tuple.tuple;
 import static org.sonar.api.web.UserRole.ISSUE_ADMIN;
 import static org.sonar.db.component.ComponentTesting.newFileDto;
+import static org.sonar.db.user.UserTesting.newUserDto;
 import static org.sonarqube.ws.client.component.ComponentsWsParameters.PARAM_BRANCH;
 import static org.sonarqube.ws.client.issue.IssuesWsParameters.DEPRECATED_FACET_MODE_DEBT;
 import static org.sonarqube.ws.client.issue.IssuesWsParameters.FACET_MODE_EFFORT;
@@ -183,8 +185,8 @@ public class SearchActionTest {
 
   @Test
   public void response_contains_all_fields_except_additional_fields() {
-    dbClient.userDao().insert(session, new UserDto().setLogin("simon").setName("Simon").setEmail("simon@email.com"));
-    dbClient.userDao().insert(session, new UserDto().setLogin("fabrice").setName("Fabrice").setEmail("fabrice@email.com"));
+    db.users().insertUser(u -> u.setLogin("simon").setName("Simon").setEmail("simon@email.com"));
+    db.users().insertUser(u -> u.setLogin("fabrice").setName("Fabrice").setEmail("fabrice@email.com"));
 
     ComponentDto project = insertComponent(ComponentTesting.newPublicProjectDto(otherOrganization2, "PROJECT_ID").setDbKey("PROJECT_KEY"));
     indexPermissions();
@@ -280,8 +282,8 @@ public class SearchActionTest {
 
   @Test
   public void issue_with_comments() {
-    dbClient.userDao().insert(session, new UserDto().setLogin("john").setName("John"));
-    dbClient.userDao().insert(session, new UserDto().setLogin("fabrice").setName("Fabrice").setEmail("fabrice@email.com"));
+    db.users().insertUser(u -> u.setLogin("john").setName("John"));
+    db.users().insertUser(u -> u.setLogin("fabrice").setName("Fabrice").setEmail("fabrice@email.com"));
 
     ComponentDto project = insertComponent(ComponentTesting.newPublicProjectDto(otherOrganization2, "PROJECT_ID").setDbKey("PROJECT_KEY"));
     indexPermissions();
@@ -316,8 +318,8 @@ public class SearchActionTest {
 
   @Test
   public void issue_with_comment_hidden() {
-    dbClient.userDao().insert(session, new UserDto().setLogin("john").setName("John").setEmail("john@email.com"));
-    dbClient.userDao().insert(session, new UserDto().setLogin("fabrice").setName("Fabrice").setEmail("fabrice@email.com"));
+    db.users().insertUser(u -> u.setLogin("john").setName("John").setEmail("john@email.com"));
+    db.users().insertUser(u -> u.setLogin("fabrice").setName("Fabrice").setEmail("fabrice@email.com"));
 
     ComponentDto project = insertComponent(ComponentTesting.newPublicProjectDto(otherOrganization1, "PROJECT_ID").setDbKey("PROJECT_KEY"));
     indexPermissions();
@@ -351,8 +353,9 @@ public class SearchActionTest {
 
   @Test
   public void load_additional_fields() {
-    dbClient.userDao().insert(session, new UserDto().setLogin("simon").setName("Simon").setEmail("simon@email.com"));
-    dbClient.userDao().insert(session, new UserDto().setLogin("fabrice").setName("Fabrice").setEmail("fabrice@email.com"));
+    db.users().insertUser(u -> u.setLogin("simon").setName("Simon").setEmail("simon@email.com"));
+    db.users().insertUser(u -> u.setLogin("fabrice").setName("Fabrice").setEmail("fabrice@email.com"));
+
     ComponentDto project = insertComponent(ComponentTesting.newPublicProjectDto(otherOrganization2, "PROJECT_ID").setDbKey("PROJECT_KEY").setLanguage("java"));
     indexPermissions();
     ComponentDto file = insertComponent(newFileDto(project, null, "FILE_ID").setDbKey("FILE_KEY").setLanguage("js"));
@@ -373,8 +376,8 @@ public class SearchActionTest {
 
   @Test
   public void load_additional_fields_with_issue_admin_permission() {
-    dbClient.userDao().insert(session, new UserDto().setLogin("simon").setName("Simon").setEmail("simon@email.com"));
-    dbClient.userDao().insert(session, new UserDto().setLogin("fabrice").setName("Fabrice").setEmail("fabrice@email.com"));
+    db.users().insertUser(u -> u.setLogin("simon").setName("Simon").setEmail("simon@email.com"));
+    db.users().insertUser(u -> u.setLogin("fabrice").setName("Fabrice").setEmail("fabrice@email.com"));
     ComponentDto project = insertComponent(ComponentTesting.newPublicProjectDto(otherOrganization1, "PROJECT_ID").setDbKey("PROJECT_KEY").setLanguage("java"));
     grantPermissionToAnyone(project, ISSUE_ADMIN);
     indexPermissions();
@@ -567,7 +570,7 @@ public class SearchActionTest {
 
   @Test
   public void filter_by_assigned_to_me() {
-    dbClient.userDao().insert(session, new UserDto().setLogin("john").setName("John").setEmail("john@email.com"));
+    dbClient.userDao().insert(session, newUserDto().setLogin("john").setName("John").setEmail("john@email.com"));
 
     ComponentDto project = insertComponent(ComponentTesting.newPublicProjectDto(defaultOrganization, "PROJECT_ID").setDbKey("PROJECT_KEY"));
     indexPermissions();
@@ -641,7 +644,7 @@ public class SearchActionTest {
 
   @Test
   public void assigned_to_me_facet_is_sticky_relative_to_assignees() {
-    dbClient.userDao().insert(session, new UserDto().setLogin("alice").setName("Alice").setEmail("alice@email.com"));
+    dbClient.userDao().insert(session, newUserDto().setLogin("alice").setName("Alice").setEmail("alice@email.com"));
 
     ComponentDto project = insertComponent(ComponentTesting.newPublicProjectDto(otherOrganization2, "PROJECT_ID").setDbKey("PROJECT_KEY"));
     indexPermissions();
index 962693cc967503b18ed7dfaa605766b56f9476a1..495a4c15c10a576127ee4b376166b260780f3b1d 100644 (file)
@@ -41,6 +41,7 @@ import org.sonar.db.metric.MetricTesting;
 import org.sonar.db.organization.OrganizationDto;
 import org.sonar.db.permission.OrganizationPermission;
 import org.sonar.db.user.UserDto;
+import org.sonar.db.user.UserTesting;
 import org.sonar.server.component.TestComponentFinder;
 import org.sonar.server.es.EsTester;
 import org.sonar.server.exceptions.BadRequestException;
@@ -59,6 +60,7 @@ import static org.sonar.api.measures.Metric.ValueType.INT;
 import static org.sonar.api.measures.Metric.ValueType.LEVEL;
 import static org.sonar.api.measures.Metric.ValueType.STRING;
 import static org.sonar.api.measures.Metric.ValueType.WORK_DUR;
+import static org.sonar.db.user.UserTesting.newUserDto;
 import static org.sonar.server.util.TypeValidationsTesting.newFullTypeValidations;
 
 public class CreateActionTest {
@@ -85,7 +87,7 @@ public class CreateActionTest {
     ws = new WsTester(new CustomMeasuresWs(new CreateAction(dbClient, userSession, System2.INSTANCE, new CustomMeasureValidator(newFullTypeValidations()),
       new CustomMeasureJsonWriter(new UserJsonWriter(userSession)), TestComponentFinder.from(db))));
 
-    db.getDbClient().userDao().insert(dbSession, new UserDto()
+    db.getDbClient().userDao().insert(dbSession, newUserDto()
       .setLogin("login")
       .setName("Login")
       .setEmail("login@login.com")
index d57eaf6b254baaacd1851766f4dd40500f924aab..dba9e84de3018f32a1b0dcea4c2faccdefde4b3b 100644 (file)
@@ -39,6 +39,7 @@ import org.sonar.db.component.SnapshotTesting;
 import org.sonar.db.measure.custom.CustomMeasureDto;
 import org.sonar.db.metric.MetricDto;
 import org.sonar.db.user.UserDto;
+import org.sonar.db.user.UserTesting;
 import org.sonar.server.component.TestComponentFinder;
 import org.sonar.server.es.EsTester;
 import org.sonar.server.exceptions.ForbiddenException;
@@ -50,6 +51,7 @@ import org.sonar.server.ws.WsTester;
 import static org.assertj.core.api.Assertions.assertThat;
 import static org.sonar.db.measure.custom.CustomMeasureTesting.newCustomMeasureDto;
 import static org.sonar.db.metric.MetricTesting.newMetricDto;
+import static org.sonar.db.user.UserTesting.newUserDto;
 
 public class SearchActionTest {
 
@@ -78,7 +80,7 @@ public class SearchActionTest {
     defaultProject = insertDefaultProject();
     userSessionRule.logIn().addProjectPermission(UserRole.ADMIN, defaultProject);
 
-    db.getDbClient().userDao().insert(dbSession, new UserDto()
+    db.getDbClient().userDao().insert(dbSession, newUserDto()
       .setLogin("login")
       .setName("Login")
       .setEmail("login@login.com")
index b065df28f12595e4cc476e513587ce6a9f7d84f3..64a48357f2e2d414645e2f3847b0c1fb2e0d45cb 100644 (file)
@@ -37,6 +37,7 @@ import org.sonar.db.metric.MetricDto;
 import org.sonar.db.metric.MetricTesting;
 import org.sonar.db.organization.OrganizationDto;
 import org.sonar.db.user.UserDto;
+import org.sonar.db.user.UserTesting;
 import org.sonar.server.es.EsTester;
 import org.sonar.server.exceptions.ForbiddenException;
 import org.sonar.server.exceptions.ServerException;
@@ -50,6 +51,7 @@ import static org.assertj.core.data.Offset.offset;
 import static org.mockito.Mockito.mock;
 import static org.mockito.Mockito.when;
 import static org.sonar.db.measure.custom.CustomMeasureTesting.newCustomMeasureDto;
+import static org.sonar.db.user.UserTesting.newUserDto;
 import static org.sonar.server.measure.custom.ws.UpdateAction.PARAM_DESCRIPTION;
 import static org.sonar.server.measure.custom.ws.UpdateAction.PARAM_ID;
 import static org.sonar.server.measure.custom.ws.UpdateAction.PARAM_VALUE;
@@ -76,7 +78,7 @@ public class UpdateActionTest {
 
     ws = new WsTester(new CustomMeasuresWs(new UpdateAction(dbClient, userSessionRule, system, validator, new CustomMeasureJsonWriter(new UserJsonWriter(userSessionRule)))));
 
-    db.getDbClient().userDao().insert(dbSession, new UserDto()
+    db.getDbClient().userDao().insert(dbSession, newUserDto()
       .setLogin("login")
       .setName("Login")
       .setEmail("login@login.com")
index 088476f12066fca07dcb799ea8dd2817b0571426..f38fa07d8d424849680e64d16547a2c241413bc1 100644 (file)
  */
 package org.sonar.server.user;
 
-import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.sonar.api.database.model.User;
 import org.sonar.api.utils.System2;
 import org.sonar.db.DbTester;
+import org.sonar.db.user.UserDto;
 
 import static org.hamcrest.CoreMatchers.is;
 import static org.hamcrest.CoreMatchers.nullValue;
 import static org.junit.Assert.assertThat;
-
+import static org.sonar.db.DbTester.create;
 
 public class DeprecatedUserFinderTest {
 
   @Rule
-  public DbTester dbTester = DbTester.create(System2.INSTANCE);
+  public DbTester dbTester = create(System2.INSTANCE);
   private DeprecatedUserFinder underTest = new DeprecatedUserFinder(dbTester.getDbClient());
-  
-  @Before
-  public void init() {
-    dbTester.prepareDbUnit(DeprecatedUserFinderTest.class, "fixture.xml");
-  }
 
   @Test
   public void shouldFindUserByLogin() {
-    
-    User user = underTest.findByLogin("simon");
-    assertThat(user.getId(), is(1));
+
+    UserDto simon = dbTester.users().insertUser(u -> u.setLogin("simon").setName("Simon Brandhof").setEmail("simon.brandhof@sonarsource.com"));
+    UserDto evgeny = dbTester.users().insertUser(u -> u.setLogin("godin").setName("Evgeny Mandrikov").setEmail("evgeny.mandrikov@sonarsource.com"));
+
+    User user = underTest.findByLogin(simon.getLogin());
+    assertThat(user.getId(), is(simon.getId()));
     assertThat(user.getLogin(), is("simon"));
     assertThat(user.getName(), is("Simon Brandhof"));
     assertThat(user.getEmail(), is("simon.brandhof@sonarsource.com"));
 
-    user = underTest.findByLogin("godin");
-    assertThat(user.getId(), is(2));
+    user = underTest.findByLogin(evgeny.getLogin());
+    assertThat(user.getId(), is(evgeny.getId()));
     assertThat(user.getLogin(), is("godin"));
     assertThat(user.getName(), is("Evgeny Mandrikov"));
     assertThat(user.getEmail(), is("evgeny.mandrikov@sonarsource.com"));
@@ -63,19 +61,22 @@ public class DeprecatedUserFinderTest {
 
   @Test
   public void shouldFindUserById() {
-    User user = underTest.findById(1);
-    assertThat(user.getId(), is(1));
+    UserDto simon = dbTester.users().insertUser(u -> u.setLogin("simon").setName("Simon Brandhof").setEmail("simon.brandhof@sonarsource.com"));
+    UserDto evgeny = dbTester.users().insertUser(u -> u.setLogin("godin").setName("Evgeny Mandrikov").setEmail("evgeny.mandrikov@sonarsource.com"));
+
+    User user = underTest.findById(simon.getId());
+    assertThat(user.getId(), is(simon.getId()));
     assertThat(user.getLogin(), is("simon"));
     assertThat(user.getName(), is("Simon Brandhof"));
     assertThat(user.getEmail(), is("simon.brandhof@sonarsource.com"));
 
-    user = underTest.findById(2);
-    assertThat(user.getId(), is(2));
+    user = underTest.findById(evgeny.getId());
+    assertThat(user.getId(), is(evgeny.getId()));
     assertThat(user.getLogin(), is("godin"));
     assertThat(user.getName(), is("Evgeny Mandrikov"));
     assertThat(user.getEmail(), is("evgeny.mandrikov@sonarsource.com"));
 
-    user = underTest.findById(3);
+    user = underTest.findById(999);
     assertThat(user, nullValue());
   }
 
index 1b30f2fe0d87cc9869ef34246d6caac3954d01f1..8659995132f81d7909e3f14661dedcfcfbb0f866 100644 (file)
@@ -263,7 +263,7 @@ public class SearchActionTest {
   public void only_return_login_and_name_when_not_logged() throws Exception {
     userSession.anonymous();
 
-    dbClient.userDao().insert(dbSession, UserTesting.newUserDto("john", "John", "john@email.com"));
+    dbClient.userDao().insert(dbSession, newUserDto("john", "John", "john@email.com"));
     dbSession.commit();
     userIndexer.indexOnStartup(null);
 
@@ -288,7 +288,7 @@ public class SearchActionTest {
       String name = String.format("User %d", index);
       List<String> scmAccounts = singletonList(String.format("user-%d", index));
 
-      UserDto userDto = dbClient.userDao().insert(dbSession, new UserDto()
+      UserDto userDto = dbClient.userDao().insert(dbSession, newUserDto()
         .setActive(true)
         .setEmail(email)
         .setLogin(login)
diff --git a/server/sonar-server/src/test/resources/org/sonar/server/user/DeprecatedUserFinderTest/fixture.xml b/server/sonar-server/src/test/resources/org/sonar/server/user/DeprecatedUserFinderTest/fixture.xml
deleted file mode 100644 (file)
index 6102835..0000000
+++ /dev/null
@@ -1,16 +0,0 @@
-<dataset>
-
-  <users id="1"
-         login="simon"
-         name="Simon Brandhof"
-         email="simon.brandhof@sonarsource.com"
-         is_root="[false]"
-         onboarded="[true]"/>
-  <users id="2"
-         login="godin"
-         name="Evgeny Mandrikov"
-         email="evgeny.mandrikov@sonarsource.com"
-         is_root="[false]"
-         onboarded="[true]"/>
-
-</dataset>