aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-dao
diff options
context:
space:
mode:
authorJacek <jacek.poreda@sonarsource.com>2022-07-11 10:40:02 +0200
committersonartech <sonartech@sonarsource.com>2022-07-25 20:03:57 +0000
commit31184bdc5b3c24d88f424c1625bedc36513884cc (patch)
tree44d98aa802286caca513d3b0bdb66f0dc780aada /server/sonar-db-dao
parentaa16d0446cd9fe127696cce147cfdba46e041b1b (diff)
downloadsonarqube-31184bdc5b3c24d88f424c1625bedc36513884cc.tar.gz
sonarqube-31184bdc5b3c24d88f424c1625bedc36513884cc.zip
SONAR-16374 Add dao for 'push_events' table
Diffstat (limited to 'server/sonar-db-dao')
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java2
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java7
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java4
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/pushevent/PushEventDao.java55
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/pushevent/PushEventDto.java67
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/pushevent/PushEventMapper.java31
-rw-r--r--server/sonar-db-dao/src/main/resources/org/sonar/db/pushevent/PushEventMapper.xml35
-rw-r--r--server/sonar-db-dao/src/test/java/org/sonar/db/pushevent/PushEventDaoTest.java70
8 files changed, 271 insertions, 0 deletions
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 01893f01de6..186b7ff51b4 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
@@ -63,6 +63,7 @@ import org.sonar.db.property.InternalComponentPropertiesDao;
import org.sonar.db.property.InternalPropertiesDao;
import org.sonar.db.property.PropertiesDao;
import org.sonar.db.purge.PurgeDao;
+import org.sonar.db.pushevent.PushEventDao;
import org.sonar.db.qualitygate.ProjectQgateAssociationDao;
import org.sonar.db.qualitygate.QualityGateConditionDao;
import org.sonar.db.qualitygate.QualityGateDao;
@@ -144,6 +145,7 @@ public class DaoModule extends Module {
ProjectQgateAssociationDao.class,
PropertiesDao.class,
PurgeDao.class,
+ PushEventDao.class,
QProfileChangeDao.class,
QProfileEditGroupsDao.class,
QProfileEditUsersDao.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 b883f7623d2..f4238569bda 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
@@ -63,6 +63,7 @@ import org.sonar.db.property.InternalComponentPropertiesDao;
import org.sonar.db.property.InternalPropertiesDao;
import org.sonar.db.property.PropertiesDao;
import org.sonar.db.purge.PurgeDao;
+import org.sonar.db.pushevent.PushEventDao;
import org.sonar.db.qualitygate.ProjectQgateAssociationDao;
import org.sonar.db.qualitygate.QualityGateConditionDao;
import org.sonar.db.qualitygate.QualityGateDao;
@@ -135,6 +136,7 @@ public class DbClient {
private final ProjectLinkDao projectLinkDao;
private final EventDao eventDao;
private final EventComponentChangeDao eventComponentChangeDao;
+ private final PushEventDao pushEventDao;
private final PurgeDao purgeDao;
private final QualityGateDao qualityGateDao;
private final QualityGateConditionDao gateConditionDao;
@@ -214,6 +216,7 @@ public class DbClient {
projectLinkDao = getDao(map, ProjectLinkDao.class);
eventDao = getDao(map, EventDao.class);
eventComponentChangeDao = getDao(map, EventComponentChangeDao.class);
+ pushEventDao = getDao(map, PushEventDao.class);
purgeDao = getDao(map, PurgeDao.class);
qualityGateDao = getDao(map, QualityGateDao.class);
qualityGateUserPermissionsDao = getDao(map, QualityGateUserPermissionsDao.class);
@@ -417,6 +420,10 @@ public class DbClient {
return eventComponentChangeDao;
}
+ public PushEventDao pushEventDao() {
+ return pushEventDao;
+ }
+
public PurgeDao purgeDao() {
return purgeDao;
}
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 76c699ab92c..4327429abc8 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
@@ -115,6 +115,8 @@ import org.sonar.db.property.PropertiesMapper;
import org.sonar.db.property.ScrapPropertyDto;
import org.sonar.db.purge.PurgeMapper;
import org.sonar.db.purge.PurgeableAnalysisDto;
+import org.sonar.db.pushevent.PushEventDto;
+import org.sonar.db.pushevent.PushEventMapper;
import org.sonar.db.qualitygate.ProjectQgateAssociationDto;
import org.sonar.db.qualitygate.ProjectQgateAssociationMapper;
import org.sonar.db.qualitygate.QualityGateConditionDto;
@@ -219,6 +221,7 @@ public class MyBatis {
confBuilder.loadAlias("ProjectCountPerAnalysisPropertyValue", ProjectCountPerAnalysisPropertyValue.class);
confBuilder.loadAlias("ProjectMapping", ProjectMappingDto.class);
confBuilder.loadAlias("PurgeableAnalysis", PurgeableAnalysisDto.class);
+ confBuilder.loadAlias("PushEvent", PushEventDto.class);
confBuilder.loadAlias("QualityGateCondition", QualityGateConditionDto.class);
confBuilder.loadAlias("QualityGate", QualityGateDto.class);
confBuilder.loadAlias("Resource", ResourceDto.class);
@@ -286,6 +289,7 @@ public class MyBatis {
ProjectQgateAssociationMapper.class,
PropertiesMapper.class,
PurgeMapper.class,
+ PushEventMapper.class,
QProfileChangeMapper.class,
QProfileEditGroupsMapper.class,
QProfileEditUsersMapper.class,
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/pushevent/PushEventDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/pushevent/PushEventDao.java
new file mode 100644
index 00000000000..6cfef794411
--- /dev/null
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/pushevent/PushEventDao.java
@@ -0,0 +1,55 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2022 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.pushevent;
+
+import org.sonar.api.utils.System2;
+import org.sonar.core.util.UuidFactory;
+import org.sonar.db.Dao;
+import org.sonar.db.DbSession;
+
+public class PushEventDao implements Dao {
+
+ private final UuidFactory uuidFactory;
+ private final System2 system2;
+
+ public PushEventDao(System2 system2, UuidFactory uuidFactory) {
+ this.system2 = system2;
+ this.uuidFactory = uuidFactory;
+ }
+
+ public PushEventDto insert(DbSession dbSession, PushEventDto event) {
+ if (event.getUuid() == null) {
+ event.setUuid(uuidFactory.create());
+ }
+
+ event.setCreatedAt(system2.now());
+ mapper(dbSession).insert(event);
+ return event;
+ }
+
+ PushEventDto selectByUuid(DbSession dbSession, String uuid) {
+ return mapper(dbSession).selectByUuid(uuid);
+ }
+
+ private static PushEventMapper mapper(DbSession session) {
+ return session.getMapper(PushEventMapper.class);
+ }
+
+}
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/pushevent/PushEventDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/pushevent/PushEventDto.java
new file mode 100644
index 00000000000..ee648fbfd03
--- /dev/null
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/pushevent/PushEventDto.java
@@ -0,0 +1,67 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2022 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.pushevent;
+
+public class PushEventDto {
+ private String uuid;
+ private String projectUuid;
+ private byte[] payload;
+ private long createdAt;
+
+ public PushEventDto() {
+ // nothing to do
+ }
+
+ public String getUuid() {
+ return uuid;
+ }
+
+ public PushEventDto setUuid(String uuid) {
+ this.uuid = uuid;
+ return this;
+ }
+
+ public String getProjectUuid() {
+ return projectUuid;
+ }
+
+ public PushEventDto setProjectUuid(String projectUuid) {
+ this.projectUuid = projectUuid;
+ return this;
+ }
+
+ public byte[] getPayload() {
+ return payload;
+ }
+
+ public PushEventDto setPayload(byte[] payload) {
+ this.payload = payload;
+ return this;
+ }
+
+ public long getCreatedAt() {
+ return createdAt;
+ }
+
+ public PushEventDto setCreatedAt(long createdAt) {
+ this.createdAt = createdAt;
+ return this;
+ }
+}
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/pushevent/PushEventMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/pushevent/PushEventMapper.java
new file mode 100644
index 00000000000..83e489148ed
--- /dev/null
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/pushevent/PushEventMapper.java
@@ -0,0 +1,31 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2022 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.pushevent;
+
+import javax.annotation.CheckForNull;
+
+public interface PushEventMapper {
+
+ void insert(PushEventDto event);
+
+ @CheckForNull
+ PushEventDto selectByUuid(String uuid);
+
+}
diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/pushevent/PushEventMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/pushevent/PushEventMapper.xml
new file mode 100644
index 00000000000..fc472afe742
--- /dev/null
+++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/pushevent/PushEventMapper.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.pushevent.PushEventMapper">
+
+ <sql id="pushEventColumns">
+ pe.uuid as uuid,
+ pe.project_uuid as projectUuid,
+ pe.payload as payload,
+ pe.created_at as createdAt
+ </sql>
+
+ <insert id="insert" parameterType="map" useGeneratedKeys="false">
+ INSERT INTO push_events (
+ uuid,
+ project_uuid,
+ payload,
+ created_at
+ )
+ VALUES (
+ #{uuid,jdbcType=VARCHAR},
+ #{projectUuid,jdbcType=VARCHAR},
+ #{payload,jdbcType=BLOB},
+ #{createdAt,jdbcType=BIGINT}
+ )
+ </insert>
+
+ <select id="selectByUuid" parameterType="String" resultType="PushEvent">
+ SELECT
+ <include refid="pushEventColumns"/>
+ FROM push_events pe
+ where
+ pe.uuid=#{uuid,jdbcType=VARCHAR}
+ </select>
+
+</mapper>
diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/pushevent/PushEventDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/pushevent/PushEventDaoTest.java
new file mode 100644
index 00000000000..1ca9bf2ad58
--- /dev/null
+++ b/server/sonar-db-dao/src/test/java/org/sonar/db/pushevent/PushEventDaoTest.java
@@ -0,0 +1,70 @@
+/*
+ * SonarQube
+ * Copyright (C) 2009-2022 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.pushevent;
+
+import org.junit.Rule;
+import org.junit.Test;
+import org.sonar.api.impl.utils.TestSystem2;
+import org.sonar.db.DbSession;
+import org.sonar.db.DbTester;
+
+import static java.nio.charset.StandardCharsets.UTF_8;
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class PushEventDaoTest {
+
+ private final TestSystem2 system2 = new TestSystem2().setNow(1L);
+
+ @Rule
+ public DbTester db = DbTester.create(system2);
+
+ private final DbSession session = db.getSession();
+ private final PushEventDao underTest = db.getDbClient().pushEventDao();
+
+ @Test
+ public void insert_events() {
+ assertThat(db.countRowsOfTable(session, "push_events")).isZero();
+
+ PushEventDto eventDtoFirst = new PushEventDto()
+ .setUuid("test-uuid")
+ .setProjectUuid("project-uuid")
+ .setPayload("some-event".getBytes(UTF_8));
+
+ PushEventDto eventDtoSecond = new PushEventDto()
+ .setProjectUuid("project-uuid")
+ .setPayload("some-event".getBytes(UTF_8));
+
+ underTest.insert(session, eventDtoFirst);
+ var generatedUuid = underTest.insert(session, eventDtoSecond);
+
+ assertThat(db.countRowsOfTable(session, "push_events"))
+ .isEqualTo(2);
+
+ assertThat(underTest.selectByUuid(session, "test-uuid"))
+ .extracting(PushEventDto::getUuid, PushEventDto::getProjectUuid, PushEventDto::getPayload, PushEventDto::getCreatedAt)
+ .containsExactly(eventDtoFirst.getUuid(), eventDtoFirst.getProjectUuid(), eventDtoFirst.getPayload(), eventDtoFirst.getCreatedAt());
+
+ assertThat(underTest.selectByUuid(session, generatedUuid.getUuid()))
+ .extracting(PushEventDto::getUuid, PushEventDto::getProjectUuid, PushEventDto::getPayload, PushEventDto::getCreatedAt)
+ .containsExactly(eventDtoSecond.getUuid(), eventDtoSecond.getProjectUuid(), eventDtoSecond.getPayload(), eventDtoSecond.getCreatedAt());
+
+ }
+
+}