]> source.dussan.org Git - sonarqube.git/commitdiff
Improve UserTokenDao coverage
authorTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 24 Nov 2015 16:07:51 +0000 (17:07 +0100)
committerTeryk Bellahsene <teryk.bellahsene@sonarsource.com>
Tue, 24 Nov 2015 16:07:51 +0000 (17:07 +0100)
sonar-db/src/test/java/org/sonar/db/user/UserTokenDaoTest.java

index 55ea930b442ea1678cddc9a5a9c6bbecb5abd885..be50fa659fa995242bfe2b4749eb0aa80b0d7f54 100644 (file)
@@ -26,9 +26,11 @@ import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
 import org.junit.experimental.categories.Category;
+import org.junit.rules.ExpectedException;
 import org.sonar.api.utils.System2;
 import org.sonar.db.DbSession;
 import org.sonar.db.DbTester;
+import org.sonar.db.RowNotFoundException;
 import org.sonar.test.DbTests;
 
 import static org.assertj.core.api.Assertions.assertThat;
@@ -38,6 +40,8 @@ import static org.sonar.db.user.UserTokenTesting.newUserToken;
 public class UserTokenDaoTest {
   @Rule
   public DbTester db = DbTester.create(System2.INSTANCE);
+  @Rule
+  public ExpectedException expectedException = ExpectedException.none();
 
   DbSession dbSession;
 
@@ -63,6 +67,24 @@ public class UserTokenDaoTest {
     assertThat(userTokenFromDb.getLogin()).isEqualTo(userToken.getLogin());
   }
 
+  @Test
+  public void select_by_token_hash() {
+    String tokenHash = "123456789";
+    insertToken(newUserToken().setTokenHash(tokenHash));
+
+    Optional<UserTokenDto> result = underTest.selectByTokenHash(dbSession, tokenHash);
+
+    Assertions.assertThat(result).isPresent();
+  }
+
+  @Test
+  public void fail_if_token_is_not_found() {
+    expectedException.expect(RowNotFoundException.class);
+    expectedException.expectMessage("User token with token hash 'unknown-token-hash' not found");
+
+    underTest.selectOrFailByTokenHash(dbSession, "unknown-token-hash");
+  }
+
   @Test
   public void select_by_login_and_name() {
     UserTokenDto userToken = newUserToken().setLogin("login").setName("name").setTokenHash("token");