diff --git a/application/src/test/java/org/thingsboard/server/service/housekeeper/HousekeeperServiceTest.java b/application/src/test/java/org/thingsboard/server/service/housekeeper/HousekeeperServiceTest.java index 41dbf711e2..a6d5ae2463 100644 --- a/application/src/test/java/org/thingsboard/server/service/housekeeper/HousekeeperServiceTest.java +++ b/application/src/test/java/org/thingsboard/server/service/housekeeper/HousekeeperServiceTest.java @@ -31,7 +31,9 @@ import org.thingsboard.server.common.data.EventInfo; import org.thingsboard.server.common.data.StringUtils; import org.thingsboard.server.common.data.alarm.Alarm; import org.thingsboard.server.common.data.alarm.AlarmComment; +import org.thingsboard.server.common.data.alarm.AlarmCommentInfo; import org.thingsboard.server.common.data.alarm.AlarmCommentSubType; +import org.thingsboard.server.common.data.alarm.AlarmCommentType; import org.thingsboard.server.common.data.alarm.AlarmRef; import org.thingsboard.server.common.data.alarm.AlarmSeverity; import org.thingsboard.server.common.data.alarm.EntityAlarm; @@ -322,6 +324,37 @@ public class HousekeeperServiceTest extends AbstractControllerTest { }); } + @Test + public void whenAlarmCommentIsDeleted_thenKeepOtherAlarmComments() throws Exception { + Device device = createDevice("test", "test"); + Alarm alarm = doPost("/api/alarm", Alarm.builder() + .tenantId(tenantId) + .originator(device.getId()) + .severity(AlarmSeverity.CRITICAL) + .type("test alarm with deleted comment") + .build(), Alarm.class); + AlarmId alarmId = alarm.getId(); + List comments = new ArrayList<>(); + for (int i = 1; i <= 3; i++) { + comments.add(doPost("/api/alarm/" + alarmId + "/comment", AlarmComment.builder() + .comment(JacksonUtil.newObjectNode().set("text", new TextNode("comment " + i))) + .build(), AlarmComment.class)); + } + assertThat(alarmCommentService.findAlarmComments(tenantId, alarmId, new PageLink(100)).getData()).hasSize(3); + + doDelete("/api/alarm/" + alarmId + "/comment/" + comments.get(0).getId()).andExpect(status().isOk()); + + TimeUnit.SECONDS.sleep(2); + List remaining = alarmCommentService.findAlarmComments(tenantId, alarmId, new PageLink(100)).getData(); + assertThat(remaining).hasSize(3); + assertThat(remaining).filteredOn(comment -> comment.getId().equals(comments.get(0).getId())) + .singleElement() + .satisfies(comment -> { + assertThat(comment.getType()).isEqualTo(AlarmCommentType.SYSTEM); + assertThat(comment.getComment().get("subtype").asText()).isEqualTo(AlarmCommentSubType.COMMENT_DELETED.name()); + }); + } + @Test public void whenDeviceIsDeleted_thenDeleteAllAlarms() throws Exception { Device device = createDevice("test", "test"); diff --git a/dao/src/main/java/org/thingsboard/server/dao/housekeeper/CleanUpService.java b/dao/src/main/java/org/thingsboard/server/dao/housekeeper/CleanUpService.java index 3c2c93e0a9..44dea223bb 100644 --- a/dao/src/main/java/org/thingsboard/server/dao/housekeeper/CleanUpService.java +++ b/dao/src/main/java/org/thingsboard/server/dao/housekeeper/CleanUpService.java @@ -11,6 +11,7 @@ import org.springframework.transaction.annotation.Transactional; import org.springframework.transaction.event.TransactionalEventListener; import org.thingsboard.server.common.data.EntityType; import org.thingsboard.server.common.data.User; +import org.thingsboard.server.common.data.alarm.Alarm; import org.thingsboard.server.common.data.housekeeper.HousekeeperTask; import org.thingsboard.server.common.data.id.AlarmId; import org.thingsboard.server.common.data.id.EntityId; @@ -61,9 +62,10 @@ public class CleanUpService { if (entityType == EntityType.TENANT) { submitTask(HousekeeperTask.deleteAiTenantData(tenantId)); } - if (entityType == EntityType.ALARM && event.getCause() != ActionCause.BULK_DELETION) { + if (entityType == EntityType.ALARM && event.getEntity() instanceof Alarm && event.getCause() != ActionCause.BULK_DELETION) { // Single-alarm delete: clean up its comments per alarm. Bulk deletions (BULK_DELETION) batch the // comment cleanup into a single list task at the orchestrator instead, to avoid one task per alarm. + // The entity check matters: deleting a single alarm comment publishes the same event with the alarm id. submitTask(HousekeeperTask.deleteAlarmComments(tenantId, new AlarmId(entityId.getId()))); } } catch (Throwable e) { diff --git a/dao/src/test/java/org/thingsboard/server/dao/housekeeper/CleanUpServiceTest.java b/dao/src/test/java/org/thingsboard/server/dao/housekeeper/CleanUpServiceTest.java index 98ed202a11..ed66d2b6d2 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/housekeeper/CleanUpServiceTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/housekeeper/CleanUpServiceTest.java @@ -7,8 +7,10 @@ import org.junit.jupiter.api.Test; import org.mockito.ArgumentCaptor; import org.thingsboard.server.common.data.User; import org.thingsboard.server.common.data.alarm.Alarm; +import org.thingsboard.server.common.data.alarm.AlarmComment; import org.thingsboard.server.common.data.housekeeper.HousekeeperTask; import org.thingsboard.server.common.data.housekeeper.HousekeeperTaskType; +import org.thingsboard.server.common.data.id.AlarmCommentId; import org.thingsboard.server.common.data.id.AlarmId; import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.UserId; @@ -70,6 +72,7 @@ class CleanUpServiceTest { DeleteEntityEvent event = DeleteEntityEvent.builder() .tenantId(tenantId) .entityId(alarmId) + .entity(new Alarm(alarmId)) .build(); cleanUpService.handleEntityDeletionEvent(event); @@ -90,12 +93,29 @@ class CleanUpServiceTest { verify(relationService, never()).deleteEntityRelations(tenantId, alarmId); } + @Test + void alarmCommentDeletionDoesNotEnqueueAlarmCommentsCleanupTask() { + AlarmId alarmId = new AlarmId(UUID.randomUUID()); + AlarmComment alarmComment = new AlarmComment(new AlarmCommentId(UUID.randomUUID())); + alarmComment.setAlarmId(alarmId); + DeleteEntityEvent event = DeleteEntityEvent.builder() + .tenantId(tenantId) + .entityId(alarmId) + .entity(alarmComment) + .build(); + + cleanUpService.handleEntityDeletionEvent(event); + + verify(housekeeperClient, never()).submitTask(any()); + } + @Test void bulkAlarmDeletionDoesNotEnqueuePerAlarmCommentsCleanupTask() { AlarmId alarmId = new AlarmId(UUID.randomUUID()); DeleteEntityEvent event = DeleteEntityEvent.builder() .tenantId(tenantId) .entityId(alarmId) + .entity(new Alarm(alarmId)) .cause(ActionCause.BULK_DELETION) .build(); diff --git a/dao/src/test/java/org/thingsboard/server/dao/housekeeper/CleanUpServiceTransactionalTest.java b/dao/src/test/java/org/thingsboard/server/dao/housekeeper/CleanUpServiceTransactionalTest.java index 4fd8b2e8f4..de9f2a09e9 100644 --- a/dao/src/test/java/org/thingsboard/server/dao/housekeeper/CleanUpServiceTransactionalTest.java +++ b/dao/src/test/java/org/thingsboard/server/dao/housekeeper/CleanUpServiceTransactionalTest.java @@ -79,9 +79,11 @@ public class CleanUpServiceTransactionalTest extends AbstractServiceTest { } private DeleteEntityEvent alarmDeletionEvent() { + AlarmId alarmId = new AlarmId(UUID.randomUUID()); return DeleteEntityEvent.builder() .tenantId(tenantId) - .entityId(new AlarmId(UUID.randomUUID())) + .entityId(alarmId) + .entity(new Alarm(alarmId)) .build(); }