]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-16374 Add dao for 'push_events' table
authorJacek <jacek.poreda@sonarsource.com>
Mon, 11 Jul 2022 08:40:02 +0000 (10:40 +0200)
committersonartech <sonartech@sonarsource.com>
Mon, 25 Jul 2022 20:03:57 +0000 (20:03 +0000)
server/sonar-db-core/src/main/java/org/sonar/db/version/SqTables.java
server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java
server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java
server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java
server/sonar-db-dao/src/main/java/org/sonar/db/pushevent/PushEventDao.java [new file with mode: 0644]
server/sonar-db-dao/src/main/java/org/sonar/db/pushevent/PushEventDto.java [new file with mode: 0644]
server/sonar-db-dao/src/main/java/org/sonar/db/pushevent/PushEventMapper.java [new file with mode: 0644]
server/sonar-db-dao/src/main/resources/org/sonar/db/pushevent/PushEventMapper.xml [new file with mode: 0644]
server/sonar-db-dao/src/test/java/org/sonar/db/pushevent/PushEventDaoTest.java [new file with mode: 0644]

index 52a73f6e1ab8481e644c426561af671945310d71..7332a39dd6e8e74bcc66f6ba69f04faecd5198d0 100644 (file)
@@ -82,6 +82,7 @@ public final class SqTables {
     "project_measures",
     "project_qprofiles",
     "properties",
+    "push_events",
     "qprofile_changes",
     "qprofile_edit_groups",
     "qprofile_edit_users",
index 01893f01de6ed93ae14fcea665531a16f84a210e..186b7ff51b4dac81bf941cb2e794459ccaf3b901 100644 (file)
@@ -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,
index b883f7623d2103ab3bb9df45c9acd21dbf6e5df9..f4238569bda7e8fc61686f8100364a52e93d3f6b 100644 (file)
@@ -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;
   }
index 76c699ab92cb7bc0fdebe4b45efad64c310854cf..4327429abc8feb54476fce1b8fdb006ad5be7d4e 100644 (file)
@@ -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 (file)
index 0000000..6cfef79
--- /dev/null
@@ -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 (file)
index 0000000..ee648fb
--- /dev/null
@@ -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 (file)
index 0000000..83e4891
--- /dev/null
@@ -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 (file)
index 0000000..fc472af
--- /dev/null
@@ -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 (file)
index 0000000..1ca9bf2
--- /dev/null
@@ -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());
+
+  }
+
+}