aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-db/src/test/java/org/sonar
diff options
context:
space:
mode:
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2015-11-23 09:04:16 +0100
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>2015-11-23 17:26:29 +0100
commit7fb14c3d55ba19b85e7361577a92ee5a0f8ccefc (patch)
tree5cfe848ea35b532f0804b7db4fcc0b9c1e0be66e /sonar-db/src/test/java/org/sonar
parentfac2525b1ba5448bbfd2efdb672d708d889e993d (diff)
downloadsonarqube-7fb14c3d55ba19b85e7361577a92ee5a0f8ccefc.tar.gz
sonarqube-7fb14c3d55ba19b85e7361577a92ee5a0f8ccefc.zip
SONAR-7039 Generate user tokens
Diffstat (limited to 'sonar-db/src/test/java/org/sonar')
-rw-r--r--sonar-db/src/test/java/org/sonar/db/DaoModuleTest.java2
-rw-r--r--sonar-db/src/test/java/org/sonar/db/user/UserDaoTest.java20
-rw-r--r--sonar-db/src/test/java/org/sonar/db/user/UserDbTester.java44
-rw-r--r--sonar-db/src/test/java/org/sonar/db/user/UserTesting.java3
-rw-r--r--sonar-db/src/test/java/org/sonar/db/user/UserTokenDaoTest.java101
-rw-r--r--sonar-db/src/test/java/org/sonar/db/user/UserTokenTesting.java34
6 files changed, 184 insertions, 20 deletions
diff --git a/sonar-db/src/test/java/org/sonar/db/DaoModuleTest.java b/sonar-db/src/test/java/org/sonar/db/DaoModuleTest.java
index 29e06e13da5..df0f4964ef9 100644
--- a/sonar-db/src/test/java/org/sonar/db/DaoModuleTest.java
+++ b/sonar-db/src/test/java/org/sonar/db/DaoModuleTest.java
@@ -30,6 +30,6 @@ public class DaoModuleTest {
public void verify_count_of_added_components() {
ComponentContainer container = new ComponentContainer();
new DaoModule().configure(container);
- assertThat(container.size()).isEqualTo(47);
+ assertThat(container.size()).isEqualTo(48);
}
}
diff --git a/sonar-db/src/test/java/org/sonar/db/user/UserDaoTest.java b/sonar-db/src/test/java/org/sonar/db/user/UserDaoTest.java
index d0fe4e7ee84..2502d75ff39 100644
--- a/sonar-db/src/test/java/org/sonar/db/user/UserDaoTest.java
+++ b/sonar-db/src/test/java/org/sonar/db/user/UserDaoTest.java
@@ -22,8 +22,6 @@ package org.sonar.db.user;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
-import org.junit.After;
-import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.experimental.categories.Category;
@@ -50,19 +48,7 @@ public class UserDaoTest {
public DbTester db = DbTester.create(system2);
UserDao underTest = db.getDbClient().userDao();
- DbSession session;
-
- @Before
- public void before() {
- db.truncateTables();
-
- this.session = db.getSession();
- }
-
- @After
- public void after() {
- this.session.close();
- }
+ DbSession session = db.getSession();
@Test
public void selectUserByLogin_ignore_inactive() {
@@ -233,7 +219,7 @@ public class UserDaoTest {
when(system2.now()).thenReturn(1500000000000L);
String login = "marius";
- boolean deactivated = underTest.deactivateUserByLogin(login);
+ boolean deactivated = underTest.deactivateUserByLogin(session, login);
assertThat(deactivated).isTrue();
assertThat(underTest.selectActiveUserByLogin(login)).isNull();
@@ -253,7 +239,7 @@ public class UserDaoTest {
db.prepareDbUnit(getClass(), "deactivate_user.xml");
String login = "does_not_exist";
- boolean deactivated = underTest.deactivateUserByLogin(login);
+ boolean deactivated = underTest.deactivateUserByLogin(session, login);
assertThat(deactivated).isFalse();
assertThat(underTest.selectActiveUserByLogin(login)).isNull();
}
diff --git a/sonar-db/src/test/java/org/sonar/db/user/UserDbTester.java b/sonar-db/src/test/java/org/sonar/db/user/UserDbTester.java
new file mode 100644
index 00000000000..ec25f9b0821
--- /dev/null
+++ b/sonar-db/src/test/java/org/sonar/db/user/UserDbTester.java
@@ -0,0 +1,44 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.db.user;
+
+import org.sonar.db.DbClient;
+import org.sonar.db.DbSession;
+import org.sonar.db.DbTester;
+
+public class UserDbTester {
+ private final DbTester db;
+ private final DbClient dbClient;
+ private final DbSession dbSession;
+
+ public UserDbTester(DbTester db) {
+ this.db = db;
+ this.dbClient = db.getDbClient();
+ this.dbSession = db.getSession();
+ }
+
+ public UserDto insertUser(UserDto userDto) {
+ UserDto updatedUser = dbClient.userDao().insert(dbSession, userDto);
+ db.commit();
+
+ return updatedUser;
+ }
+}
diff --git a/sonar-db/src/test/java/org/sonar/db/user/UserTesting.java b/sonar-db/src/test/java/org/sonar/db/user/UserTesting.java
index 04bb4ab02bb..77799132dad 100644
--- a/sonar-db/src/test/java/org/sonar/db/user/UserTesting.java
+++ b/sonar-db/src/test/java/org/sonar/db/user/UserTesting.java
@@ -22,14 +22,13 @@ package org.sonar.db.user;
import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic;
import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
-import static org.apache.commons.lang.math.RandomUtils.nextBoolean;
import static org.apache.commons.lang.math.RandomUtils.nextLong;
public class UserTesting {
public static UserDto newUserDto() {
UserDto user = new UserDto()
- .setActive(nextBoolean())
+ .setActive(true)
.setName(randomAlphanumeric(30))
.setEmail(randomAlphabetic(30))
.setLogin(randomAlphanumeric(30));
diff --git a/sonar-db/src/test/java/org/sonar/db/user/UserTokenDaoTest.java b/sonar-db/src/test/java/org/sonar/db/user/UserTokenDaoTest.java
new file mode 100644
index 00000000000..55ea930b442
--- /dev/null
+++ b/sonar-db/src/test/java/org/sonar/db/user/UserTokenDaoTest.java
@@ -0,0 +1,101 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.db.user;
+
+import com.google.common.base.Optional;
+import org.assertj.guava.api.Assertions;
+import org.junit.Before;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.experimental.categories.Category;
+import org.sonar.api.utils.System2;
+import org.sonar.db.DbSession;
+import org.sonar.db.DbTester;
+import org.sonar.test.DbTests;
+
+import static org.assertj.core.api.Assertions.assertThat;
+import static org.sonar.db.user.UserTokenTesting.newUserToken;
+
+@Category(DbTests.class)
+public class UserTokenDaoTest {
+ @Rule
+ public DbTester db = DbTester.create(System2.INSTANCE);
+
+ DbSession dbSession;
+
+ UserTokenDao underTest;
+
+ @Before
+ public void setUp() {
+ underTest = db.getDbClient().userTokenDao();
+ dbSession = db.getSession();
+ }
+
+ @Test
+ public void insert_token() {
+ UserTokenDto userToken = newUserToken();
+
+ insertToken(userToken);
+
+ UserTokenDto userTokenFromDb = underTest.selectOrFailByTokenHash(dbSession, userToken.getTokenHash());
+ assertThat(userTokenFromDb).isNotNull();
+ assertThat(userTokenFromDb.getName()).isEqualTo(userToken.getName());
+ assertThat(userTokenFromDb.getCreatedAt()).isEqualTo(userToken.getCreatedAt());
+ assertThat(userTokenFromDb.getTokenHash()).isEqualTo(userToken.getTokenHash());
+ assertThat(userTokenFromDb.getLogin()).isEqualTo(userToken.getLogin());
+ }
+
+ @Test
+ public void select_by_login_and_name() {
+ UserTokenDto userToken = newUserToken().setLogin("login").setName("name").setTokenHash("token");
+ insertToken(userToken);
+
+ Optional<UserTokenDto> optionalResultByLoginAndName = underTest.selectByLoginAndName(dbSession, userToken.getLogin(), userToken.getName());
+ UserTokenDto resultByLoginAndName = optionalResultByLoginAndName.get();
+ Optional<UserTokenDto> unfoundResult1 = underTest.selectByLoginAndName(dbSession, "unknown-login", userToken.getName());
+ Optional<UserTokenDto> unfoundResult2 = underTest.selectByLoginAndName(dbSession, userToken.getLogin(), "unknown-name");
+
+ Assertions.assertThat(unfoundResult1).isAbsent();
+ Assertions.assertThat(unfoundResult2).isAbsent();
+ assertThat(resultByLoginAndName.getLogin()).isEqualTo(userToken.getLogin());
+ assertThat(resultByLoginAndName.getName()).isEqualTo(userToken.getName());
+ assertThat(resultByLoginAndName.getCreatedAt()).isEqualTo(userToken.getCreatedAt());
+ assertThat(resultByLoginAndName.getTokenHash()).isEqualTo(userToken.getTokenHash());
+ }
+
+ @Test
+ public void delete_tokens_by_login() {
+ insertToken(newUserToken().setLogin("login-to-delete"));
+ insertToken(newUserToken().setLogin("login-to-delete"));
+ insertToken(newUserToken().setLogin("login-to-keep"));
+
+ underTest.deleteByLogin(dbSession, "login-to-delete");
+ db.commit();
+
+ assertThat(underTest.selectByLogin(dbSession, "login-to-delete")).isEmpty();
+ assertThat(underTest.selectByLogin(dbSession, "login-to-keep")).hasSize(1);
+ }
+
+ private void insertToken(UserTokenDto userToken) {
+ underTest.insert(dbSession, userToken);
+ dbSession.commit();
+ }
+}
diff --git a/sonar-db/src/test/java/org/sonar/db/user/UserTokenTesting.java b/sonar-db/src/test/java/org/sonar/db/user/UserTokenTesting.java
new file mode 100644
index 00000000000..84536ed7265
--- /dev/null
+++ b/sonar-db/src/test/java/org/sonar/db/user/UserTokenTesting.java
@@ -0,0 +1,34 @@
+/*
+ * SonarQube, open source software quality management tool.
+ * Copyright (C) 2008-2014 SonarSource
+ * mailto:contact AT sonarsource DOT com
+ *
+ * SonarQube 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.
+ *
+ * SonarQube 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.db.user;
+
+import static org.apache.commons.lang.RandomStringUtils.randomAlphanumeric;
+import static org.apache.commons.lang.math.RandomUtils.nextLong;
+
+public class UserTokenTesting {
+ public static UserTokenDto newUserToken() {
+ return new UserTokenDto()
+ .setLogin(randomAlphanumeric(255))
+ .setName(randomAlphanumeric(255))
+ .setTokenHash(randomAlphanumeric(40))
+ .setCreatedAt(nextLong());
+ }
+}