aboutsummaryrefslogtreecommitdiffstats
path: root/server/sonar-db-dao
diff options
context:
space:
mode:
authorDuarte Meneses <duarte.meneses@sonarsource.com>2019-08-02 15:43:19 -0500
committerSonarTech <sonartech@sonarsource.com>2019-09-24 20:21:12 +0200
commit09a0e34e0e4f22bb848a7339d6fe0153ac1ae97b (patch)
tree1db6a98491cc9ee0944f769ff0556aa99edeb3dc /server/sonar-db-dao
parent7ebbfdf26432552edeb1057555fb51596a1ca5b6 (diff)
downloadsonarqube-09a0e34e0e4f22bb848a7339d6fe0153ac1ae97b.tar.gz
sonarqube-09a0e34e0e4f22bb848a7339d6fe0153ac1ae97b.zip
Create UUID for new_code_periods
Diffstat (limited to 'server/sonar-db-dao')
-rw-r--r--server/sonar-db-dao/src/main/java/org/sonar/db/newcodeperiod/NewCodePeriodDao.java12
-rw-r--r--server/sonar-db-dao/src/test/java/org/sonar/db/newcodeperiod/NewCodePeriodDaoTest.java28
2 files changed, 26 insertions, 14 deletions
diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/newcodeperiod/NewCodePeriodDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/newcodeperiod/NewCodePeriodDao.java
index 24896250e22..6ef05723cff 100644
--- a/server/sonar-db-dao/src/main/java/org/sonar/db/newcodeperiod/NewCodePeriodDao.java
+++ b/server/sonar-db-dao/src/main/java/org/sonar/db/newcodeperiod/NewCodePeriodDao.java
@@ -21,6 +21,7 @@ package org.sonar.db.newcodeperiod;
import java.util.Optional;
import org.sonar.api.utils.System2;
+import org.sonar.core.util.UuidFactory;
import org.sonar.db.Dao;
import org.sonar.db.DbSession;
@@ -29,9 +30,11 @@ import static java.util.Objects.requireNonNull;
public class NewCodePeriodDao implements Dao {
private final System2 system2;
+ private final UuidFactory uuidFactory;
- public NewCodePeriodDao(System2 system2) {
+ public NewCodePeriodDao(System2 system2, UuidFactory uuidFactory) {
this.system2 = system2;
+ this.uuidFactory = uuidFactory;
}
public Optional<NewCodePeriodDto> selectByUuid(DbSession dbSession, String uuid) {
@@ -44,9 +47,10 @@ public class NewCodePeriodDao implements Dao {
public void insert(DbSession dbSession, NewCodePeriodDto dto) {
requireNonNull(dto.getType(), "Type of NewCodePeriod must be specified.");
- requireNonNull(dto.getValue(), "Value of NewCodePeriod must be specified.");
long currentTime = system2.now();
- mapper(dbSession).insert(dto.setCreatedAt(currentTime).setUpdatedAt(currentTime));
+ mapper(dbSession).insert(dto.setCreatedAt(currentTime)
+ .setUpdatedAt(currentTime)
+ .setUuid(uuidFactory.create()));
}
public void upsert(DbSession dbSession, NewCodePeriodDto dto) {
@@ -55,13 +59,13 @@ public class NewCodePeriodDao implements Dao {
dto.setUpdatedAt(currentTime);
if (mapper.update(dto) == 0) {
dto.setCreatedAt(currentTime);
+ dto.setUuid(uuidFactory.create());
mapper.insert(dto);
}
}
public void update(DbSession dbSession, NewCodePeriodDto dto) {
requireNonNull(dto.getType(), "Type of NewCodePeriod must be specified.");
- requireNonNull(dto.getValue(), "Value of NewCodePeriod must be specified.");
mapper(dbSession).update(dto.setUpdatedAt(system2.now()));
}
diff --git a/server/sonar-db-dao/src/test/java/org/sonar/db/newcodeperiod/NewCodePeriodDaoTest.java b/server/sonar-db-dao/src/test/java/org/sonar/db/newcodeperiod/NewCodePeriodDaoTest.java
index d7f30f67b1f..14f2fe68bf9 100644
--- a/server/sonar-db-dao/src/test/java/org/sonar/db/newcodeperiod/NewCodePeriodDaoTest.java
+++ b/server/sonar-db-dao/src/test/java/org/sonar/db/newcodeperiod/NewCodePeriodDaoTest.java
@@ -24,10 +24,13 @@ import org.junit.Rule;
import org.junit.Test;
import org.junit.rules.ExpectedException;
import org.sonar.api.utils.System2;
+import org.sonar.core.util.UuidFactory;
import org.sonar.db.DbSession;
import org.sonar.db.DbTester;
import static org.assertj.core.api.Assertions.assertThat;
+import static org.mockito.Mockito.mock;
+import static org.mockito.Mockito.when;
public class NewCodePeriodDaoTest {
@@ -38,13 +41,13 @@ public class NewCodePeriodDaoTest {
public ExpectedException expectedException = ExpectedException.none();
private DbSession dbSession = db.getSession();
-
- private NewCodePeriodDao underTest = new NewCodePeriodDao(System2.INSTANCE);
+ private UuidFactory uuidFactory = mock(UuidFactory.class);
+ private NewCodePeriodDao underTest = new NewCodePeriodDao(System2.INSTANCE, uuidFactory);
@Test
public void insert_new_code_period() {
+ when(uuidFactory.create()).thenReturn("uuid-1");
underTest.insert(dbSession, new NewCodePeriodDto()
- .setUuid("uuid-1")
.setProjectUuid("proj-uuid")
.setBranchUuid("branch-uuid")
.setType(NewCodePeriodType.NUMBER_OF_DAYS)
@@ -67,8 +70,9 @@ public class NewCodePeriodDaoTest {
@Test
public void update_new_code_period() {
+ when(uuidFactory.create()).thenReturn("uuid-1");
+
NewCodePeriodDto dto = db.newCodePeriods().insert(new NewCodePeriodDto()
- .setUuid("uuid-1")
.setProjectUuid("proj-uuid")
.setBranchUuid("branch-uuid")
.setType(NewCodePeriodType.NUMBER_OF_DAYS)
@@ -96,8 +100,9 @@ public class NewCodePeriodDaoTest {
@Test
public void insert_with_upsert() {
+ when(uuidFactory.create()).thenReturn("uuid-1");
+
underTest.upsert(dbSession, new NewCodePeriodDto()
- .setUuid("uuid-1")
.setProjectUuid("proj-uuid")
.setBranchUuid("branch-uuid")
.setType(NewCodePeriodType.NUMBER_OF_DAYS)
@@ -120,15 +125,15 @@ public class NewCodePeriodDaoTest {
@Test
public void update_with_upsert() {
+ when(uuidFactory.create()).thenReturn("uuid-1");
+
db.newCodePeriods().insert(new NewCodePeriodDto()
- .setUuid("uuid-1")
.setProjectUuid("proj-uuid")
.setBranchUuid("branch-uuid")
.setType(NewCodePeriodType.NUMBER_OF_DAYS)
.setValue("5"));
underTest.upsert(dbSession, new NewCodePeriodDto()
- .setUuid("uuid-1")
.setType(NewCodePeriodType.SPECIFIC_ANALYSIS)
.setValue("analysis-uuid"));
@@ -149,8 +154,9 @@ public class NewCodePeriodDaoTest {
@Test
public void select_by_project_and_branch_uuids() {
+ when(uuidFactory.create()).thenReturn("uuid-1");
+
NewCodePeriodDto dto = db.newCodePeriods().insert(new NewCodePeriodDto()
- .setUuid("uuid-1")
.setProjectUuid("proj-uuid")
.setBranchUuid("branch-uuid")
.setType(NewCodePeriodType.NUMBER_OF_DAYS)
@@ -172,8 +178,9 @@ public class NewCodePeriodDaoTest {
@Test
public void select_by_project_uuid() {
+ when(uuidFactory.create()).thenReturn("uuid-1");
+
NewCodePeriodDto dto = db.newCodePeriods().insert(new NewCodePeriodDto()
- .setUuid("uuid-1")
.setProjectUuid("proj-uuid")
.setBranchUuid(null)
.setType(NewCodePeriodType.NUMBER_OF_DAYS)
@@ -195,8 +202,9 @@ public class NewCodePeriodDaoTest {
@Test
public void select_global() {
+ when(uuidFactory.create()).thenReturn("uuid-1");
+
db.newCodePeriods().insert(new NewCodePeriodDto()
- .setUuid("uuid-1")
.setProjectUuid(null)
.setBranchUuid(null)
.setType(NewCodePeriodType.NUMBER_OF_DAYS)