Skip to content

Commit

Permalink
SONAR-23978 Add DAO for user_ai_tool_usages table
Browse files Browse the repository at this point in the history
  • Loading branch information
leo-geoffroy-sonarsource authored and sonartech committed Dec 20, 2024
1 parent e118109 commit eb6c821
Show file tree
Hide file tree
Showing 9 changed files with 287 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
/*
* SonarQube
* Copyright (C) 2009-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program 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.
*
* This program 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.ai;

import java.util.stream.IntStream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;

import static org.assertj.core.api.AssertionsForClassTypes.tuple;
import static org.assertj.core.api.AssertionsForInterfaceTypes.assertThat;

class UserAiToolUsageDaoIT {
@RegisterExtension
private final DbTester db = DbTester.create();

private final DbSession dbSession = db.getSession();

private final UserAiToolUsageDao underTest = db.getDbClient().userAiToolUsageDao();

@Test
void insert_shouldSaveSingleEntry() {
UserAiToolUsageDto dto = new UserAiToolUsageDto()
.setUuid("uuid")
.setUserUuid("userUuid")
.setActivatedAt(1L)
.setLastActivityAt(2L);

underTest.insert(dbSession, dto);

assertThat(underTest.selectAll(dbSession))
.extracting(UserAiToolUsageDto::getUuid, UserAiToolUsageDto::getUserUuid, UserAiToolUsageDto::getActivatedAt, UserAiToolUsageDto::getLastActivityAt)
.containsExactly(tuple("uuid", "userUuid", 1L, 2L));
}

@Test
void insert_shouldSaveMultipleEntry() {
IntStream.range(0, 10).forEach(i -> {
UserAiToolUsageDto dto = new UserAiToolUsageDto()
.setUuid("uuid" + i)
.setUserUuid("userUuid" + i)
.setActivatedAt(1L + i)
.setLastActivityAt(2L + i);
underTest.insert(dbSession, dto);
});

assertThat(underTest.selectAll(dbSession))
.hasSize(10);
}

@Test
void select_whenLastActivityIsMissing_shouldReturnLastActivityEmpty() {
UserAiToolUsageDto dto = new UserAiToolUsageDto()
.setUuid("uuid")
.setUserUuid("userUuid")
.setActivatedAt(1L);

underTest.insert(dbSession, dto);

assertThat(underTest.selectAll(dbSession))
.extracting(UserAiToolUsageDto::getUuid, UserAiToolUsageDto::getUserUuid, UserAiToolUsageDto::getActivatedAt, UserAiToolUsageDto::getLastActivityAt)
.containsExactly(tuple("uuid", "userUuid", 1L, null));
}
}
2 changes: 2 additions & 0 deletions server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
import org.sonar.db.user.UserDismissedMessagesDao;
import org.sonar.db.user.UserGroupDao;
import org.sonar.db.user.UserTokenDao;
import org.sonar.db.user.ai.UserAiToolUsageDao;
import org.sonar.db.webhook.WebhookDao;
import org.sonar.db.webhook.WebhookDeliveryDao;

Expand Down Expand Up @@ -196,6 +197,7 @@ public class DaoModule extends Module {
SnapshotDao.class,
SessionTokensDao.class,
TelemetryMetricsSentDao.class,
UserAiToolUsageDao.class,
UserDao.class,
UserDismissedMessagesDao.class,
UserGroupDao.class,
Expand Down
7 changes: 7 additions & 0 deletions server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,7 @@
import org.sonar.db.user.UserDismissedMessagesDao;
import org.sonar.db.user.UserGroupDao;
import org.sonar.db.user.UserTokenDao;
import org.sonar.db.user.ai.UserAiToolUsageDao;
import org.sonar.db.webhook.WebhookDao;
import org.sonar.db.webhook.WebhookDeliveryDao;

Expand Down Expand Up @@ -135,6 +136,7 @@ public class DbClient {
private final UserDao userDao;
private final UserGroupDao userGroupDao;
private final UserTokenDao userTokenDao;
private final UserAiToolUsageDao userAiToolUsageDao;
private final GroupMembershipDao groupMembershipDao;
private final RoleDao roleDao;
private final GroupPermissionDao groupPermissionDao;
Expand Down Expand Up @@ -232,6 +234,7 @@ public DbClient(Database database, MyBatis myBatis, DBSessions dbSessions, Dao..
userDao = getDao(map, UserDao.class);
userGroupDao = getDao(map, UserGroupDao.class);
userTokenDao = getDao(map, UserTokenDao.class);
userAiToolUsageDao = getDao(map, UserAiToolUsageDao.class);
groupMembershipDao = getDao(map, GroupMembershipDao.class);
roleDao = getDao(map, RoleDao.class);
groupPermissionDao = getDao(map, GroupPermissionDao.class);
Expand Down Expand Up @@ -412,6 +415,10 @@ public UserDao userDao() {
return userDao;
}

public UserAiToolUsageDao userAiToolUsageDao() {
return userAiToolUsageDao;
}

public UserGroupDao userGroupDao() {
return userGroupDao;
}
Expand Down
2 changes: 2 additions & 0 deletions server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,7 @@
import org.sonar.db.user.UserTokenCount;
import org.sonar.db.user.UserTokenDto;
import org.sonar.db.user.UserTokenMapper;
import org.sonar.db.user.ai.UserAiToolUsageMapper;
import org.sonar.db.webhook.WebhookDeliveryMapper;
import org.sonar.db.webhook.WebhookMapper;
import org.springframework.beans.factory.annotation.Autowired;
Expand Down Expand Up @@ -359,6 +360,7 @@ public void start() {
SessionTokenMapper.class,
SnapshotMapper.class,
TelemetryMetricsSentMapper.class,
UserAiToolUsageMapper.class,
UserDismissedMessagesMapper.class,
UserGroupMapper.class,
UserMapper.class,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* SonarQube
* Copyright (C) 2009-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program 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.
*
* This program 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.ai;

import java.util.List;
import org.sonar.db.Dao;
import org.sonar.db.DbSession;

public class UserAiToolUsageDao implements Dao {

public void insert(DbSession dbSession, UserAiToolUsageDto userAiToolUsageDto) {
mapper(dbSession).insert(userAiToolUsageDto);
}

public List<UserAiToolUsageDto> selectAll(DbSession dbSession) {
return mapper(dbSession).selectAll();
}

private static UserAiToolUsageMapper mapper(DbSession session) {
return session.getMapper(UserAiToolUsageMapper.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
/*
* SonarQube
* Copyright (C) 2009-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program 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.
*
* This program 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.ai;

import javax.annotation.CheckForNull;
import javax.annotation.Nullable;

public class UserAiToolUsageDto {

private String uuid = null;
private String userUuid = null;
private Long activatedAt = null;
private Long lastActivityAt = null;

public String getUuid() {
return uuid;
}

public UserAiToolUsageDto setUuid(String uuid) {
this.uuid = uuid;
return this;
}

public String getUserUuid() {
return userUuid;
}

public UserAiToolUsageDto setUserUuid(String userUuid) {
this.userUuid = userUuid;
return this;
}

public Long getActivatedAt() {
return activatedAt;
}

public UserAiToolUsageDto setActivatedAt(Long activatedAt) {
this.activatedAt = activatedAt;
return this;
}

@CheckForNull
public Long getLastActivityAt() {
return lastActivityAt;
}

public UserAiToolUsageDto setLastActivityAt(@Nullable Long lastActivityAt) {
this.lastActivityAt = lastActivityAt;
return this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/*
* SonarQube
* Copyright (C) 2009-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program 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.
*
* This program 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.ai;

import java.util.List;

public interface UserAiToolUsageMapper {

void insert(UserAiToolUsageDto userAiToolUsageDto);

List<UserAiToolUsageDto> selectAll();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/*
* SonarQube
* Copyright (C) 2009-2024 SonarSource SA
* mailto:info AT sonarsource DOT com
*
* This program 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.
*
* This program 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.
*/
@ParametersAreNonnullByDefault
package org.sonar.db.user.ai;

import javax.annotation.ParametersAreNonnullByDefault;
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE mapper PUBLIC "-//mybatis.org//DTD Mapper 3.0//EN" "mybatis-3-mapper.dtd">

<mapper namespace="org.sonar.db.user.ai.UserAiToolUsageMapper">

<sql id="columns">
uatu.uuid as uuid,
uatu.user_uuid as "userUuid",
uatu.activated_at as "activatedAt",
uatu.last_activity_at as "lastActivityAt"
</sql>

<select id="selectAll" parameterType="String" resultType="org.sonar.db.user.ai.UserAiToolUsageDto">
select
<include refid="columns"/>
from user_ai_tool_usages uatu
</select>

<insert id="insert" parameterType="org.sonar.db.user.ai.UserAiToolUsageDto" useGeneratedKeys="false">
insert into user_ai_tool_usages
(
uuid,
user_uuid,
activated_at,
last_activity_at
)
values (
#{uuid, jdbcType=VARCHAR},
#{userUuid, jdbcType=VARCHAR},
#{activatedAt, jdbcType=BIGINT},
#{lastActivityAt, jdbcType=BIGINT}
)
</insert>

</mapper>

0 comments on commit eb6c821

Please sign in to comment.