moved logic to DefaultTbAlarmCommentService

This commit is contained in:
dashevchenko 2023-03-21 13:29:46 +02:00
parent ce7d56332b
commit b6adbd6ec8
7 changed files with 46 additions and 35 deletions

View File

@ -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 { 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); checkParameter(ALARM_ID, strAlarmId);
AlarmId alarmId = new AlarmId(toUUID(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)); AlarmCommentId alarmCommentId = new AlarmCommentId(toUUID(strCommentId));
AlarmComment alarmComment = checkAlarmCommentId(alarmCommentId, alarmId); AlarmComment alarmComment = checkAlarmCommentId(alarmCommentId, alarmId);
@ -117,10 +117,7 @@ public class AlarmCommentController extends BaseController {
) throws Exception { ) throws Exception {
checkParameter(ALARM_ID, strAlarmId); checkParameter(ALARM_ID, strAlarmId);
AlarmId alarmId = new AlarmId(toUUID(strAlarmId)); AlarmId alarmId = new AlarmId(toUUID(strAlarmId));
Alarm alarm = alarmService.findAlarmByIdAsync(getCurrentUser().getTenantId(), alarmId).get(); Alarm alarm = checkAlarmId(alarmId, Operation.READ);
checkNotNull(alarm, "Alarm with id [" + alarmId + "] is not found");
checkEntityId(alarm.getOriginator(), Operation.READ);
PageLink pageLink = createPageLink(pageSize, page, null, sortProperty, sortOrder); PageLink pageLink = createPageLink(pageSize, page, null, sortProperty, sortOrder);
return checkNotNull(alarmCommentService.findAlarmComments(alarm.getTenantId(), alarmId, pageLink)); return checkNotNull(alarmCommentService.findAlarmComments(alarm.getTenantId(), alarmId, pageLink));
} }

View File

@ -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.AlarmComment;
import org.thingsboard.server.common.data.alarm.AlarmCommentType; import org.thingsboard.server.common.data.alarm.AlarmCommentType;
import org.thingsboard.server.common.data.audit.ActionType; 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.exception.ThingsboardException;
import org.thingsboard.server.common.data.id.UserId; import org.thingsboard.server.common.data.id.UserId;
import org.thingsboard.server.service.entitiy.AbstractTbEntityService; import org.thingsboard.server.service.entitiy.AbstractTbEntityService;
@ -47,8 +48,17 @@ public class DefaultTbAlarmCommentService extends AbstractTbEntityService implem
} }
@Override @Override
public void deleteAlarmComment(Alarm alarm, AlarmComment alarmComment, User user) { public void deleteAlarmComment(Alarm alarm, AlarmComment alarmComment, User user) throws ThingsboardException {
alarmCommentService.deleteAlarmComment(alarm.getTenantId(), alarmComment, user); if (alarmComment.getType() == AlarmCommentType.OTHER) {
notificationEntityService.notifyAlarmComment(alarm, alarmComment, ActionType.DELETED_COMMENT, user); 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);
}
} }
} }

View File

@ -23,5 +23,5 @@ import org.thingsboard.server.common.data.exception.ThingsboardException;
public interface TbAlarmCommentService { public interface TbAlarmCommentService {
AlarmComment saveAlarmComment(Alarm alarm, AlarmComment alarmComment, User user) throws ThingsboardException; 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;
} }

View File

@ -28,9 +28,11 @@ import org.thingsboard.server.cluster.TbClusterService;
import org.thingsboard.server.common.data.User; import org.thingsboard.server.common.data.User;
import org.thingsboard.server.common.data.alarm.Alarm; import org.thingsboard.server.common.data.alarm.Alarm;
import org.thingsboard.server.common.data.alarm.AlarmComment; 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.exception.ThingsboardException;
import org.thingsboard.server.common.data.id.AlarmCommentId; import org.thingsboard.server.common.data.id.AlarmCommentId;
import org.thingsboard.server.common.data.id.AlarmId; 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.AlarmCommentService;
import org.thingsboard.server.dao.alarm.AlarmService; import org.thingsboard.server.dao.alarm.AlarmService;
import org.thingsboard.server.dao.customer.CustomerService; import org.thingsboard.server.dao.customer.CustomerService;
@ -41,6 +43,7 @@ import org.thingsboard.server.service.telemetry.AlarmSubscriptionService;
import java.util.UUID; import java.util.UUID;
import static org.assertj.core.api.Assertions.assertThatThrownBy;
import static org.mockito.ArgumentMatchers.any; import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.eq; import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doNothing; import static org.mockito.Mockito.doNothing;
@ -84,14 +87,28 @@ public class DefaultTbAlarmCommentServiceTest {
} }
@Test @Test
public void testDelete() { public void testDelete() throws ThingsboardException {
var alarmId = new AlarmId(UUID.randomUUID()); var alarmId = new AlarmId(UUID.randomUUID());
var alarmComment = new AlarmComment(); var alarmComment = new AlarmComment();
alarmComment.setAlarmId(alarmId); alarmComment.setAlarmId(alarmId);
alarmComment.setUserId(new UserId(UUID.randomUUID()));
alarmComment.setType(AlarmCommentType.OTHER);
doNothing().when(alarmCommentService).deleteAlarmComment(Mockito.any(), eq(alarmComment), Mockito.any()); when(alarmCommentService.saveAlarmComment(Mockito.any(), eq(alarmComment))).thenReturn(alarmComment);
service.deleteAlarmComment(new Alarm(alarmId), new AlarmComment(), new User()); service.deleteAlarmComment(new Alarm(alarmId), alarmComment, new User());
verify(notificationEntityService, times(1)).notifyAlarmComment(any(), any(), any(), any()); 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");
}
} }

