diff options
author | Léo Geoffroy <leo.geoffroy@sonarsource.com> | 2024-12-18 10:48:26 +0100 |
---|---|---|
committer | sonartech <sonartech@sonarsource.com> | 2024-12-20 20:03:11 +0000 |
commit | eb6c821da27dcb4af7545e5605cdd360f81f22f7 (patch) | |
tree | 5d93d05d775216e581ae5265c24366099355bf2c | |
parent | e1181094ddb77f6e596615d031e3dc93425443ba (diff) | |
download | sonarqube-eb6c821da27dcb4af7545e5605cdd360f81f22f7.tar.gz sonarqube-eb6c821da27dcb4af7545e5605cdd360f81f22f7.zip |
SONAR-23978 Add DAO for user_ai_tool_usages table
9 files changed, 287 insertions, 0 deletions
diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/user/ai/UserAiToolUsageDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/user/ai/UserAiToolUsageDaoIT.java new file mode 100644 index 00000000000..7958b621ed8 --- /dev/null +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/user/ai/UserAiToolUsageDaoIT.java @@ -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)); + } +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java b/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java index 9f3ed9ab4fe..f2e51452023 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java @@ -107,6 +107,7 @@ import org.sonar.db.user.UserDao; 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; @@ -196,6 +197,7 @@ public class DaoModule extends Module { SnapshotDao.class, SessionTokensDao.class, TelemetryMetricsSentDao.class, + UserAiToolUsageDao.class, UserDao.class, UserDismissedMessagesDao.class, UserGroupDao.class, diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java b/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java index fc0faf5c348..027055a5075 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java @@ -107,6 +107,7 @@ import org.sonar.db.user.UserDao; 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; @@ -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; @@ -232,6 +234,7 @@ public class DbClient { 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); @@ -412,6 +415,10 @@ public class DbClient { return userDao; } + public UserAiToolUsageDao userAiToolUsageDao() { + return userAiToolUsageDao; + } + public UserGroupDao userGroupDao() { return userGroupDao; } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java b/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java index bdbccd5491e..7dfa94e518a 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java @@ -183,6 +183,7 @@ import org.sonar.db.user.UserTelemetryDto; 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; @@ -359,6 +360,7 @@ public class MyBatis { SessionTokenMapper.class, SnapshotMapper.class, TelemetryMetricsSentMapper.class, + UserAiToolUsageMapper.class, UserDismissedMessagesMapper.class, UserGroupMapper.class, UserMapper.class, diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/user/ai/UserAiToolUsageDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/ai/UserAiToolUsageDao.java new file mode 100644 index 00000000000..e678b59da6b --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/ai/UserAiToolUsageDao.java @@ -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); + } +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/user/ai/UserAiToolUsageDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/ai/UserAiToolUsageDto.java new file mode 100644 index 00000000000..63d90cb0855 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/ai/UserAiToolUsageDto.java @@ -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; + } +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/user/ai/UserAiToolUsageMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/ai/UserAiToolUsageMapper.java new file mode 100644 index 00000000000..6c3ac6c3043 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/ai/UserAiToolUsageMapper.java @@ -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(); +} diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/user/ai/package-info.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/ai/package-info.java new file mode 100644 index 00000000000..dc395949b00 --- /dev/null +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/ai/package-info.java @@ -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; diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/user/ai/UserAiToolUsageMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/user/ai/UserAiToolUsageMapper.xml new file mode 100644 index 00000000000..9967369403d --- /dev/null +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/user/ai/UserAiToolUsageMapper.xml @@ -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> |