|
|
|
@ -15,6 +15,7 @@ |
|
|
|
*/ |
|
|
|
package org.thingsboard.server.service.entitiy.alarm; |
|
|
|
|
|
|
|
import com.fasterxml.jackson.databind.node.ObjectNode; |
|
|
|
import lombok.extern.slf4j.Slf4j; |
|
|
|
import org.junit.Test; |
|
|
|
import org.junit.runner.RunWith; |
|
|
|
@ -23,14 +24,20 @@ import org.springframework.boot.test.mock.mockito.SpyBean; |
|
|
|
import org.springframework.test.context.ContextConfiguration; |
|
|
|
import org.springframework.test.context.TestPropertySource; |
|
|
|
import org.springframework.test.context.junit4.SpringRunner; |
|
|
|
import org.thingsboard.common.util.JacksonUtil; |
|
|
|
import org.thingsboard.server.cluster.TbClusterService; |
|
|
|
import org.thingsboard.server.common.data.User; |
|
|
|
import org.thingsboard.server.common.data.alarm.Alarm; |
|
|
|
import org.thingsboard.server.common.data.alarm.AlarmApiCallResult; |
|
|
|
import org.thingsboard.server.common.data.alarm.AlarmComment; |
|
|
|
import org.thingsboard.server.common.data.alarm.AlarmCommentType; |
|
|
|
import org.thingsboard.server.common.data.alarm.AlarmInfo; |
|
|
|
import org.thingsboard.server.common.data.audit.ActionType; |
|
|
|
import org.thingsboard.server.common.data.exception.ThingsboardException; |
|
|
|
import org.thingsboard.server.common.data.id.AlarmId; |
|
|
|
import org.thingsboard.server.common.data.id.TenantId; |
|
|
|
import org.thingsboard.server.common.data.id.UserId; |
|
|
|
import org.thingsboard.server.common.data.page.PageData; |
|
|
|
import org.thingsboard.server.dao.alarm.AlarmService; |
|
|
|
import org.thingsboard.server.dao.customer.CustomerService; |
|
|
|
import org.thingsboard.server.dao.edge.EdgeService; |
|
|
|
@ -39,6 +46,8 @@ import org.thingsboard.server.service.executors.DbCallbackExecutorService; |
|
|
|
import org.thingsboard.server.service.sync.vc.EntitiesVersionControlService; |
|
|
|
import org.thingsboard.server.service.telemetry.AlarmSubscriptionService; |
|
|
|
|
|
|
|
import java.util.Collections; |
|
|
|
import java.util.List; |
|
|
|
import java.util.UUID; |
|
|
|
|
|
|
|
import static org.mockito.ArgumentMatchers.any; |
|
|
|
@ -123,4 +132,61 @@ public class DefaultTbAlarmServiceTest { |
|
|
|
verify(notificationEntityService, times(1)).logEntityAction(any(), any(), any(), any(), eq(ActionType.DELETED), any()); |
|
|
|
verify(alarmSubscriptionService, times(1)).deleteAlarm(any(), any()); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void testUnassignAlarm() throws ThingsboardException { |
|
|
|
AlarmInfo alarm = new AlarmInfo(); |
|
|
|
alarm.setId(new AlarmId(UUID.randomUUID())); |
|
|
|
when(alarmSubscriptionService.unassignAlarm(any(), any(), anyLong())) |
|
|
|
.thenReturn(AlarmApiCallResult.builder().successful(true).modified(true).alarm(alarm).build()); |
|
|
|
|
|
|
|
User user = new User(); |
|
|
|
user.setEmail("testEmail@gmail.com"); |
|
|
|
user.setId(new UserId(UUID.randomUUID())); |
|
|
|
service.unassign(new Alarm(), 0L, user); |
|
|
|
|
|
|
|
ObjectNode commentNode = JacksonUtil.newObjectNode(); |
|
|
|
commentNode.put("subtype", "ASSIGN"); |
|
|
|
commentNode.put("text", "Alarm was unassigned by user " + user.getTitle()); |
|
|
|
commentNode.put("userId", user.getId().getId().toString()); |
|
|
|
AlarmComment expectedAlarmComment = AlarmComment.builder() |
|
|
|
.alarmId(alarm.getId()) |
|
|
|
.type(AlarmCommentType.SYSTEM) |
|
|
|
.comment(commentNode) |
|
|
|
.build(); |
|
|
|
|
|
|
|
verify(alarmCommentService, times(1)) |
|
|
|
.saveAlarmComment(eq(alarm), eq(expectedAlarmComment), eq(user)); |
|
|
|
} |
|
|
|
|
|
|
|
@Test |
|
|
|
public void testUnassignDeletedUserAlarms() throws ThingsboardException { |
|
|
|
AlarmInfo alarm = new AlarmInfo(); |
|
|
|
alarm.setId(new AlarmId(UUID.randomUUID())); |
|
|
|
|
|
|
|
when(alarmService.findAlarmIdsByAssigneeId(any(), any(), any())) |
|
|
|
.thenReturn(new PageData<>(List.of(alarm.getId()), 0, 1, false)) |
|
|
|
.thenReturn(new PageData<>(Collections.EMPTY_LIST, 0, 0, false)); |
|
|
|
when(alarmSubscriptionService.unassignAlarm(any(), any(), anyLong())) |
|
|
|
.thenReturn(AlarmApiCallResult.builder().successful(true).modified(true).alarm(alarm).build()); |
|
|
|
|
|
|
|
User user = new User(); |
|
|
|
user.setEmail("testEmail@gmail.com"); |
|
|
|
user.setId(new UserId(UUID.randomUUID())); |
|
|
|
service.unassignDeletedUserAlarms(new TenantId(UUID.randomUUID()), user, System.currentTimeMillis()); |
|
|
|
|
|
|
|
ObjectNode commentNode = JacksonUtil.newObjectNode(); |
|
|
|
commentNode.put("subtype", "ASSIGN"); |
|
|
|
commentNode.put("text", String.format("Alarm was unassigned because user %s - was deleted", user.getTitle())); |
|
|
|
AlarmComment expectedAlarmComment = AlarmComment.builder() |
|
|
|
.alarmId(alarm.getId()) |
|
|
|
.type(AlarmCommentType.SYSTEM) |
|
|
|
.comment(commentNode) |
|
|
|
.build(); |
|
|
|
|
|
|
|
verify(alarmCommentService, times(1)) |
|
|
|
.saveAlarmComment(eq(alarm), eq(expectedAlarmComment), eq(null)); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
} |
|
|
|
|