diff options
author | Duarte Meneses <duarte.meneses@sonarsource.com> | 2019-08-02 15:01:10 -0500 |
---|---|---|
committer | SonarTech <sonartech@sonarsource.com> | 2019-09-24 20:21:12 +0200 |
commit | 52db58fa073303bbc75b25341e996ec78ff00f04 (patch) | |
tree | f02fb0b99f58115417f13a193c6fa3be1d744787 | |
parent | 0279e128fd3e0dfe0585b02398dbbea35e2dd83f (diff) | |
download | sonarqube-52db58fa073303bbc75b25341e996ec78ff00f04.tar.gz sonarqube-52db58fa073303bbc75b25341e996ec78ff00f04.zip |
add Upsert to NewCodePeriodDao
4 files changed, 87 insertions, 13 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 a99c2f5a83a..24896250e22 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 @@ -49,6 +49,16 @@ public class NewCodePeriodDao implements Dao { mapper(dbSession).insert(dto.setCreatedAt(currentTime).setUpdatedAt(currentTime)); } + public void upsert(DbSession dbSession, NewCodePeriodDto dto) { + NewCodePeriodMapper mapper = mapper(dbSession); + long currentTime = system2.now(); + dto.setUpdatedAt(currentTime); + if (mapper.update(dto) == 0) { + dto.setCreatedAt(currentTime); + 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."); diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/newcodeperiod/NewCodePeriodMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/newcodeperiod/NewCodePeriodMapper.java index db9a29cca73..f6381ca1433 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/newcodeperiod/NewCodePeriodMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/newcodeperiod/NewCodePeriodMapper.java @@ -30,7 +30,7 @@ public interface NewCodePeriodMapper { void insert(NewCodePeriodDto dto); - void update(NewCodePeriodDto dto); + int update(NewCodePeriodDto dto); NewCodePeriodDto selectByProject(String projectUuid); 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 28d31133acc..d7f30f67b1f 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 @@ -95,6 +95,59 @@ public class NewCodePeriodDaoTest { } @Test + public void insert_with_upsert() { + underTest.upsert(dbSession, new NewCodePeriodDto() + .setUuid("uuid-1") + .setProjectUuid("proj-uuid") + .setBranchUuid("branch-uuid") + .setType(NewCodePeriodType.NUMBER_OF_DAYS) + .setValue("5")); + + Optional<NewCodePeriodDto> resultOpt = underTest.selectByUuid(dbSession, "uuid-1"); + + assertThat(resultOpt).isNotNull(); + assertThat(resultOpt).isNotEmpty(); + + NewCodePeriodDto result = resultOpt.get(); + assertThat(result.getUuid()).isEqualTo("uuid-1"); + assertThat(result.getProjectUuid()).isEqualTo("proj-uuid"); + assertThat(result.getBranchUuid()).isEqualTo("branch-uuid"); + assertThat(result.getType()).isEqualTo(NewCodePeriodType.NUMBER_OF_DAYS); + assertThat(result.getValue()).isEqualTo("5"); + assertThat(result.getCreatedAt()).isNotEqualTo(0); + assertThat(result.getUpdatedAt()).isNotEqualTo(0); + } + + @Test + public void update_with_upsert() { + 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")); + + Optional<NewCodePeriodDto> resultOpt = underTest.selectByUuid(dbSession, "uuid-1"); + + assertThat(resultOpt).isNotNull(); + assertThat(resultOpt).isNotEmpty(); + + NewCodePeriodDto result = resultOpt.get(); + assertThat(result.getUuid()).isEqualTo("uuid-1"); + assertThat(result.getProjectUuid()).isEqualTo("proj-uuid"); + assertThat(result.getBranchUuid()).isEqualTo("branch-uuid"); + assertThat(result.getType()).isEqualTo(NewCodePeriodType.SPECIFIC_ANALYSIS); + assertThat(result.getValue()).isEqualTo("analysis-uuid"); + assertThat(result.getCreatedAt()).isNotEqualTo(0); + assertThat(result.getUpdatedAt()).isNotEqualTo(0); + } + + @Test public void select_by_project_and_branch_uuids() { NewCodePeriodDto dto = db.newCodePeriods().insert(new NewCodePeriodDto() .setUuid("uuid-1") diff --git a/server/sonar-server/src/test/java/org/sonar/server/setting/ws/SetNewCodePeriodActionTest.java b/server/sonar-server/src/test/java/org/sonar/server/setting/ws/SetNewCodePeriodActionTest.java index 3d3b1a738bf..d0d5c9400a0 100644 --- a/server/sonar-server/src/test/java/org/sonar/server/setting/ws/SetNewCodePeriodActionTest.java +++ b/server/sonar-server/src/test/java/org/sonar/server/setting/ws/SetNewCodePeriodActionTest.java @@ -19,6 +19,7 @@ */ package org.sonar.server.setting.ws; +import javax.annotation.Nullable; import org.junit.Rule; import org.junit.Test; import org.junit.rules.ExpectedException; @@ -33,6 +34,7 @@ import org.sonar.db.component.ComponentDbTester; import org.sonar.db.component.ComponentDto; import org.sonar.db.component.SnapshotDto; import org.sonar.db.newcodeperiod.NewCodePeriodDao; +import org.sonar.db.newcodeperiod.NewCodePeriodType; import org.sonar.server.component.ComponentFinder; import org.sonar.server.component.TestComponentFinder; import org.sonar.server.exceptions.ForbiddenException; @@ -41,7 +43,7 @@ import org.sonar.server.tester.UserSessionRule; import org.sonar.server.ws.WsActionTester; import static org.assertj.core.api.Assertions.assertThat; -import static org.mockito.Mockito.mock; +import static org.assertj.core.api.Assertions.entry; public class SetNewCodePeriodActionTest { @Rule @@ -55,7 +57,8 @@ public class SetNewCodePeriodActionTest { private DbClient dbClient = db.getDbClient(); private DbSession dbSession = db.getSession(); private ComponentFinder componentFinder = TestComponentFinder.from(db); - private NewCodePeriodDao dao = mock(NewCodePeriodDao.class); + private NewCodePeriodDao dao = new NewCodePeriodDao(System2.INSTANCE); + private DbTester dbTester = DbTester.create(); private SetNewCodePeriodAction underTest = new SetNewCodePeriodAction(dbClient, userSession, componentFinder, dao); private WsActionTester ws = new WsActionTester(underTest); @@ -118,7 +121,7 @@ public class SetNewCodePeriodActionTest { ws.newRequest() .setParam("projectKey", project.getKey()) - .setParam("type", "analysis") + .setParam("type", "specific_analysis") .execute(); } @@ -146,7 +149,7 @@ public class SetNewCodePeriodActionTest { ws.newRequest() .setParam("projectKey", project.getKey()) .setParam("branchKey", "master") - .setParam("type", "days") + .setParam("type", "number_of_days") .execute(); } @@ -159,7 +162,7 @@ public class SetNewCodePeriodActionTest { ws.newRequest() .setParam("projectKey", project.getKey()) - .setParam("type", "analysis") + .setParam("type", "specific_analysis") .setParam("branchKey", "master") .execute(); } @@ -188,7 +191,7 @@ public class SetNewCodePeriodActionTest { ws.newRequest() .setParam("projectKey", project.getKey()) - .setParam("type", "days") + .setParam("type", "number_of_days") .setParam("branchKey", "master") .setParam("value", "unknown") .execute(); @@ -203,7 +206,7 @@ public class SetNewCodePeriodActionTest { ws.newRequest() .setParam("projectKey", project.getKey()) - .setParam("type", "analysis") + .setParam("type", "specific_analysis") .setParam("branchKey", "master") .setParam("value", "unknown") .execute(); @@ -310,8 +313,8 @@ public class SetNewCodePeriodActionTest { ws.newRequest() .setParam("type", "previous_version") .execute(); - // TODO + assertTableContainsOnly(null, null, NewCodePeriodType.PREVIOUS_VERSION, null); } @Test @@ -320,10 +323,10 @@ public class SetNewCodePeriodActionTest { logInAsProjectAdministrator(project); ws.newRequest() .setParam("projectKey", project.getKey()) - .setParam("type", "days") + .setParam("type", "number_of_days") .setParam("value", "5") .execute(); - // TODO + assertTableContainsOnly(project.getKey(), null, NewCodePeriodType.NUMBER_OF_DAYS, "5"); } @@ -339,11 +342,19 @@ public class SetNewCodePeriodActionTest { ws.newRequest() .setParam("projectKey", project.getKey()) - .setParam("type", "analysis") + .setParam("type", "specific_analysis") .setParam("branchKey", "branch") .setParam("value", analysisBranch.getUuid()) .execute(); - // TODO + + assertTableContainsOnly(project.getKey(), "branch", NewCodePeriodType.SPECIFIC_ANALYSIS, analysisBranch.getUuid()); + + } + + private void assertTableContainsOnly(@Nullable String projectKey, @Nullable String branchKey, NewCodePeriodType type, @Nullable String value) { + dbTester.countRowsOfTable(dbSession, "new_code_period"); + assertThat(dbTester.selectFirst(dbSession, "select projectKey, branchKey, type, value from new_code_period")) + .containsOnly(entry("projectKey", projectKey), entry("branchKey", branchKey), entry("type", type), entry("value", value)); } private void logInAsProjectAdministrator(ComponentDto project) { |