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;
public class UserTokenDaoTest {
@Rule
public DbTester db = DbTester.create(System2.INSTANCE);
+ @Rule
+ public ExpectedException expectedException = ExpectedException.none();
DbSession dbSession;
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");