View File

@ -28,7 +28,7 @@ import org.thingsboard.server.common.data.page.PageLink;
public interface AlarmCommentService { public interface AlarmCommentService {
AlarmComment createOrUpdateAlarmComment(TenantId tenantId, AlarmComment alarmComment); 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); PageData<AlarmCommentInfo> findAlarmComments(TenantId tenantId, AlarmId alarmId, PageLink pageLink);

View File

@ -43,7 +43,7 @@ import static org.thingsboard.server.dao.service.Validator.validateId;
@Service @Service
@Slf4j @Slf4j
public class BaseAlarmCommentService extends AbstractEntityService implements AlarmCommentService{ public class BaseAlarmCommentService extends AbstractEntityService implements AlarmCommentService {
@Autowired @Autowired
private AlarmCommentDao alarmCommentDao; private AlarmCommentDao alarmCommentDao;
@ -62,20 +62,10 @@ public class BaseAlarmCommentService extends AbstractEntityService implements Al
} }
@Override @Override
public void deleteAlarmComment(TenantId tenantId, AlarmComment alarmComment, User user) { public AlarmComment saveAlarmComment(TenantId tenantId, AlarmComment alarmComment) {
log.debug("Deleting Alarm Comment: {}", alarmComment); log.debug("Deleting Alarm Comment: {}", alarmComment);
alarmCommentDataValidator.validate(alarmComment, c -> tenantId);
if (alarmComment.getType() == AlarmCommentType.OTHER) { return alarmCommentDao.save(tenantId, alarmComment);
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());
}
} }
@Override @Override

View File

@ -80,7 +80,7 @@ public abstract class BaseAlarmCommentServiceTest extends AbstractServiceTest {
@Test @Test
public void testSaveAndFetchAlarmComment() throws ExecutionException, InterruptedException { public void testCreateAndFetchAlarmComment() throws ExecutionException, InterruptedException {
AlarmComment alarmComment = AlarmComment.builder().alarmId(alarm.getId()) AlarmComment alarmComment = AlarmComment.builder().alarmId(alarm.getId())
.userId(user.getId()) .userId(user.getId())
.type(OTHER) .type(OTHER)
@ -143,7 +143,7 @@ public abstract class BaseAlarmCommentServiceTest extends AbstractServiceTest {
} }
@Test @Test
public void testDeleteAlarmComment() throws ExecutionException, InterruptedException { public void testSaveAlarmComment() throws ExecutionException, InterruptedException {
UserId userId = new UserId(UUID.randomUUID()); UserId userId = new UserId(UUID.randomUUID());
AlarmComment alarmComment = AlarmComment.builder().alarmId(alarm.getId()) AlarmComment alarmComment = AlarmComment.builder().alarmId(alarm.getId())
.userId(userId) .userId(userId)
@ -153,15 +153,12 @@ public abstract class BaseAlarmCommentServiceTest extends AbstractServiceTest {
AlarmComment createdComment = alarmCommentService.createOrUpdateAlarmComment(tenantId, alarmComment); AlarmComment createdComment = alarmCommentService.createOrUpdateAlarmComment(tenantId, alarmComment);
Assert.assertNotNull(createdComment); createdComment.setType(AlarmCommentType.SYSTEM);
Assert.assertNotNull(createdComment.getId()); createdComment.setUserId(null);
alarmCommentService.saveAlarmComment(tenantId, createdComment);
alarmCommentService.deleteAlarmComment(tenantId, createdComment, user);
AlarmComment fetched = alarmCommentService.findAlarmCommentByIdAsync(tenantId, createdComment.getId()).get(); AlarmComment fetched = alarmCommentService.findAlarmCommentByIdAsync(tenantId, createdComment.getId()).get();
Assert.assertNull(fetched.getUserId()); Assert.assertNull(fetched.getUserId());
Assert.assertEquals(AlarmCommentType.SYSTEM, fetched.getType()); 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()));
} }
} }