Browse Source

SONAR-16374 Add dao for 'push_events' table

tags/9.6.0.59041
Jacek 1 year ago
parent
commit
31184bdc5b

+ 1
- 0
server/sonar-db-core/src/main/java/org/sonar/db/version/SqTables.java View File

@@ -82,6 +82,7 @@ public final class SqTables {
"project_measures",
"project_qprofiles",
"properties",
"push_events",
"qprofile_changes",
"qprofile_edit_groups",
"qprofile_edit_users",

+ 2
- 0
server/sonar-db-dao/src/main/java/org/sonar/db/DaoModule.java View 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,

+ 7
- 0
server/sonar-db-dao/src/main/java/org/sonar/db/DbClient.java View 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;
}

+ 4
- 0
server/sonar-db-dao/src/main/java/org/sonar/db/MyBatis.java View 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,

+ 55
- 0
server/sonar-db-dao/src/main/java/org/sonar/db/pushevent/PushEventDao.java View File

@@ -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);
}

}

+ 67
- 0
server/sonar-db-dao/src/main/java/org/sonar/db/pushevent/PushEventDto.java View File

@@ -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;
}
}

+ 31
- 0
server/sonar-db-dao/src/main/java/org/sonar/db/pushevent/PushEventMapper.java View File

@@ -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);

}

+ 35
- 0
server/sonar-db-dao/src/main/resources/org/sonar/db/pushevent/PushEventMapper.xml View File

@@ -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>

+ 70
- 0
server/sonar-db-dao/src/test/java/org/sonar/db/pushevent/PushEventDaoTest.java View File

@@ -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());

}

}

Loading…
Cancel
Save