From fad21dcc7650b458bda97e5ef72e3d2090064894 Mon Sep 17 00:00:00 2001 From: Nolwenn Cadic <98824442+Nolwenn-cadic-sonarsource@users.noreply.github.com> Date: Thu, 17 Aug 2023 12:15:05 +0200 Subject: [PATCH] SONAR-20156 Create api/dismiss_message/dismiss endpoint --- .../HandleUnanalyzedLanguagesStep.java | 4 +- .../HandleUnanalyzedLanguagesStepTest.java | 4 +- .../step/PersistAnalysisWarningsStepTest.java | 4 +- .../org/sonar/ce/task/log/CeTaskMessages.java | 10 +- .../java/org/sonar/db/ce/CeActivityDaoIT.java | 3 +- .../org/sonar/db/ce/CeTaskMessageDaoIT.java | 25 ++-- .../org/sonar/db/purge/PurgeCommandsIT.java | 8 +- .../java/org/sonar/db/purge/PurgeDaoIT.java | 10 +- .../db/user/UserDismissedMessagesDaoIT.java | 65 +++++---- .../org/sonar/db/ce/CeTaskMessageDao.java | 3 +- .../org/sonar/db/ce/CeTaskMessageDto.java | 13 +- .../MessageType.java} | 13 +- .../db/user/UserDismissedMessageDto.java | 17 ++- .../db/user/UserDismissedMessagesDao.java | 15 +- .../db/user/UserDismissedMessagesMapper.java | 6 +- .../db/user/UserDismissedMessagesMapper.xml | 20 ++- .../java/org/sonar/db/user/UserDbTester.java | 16 ++- .../common/user/service/UserServiceIT.java | 10 +- .../startup/UpgradeSuggestionsCleanerIT.java | 28 ++-- .../startup/UpgradeSuggestionsCleaner.java | 6 +- .../sonar/server/ce/ws/ActivityActionIT.java | 4 +- .../server/ce/ws/AnalysisStatusActionIT.java | 12 +- .../sonar/server/ce/ws/ComponentActionIT.java | 4 +- .../ce/ws/DismissAnalysisWarningActionIT.java | 32 +++-- .../org/sonar/server/ce/ws/TaskActionIT.java | 14 +- .../dismissmessage/ws/DismissActionIT.java | 122 +++++++++++++++++ .../ws/DismissActionParameterizedIT.java | 106 ++++++++++++++ .../server/user/ws/DeactivateActionIT.java | 10 +- .../ce/ws/DismissAnalysisWarningAction.java | 12 +- .../dismissmessage/ws/DismissAction.java | 129 ++++++++++++++++++ .../dismissmessage/ws/DismissActionWs.java | 41 ++++++ .../ws/DismissMessageWsAction.java | 25 ++++ .../ws/DismissMessageWsModule.java | 31 +++++ .../sonar/server/ce/ws/TaskFormatterTest.java | 12 +- .../platformlevel/PlatformLevel4.java | 4 + 35 files changed, 679 insertions(+), 159 deletions(-) rename server/sonar-db-dao/src/main/java/org/sonar/db/{ce/CeTaskMessageType.java => dismissmessage/MessageType.java} (77%) create mode 100644 server/sonar-webserver-webapi/src/it/java/org/sonar/server/dismissmessage/ws/DismissActionIT.java create mode 100644 server/sonar-webserver-webapi/src/it/java/org/sonar/server/dismissmessage/ws/DismissActionParameterizedIT.java create mode 100644 server/sonar-webserver-webapi/src/main/java/org/sonar/server/dismissmessage/ws/DismissAction.java create mode 100644 server/sonar-webserver-webapi/src/main/java/org/sonar/server/dismissmessage/ws/DismissActionWs.java create mode 100644 server/sonar-webserver-webapi/src/main/java/org/sonar/server/dismissmessage/ws/DismissMessageWsAction.java create mode 100644 server/sonar-webserver-webapi/src/main/java/org/sonar/server/dismissmessage/ws/DismissMessageWsModule.java diff --git a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/language/HandleUnanalyzedLanguagesStep.java b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/language/HandleUnanalyzedLanguagesStep.java index 1a97da5b50c..10959fb405e 100644 --- a/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/language/HandleUnanalyzedLanguagesStep.java +++ b/server/sonar-ce-task-projectanalysis/src/main/java/org/sonar/ce/task/projectanalysis/language/HandleUnanalyzedLanguagesStep.java @@ -36,7 +36,7 @@ import org.sonar.ce.task.projectanalysis.metric.MetricRepository; import org.sonar.ce.task.step.ComputationStep; import org.sonar.core.platform.EditionProvider; import org.sonar.core.platform.PlatformEditionProvider; -import org.sonar.db.ce.CeTaskMessageType; +import org.sonar.db.dismissmessage.MessageType; import static java.lang.String.format; import static org.sonar.core.language.UnanalyzedLanguages.C; @@ -124,7 +124,7 @@ public class HandleUnanalyzedLanguagesStep implements ComputationStep { } String message = format(LANGUAGE_UPGRADE_MESSAGE, fileCountLabel, languageLabel, sortedLanguageMap.size() == 1 ? "this file" : "these files"); - return new CeTaskMessages.Message(message, system.now(), CeTaskMessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE); + return new CeTaskMessages.Message(message, system.now(), MessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE); } private void computeMeasures(Map filesPerLanguage) { diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/language/HandleUnanalyzedLanguagesStepTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/language/HandleUnanalyzedLanguagesStepTest.java index 0a72338702d..1df4d69e30b 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/language/HandleUnanalyzedLanguagesStepTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/language/HandleUnanalyzedLanguagesStepTest.java @@ -40,7 +40,7 @@ import org.sonar.ce.task.projectanalysis.metric.MetricRepositoryRule; import org.sonar.ce.task.step.TestComputationStepContext; import org.sonar.core.platform.EditionProvider; import org.sonar.core.platform.PlatformEditionProvider; -import org.sonar.db.ce.CeTaskMessageType; +import org.sonar.db.dismissmessage.MessageType; import org.sonar.scanner.protocol.output.ScannerReport; import static com.google.common.collect.ImmutableList.of; @@ -112,7 +112,7 @@ public class HandleUnanalyzedLanguagesStepTest { " C++ and SomeLang cannot be analyzed with your current SonarQube edition. Please consider" + " upgrading to Developer Edition to find Bugs," + " Code Smells, Vulnerabilities and Security Hotspots in these files.", - CeTaskMessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE)); + MessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE)); assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, UNANALYZED_C_KEY).get().getIntValue()).isEqualTo(10); assertThat(measureRepository.getAddedRawMeasure(PROJECT_REF, UNANALYZED_CPP_KEY).get().getIntValue()).isEqualTo(20); } diff --git a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/PersistAnalysisWarningsStepTest.java b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/PersistAnalysisWarningsStepTest.java index 15088a76fc5..d0c2a2c4180 100644 --- a/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/PersistAnalysisWarningsStepTest.java +++ b/server/sonar-ce-task-projectanalysis/src/test/java/org/sonar/ce/task/projectanalysis/step/PersistAnalysisWarningsStepTest.java @@ -30,7 +30,7 @@ import org.mockito.junit.MockitoJUnitRunner; import org.sonar.ce.task.log.CeTaskMessages; import org.sonar.ce.task.projectanalysis.batch.BatchReportReaderRule; import org.sonar.ce.task.step.TestComputationStepContext; -import org.sonar.db.ce.CeTaskMessageType; +import org.sonar.db.dismissmessage.MessageType; import org.sonar.scanner.protocol.output.ScannerReport; import static com.google.common.collect.ImmutableList.of; @@ -71,7 +71,7 @@ public class PersistAnalysisWarningsStepTest { verify(ceTaskMessages, times(1)).addAll(argumentCaptor.capture()); assertThat(argumentCaptor.getValue()) .extracting(CeTaskMessages.Message::getText, CeTaskMessages.Message::getType) - .containsExactly(tuple("warning 1", CeTaskMessageType.GENERIC), tuple("warning 2", CeTaskMessageType.GENERIC)); + .containsExactly(tuple("warning 1", MessageType.GENERIC), tuple("warning 2", MessageType.GENERIC)); } @Test diff --git a/server/sonar-ce-task/src/main/java/org/sonar/ce/task/log/CeTaskMessages.java b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/log/CeTaskMessages.java index 9c9ce465f61..a90d92ece04 100644 --- a/server/sonar-ce-task/src/main/java/org/sonar/ce/task/log/CeTaskMessages.java +++ b/server/sonar-ce-task/src/main/java/org/sonar/ce/task/log/CeTaskMessages.java @@ -23,7 +23,7 @@ import java.util.Collection; import java.util.Objects; import javax.annotation.concurrent.Immutable; import org.sonar.api.ce.ComputeEngineSide; -import org.sonar.db.ce.CeTaskMessageType; +import org.sonar.db.dismissmessage.MessageType; import static com.google.common.base.Preconditions.checkArgument; import static java.util.Objects.requireNonNull; @@ -49,9 +49,9 @@ public interface CeTaskMessages { class Message { private final String text; private final long timestamp; - private final CeTaskMessageType type; + private final MessageType type; - public Message(String text, long timestamp, CeTaskMessageType type) { + public Message(String text, long timestamp, MessageType type) { requireNonNull(text, "Text can't be null"); checkArgument(!text.isEmpty(), "Text can't be empty"); checkArgument(timestamp >= 0, "Timestamp can't be less than 0"); @@ -61,7 +61,7 @@ public interface CeTaskMessages { } public Message(String text, long timestamp) { - this(text, timestamp, CeTaskMessageType.GENERIC); + this(text, timestamp, MessageType.GENERIC); } public String getText() { @@ -72,7 +72,7 @@ public interface CeTaskMessages { return timestamp; } - public CeTaskMessageType getType() { + public MessageType getType() { return type; } diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/ce/CeActivityDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/ce/CeActivityDaoIT.java index 1b3ff6f93d2..1b2def40207 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/ce/CeActivityDaoIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/ce/CeActivityDaoIT.java @@ -47,6 +47,7 @@ import org.sonar.db.DbSession; import org.sonar.db.DbTester; import org.sonar.db.Pagination; import org.sonar.db.component.BranchDto; +import org.sonar.db.dismissmessage.MessageType; import org.sonar.db.project.ProjectDto; import static java.util.Collections.emptyList; @@ -153,7 +154,7 @@ public class CeActivityDaoIT { .setUuid(UuidFactoryFast.getInstance().create()) .setTaskUuid(task.getUuid()) .setMessage("message_" + task.getUuid() + "_" + i) - .setType(CeTaskMessageType.GENERIC) + .setType(MessageType.GENERIC) .setCreatedAt(task.getUuid().hashCode() + i); } 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 0e5b6a295e7..6d16f66c0f3 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 @@ -27,6 +27,7 @@ import org.junit.Test; import org.sonar.api.utils.System2; import org.sonar.db.DbSession; import org.sonar.db.DbTester; +import org.sonar.db.dismissmessage.MessageType; import org.sonar.db.project.ProjectDto; import org.sonar.db.user.UserDto; @@ -44,7 +45,7 @@ public class CeTaskMessageDaoIT { .setUuid("uuid_1") .setTaskUuid("task_uuid_1") .setMessage("message_1") - .setType(CeTaskMessageType.GENERIC) + .setType(MessageType.GENERIC) .setCreatedAt(1_222_333L)); dbTester.getSession().commit(); @@ -52,9 +53,9 @@ public class CeTaskMessageDaoIT { dbTester.select("select uuid as \"UUID\", task_uuid as \"TASK_UUID\", message as \"MESSAGE\", message_type as \"TYPE\", " + "created_at as \"CREATED_AT\" from ce_task_message")) .hasSize(1) - .extracting(t -> t.get("UUID"), t -> t.get("TASK_UUID"), t -> t.get("MESSAGE"), t -> CeTaskMessageType.valueOf((String) t.get("TYPE")), + .extracting(t -> t.get("UUID"), t -> t.get("TASK_UUID"), t -> t.get("MESSAGE"), t -> MessageType.valueOf((String) t.get("TYPE")), t -> t.get("CREATED_AT")) - .containsOnly(Tuple.tuple("uuid_1", "task_uuid_1", "message_1", CeTaskMessageType.GENERIC, 1_222_333L)); + .containsOnly(Tuple.tuple("uuid_1", "task_uuid_1", "message_1", MessageType.GENERIC, 1_222_333L)); } @Test @@ -78,13 +79,13 @@ public class CeTaskMessageDaoIT { public void deleteByType_deletes_messages_of_given_type() { String task1 = "task1"; CeTaskMessageDto[] messages = { - insertMessage(task1, 0, 1_222_333L, CeTaskMessageType.GENERIC), - insertMessage(task1, 1, 2_222_333L, CeTaskMessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE), - insertMessage(task1, 2, 1_111_333L, CeTaskMessageType.GENERIC), - insertMessage(task1, 3, 1_222_111L, CeTaskMessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE) + insertMessage(task1, 0, 1_222_333L, MessageType.GENERIC), + insertMessage(task1, 1, 2_222_333L, MessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE), + insertMessage(task1, 2, 1_111_333L, MessageType.GENERIC), + insertMessage(task1, 3, 1_222_111L, MessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE) }; - underTest.deleteByType(dbTester.getSession(), CeTaskMessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE); + underTest.deleteByType(dbTester.getSession(), MessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE); assertThat(underTest.selectByUuid(dbTester.getSession(), messages[0].getUuid())).isPresent(); assertThat(underTest.selectByUuid(dbTester.getSession(), messages[1].getUuid())).isEmpty(); @@ -106,10 +107,10 @@ public class CeTaskMessageDaoIT { public void selectNonDismissedByUserAndTask_returns_non_dismissed_messages() { UserDto user = dbTester.users().insertUser(); ProjectDto project = dbTester.components().insertPrivateProject().getProjectDto(); - dbTester.users().insertUserDismissedMessage(user, project, CeTaskMessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE); + dbTester.users().insertUserDismissedMessageOnProject(user, project, MessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE); String taskUuid = "17ae66e6-fe83-4c80-b704-4b04e9c5abe8"; CeTaskMessageDto msg1 = insertMessage(taskUuid, 1, 1_222_333L); - insertMessage(taskUuid, 2, 1_222_334L, CeTaskMessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE); + insertMessage(taskUuid, 2, 1_222_334L, MessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE); CeTaskMessageDto msg3 = insertMessage(taskUuid, 3, 1_222_335L); List messages = underTest.selectNonDismissedByUserAndTask(dbTester.getSession(), taskUuid, user.getUuid()); @@ -118,10 +119,10 @@ public class CeTaskMessageDaoIT { } private CeTaskMessageDto insertMessage(String taskUuid, int i, long createdAt) { - return insertMessage(taskUuid, i, createdAt, CeTaskMessageType.GENERIC); + return insertMessage(taskUuid, i, createdAt, MessageType.GENERIC); } - private CeTaskMessageDto insertMessage(String taskUuid, int i, long createdAt, CeTaskMessageType messageType) { + private CeTaskMessageDto insertMessage(String taskUuid, int i, long createdAt, MessageType messageType) { CeTaskMessageDto res = new CeTaskMessageDto() .setUuid("message_" + i) .setTaskUuid(taskUuid) diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/purge/PurgeCommandsIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/purge/PurgeCommandsIT.java index b89a854e009..890a6f81ee7 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/purge/PurgeCommandsIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/purge/PurgeCommandsIT.java @@ -42,7 +42,7 @@ import org.sonar.api.issue.impact.SoftwareQuality; import org.sonar.api.utils.System2; import org.sonar.core.util.UuidFactoryFast; import org.sonar.db.DbTester; -import org.sonar.db.ce.CeTaskMessageType; +import org.sonar.db.dismissmessage.MessageType; import org.sonar.db.component.BranchDto; import org.sonar.db.component.ComponentDto; import org.sonar.db.component.ComponentTesting; @@ -702,9 +702,9 @@ public class PurgeCommandsIT { UserDto user2 = dbTester.users().insertUser(); ProjectDto project = dbTester.components().insertPrivateProject().getProjectDto(); ProjectDto anotherProject = dbTester.components().insertPrivateProject().getProjectDto(); - dbTester.users().insertUserDismissedMessage(user1, project, CeTaskMessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE); - dbTester.users().insertUserDismissedMessage(user2, project, CeTaskMessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE); - dbTester.users().insertUserDismissedMessage(user1, anotherProject, CeTaskMessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE); + dbTester.users().insertUserDismissedMessageOnProject(user1, project, MessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE); + dbTester.users().insertUserDismissedMessageOnProject(user2, project, MessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE); + dbTester.users().insertUserDismissedMessageOnProject(user1, anotherProject, MessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE); PurgeCommands purgeCommands = new PurgeCommands(dbTester.getSession(), profiler, system2); purgeCommands.deleteUserDismissedMessages(project.getUuid()); diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/purge/PurgeDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/purge/PurgeDaoIT.java index 5fa203103b0..808bb724f12 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/purge/PurgeDaoIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/purge/PurgeDaoIT.java @@ -62,7 +62,7 @@ import org.sonar.db.ce.CeQueueDto; import org.sonar.db.ce.CeQueueDto.Status; import org.sonar.db.ce.CeTaskCharacteristicDto; import org.sonar.db.ce.CeTaskMessageDto; -import org.sonar.db.ce.CeTaskMessageType; +import org.sonar.db.dismissmessage.MessageType; import org.sonar.db.ce.CeTaskTypes; import org.sonar.db.component.BranchDto; import org.sonar.db.component.BranchType; @@ -910,9 +910,9 @@ public class PurgeDaoIT { ProjectDto project = db.components().insertPrivateProject().getProjectDto(); ProjectDto anotherProject = db.components().insertPrivateProject().getProjectDto(); - UserDismissedMessageDto msg1 = db.users().insertUserDismissedMessage(user1, project, CeTaskMessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE); - UserDismissedMessageDto msg2 = db.users().insertUserDismissedMessage(user2, project, CeTaskMessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE); - UserDismissedMessageDto msg3 = db.users().insertUserDismissedMessage(user1, anotherProject, CeTaskMessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE); + UserDismissedMessageDto msg1 = db.users().insertUserDismissedMessageOnProject(user1, project, MessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE); + UserDismissedMessageDto msg2 = db.users().insertUserDismissedMessageOnProject(user2, project, MessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE); + UserDismissedMessageDto msg3 = db.users().insertUserDismissedMessageOnProject(user1, anotherProject, MessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE); assertThat(uuidsIn("user_dismissed_messages")).containsOnly(msg1.getUuid(), msg2.getUuid(), msg3.getUuid()); @@ -2002,7 +2002,7 @@ public class PurgeDaoIT { .setUuid(UuidFactoryFast.getInstance().create()) .setTaskUuid(uuid) .setMessage("key_" + uuid.hashCode() + i) - .setType(CeTaskMessageType.GENERIC) + .setType(MessageType.GENERIC) .setCreatedAt(2_333_444L + i)) .forEach(dto -> dbClient.ceTaskMessageDao().insert(dbSession, dto)); dbSession.commit(); diff --git a/server/sonar-db-dao/src/it/java/org/sonar/db/user/UserDismissedMessagesDaoIT.java b/server/sonar-db-dao/src/it/java/org/sonar/db/user/UserDismissedMessagesDaoIT.java index 1746aa2cace..aabd4617f56 100644 --- a/server/sonar-db-dao/src/it/java/org/sonar/db/user/UserDismissedMessagesDaoIT.java +++ b/server/sonar-db-dao/src/it/java/org/sonar/db/user/UserDismissedMessagesDaoIT.java @@ -26,12 +26,12 @@ import org.junit.Test; import org.sonar.api.utils.System2; import org.sonar.core.util.Uuids; import org.sonar.db.DbTester; -import org.sonar.db.ce.CeTaskMessageType; +import org.sonar.db.dismissmessage.MessageType; import org.sonar.db.project.ProjectDto; import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.tuple; -import static org.sonar.db.ce.CeTaskMessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE; +import static org.sonar.db.dismissmessage.MessageType.SUGGEST_DEVELOPER_EDITION_UPGRADE; public class UserDismissedMessagesDaoIT { @@ -48,13 +48,13 @@ public class UserDismissedMessagesDaoIT { underTest.insert(db.getSession(), dto); - Optional dtoFromDb = underTest.selectByUserAndProjectAndMessageType(db.getSession(), user, project, dto.getCeMessageType()); + Optional dtoFromDb = underTest.selectByUserAndProjectAndMessageType(db.getSession(), user.getUuid(), project, dto.getMessageType()); assertThat(dtoFromDb).isPresent(); assertThat(dtoFromDb.get().getUuid()).isEqualTo(dto.getUuid()); assertThat(dtoFromDb.get().getUserUuid()).isEqualTo(dto.getUserUuid()); assertThat(dtoFromDb.get().getProjectUuid()).isEqualTo(dto.getProjectUuid()); - assertThat(dtoFromDb.get().getCeMessageType()).isEqualTo(dto.getCeMessageType()); - assertThat(dtoFromDb.get().getCeMessageType().isDismissible()).isEqualTo(dto.getCeMessageType().isDismissible()); + assertThat(dtoFromDb.get().getMessageType()).isEqualTo(dto.getMessageType()); + assertThat(dtoFromDb.get().getMessageType().isDismissible()).isEqualTo(dto.getMessageType().isDismissible()); assertThat(dtoFromDb.get().getCreatedAt()).isEqualTo(dto.getCreatedAt()); } @@ -62,23 +62,36 @@ public class UserDismissedMessagesDaoIT { public void selectByUserAndProjectAndMessageType_returns_object_if_record_found() { UserDto user = db.users().insertUser(); ProjectDto project = db.components().insertPrivateProject().getProjectDto(); - db.users().insertUserDismissedMessage(user, project, CeTaskMessageType.GENERIC); + db.users().insertUserDismissedMessageOnProject(user, project, MessageType.GENERIC); - Optional result = underTest.selectByUserAndProjectAndMessageType(db.getSession(), user, project, CeTaskMessageType.GENERIC); + Optional result = underTest.selectByUserAndProjectAndMessageType(db.getSession(), user.getUuid(), project, MessageType.GENERIC); assertThat(result).isPresent(); assertThat(result.get().getUserUuid()).isEqualTo(user.getUuid()); assertThat(result.get().getProjectUuid()).isEqualTo(project.getUuid()); - assertThat(result.get().getCeMessageType()).isEqualTo(CeTaskMessageType.GENERIC); + assertThat(result.get().getMessageType()).isEqualTo(MessageType.GENERIC); + } + + @Test + public void selectByUserAndMessageType_returns_object_if_record_found() { + UserDto user = db.users().insertUser(); + db.users().insertUserDismissedMessageOnInstance(user, MessageType.GENERIC); + + Optional result = underTest.selectByUserUuidAndMessageType(db.getSession(), user.getUuid(),MessageType.GENERIC); + + assertThat(result).isPresent(); + assertThat(result.get().getUserUuid()).isEqualTo(user.getUuid()); + assertThat(result.get().getProjectUuid()).isNull(); + assertThat(result.get().getMessageType()).isEqualTo(MessageType.GENERIC); } @Test public void selectByUserAndProjectAndMessageType_returns_absent_if_no_record_found() { UserDto user = db.users().insertUser(); ProjectDto project = db.components().insertPrivateProject().getProjectDto(); - db.users().insertUserDismissedMessage(user, project, CeTaskMessageType.GENERIC); + db.users().insertUserDismissedMessageOnProject(user, project, MessageType.GENERIC); - Optional result = underTest.selectByUserAndProjectAndMessageType(db.getSession(), user, project, SUGGEST_DEVELOPER_EDITION_UPGRADE); + Optional result = underTest.selectByUserAndProjectAndMessageType(db.getSession(), user.getUuid(), project, SUGGEST_DEVELOPER_EDITION_UPGRADE); assertThat(result).isNotPresent(); } @@ -89,19 +102,19 @@ public class UserDismissedMessagesDaoIT { UserDto user2 = db.users().insertUser(); ProjectDto project1 = db.components().insertPrivateProject().getProjectDto(); ProjectDto project2 = db.components().insertPrivateProject().getProjectDto(); - db.users().insertUserDismissedMessage(user1, project1, CeTaskMessageType.GENERIC); - db.users().insertUserDismissedMessage(user1, project2, CeTaskMessageType.GENERIC); - UserDismissedMessageDto dto1 = db.users().insertUserDismissedMessage(user2, project1, CeTaskMessageType.GENERIC); - UserDismissedMessageDto dto2 = db.users().insertUserDismissedMessage(user2, project2, CeTaskMessageType.GENERIC); + db.users().insertUserDismissedMessageOnProject(user1, project1, MessageType.GENERIC); + db.users().insertUserDismissedMessageOnProject(user1, project2, MessageType.GENERIC); + UserDismissedMessageDto dto1 = db.users().insertUserDismissedMessageOnProject(user2, project1, MessageType.GENERIC); + UserDismissedMessageDto dto2 = db.users().insertUserDismissedMessageOnProject(user2, project2, MessageType.GENERIC); List result = underTest.selectByUser(db.getSession(), user2); assertThat(result).hasSize(2); assertThat(result).extracting(UserDismissedMessageDto::getUuid, UserDismissedMessageDto::getUserUuid, UserDismissedMessageDto::getProjectUuid, - UserDismissedMessageDto::getCeMessageType) + UserDismissedMessageDto::getMessageType) .containsExactlyInAnyOrder( - tuple(dto1.getUuid(), user2.getUuid(), project1.getUuid(), CeTaskMessageType.GENERIC), - tuple(dto2.getUuid(), user2.getUuid(), project2.getUuid(), CeTaskMessageType.GENERIC)); + tuple(dto1.getUuid(), user2.getUuid(), project1.getUuid(), MessageType.GENERIC), + tuple(dto2.getUuid(), user2.getUuid(), project2.getUuid(), MessageType.GENERIC)); } @Test @@ -110,10 +123,10 @@ public class UserDismissedMessagesDaoIT { UserDto user2 = db.users().insertUser(); ProjectDto project1 = db.components().insertPrivateProject().getProjectDto(); ProjectDto project2 = db.components().insertPrivateProject().getProjectDto(); - db.users().insertUserDismissedMessage(user1, project1, CeTaskMessageType.GENERIC); - db.users().insertUserDismissedMessage(user1, project2, CeTaskMessageType.GENERIC); - db.users().insertUserDismissedMessage(user2, project1, CeTaskMessageType.GENERIC); - db.users().insertUserDismissedMessage(user2, project2, CeTaskMessageType.GENERIC); + db.users().insertUserDismissedMessageOnProject(user1, project1, MessageType.GENERIC); + db.users().insertUserDismissedMessageOnProject(user1, project2, MessageType.GENERIC); + db.users().insertUserDismissedMessageOnProject(user2, project1, MessageType.GENERIC); + db.users().insertUserDismissedMessageOnProject(user2, project2, MessageType.GENERIC); underTest.deleteByUser(db.getSession(), user2); @@ -127,10 +140,10 @@ public class UserDismissedMessagesDaoIT { UserDto user2 = db.users().insertUser(); ProjectDto project1 = db.components().insertPrivateProject().getProjectDto(); ProjectDto project2 = db.components().insertPrivateProject().getProjectDto(); - UserDismissedMessageDto dto1 = db.users().insertUserDismissedMessage(user1, project1, CeTaskMessageType.GENERIC); - db.users().insertUserDismissedMessage(user1, project2, SUGGEST_DEVELOPER_EDITION_UPGRADE); - db.users().insertUserDismissedMessage(user2, project1, SUGGEST_DEVELOPER_EDITION_UPGRADE); - UserDismissedMessageDto dto2 = db.users().insertUserDismissedMessage(user2, project2, CeTaskMessageType.GENERIC); + UserDismissedMessageDto dto1 = db.users().insertUserDismissedMessageOnProject(user1, project1, MessageType.GENERIC); + db.users().insertUserDismissedMessageOnProject(user1, project2, SUGGEST_DEVELOPER_EDITION_UPGRADE); + db.users().insertUserDismissedMessageOnProject(user2, project1, SUGGEST_DEVELOPER_EDITION_UPGRADE); + UserDismissedMessageDto dto2 = db.users().insertUserDismissedMessageOnProject(user2, project2, MessageType.GENERIC); underTest.deleteByType(db.getSession(), SUGGEST_DEVELOPER_EDITION_UPGRADE); @@ -145,7 +158,7 @@ public class UserDismissedMessagesDaoIT { public static UserDismissedMessageDto newDto(ProjectDto project, UserDto user) { return new UserDismissedMessageDto() .setUuid(Uuids.createFast()) - .setCeMessageType(CeTaskMessageType.GENERIC) + .setMessageType(MessageType.GENERIC) .setUserUuid(user.getUuid()) .setProjectUuid(project.getUuid()); } 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 588827cef60..89d1b36e044 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 @@ -23,6 +23,7 @@ import java.util.List; import java.util.Optional; import org.sonar.db.Dao; import org.sonar.db.DbSession; +import org.sonar.db.dismissmessage.MessageType; public class CeTaskMessageDao implements Dao { public void insert(DbSession dbSession, CeTaskMessageDto dto) { @@ -41,7 +42,7 @@ public class CeTaskMessageDao implements Dao { return getMapper(dbSession).selectNonDismissedByUserAndTask(taskUuid, userUuid); } - public void deleteByType(DbSession session, CeTaskMessageType type) { + public void deleteByType(DbSession session, MessageType type) { getMapper(session).deleteByType(type.name()); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeTaskMessageDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeTaskMessageDto.java index 1b3b710fa81..52831188c2b 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeTaskMessageDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeTaskMessageDto.java @@ -20,6 +20,7 @@ package org.sonar.db.ce; import com.google.common.annotations.VisibleForTesting; +import org.sonar.db.dismissmessage.MessageType; import static com.google.common.base.Preconditions.checkArgument; import static org.apache.commons.lang.StringUtils.abbreviate; @@ -35,19 +36,25 @@ public class CeTaskMessageDto { * UUID of the task the message belongs to. Not null */ private String taskUuid; + /** * The text of the message. Not null */ private String message; + /** * Type of the message */ - private CeTaskMessageType type; + private MessageType type; /** * Timestamp the message was created. Not null */ private long createdAt; + public CeTaskMessageDto() { + //Nothing to do + } + public String getUuid() { return uuid; } @@ -76,11 +83,11 @@ public class CeTaskMessageDto { return this; } - public CeTaskMessageType getType() { + public MessageType getType() { return type; } - public CeTaskMessageDto setType(CeTaskMessageType type) { + public CeTaskMessageDto setType(MessageType type) { this.type = type; return this; } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeTaskMessageType.java b/server/sonar-db-dao/src/main/java/org/sonar/db/dismissmessage/MessageType.java similarity index 77% rename from server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeTaskMessageType.java rename to server/sonar-db-dao/src/main/java/org/sonar/db/dismissmessage/MessageType.java index da7a1ddb2b2..8a482827abd 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/ce/CeTaskMessageType.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/dismissmessage/MessageType.java @@ -17,17 +17,22 @@ * 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.ce; +package org.sonar.db.dismissmessage; -public enum CeTaskMessageType { +public enum MessageType { INFO(false, false), GENERIC(false, true), - SUGGEST_DEVELOPER_EDITION_UPGRADE(true, true); + SUGGEST_DEVELOPER_EDITION_UPGRADE(true, true), + GLOBAL_NCD_90(true, false), + GLOBAL_NCD_PAGE_90(true, false), + PROJECT_NCD_90(true, false), + PROJECT_NCD_PAGE_90(true, false), + BRANCH_NCD_90(true, false); private final boolean dismissible; private final boolean isWarning; - CeTaskMessageType(boolean dismissible, boolean isWarning) { + MessageType(boolean dismissible, boolean isWarning) { this.dismissible = dismissible; this.isWarning = isWarning; } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDismissedMessageDto.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDismissedMessageDto.java index 26db8577a53..c2154f076fe 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDismissedMessageDto.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDismissedMessageDto.java @@ -19,7 +19,9 @@ */ package org.sonar.db.user; -import org.sonar.db.ce.CeTaskMessageType; +import javax.annotation.CheckForNull; +import javax.annotation.Nullable; +import org.sonar.db.dismissmessage.MessageType; public class UserDismissedMessageDto { @@ -31,11 +33,12 @@ public class UserDismissedMessageDto { /** * Uuid of the project for which the message type was dismissed */ + @CheckForNull private String projectUuid; /** * Message type of the dismissed message */ - private CeTaskMessageType ceMessageType; + private MessageType messageType; /** * Technical creation date */ @@ -67,17 +70,17 @@ public class UserDismissedMessageDto { return projectUuid; } - public UserDismissedMessageDto setProjectUuid(String projectUuid) { + public UserDismissedMessageDto setProjectUuid(@Nullable String projectUuid) { this.projectUuid = projectUuid; return this; } - public CeTaskMessageType getCeMessageType() { - return ceMessageType; + public MessageType getMessageType() { + return messageType; } - public UserDismissedMessageDto setCeMessageType(CeTaskMessageType ceMessageType) { - this.ceMessageType = ceMessageType; + public UserDismissedMessageDto setMessageType(MessageType messageType) { + this.messageType = messageType; return this; } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDismissedMessagesDao.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDismissedMessagesDao.java index b1b4ba41cc8..577567e54e6 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDismissedMessagesDao.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDismissedMessagesDao.java @@ -24,7 +24,7 @@ import java.util.Optional; import org.sonar.api.utils.System2; import org.sonar.db.Dao; import org.sonar.db.DbSession; -import org.sonar.db.ce.CeTaskMessageType; +import org.sonar.db.dismissmessage.MessageType; import org.sonar.db.project.ProjectDto; public class UserDismissedMessagesDao implements Dao { @@ -40,9 +40,14 @@ public class UserDismissedMessagesDao implements Dao { return dto; } - public Optional selectByUserAndProjectAndMessageType(DbSession session, UserDto user, ProjectDto project, - CeTaskMessageType ceMessageType) { - return mapper(session).selectByUserUuidAndProjectUuidAndMessageType(user.getUuid(), project.getUuid(), ceMessageType.name()); + public Optional selectByUserAndProjectAndMessageType(DbSession session, String userUuid, ProjectDto project, + MessageType messageType) { + return mapper(session).selectByUserUuidAndProjectUuidAndMessageType(userUuid, project.getUuid(), messageType.name()); + } + + public Optional selectByUserUuidAndMessageType(DbSession session, String userUuid, + MessageType messageType) { + return mapper(session).selectByUserUuidAndMessageType(userUuid, messageType.name()); } public List selectByUser(DbSession session, UserDto user) { @@ -53,7 +58,7 @@ public class UserDismissedMessagesDao implements Dao { mapper(session).deleteByUserUuid(user.getUuid()); } - public void deleteByType(DbSession session, CeTaskMessageType type) { + public void deleteByType(DbSession session, MessageType type) { mapper(session).deleteByType(type.name()); } diff --git a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDismissedMessagesMapper.java b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDismissedMessagesMapper.java index 367ae5b5f9d..0f27dd75cd7 100644 --- a/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDismissedMessagesMapper.java +++ b/server/sonar-db-dao/src/main/java/org/sonar/db/user/UserDismissedMessagesMapper.java @@ -27,11 +27,13 @@ public interface UserDismissedMessagesMapper { void insert(@Param("dto") UserDismissedMessageDto dto); Optional selectByUserUuidAndProjectUuidAndMessageType(@Param("userUuid") String userUuid, @Param("projectUuid") String projectUuid, - @Param("ceMessageType") String ceMessageType); + @Param("messageType") String messageType); + + Optional selectByUserUuidAndMessageType(@Param("userUuid") String userUuid, @Param("messageType") String messageType); List selectByUserUuid(@Param("userUuid") String userUuid); void deleteByUserUuid(@Param("userUuid") String userUuid); - void deleteByType(@Param("ceMessageType") String ceMessageType); + void deleteByType(@Param("messageType") String messageType); } diff --git a/server/sonar-db-dao/src/main/resources/org/sonar/db/user/UserDismissedMessagesMapper.xml b/server/sonar-db-dao/src/main/resources/org/sonar/db/user/UserDismissedMessagesMapper.xml index 12fad9f4643..bf10b175907 100644 --- a/server/sonar-db-dao/src/main/resources/org/sonar/db/user/UserDismissedMessagesMapper.xml +++ b/server/sonar-db-dao/src/main/resources/org/sonar/db/user/UserDismissedMessagesMapper.xml @@ -7,7 +7,7 @@ udm.uuid, udm.user_uuid as userUuid, udm.project_uuid as projectUuid, - udm.message_type as ceMessageType, + udm.message_type as messageType, udm.created_at as createdAt @@ -24,7 +24,7 @@ #{dto.uuid,jdbcType=VARCHAR}, #{dto.userUuid,jdbcType=VARCHAR}, #{dto.projectUuid,jdbcType=VARCHAR}, - #{dto.ceMessageType,jdbcType=VARCHAR}, + #{dto.messageType,jdbcType=VARCHAR}, #{dto.createdAt,jdbcType=BIGINT} ) @@ -37,10 +37,20 @@ where udm.user_uuid=#{userUuid,jdbcType=VARCHAR} and udm.project_uuid=#{projectUuid,jdbcType=VARCHAR} and - udm.message_type=#{ceMessageType,jdbcType=VARCHAR} + udm.message_type=#{messageType,jdbcType=VARCHAR} - + select + + from + user_dismissed_messages udm + where + udm.user_uuid=#{userUuid,jdbcType=VARCHAR} and + udm.message_type=#{messageType,jdbcType=VARCHAR} + + +