From e766007679a913e498584004acf96d55eb58329d Mon Sep 17 00:00:00 2001 From: Wojtek Wajerowicz <115081248+wojciech-wajerowicz-sonarsource@users.noreply.github.com> Date: Thu, 15 Jun 2023 13:37:23 +0200 Subject: [PATCH] SONAR-19383 Remove unnecessary methods from CeTaskMessagesDao. (#8539) --- .../org/sonar/db/ce/CeTaskMessageDaoIT.java | 42 ++---------------- .../org/sonar/db/ce/CeTaskMessageDao.java | 6 --- .../org/sonar/db/ce/CeTaskMessageMapper.java | 2 - .../org/sonar/db/ce/CeTaskMessageMapper.xml | 43 +++++++------------ .../startup/UpgradeSuggestionsCleanerIT.java | 20 +++++++-- .../server/ce/ws/AnalysisStatusAction.java | 4 +- 6 files changed, 39 insertions(+), 78 deletions(-) diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/ce/CeTaskMessageDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/ce/CeTaskMessageDaoIT.java index a6d00359b2d..0e5b6a295e7 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/ce/CeTaskMessageDaoIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/ce/CeTaskMessageDaoIT.java @@ -30,7 +30,6 @@ import org.sonar.db.DbTester; import org.sonar.db.project.ProjectDto; import org.sonar.db.user.UserDto; -import static org.apache.commons.lang.RandomStringUtils.randomAlphabetic; import static org.assertj.core.api.Assertions.assertThat; public class CeTaskMessageDaoIT { @@ -58,40 +57,6 @@ public class CeTaskMessageDaoIT { .containsOnly(Tuple.tuple("uuid_1", "task_uuid_1", "message_1", CeTaskMessageType.GENERIC, 1_222_333L)); } - @Test - public void selectByTask_returns_empty_on_empty_table() { - String taskUuid = randomAlphabetic(10); - - List dto = underTest.selectByTask(dbTester.getSession(), taskUuid); - - assertThat(dto).isEmpty(); - } - - @Test - public void selectByTask_returns_message_of_task_ordered_by_CREATED_AT_asc() { - String task1 = "task1"; - String task2 = "task2"; - CeTaskMessageDto[] messages = { - insertMessage(task1, 0, 1_222_333L), - insertMessage(task2, 1, 2_222_333L), - insertMessage(task2, 2, 1_111_333L), - insertMessage(task1, 3, 1_222_111L), - insertMessage(task1, 4, 222_111L), - insertMessage(task1, 5, 3_222_111L) - }; - - assertThat(underTest.selectByTask(dbTester.getSession(), task1)) - .extracting(CeTaskMessageDto::getUuid) - .containsExactly(messages[4].getUuid(), messages[3].getUuid(), messages[0].getUuid(), messages[5].getUuid()); - - assertThat(underTest.selectByTask(dbTester.getSession(), task2)) - .extracting(CeTaskMessageDto::getUuid) - .containsExactly(messages[2].getUuid(), messages[1].getUuid()); - - assertThat(underTest.selectByTask(dbTester.getSession(), randomAlphabetic(5))) - .isEmpty(); - } - @Test public void selectByUuid_returns_object_if_found() { CeTaskMessageDto dto = insertMessage("526787a4-e8af-46c0-b340-8c48188646a5", 1, 1_222_333L); @@ -121,9 +86,10 @@ public class CeTaskMessageDaoIT { underTest.deleteByType(dbTester.getSession(), CeTaskMessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE); - assertThat(underTest.selectByTask(dbTester.getSession(), task1)) - .extracting(CeTaskMessageDto::getUuid) - .containsExactlyInAnyOrder(messages[0].getUuid(), messages[2].getUuid()); + assertThat(underTest.selectByUuid(dbTester.getSession(), messages[0].getUuid())).isPresent(); + assertThat(underTest.selectByUuid(dbTester.getSession(), messages[1].getUuid())).isEmpty(); + assertThat(underTest.selectByUuid(dbTester.getSession(), messages[2].getUuid())).isPresent(); + assertThat(underTest.selectByUuid(dbTester.getSession(), messages[3].getUuid())).isEmpty(); } @Test diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeTaskMessageDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeTaskMessageDao.java index 272f99377a8..588827cef60 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeTaskMessageDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeTaskMessageDao.java @@ -33,12 +33,6 @@ public class CeTaskMessageDao implements Dao { return getMapper(dbSession).selectByUuid(uuid); } - /** - * @return the messages for the specific task, if any, in ascending order of column {@code CREATED_AT}. - */ - public List selectByTask(DbSession dbSession, String taskUuid) { - return getMapper(dbSession).selectByTask(taskUuid); - } /** * @return the non dismissed messages for the specific task and specific user, if any, in ascending order of column {@code CREATED_AT}. diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeTaskMessageMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeTaskMessageMapper.java index e4600621889..67b8808dc37 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeTaskMessageMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeTaskMessageMapper.java @@ -28,8 +28,6 @@ public interface CeTaskMessageMapper { Optional selectByUuid(@Param("uuid") String uuid); - List selectByTask(@Param("taskUuid") String taskUuid); - List selectNonDismissedByUserAndTask(@Param("taskUuid") String taskUuid, @Param("userUuid") String userUuid); void deleteByType(@Param("ceMessageType") String ceMessageType); diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/ce/CeTaskMessageMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/ce/CeTaskMessageMapper.xml index 9666696891b..d4c7db4be42 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/ce/CeTaskMessageMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/ce/CeTaskMessageMapper.xml @@ -20,33 +20,6 @@ ctm.uuid=#{uuid,jdbcType=VARCHAR} - - - - insert into ce_task_message ( @@ -65,6 +38,22 @@ ) + + delete from ce_task_message where message_type = #{ceMessageType, jdbcType=VARCHAR} diff --git a/server/sonar-webserver-core/src/it/java/org/sonar/server/startup/UpgradeSuggestionsCleanerIT.java b/server/sonar-webserver-core/src/it/java/org/sonar/server/startup/UpgradeSuggestionsCleanerIT.java index fdf008ac82a..b621a7e8238 100644 --- a/server/sonar-webserver-core/src/it/java/org/sonar/server/startup/UpgradeSuggestionsCleanerIT.java +++ b/server/sonar-webserver-core/src/it/java/org/sonar/server/startup/UpgradeSuggestionsCleanerIT.java @@ -22,6 +22,7 @@ package org.sonar.server.startup; import com.tngtech.java.junit.dataprovider.DataProvider; import com.tngtech.java.junit.dataprovider.DataProviderRunner; import com.tngtech.java.junit.dataprovider.UseDataProvider; +import java.util.List; import org.junit.Before; import org.junit.Rule; import org.junit.Test; @@ -30,6 +31,8 @@ import org.sonar.api.SonarEdition; import org.sonar.api.SonarRuntime; import org.sonar.api.utils.System2; import org.sonar.db.DbTester; +import org.sonar.db.ce.CeActivityDto; +import org.sonar.db.ce.CeQueueDto; import org.sonar.db.ce.CeTaskMessageDto; import org.sonar.db.ce.CeTaskMessageType; import org.sonar.db.user.UserDismissedMessageDto; @@ -79,6 +82,7 @@ public class UpgradeSuggestionsCleanerIT { @UseDataProvider("editionsWithCleanup") public void start_cleans_up_obsolete_upgrade_suggestions(SonarEdition edition) { when(sonarRuntime.getEdition()).thenReturn(edition); + insertTask(TASK_UUID); insertCeTaskMessage("ctm1", CeTaskMessageType.GENERIC, "msg1"); insertCeTaskMessage("ctm2", CeTaskMessageType.GENERIC, "msg2"); insertCeTaskMessage("ctm3", CeTaskMessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE, "upgrade-msg-1"); @@ -88,7 +92,7 @@ public class UpgradeSuggestionsCleanerIT { underTest.start(); underTest.stop(); - assertThat(dbTester.getDbClient().ceTaskMessageDao().selectByTask(dbTester.getSession(), TASK_UUID)) + assertThat(getTaskMessages(TASK_UUID)) .extracting(CeTaskMessageDto::getUuid) .containsExactly("ctm1", "ctm2"); assertThat(dbTester.getDbClient().userDismissedMessagesDao().selectByUser(dbTester.getSession(), user)) @@ -99,6 +103,7 @@ public class UpgradeSuggestionsCleanerIT { @Test public void start_does_nothing_in_community_edition() { when(sonarRuntime.getEdition()).thenReturn(SonarEdition.COMMUNITY); + insertTask(TASK_UUID); insertCeTaskMessage("ctm1", CeTaskMessageType.GENERIC, "msg1"); insertCeTaskMessage("ctm2", CeTaskMessageType.GENERIC, "msg2"); insertCeTaskMessage("ctm3", CeTaskMessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE, "upgrade-msg-1"); @@ -107,7 +112,7 @@ public class UpgradeSuggestionsCleanerIT { underTest.start(); - assertThat(dbTester.getDbClient().ceTaskMessageDao().selectByTask(dbTester.getSession(), TASK_UUID)) + assertThat(getTaskMessages(TASK_UUID)) .extracting(CeTaskMessageDto::getUuid) .containsExactly("ctm1", "ctm2", "ctm3"); assertThat(dbTester.getDbClient().userDismissedMessagesDao().selectByUser(dbTester.getSession(), user)) @@ -122,10 +127,19 @@ public class UpgradeSuggestionsCleanerIT { underTest.start(); - assertThat(dbTester.getDbClient().ceTaskMessageDao().selectByTask(dbTester.getSession(), TASK_UUID)).isEmpty(); + assertThat(getTaskMessages(TASK_UUID)).isEmpty(); assertThat(dbTester.getDbClient().userDismissedMessagesDao().selectByUser(dbTester.getSession(), user)).isEmpty(); } + private List getTaskMessages(String taskUuid) { + return dbTester.getDbClient().ceActivityDao().selectByUuid(dbTester.getSession(), taskUuid).map(CeActivityDto::getCeTaskMessageDtos).orElse(List.of()); + } + + private void insertTask(String taskUuid) { + dbTester.getDbClient().ceActivityDao().insert(dbTester.getSession(), + new CeActivityDto(new CeQueueDto().setUuid(taskUuid).setTaskType("ISSUE_SYNC")).setStatus(CeActivityDto.Status.FAILED)); + } + private void insertCeTaskMessage(String uuid, CeTaskMessageType messageType, String msg) { CeTaskMessageDto dto = new CeTaskMessageDto() .setUuid(uuid) diff --git a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/AnalysisStatusAction.java b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/AnalysisStatusAction.java index 2f8f202297e..47523fe61cf 100644 --- a/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/AnalysisStatusAction.java +++ b/server/sonar-webserver-webapi/src/main/java/org/sonar/server/ce/ws/AnalysisStatusAction.java @@ -126,9 +126,9 @@ public class AnalysisStatusAction implements CeWsAction { List warnings; String userUuid = userSession.getUuid(); if (userUuid != null) { - warnings = dbClient.ceTaskMessageDao().selectNonDismissedByUserAndTask(dbSession, lastActivity.getUuid(), userUuid); + warnings = dbClient.ceTaskMessageDao().selectNonDismissedByUserAndTask(dbSession, lastActivity.getUuid(), userUuid); } else { - warnings = dbClient.ceTaskMessageDao().selectByTask(dbSession, lastActivity.getUuid()); + warnings = lastActivity.getCeTaskMessageDtos(); } List result = warnings.stream().map(dto -> AnalysisStatusWsResponse.Warning.newBuilder() -- 2.39.5