2 * SonarQube, open source software quality management tool.
3 * Copyright (C) 2008-2014 SonarSource
4 * mailto:contact AT sonarsource DOT com
6 * SonarQube 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 * SonarQube 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.
21 package org.sonar.server.db.migrations.v51;
23 import org.junit.Before;
24 import org.junit.ClassRule;
25 import org.junit.Test;
26 import org.sonar.api.utils.System2;
27 import org.sonar.core.persistence.DbTester;
28 import org.sonar.server.db.migrations.DatabaseMigration;
32 import static org.assertj.core.api.Assertions.assertThat;
33 import static org.mockito.Mockito.mock;
34 import static org.mockito.Mockito.when;
36 public class CopyScmAccountsFromAuthorsToUsersTest {
39 public static DbTester db = new DbTester().schema(CopyScmAccountsFromAuthorsToUsersTest.class, "schema.sql");
41 DatabaseMigration migration;
42 System2 system = mock(System2.class);
45 public void setUp() throws Exception {
46 db.executeUpdateSql("truncate table authors");
47 db.executeUpdateSql("truncate table users");
48 migration = new CopyScmAccountsFromAuthorsToUsers(db.database(), system);
52 public void migrate() throws Exception {
53 db.prepareDbUnit(getClass(), "before.xml");
54 Long oldDate = 1500000000000L;
55 Long updatedDate = 2000000000000L;
56 when(system.now()).thenReturn(updatedDate);
60 User simon = getUserByLogin("simon");
61 assertThat(simon.scmAccounts).isEqualTo(",Simon B,simon@codehaus.org,");
62 assertThat(simon.updatedAt).isEqualTo(updatedDate);
64 User fabrice = getUserByLogin("fabrice");
65 assertThat(fabrice.scmAccounts).isEqualTo(",fab,");
66 assertThat(fabrice.updatedAt).isEqualTo(updatedDate);
68 assertThat(getUserByLogin("julien").updatedAt).isEqualTo(oldDate);
69 assertThat(getUserByLogin("jb").updatedAt).isEqualTo(oldDate);
70 assertThat(getUserByLogin("disable").updatedAt).isEqualTo(oldDate);
71 assertThat(getUserByLogin("teryk").updatedAt).isEqualTo(oldDate);
72 assertThat(getUserByLogin("teryk2").updatedAt).isEqualTo(oldDate);
76 public void nothing_to_migrate_when_no_authors() throws Exception {
77 db.prepareDbUnit(getClass(), "no_authors.xml");
78 Long oldDate = 1500000000000L;
79 Long updatedDate = 2000000000000L;
80 when(system.now()).thenReturn(updatedDate);
84 assertThat(db.countSql("SELECT count(*) FROM USERS WHERE updated_at=" + updatedDate)).isEqualTo(0);
85 assertThat(db.countSql("SELECT count(*) FROM USERS WHERE updated_at=" + oldDate)).isEqualTo(7);
88 private User getUserByLogin(String login) {
89 return new User(db.selectFirst("SELECT u.scm_Accounts as \"scmAccounts\", u.updated_at as \"updatedAt\" FROM users u WHERE u.login='" + login + "'"));
92 private static class User {
96 User(Map<String, Object> map) {
97 scmAccounts = (String) map.get("scmAccounts");
98 updatedAt = (Long) map.get("updatedAt");