3 * Copyright (C) 2009-2016 SonarSource SA
4 * mailto:contact AT sonarsource DOT com
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 3 of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public License
17 * along with this program; if not, write to the Free Software Foundation,
18 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 package org.sonar.server.platform.db.migration.version.v63;
22 import java.sql.SQLException;
23 import org.junit.Rule;
24 import org.junit.Test;
25 import org.junit.rules.ExpectedException;
26 import org.sonar.api.utils.internal.TestSystem2;
27 import org.sonar.db.DbTester;
28 import org.sonar.db.user.UserDto;
29 import org.sonar.db.user.UserTesting;
31 public class UnsetUserRootFlagsTest {
33 private static final long CREATED_AT = 1_500L;
34 private static final long FIXED_AT = 1_600L;
37 public ExpectedException expectedException = ExpectedException.none();
39 private TestSystem2 system = new TestSystem2().setNow(FIXED_AT);
42 public DbTester db = DbTester.createForSchema(system, UnsetUserRootFlagsTest.class, "in_progress_users.sql");
44 private UnsetUserRootFlags underTest = new UnsetUserRootFlags(db.database(), system);
47 public void sets_USERS_IS_ROOT_to_false() throws SQLException {
48 UserDto root1 = db.users().makeRoot(createUser());
49 UserDto user1 = createUser();
50 UserDto root2 = db.users().makeRoot(createUser());
51 UserDto user2 = createUser();
55 verifyNotRoot(CREATED_AT, user1, user2);
56 verifyNotRoot(FIXED_AT, root1, root2);
60 public void migration_is_reentrant() throws SQLException {
61 UserDto root = db.users().makeRoot(createUser());
64 verifyNotRoot(FIXED_AT, root);
66 system.setNow(FIXED_AT + 100L);
68 verifyNotRoot(FIXED_AT, root);
71 private void verifyNotRoot(long updatedAt, UserDto... users) {
72 for (UserDto user : users) {
73 db.rootFlag().verify(user, false, updatedAt);
77 private UserDto createUser() {
78 return db.users().insertUser(UserTesting.newUserDto()
79 .setCreatedAt(CREATED_AT)
80 .setUpdatedAt(CREATED_AT));