moved logic to DefaultTbAlarmCommentService
This commit is contained in:
parent
ce7d56332b
commit
b6adbd6ec8
@ -90,7 +90,7 @@ public class AlarmCommentController extends BaseController {
|
||||
public void deleteAlarmComment(@ApiParam(value = ALARM_ID_PARAM_DESCRIPTION) @PathVariable(ALARM_ID) String strAlarmId, @ApiParam(value = ALARM_COMMENT_ID_PARAM_DESCRIPTION) @PathVariable(ALARM_COMMENT_ID) String strCommentId) throws ThingsboardException {
|
||||
checkParameter(ALARM_ID, strAlarmId);
|
||||
AlarmId alarmId = new AlarmId(toUUID(strAlarmId));
|
||||
Alarm alarm = checkAlarmId(alarmId, Operation.DELETE);
|
||||
Alarm alarm = checkAlarmId(alarmId, Operation.WRITE);
|
||||
|
||||
AlarmCommentId alarmCommentId = new AlarmCommentId(toUUID(strCommentId));
|
||||
AlarmComment alarmComment = checkAlarmCommentId(alarmCommentId, alarmId);
|
||||
@ -117,10 +117,7 @@ public class AlarmCommentController extends BaseController {
|
||||
) throws Exception {
|
||||
checkParameter(ALARM_ID, strAlarmId);
|
||||
AlarmId alarmId = new AlarmId(toUUID(strAlarmId));
|
||||
Alarm alarm = alarmService.findAlarmByIdAsync(getCurrentUser().getTenantId(), alarmId).get();
|
||||
checkNotNull(alarm, "Alarm with id [" + alarmId + "] is not found");
|
||||
checkEntityId(alarm.getOriginator(), Operation.READ);
|
||||
|
||||
Alarm alarm = checkAlarmId(alarmId, Operation.READ);
|
||||
PageLink pageLink = createPageLink(pageSize, page, null, sortProperty, sortOrder);
|
||||
return checkNotNull(alarmCommentService.findAlarmComments(alarm.getTenantId(), alarmId, pageLink));
|
||||
}
|
||||
|
||||
@ -24,6 +24,7 @@ import org.thingsboard.server.common.data.alarm.Alarm;
|
||||
import org.thingsboard.server.common.data.alarm.AlarmComment;
|
||||
import org.thingsboard.server.common.data.alarm.AlarmCommentType;
|
||||
import org.thingsboard.server.common.data.audit.ActionType;
|
||||
import org.thingsboard.server.common.data.exception.ThingsboardErrorCode;
|
||||
import org.thingsboard.server.common.data.exception.ThingsboardException;
|
||||
import org.thingsboard.server.common.data.id.UserId;
|
||||
import org.thingsboard.server.service.entitiy.AbstractTbEntityService;
|
||||
@ -47,8 +48,17 @@ public class DefaultTbAlarmCommentService extends AbstractTbEntityService implem
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteAlarmComment(Alarm alarm, AlarmComment alarmComment, User user) {
|
||||
alarmCommentService.deleteAlarmComment(alarm.getTenantId(), alarmComment, user);
|
||||
notificationEntityService.notifyAlarmComment(alarm, alarmComment, ActionType.DELETED_COMMENT, user);
|
||||
public void deleteAlarmComment(Alarm alarm, AlarmComment alarmComment, User user) throws ThingsboardException {
|
||||
if (alarmComment.getType() == AlarmCommentType.OTHER) {
|
||||
alarmComment.setType(AlarmCommentType.SYSTEM);
|
||||
alarmComment.setUserId(null);
|
||||
alarmComment.setComment(JacksonUtil.newObjectNode().put("text",
|
||||
String.format("User %s deleted his comment",
|
||||
(user.getFirstName() == null || user.getLastName() == null) ? user.getName() : user.getFirstName() + " " + user.getLastName())));
|
||||
AlarmComment savedAlarmComment = checkNotNull(alarmCommentService.saveAlarmComment(alarm.getTenantId(), alarmComment));
|
||||
notificationEntityService.notifyAlarmComment(alarm, savedAlarmComment, ActionType.DELETED_COMMENT, user);
|
||||
} else {
|
||||
throw new ThingsboardException("System comment could not be deleted", ThingsboardErrorCode.BAD_REQUEST_PARAMS);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -23,5 +23,5 @@ import org.thingsboard.server.common.data.exception.ThingsboardException;
|
||||
public interface TbAlarmCommentService {
|
||||
AlarmComment saveAlarmComment(Alarm alarm, AlarmComment alarmComment, User user) throws ThingsboardException;
|
||||
|
||||
void deleteAlarmComment(Alarm alarm, AlarmComment alarmComment, User user);
|
||||
void deleteAlarmComment(Alarm alarm, AlarmComment alarmComment, User user) throws ThingsboardException;
|
||||
}
|
||||
|
||||
@ -28,9 +28,11 @@ 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.AlarmComment;
|
||||
import org.thingsboard.server.common.data.alarm.AlarmCommentType;
|
||||
import org.thingsboard.server.common.data.exception.ThingsboardException;
|
||||
import org.thingsboard.server.common.data.id.AlarmCommentId;
|
||||
import org.thingsboard.server.common.data.id.AlarmId;
|
||||
import org.thingsboard.server.common.data.id.UserId;
|
||||
import org.thingsboard.server.dao.alarm.AlarmCommentService;
|
||||
import org.thingsboard.server.dao.alarm.AlarmService;
|
||||
import org.thingsboard.server.dao.customer.CustomerService;
|
||||
@ -41,6 +43,7 @@ import org.thingsboard.server.service.telemetry.AlarmSubscriptionService;
|
||||
|
||||
import java.util.UUID;
|
||||
|
||||
import static org.assertj.core.api.Assertions.assertThatThrownBy;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.doNothing;
|
||||
@ -84,14 +87,28 @@ public class DefaultTbAlarmCommentServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDelete() {
|
||||
public void testDelete() throws ThingsboardException {
|
||||
var alarmId = new AlarmId(UUID.randomUUID());
|
||||
var alarmComment = new AlarmComment();
|
||||
alarmComment.setAlarmId(alarmId);
|
||||
alarmComment.setUserId(new UserId(UUID.randomUUID()));
|
||||
alarmComment.setType(AlarmCommentType.OTHER);
|
||||
|
||||
doNothing().when(alarmCommentService).deleteAlarmComment(Mockito.any(), eq(alarmComment), Mockito.any());
|
||||
service.deleteAlarmComment(new Alarm(alarmId), new AlarmComment(), new User());
|
||||
when(alarmCommentService.saveAlarmComment(Mockito.any(), eq(alarmComment))).thenReturn(alarmComment);
|
||||
service.deleteAlarmComment(new Alarm(alarmId), alarmComment, new User());
|
||||
|
||||
verify(notificationEntityService, times(1)).notifyAlarmComment(any(), any(), any(), any());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testShouldNotDeleteSystemComment() {
|
||||
var alarmId = new AlarmId(UUID.randomUUID());
|
||||
var alarmComment = new AlarmComment();
|
||||
alarmComment.setAlarmId(alarmId);
|
||||
alarmComment.setType(AlarmCommentType.SYSTEM);
|
||||
|
||||
assertThatThrownBy(() -> service.deleteAlarmComment(new Alarm(alarmId), alarmComment, new User()))
|
||||
.isInstanceOf(ThingsboardException.class)
|
||||
.hasMessageContaining("System comment could not be deleted");
|
||||
}
|
||||
}
|
||||
@ -28,7 +28,7 @@ import org.thingsboard.server.common.data.page.PageLink;
|
||||
public interface AlarmCommentService {
|
||||
AlarmComment createOrUpdateAlarmComment(TenantId tenantId, AlarmComment alarmComment);
|
||||
|
||||
void deleteAlarmComment(TenantId tenantId, AlarmComment alarmComment, User user);
|
||||
AlarmComment saveAlarmComment(TenantId tenantId, AlarmComment alarmComment);
|
||||
|
||||
PageData<AlarmCommentInfo> findAlarmComments(TenantId tenantId, AlarmId alarmId, PageLink pageLink);
|
||||
|
||||
|
||||
@ -62,20 +62,10 @@ public class BaseAlarmCommentService extends AbstractEntityService implements Al
|
||||
}
|
||||
|
||||
@Override
|
||||
public void deleteAlarmComment(TenantId tenantId, AlarmComment alarmComment, User user) {
|
||||
public AlarmComment saveAlarmComment(TenantId tenantId, AlarmComment alarmComment) {
|
||||
log.debug("Deleting Alarm Comment: {}", alarmComment);
|
||||
|
||||
if (alarmComment.getType() == AlarmCommentType.OTHER) {
|
||||
alarmComment.setType(AlarmCommentType.SYSTEM);
|
||||
alarmComment.setUserId(null);
|
||||
alarmComment.setComment(JacksonUtil.newObjectNode().put("text",
|
||||
String.format("User %s deleted his comment",
|
||||
(user.getFirstName() == null || user.getLastName() == null) ? user.getName() : user.getFirstName() + " " + user.getLastName())));
|
||||
alarmCommentDao.save(tenantId, alarmComment);
|
||||
}
|
||||
else {
|
||||
alarmCommentDao.deleteAlarmComment(tenantId, alarmComment.getId());
|
||||
}
|
||||
alarmCommentDataValidator.validate(alarmComment, c -> tenantId);
|
||||
return alarmCommentDao.save(tenantId, alarmComment);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
||||
@ -80,7 +80,7 @@ public abstract class BaseAlarmCommentServiceTest extends AbstractServiceTest {
|
||||
|
||||
|
||||
@Test
|
||||
public void testSaveAndFetchAlarmComment() throws ExecutionException, InterruptedException {
|
||||
public void testCreateAndFetchAlarmComment() throws ExecutionException, InterruptedException {
|
||||
AlarmComment alarmComment = AlarmComment.builder().alarmId(alarm.getId())
|
||||
.userId(user.getId())
|
||||
.type(OTHER)
|
||||
@ -143,7 +143,7 @@ public abstract class BaseAlarmCommentServiceTest extends AbstractServiceTest {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testDeleteAlarmComment() throws ExecutionException, InterruptedException {
|
||||
public void testSaveAlarmComment() throws ExecutionException, InterruptedException {
|
||||
UserId userId = new UserId(UUID.randomUUID());
|
||||
AlarmComment alarmComment = AlarmComment.builder().alarmId(alarm.getId())
|
||||
.userId(userId)
|
||||
@ -153,15 +153,12 @@ public abstract class BaseAlarmCommentServiceTest extends AbstractServiceTest {
|
||||
|
||||
AlarmComment createdComment = alarmCommentService.createOrUpdateAlarmComment(tenantId, alarmComment);
|
||||
|
||||
Assert.assertNotNull(createdComment);
|
||||
Assert.assertNotNull(createdComment.getId());
|
||||
|
||||
alarmCommentService.deleteAlarmComment(tenantId, createdComment, user);
|
||||
createdComment.setType(AlarmCommentType.SYSTEM);
|
||||
createdComment.setUserId(null);
|
||||
alarmCommentService.saveAlarmComment(tenantId, createdComment);
|
||||
|
||||
AlarmComment fetched = alarmCommentService.findAlarmCommentByIdAsync(tenantId, createdComment.getId()).get();
|
||||
Assert.assertNull(fetched.getUserId());
|
||||
Assert.assertEquals(AlarmCommentType.SYSTEM, fetched.getType());
|
||||
Assert.assertEquals(fetched.getComment().get("text").asText(), String.format("User %s deleted his comment",
|
||||
(user.getFirstName() == null || user.getLastName() == null) ? user.getName() : user.getFirstName() + " " + user.getLastName()));
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user