refactoring: alarm - test start

This commit is contained in:
nickAS21 2022-06-12 20:28:12 +03:00
parent 5fef80a990
commit 0cd24bb16e
9 changed files with 269 additions and 44 deletions

View File

@ -147,7 +147,7 @@ public class AlarmController extends BaseController {
try { try {
AlarmId alarmId = new AlarmId(toUUID(strAlarmId)); AlarmId alarmId = new AlarmId(toUUID(strAlarmId));
Alarm alarm = checkAlarmId(alarmId, Operation.WRITE); Alarm alarm = checkAlarmId(alarmId, Operation.WRITE);
return tbAlarmService.delete(alarm, getCurrentUser()); return tbAlarmService.delete(alarm, getCurrentUser().getCustomerId(), getCurrentUser());
} catch (Exception e) { } catch (Exception e) {
throw handleException(e); throw handleException(e);
} }

View File

@ -52,6 +52,7 @@ import org.thingsboard.server.service.gateway_device.GatewayNotificationsService
import org.thingsboard.server.service.security.model.SecurityUser; import org.thingsboard.server.service.security.model.SecurityUser;
import java.util.List; import java.util.List;
import java.util.Locale;
@Slf4j @Slf4j
@Service @Service
@ -335,28 +336,7 @@ public class DefaultTbNotificationEntityService implements TbNotificationEntityS
return null; return null;
} }
private EdgeEventActionType edgeTypeByActionType(ActionType actionType) { public static EdgeEventActionType edgeTypeByActionType(ActionType actionType) {
switch (actionType) { return EdgeEventActionType.valueOf(actionType.toString().toUpperCase(Locale.ENGLISH));
case ADDED:
return EdgeEventActionType.ADDED;
case UPDATED:
return EdgeEventActionType.UPDATED;
case ALARM_ACK:
return EdgeEventActionType.ALARM_ACK;
case ALARM_CLEAR:
return EdgeEventActionType.ALARM_CLEAR;
case DELETED:
return EdgeEventActionType.DELETED;
case RELATION_ADD_OR_UPDATE:
return EdgeEventActionType.RELATION_ADD_OR_UPDATE;
case RELATION_DELETED:
return EdgeEventActionType.RELATION_DELETED;
case ASSIGNED_TO_EDGE:
return EdgeEventActionType.ASSIGNED_TO_EDGE;
case UNASSIGNED_FROM_EDGE:
return EdgeEventActionType.UNASSIGNED_FROM_EDGE;
default:
return null;
}
} }
} }

View File

@ -23,6 +23,7 @@ import org.thingsboard.server.common.data.alarm.Alarm;
import org.thingsboard.server.common.data.alarm.AlarmStatus; import org.thingsboard.server.common.data.alarm.AlarmStatus;
import org.thingsboard.server.common.data.audit.ActionType; import org.thingsboard.server.common.data.audit.ActionType;
import org.thingsboard.server.common.data.exception.ThingsboardException; import org.thingsboard.server.common.data.exception.ThingsboardException;
import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.EdgeId; import org.thingsboard.server.common.data.id.EdgeId;
import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.queue.util.TbCoreComponent; import org.thingsboard.server.queue.util.TbCoreComponent;
@ -77,12 +78,13 @@ public class DefaultTbAlarmService extends AbstractTbEntityService implements Tb
} }
@Override @Override
public Boolean delete(Alarm alarm, SecurityUser user) throws ThingsboardException { public Boolean delete(Alarm alarm, CustomerId customerId, SecurityUser user) throws ThingsboardException {
TenantId tenantId = alarm.getTenantId();
try { try {
List<EdgeId> relatedEdgeIds = findRelatedEdgeIds(user.getTenantId(), alarm.getOriginator()); List<EdgeId> relatedEdgeIds = findRelatedEdgeIds(tenantId, alarm.getOriginator());
notificationEntityService.notifyDeleteAlarm(user.getTenantId(), alarm, alarm.getOriginator(), user.getCustomerId(), notificationEntityService.notifyDeleteAlarm(tenantId, alarm, alarm.getOriginator(), customerId,
relatedEdgeIds, user, JacksonUtil.OBJECT_MAPPER.writeValueAsString(alarm)); relatedEdgeIds, user, JacksonUtil.OBJECT_MAPPER.writeValueAsString(alarm));
return alarmService.deleteAlarm(user.getTenantId(), alarm.getId()).isSuccessful(); return alarmService.deleteAlarm(tenantId, alarm.getId()).isSuccessful();
} catch (Exception e) { } catch (Exception e) {
throw handleException(e); throw handleException(e);
} }

View File

@ -17,6 +17,7 @@ package org.thingsboard.server.service.entitiy.alarm;
import org.thingsboard.server.common.data.alarm.Alarm; import org.thingsboard.server.common.data.alarm.Alarm;
import org.thingsboard.server.common.data.exception.ThingsboardException; import org.thingsboard.server.common.data.exception.ThingsboardException;
import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.service.security.model.SecurityUser; import org.thingsboard.server.service.security.model.SecurityUser;
public interface TbAlarmService { public interface TbAlarmService {
@ -27,5 +28,5 @@ public interface TbAlarmService {
void clear(Alarm alarm, SecurityUser user) throws ThingsboardException; void clear(Alarm alarm, SecurityUser user) throws ThingsboardException;
Boolean delete(Alarm alarm, SecurityUser user) throws ThingsboardException; Boolean delete(Alarm alarm, CustomerId customerId, SecurityUser user) throws ThingsboardException;
} }

View File

@ -45,7 +45,7 @@ import static org.assertj.core.api.Assertions.assertThat;
@EnableWebSocket @EnableWebSocket
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT) @SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
@Slf4j @Slf4j
public abstract class AbstractControllerTest extends AbstractWebTest { public abstract class AbstractControllerTest extends AbstractNotifyEntityTest {
public static final String WS_URL = "ws://localhost:"; public static final String WS_URL = "ws://localhost:";

View File

@ -0,0 +1,132 @@
/**
* Copyright © 2016-2022 The Thingsboard Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.thingsboard.server.controller;
import org.mockito.Mockito;
import org.springframework.boot.test.mock.mockito.SpyBean;
import org.thingsboard.server.cluster.TbClusterService;
import org.thingsboard.server.common.data.EntityType;
import org.thingsboard.server.common.data.HasName;
import org.thingsboard.server.common.data.audit.ActionType;
import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.id.EntityIdFactory;
import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.id.UserId;
import org.thingsboard.server.dao.audit.AuditLogService;
import org.thingsboard.server.dao.model.ModelConstants;
import java.util.Locale;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
import static org.thingsboard.server.service.entitiy.DefaultTbNotificationEntityService.edgeTypeByActionType;
public abstract class AbstractNotifyEntityTest extends AbstractWebTest {
@SpyBean
protected TbClusterService tbClusterService;
@SpyBean
protected AuditLogService auditLogService;
protected void testNotifyEntityOk(HasName entity, EntityId entityId, EntityId originatorId,
TenantId tenantId, CustomerId customerId, UserId userId, String userName,
ActionType actionType) {
testSendNotificationMsgToEdgeServiceOk(entityId, tenantId, actionType);
testLogEntityActionOk(entity, originatorId, tenantId, customerId, userId, userName, actionType);
testPushMsgToRuleEngineOk(entity, originatorId, tenantId, customerId, userId, userName, actionType);
Mockito.reset(tbClusterService, auditLogService);
}
protected void testNotifyEntityDeleteOk(HasName entity, EntityId entityId, EntityId originatorId,
TenantId tenantId, CustomerId customerId, UserId userId, String userName,
ActionType actionType) {
Mockito.verify(tbClusterService, never()).sendNotificationMsgToEdgeService(Mockito.any(),
Mockito.any(), Mockito.any(entityId.getClass()), Mockito.any(), Mockito.any(), Mockito.any());
testLogEntityActionOk(entity, originatorId, tenantId, customerId, userId, userName, actionType);
testPushMsgToRuleEngineOk(entity, originatorId, tenantId, customerId, userId, userName, actionType);
}
private void testNotifyEntityError(EntityId entityId, HasName entity, TenantId tenantId,
UserId userId, String userName, ActionType actionType, Exception exp,
Object... additionalInfo) {
CustomerId customer_NULL_UUID = (CustomerId) EntityIdFactory.getByTypeAndUuid(EntityType.CUSTOMER, ModelConstants.NULL_UUID);
EntityId entity_NULL_UUID = EntityIdFactory.getByTypeAndUuid(EntityType.valueOf(entity.getClass().toString().substring(entity.getClass().toString().lastIndexOf(".") + 1).toUpperCase(Locale.ENGLISH)),
ModelConstants.NULL_UUID);
testNotificationMsgToEdgeServiceNever(entityId);
if (additionalInfo.length > 0) {
Mockito.verify(auditLogService, times(1)).logEntityAction(Mockito.eq(tenantId),
Mockito.eq(customer_NULL_UUID), Mockito.eq(userId), Mockito.eq(userName),
Mockito.eq(entity_NULL_UUID), Mockito.any(entity.getClass()), Mockito.eq(actionType),
Mockito.any(exp.getClass()), Mockito.eq(additionalInfo));
} else {
Mockito.verify(auditLogService, times(1)).logEntityAction(Mockito.eq(tenantId),
Mockito.eq(customer_NULL_UUID), Mockito.eq(userId), Mockito.eq(userName),
Mockito.eq(entity_NULL_UUID), Mockito.any(entity.getClass()), Mockito.eq(actionType),
Mockito.any(exp.getClass()), Mockito.isNull());
}
Mockito.verify(tbClusterService, never()).pushMsgToRuleEngine(Mockito.any(), Mockito.any(entityId.getClass()),
Mockito.any(), Mockito.any());
Mockito.reset(tbClusterService, auditLogService);
}
protected void testNotifyEntityNever(EntityId entityId, HasName entity) {
testNotificationMsgToEdgeServiceNever(entityId);
testLogEntityActionNever(entityId, entity);
testPushMsgToRuleEngineNever(entityId);
}
protected void testNotificationMsgToEdgeServiceNever(EntityId entityId) {
Mockito.verify(tbClusterService, never()).sendNotificationMsgToEdgeService(Mockito.any(),
Mockito.any(), Mockito.any(entityId.getClass()), Mockito.any(), Mockito.any(), Mockito.any());
}
protected void testLogEntityActionNever(EntityId entityId, HasName entity) {
Mockito.verify(auditLogService, never()).logEntityAction(Mockito.any(), Mockito.any(),
Mockito.any(), Mockito.any(), Mockito.any(entityId.getClass()), Mockito.any(entity.getClass()),
Mockito.any(), Mockito.any());
}
protected void testPushMsgToRuleEngineNever(EntityId entityId) {
Mockito.verify(tbClusterService, never()).pushMsgToRuleEngine(Mockito.any(),
Mockito.any(entityId.getClass()), Mockito.any(), Mockito.any());
}
private void testLogEntityActionOk(HasName entity, EntityId originatorId,
TenantId tenantId, CustomerId customerId, UserId userId, String userName,
ActionType actionType) {
Mockito.verify(auditLogService, times(1)).logEntityAction(Mockito.eq(tenantId), Mockito.eq(customerId),
Mockito.eq(userId), Mockito.eq(userName), Mockito.eq(originatorId),
Mockito.eq(entity), Mockito.eq(actionType), Mockito.isNull());
}
private void testPushMsgToRuleEngineOk(HasName entity, EntityId originatorId,
TenantId tenantId, CustomerId customerId, UserId userId, String userName,
ActionType actionType) {
Mockito.verify(auditLogService, times(1)).logEntityAction(Mockito.eq(tenantId), Mockito.eq(customerId),
Mockito.eq(userId), Mockito.eq(userName), Mockito.eq(originatorId),
Mockito.eq(entity), Mockito.eq(actionType), Mockito.isNull());
}
private void testSendNotificationMsgToEdgeServiceOk(EntityId entityId, TenantId tenantId, ActionType actionType) {
Mockito.verify(tbClusterService, times(1)).sendNotificationMsgToEdgeService(Mockito.eq(tenantId),
Mockito.isNull(), Mockito.eq(entityId), Mockito.isNull(), Mockito.isNull(),
Mockito.eq(edgeTypeByActionType(actionType)));
}
}

View File

@ -75,11 +75,6 @@ import org.thingsboard.server.common.data.id.UserId;
import org.thingsboard.server.common.data.page.PageData; import org.thingsboard.server.common.data.page.PageData;
import org.thingsboard.server.common.data.page.PageLink; import org.thingsboard.server.common.data.page.PageLink;
import org.thingsboard.server.common.data.page.TimePageLink; import org.thingsboard.server.common.data.page.TimePageLink;
import org.thingsboard.server.common.data.queue.ProcessingStrategy;
import org.thingsboard.server.common.data.queue.ProcessingStrategyType;
import org.thingsboard.server.common.data.queue.Queue;
import org.thingsboard.server.common.data.queue.SubmitStrategy;
import org.thingsboard.server.common.data.queue.SubmitStrategyType;
import org.thingsboard.server.common.data.security.Authority; import org.thingsboard.server.common.data.security.Authority;
import org.thingsboard.server.config.ThingsboardSecurityConfiguration; import org.thingsboard.server.config.ThingsboardSecurityConfiguration;
import org.thingsboard.server.dao.tenant.TenantProfileService; import org.thingsboard.server.dao.tenant.TenantProfileService;
@ -137,7 +132,9 @@ public abstract class AbstractWebTest extends AbstractInMemoryStorageTest {
protected TenantId tenantId; protected TenantId tenantId;
protected UserId tenantAdminUserId; protected UserId tenantAdminUserId;
protected CustomerId tenantAdminCustomerId;
protected CustomerId customerId; protected CustomerId customerId;
protected UserId customerUserId;
@SuppressWarnings("rawtypes") @SuppressWarnings("rawtypes")
private HttpMessageConverter mappingJackson2HttpMessageConverter; private HttpMessageConverter mappingJackson2HttpMessageConverter;
@ -202,6 +199,7 @@ public abstract class AbstractWebTest extends AbstractInMemoryStorageTest {
tenantAdmin = createUserAndLogin(tenantAdmin, TENANT_ADMIN_PASSWORD); tenantAdmin = createUserAndLogin(tenantAdmin, TENANT_ADMIN_PASSWORD);
tenantAdminUserId = tenantAdmin.getId(); tenantAdminUserId = tenantAdmin.getId();
tenantAdminCustomerId = tenantAdmin.getCustomerId();
Customer customer = new Customer(); Customer customer = new Customer();
customer.setTitle("Customer"); customer.setTitle("Customer");
@ -215,7 +213,8 @@ public abstract class AbstractWebTest extends AbstractInMemoryStorageTest {
customerUser.setCustomerId(savedCustomer.getId()); customerUser.setCustomerId(savedCustomer.getId());
customerUser.setEmail(CUSTOMER_USER_EMAIL); customerUser.setEmail(CUSTOMER_USER_EMAIL);
createUserAndLogin(customerUser, CUSTOMER_USER_PASSWORD); customerUser = createUserAndLogin(customerUser, CUSTOMER_USER_PASSWORD);
customerUserId = customerUser.getId();
logout(); logout();

View File

@ -15,17 +15,21 @@
*/ */
package org.thingsboard.server.controller; package org.thingsboard.server.controller;
import lombok.extern.slf4j.Slf4j;
import org.junit.After; import org.junit.After;
import org.junit.Assert; import org.junit.Assert;
import org.junit.Before; import org.junit.Before;
import org.junit.Test; import org.junit.Test;
import org.mockito.Mockito;
import org.thingsboard.server.common.data.Device; import org.thingsboard.server.common.data.Device;
import org.thingsboard.server.common.data.alarm.Alarm; import org.thingsboard.server.common.data.alarm.Alarm;
import org.thingsboard.server.common.data.alarm.AlarmSeverity; import org.thingsboard.server.common.data.alarm.AlarmSeverity;
import org.thingsboard.server.common.data.alarm.AlarmStatus; import org.thingsboard.server.common.data.alarm.AlarmStatus;
import org.thingsboard.server.common.data.audit.ActionType;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status; import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
@Slf4j
public abstract class BaseAlarmControllerTest extends AbstractControllerTest { public abstract class BaseAlarmControllerTest extends AbstractControllerTest {
public static final String TEST_ALARM_TYPE = "Test"; public static final String TEST_ALARM_TYPE = "Test";
@ -56,36 +60,68 @@ public abstract class BaseAlarmControllerTest extends AbstractControllerTest {
@Test @Test
public void testCreateAlarmViaCustomer() throws Exception { public void testCreateAlarmViaCustomer() throws Exception {
loginCustomerUser(); loginCustomerUser();
createAlarm(TEST_ALARM_TYPE);
Mockito.reset(tbClusterService, auditLogService);
Alarm alarm = createAlarm(TEST_ALARM_TYPE);
testNotifyEntityOk(alarm, alarm.getId(), alarm.getOriginator(),
tenantId, customerId, customerUserId, CUSTOMER_USER_EMAIL, ActionType.ADDED);
logout(); logout();
} }
@Test @Test
public void testCreateAlarmViaTenant() throws Exception { public void testCreateAlarmViaTenant() throws Exception {
loginTenantAdmin(); loginTenantAdmin();
createAlarm(TEST_ALARM_TYPE);
Mockito.reset(tbClusterService, auditLogService);
Alarm alarm = createAlarm(TEST_ALARM_TYPE);
testNotifyEntityOk(alarm, alarm.getId(), alarm.getOriginator(),
tenantId, customerId, tenantAdminUserId, TENANT_ADMIN_EMAIL, ActionType.ADDED);
logout(); logout();
} }
@Test @Test
public void testUpdateAlarmViaCustomer() throws Exception { public void testUpdateAlarmViaCustomer() throws Exception {
loginCustomerUser(); loginCustomerUser();
Mockito.reset(tbClusterService, auditLogService);
Alarm alarm = createAlarm(TEST_ALARM_TYPE); Alarm alarm = createAlarm(TEST_ALARM_TYPE);
testNotifyEntityOk(alarm, alarm.getId(), alarm.getOriginator(),
tenantId, customerId, customerUserId, CUSTOMER_USER_EMAIL, ActionType.ADDED);
alarm.setSeverity(AlarmSeverity.MAJOR); alarm.setSeverity(AlarmSeverity.MAJOR);
Alarm updatedAlarm = doPost("/api/alarm", alarm, Alarm.class); Alarm updatedAlarm = doPost("/api/alarm", alarm, Alarm.class);
Assert.assertNotNull(updatedAlarm); Assert.assertNotNull(updatedAlarm);
Assert.assertEquals(AlarmSeverity.MAJOR, updatedAlarm.getSeverity()); Assert.assertEquals(AlarmSeverity.MAJOR, updatedAlarm.getSeverity());
testNotifyEntityOk(alarm, alarm.getId(), alarm.getOriginator(),
tenantId, customerId, customerUserId, CUSTOMER_USER_EMAIL, ActionType.UPDATED);
logout(); logout();
} }
@Test @Test
public void testUpdateAlarmViaTenant() throws Exception { public void testUpdateAlarmViaTenant() throws Exception {
loginTenantAdmin(); loginTenantAdmin();
Mockito.reset(tbClusterService, auditLogService);
Alarm alarm = createAlarm(TEST_ALARM_TYPE); Alarm alarm = createAlarm(TEST_ALARM_TYPE);
testNotifyEntityOk(alarm, alarm.getId(), alarm.getOriginator(),
tenantId, customerId, tenantAdminUserId, TENANT_ADMIN_EMAIL, ActionType.ADDED);
alarm.setSeverity(AlarmSeverity.MAJOR); alarm.setSeverity(AlarmSeverity.MAJOR);
Alarm updatedAlarm = doPost("/api/alarm", alarm, Alarm.class); Alarm updatedAlarm = doPost("/api/alarm", alarm, Alarm.class);
Assert.assertNotNull(updatedAlarm); Assert.assertNotNull(updatedAlarm);
Assert.assertEquals(AlarmSeverity.MAJOR, updatedAlarm.getSeverity()); Assert.assertEquals(AlarmSeverity.MAJOR, updatedAlarm.getSeverity());
testNotifyEntityOk(updatedAlarm, updatedAlarm.getId(), updatedAlarm.getOriginator(),
tenantId, customerId, tenantAdminUserId, TENANT_ADMIN_EMAIL, ActionType.UPDATED);
logout(); logout();
} }
@ -93,9 +129,15 @@ public abstract class BaseAlarmControllerTest extends AbstractControllerTest {
public void testUpdateAlarmViaDifferentTenant() throws Exception { public void testUpdateAlarmViaDifferentTenant() throws Exception {
loginTenantAdmin(); loginTenantAdmin();
Alarm alarm = createAlarm(TEST_ALARM_TYPE); Alarm alarm = createAlarm(TEST_ALARM_TYPE);
alarm.setSeverity(AlarmSeverity.MAJOR); alarm.setSeverity(AlarmSeverity.MAJOR);
loginDifferentTenant(); loginDifferentTenant();
Mockito.reset(tbClusterService, auditLogService);
doPost("/api/alarm", alarm).andExpect(status().isForbidden()); doPost("/api/alarm", alarm).andExpect(status().isForbidden());
testNotifyEntityNever(alarm.getId(), alarm);
logout(); logout();
} }
@ -103,9 +145,15 @@ public abstract class BaseAlarmControllerTest extends AbstractControllerTest {
public void testUpdateAlarmViaDifferentCustomer() throws Exception { public void testUpdateAlarmViaDifferentCustomer() throws Exception {
loginCustomerUser(); loginCustomerUser();
Alarm alarm = createAlarm(TEST_ALARM_TYPE); Alarm alarm = createAlarm(TEST_ALARM_TYPE);
loginDifferentCustomer(); loginDifferentCustomer();
alarm.setSeverity(AlarmSeverity.MAJOR); alarm.setSeverity(AlarmSeverity.MAJOR);
Mockito.reset(tbClusterService, auditLogService);
doPost("/api/alarm", alarm).andExpect(status().isForbidden()); doPost("/api/alarm", alarm).andExpect(status().isForbidden());
testNotifyEntityNever(alarm.getId(), alarm);
logout(); logout();
} }
@ -113,7 +161,13 @@ public abstract class BaseAlarmControllerTest extends AbstractControllerTest {
public void testDeleteAlarmViaCustomer() throws Exception { public void testDeleteAlarmViaCustomer() throws Exception {
loginCustomerUser(); loginCustomerUser();
Alarm alarm = createAlarm(TEST_ALARM_TYPE); Alarm alarm = createAlarm(TEST_ALARM_TYPE);
Mockito.reset(tbClusterService, auditLogService);
doDelete("/api/alarm/" + alarm.getId()).andExpect(status().isOk()); doDelete("/api/alarm/" + alarm.getId()).andExpect(status().isOk());
testNotifyEntityDeleteOk(alarm, alarm.getId(), alarm.getOriginator(),
tenantId, customerId, customerUserId, CUSTOMER_USER_EMAIL, ActionType.DELETED);
logout(); logout();
} }
@ -121,7 +175,13 @@ public abstract class BaseAlarmControllerTest extends AbstractControllerTest {
public void testDeleteAlarmViaTenant() throws Exception { public void testDeleteAlarmViaTenant() throws Exception {
loginTenantAdmin(); loginTenantAdmin();
Alarm alarm = createAlarm(TEST_ALARM_TYPE); Alarm alarm = createAlarm(TEST_ALARM_TYPE);
Mockito.reset(tbClusterService, auditLogService);
doDelete("/api/alarm/" + alarm.getId()).andExpect(status().isOk()); doDelete("/api/alarm/" + alarm.getId()).andExpect(status().isOk());
testNotifyEntityDeleteOk(alarm, alarm.getId(), alarm.getOriginator(),
tenantId, tenantAdminCustomerId, tenantAdminUserId, TENANT_ADMIN_EMAIL, ActionType.DELETED);
logout(); logout();
} }
@ -129,8 +189,15 @@ public abstract class BaseAlarmControllerTest extends AbstractControllerTest {
public void testDeleteAlarmViaDifferentTenant() throws Exception { public void testDeleteAlarmViaDifferentTenant() throws Exception {
loginTenantAdmin(); loginTenantAdmin();
Alarm alarm = createAlarm(TEST_ALARM_TYPE); Alarm alarm = createAlarm(TEST_ALARM_TYPE);
loginDifferentTenant(); loginDifferentTenant();
Mockito.reset(tbClusterService, auditLogService);
doDelete("/api/alarm/" + alarm.getId()).andExpect(status().isForbidden()); doDelete("/api/alarm/" + alarm.getId()).andExpect(status().isForbidden());
testNotifyEntityNever(alarm.getId(), alarm);
logout(); logout();
} }
@ -139,7 +206,13 @@ public abstract class BaseAlarmControllerTest extends AbstractControllerTest {
loginCustomerUser(); loginCustomerUser();
Alarm alarm = createAlarm(TEST_ALARM_TYPE); Alarm alarm = createAlarm(TEST_ALARM_TYPE);
loginDifferentCustomer(); loginDifferentCustomer();
Mockito.reset(tbClusterService, auditLogService);
doDelete("/api/alarm/" + alarm.getId()).andExpect(status().isForbidden()); doDelete("/api/alarm/" + alarm.getId()).andExpect(status().isForbidden());
testNotifyEntityNever(alarm.getId(), alarm);
logout(); logout();
} }
@ -147,10 +220,17 @@ public abstract class BaseAlarmControllerTest extends AbstractControllerTest {
public void testClearAlarmViaCustomer() throws Exception { public void testClearAlarmViaCustomer() throws Exception {
loginCustomerUser(); loginCustomerUser();
Alarm alarm = createAlarm(TEST_ALARM_TYPE); Alarm alarm = createAlarm(TEST_ALARM_TYPE);
Mockito.reset(tbClusterService, auditLogService);
doPost("/api/alarm/" + alarm.getId() + "/clear").andExpect(status().isOk()); doPost("/api/alarm/" + alarm.getId() + "/clear").andExpect(status().isOk());
Alarm foundAlarm = doGet("/api/alarm/" + alarm.getId(), Alarm.class); Alarm foundAlarm = doGet("/api/alarm/" + alarm.getId(), Alarm.class);
Assert.assertNotNull(foundAlarm); Assert.assertNotNull(foundAlarm);
Assert.assertEquals(AlarmStatus.CLEARED_UNACK, foundAlarm.getStatus()); Assert.assertEquals(AlarmStatus.CLEARED_UNACK, foundAlarm.getStatus());
testNotifyEntityOk(foundAlarm, foundAlarm.getId(), foundAlarm.getOriginator(),
tenantId, customerId, customerUserId, CUSTOMER_USER_EMAIL, ActionType.ALARM_CLEAR);
logout(); logout();
} }
@ -158,10 +238,16 @@ public abstract class BaseAlarmControllerTest extends AbstractControllerTest {
public void testClearAlarmViaTenant() throws Exception { public void testClearAlarmViaTenant() throws Exception {
loginTenantAdmin(); loginTenantAdmin();
Alarm alarm = createAlarm(TEST_ALARM_TYPE); Alarm alarm = createAlarm(TEST_ALARM_TYPE);
Mockito.reset(tbClusterService, auditLogService);
doPost("/api/alarm/" + alarm.getId() + "/clear").andExpect(status().isOk()); doPost("/api/alarm/" + alarm.getId() + "/clear").andExpect(status().isOk());
Alarm foundAlarm = doGet("/api/alarm/" + alarm.getId(), Alarm.class); Alarm foundAlarm = doGet("/api/alarm/" + alarm.getId(), Alarm.class);
Assert.assertNotNull(foundAlarm); Assert.assertNotNull(foundAlarm);
Assert.assertEquals(AlarmStatus.CLEARED_UNACK, foundAlarm.getStatus()); Assert.assertEquals(AlarmStatus.CLEARED_UNACK, foundAlarm.getStatus());
testNotifyEntityOk(foundAlarm, foundAlarm.getId(), foundAlarm.getOriginator(),
tenantId, customerId, tenantAdminUserId, TENANT_ADMIN_EMAIL, ActionType.ALARM_CLEAR);
logout(); logout();
} }
@ -169,10 +255,17 @@ public abstract class BaseAlarmControllerTest extends AbstractControllerTest {
public void testAcknowledgeAlarmViaCustomer() throws Exception { public void testAcknowledgeAlarmViaCustomer() throws Exception {
loginCustomerUser(); loginCustomerUser();
Alarm alarm = createAlarm(TEST_ALARM_TYPE); Alarm alarm = createAlarm(TEST_ALARM_TYPE);
Mockito.reset(tbClusterService, auditLogService);
doPost("/api/alarm/" + alarm.getId() + "/ack").andExpect(status().isOk()); doPost("/api/alarm/" + alarm.getId() + "/ack").andExpect(status().isOk());
Alarm foundAlarm = doGet("/api/alarm/" + alarm.getId(), Alarm.class); Alarm foundAlarm = doGet("/api/alarm/" + alarm.getId(), Alarm.class);
Assert.assertNotNull(foundAlarm); Assert.assertNotNull(foundAlarm);
Assert.assertEquals(AlarmStatus.ACTIVE_ACK, foundAlarm.getStatus()); Assert.assertEquals(AlarmStatus.ACTIVE_ACK, foundAlarm.getStatus());
testNotifyEntityOk(foundAlarm, foundAlarm.getId(), foundAlarm.getOriginator(),
tenantId, customerId, customerUserId, CUSTOMER_USER_EMAIL, ActionType.ALARM_ACK);
logout(); logout();
} }
@ -181,7 +274,12 @@ public abstract class BaseAlarmControllerTest extends AbstractControllerTest {
loginCustomerUser(); loginCustomerUser();
Alarm alarm = createAlarm(TEST_ALARM_TYPE); Alarm alarm = createAlarm(TEST_ALARM_TYPE);
loginDifferentCustomer(); loginDifferentCustomer();
Mockito.reset(tbClusterService, auditLogService);
doPost("/api/alarm/" + alarm.getId() + "/clear").andExpect(status().isForbidden()); doPost("/api/alarm/" + alarm.getId() + "/clear").andExpect(status().isForbidden());
testNotifyEntityNever(alarm.getId(), alarm);
logout(); logout();
} }
@ -190,7 +288,12 @@ public abstract class BaseAlarmControllerTest extends AbstractControllerTest {
loginTenantAdmin(); loginTenantAdmin();
Alarm alarm = createAlarm(TEST_ALARM_TYPE); Alarm alarm = createAlarm(TEST_ALARM_TYPE);
loginDifferentTenant(); loginDifferentTenant();
Mockito.reset(tbClusterService, auditLogService);
doPost("/api/alarm/" + alarm.getId() + "/clear").andExpect(status().isForbidden()); doPost("/api/alarm/" + alarm.getId() + "/clear").andExpect(status().isForbidden());
testNotifyEntityNever(alarm.getId(), alarm);
logout(); logout();
} }
@ -199,7 +302,12 @@ public abstract class BaseAlarmControllerTest extends AbstractControllerTest {
loginCustomerUser(); loginCustomerUser();
Alarm alarm = createAlarm(TEST_ALARM_TYPE); Alarm alarm = createAlarm(TEST_ALARM_TYPE);
loginDifferentCustomer(); loginDifferentCustomer();
Mockito.reset(tbClusterService, auditLogService);
doPost("/api/alarm/" + alarm.getId() + "/ack").andExpect(status().isForbidden()); doPost("/api/alarm/" + alarm.getId() + "/ack").andExpect(status().isForbidden());
testNotifyEntityNever(alarm.getId(), alarm);
logout(); logout();
} }
@ -208,7 +316,12 @@ public abstract class BaseAlarmControllerTest extends AbstractControllerTest {
loginTenantAdmin(); loginTenantAdmin();
Alarm alarm = createAlarm(TEST_ALARM_TYPE); Alarm alarm = createAlarm(TEST_ALARM_TYPE);
loginDifferentTenant(); loginDifferentTenant();
Mockito.reset(tbClusterService, auditLogService);
doPost("/api/alarm/" + alarm.getId() + "/ack").andExpect(status().isForbidden()); doPost("/api/alarm/" + alarm.getId() + "/ack").andExpect(status().isForbidden());
testNotifyEntityNever(alarm.getId(), alarm);
logout(); logout();
} }
@ -221,10 +334,8 @@ public abstract class BaseAlarmControllerTest extends AbstractControllerTest {
.severity(AlarmSeverity.CRITICAL) .severity(AlarmSeverity.CRITICAL)
.type(type) .type(type)
.build(); .build();
alarm = doPost("/api/alarm", alarm, Alarm.class); alarm = doPost("/api/alarm", alarm, Alarm.class);
Assert.assertNotNull(alarm); Assert.assertNotNull(alarm);
return alarm; return alarm;
} }
} }

View File

@ -40,12 +40,12 @@ import org.thingsboard.server.common.data.device.profile.AlarmConditionFilter;
import org.thingsboard.server.common.data.device.profile.AlarmConditionFilterKey; import org.thingsboard.server.common.data.device.profile.AlarmConditionFilterKey;
import org.thingsboard.server.common.data.device.profile.AlarmConditionKeyType; import org.thingsboard.server.common.data.device.profile.AlarmConditionKeyType;
import org.thingsboard.server.common.data.device.profile.AlarmRule; import org.thingsboard.server.common.data.device.profile.AlarmRule;
import org.thingsboard.server.common.data.device.profile.CustomTimeSchedule;
import org.thingsboard.server.common.data.device.profile.CustomTimeScheduleItem;
import org.thingsboard.server.common.data.device.profile.DeviceProfileAlarm; import org.thingsboard.server.common.data.device.profile.DeviceProfileAlarm;
import org.thingsboard.server.common.data.device.profile.DeviceProfileData; import org.thingsboard.server.common.data.device.profile.DeviceProfileData;
import org.thingsboard.server.common.data.device.profile.DurationAlarmConditionSpec; import org.thingsboard.server.common.data.device.profile.DurationAlarmConditionSpec;
import org.thingsboard.server.common.data.device.profile.RepeatingAlarmConditionSpec; import org.thingsboard.server.common.data.device.profile.RepeatingAlarmConditionSpec;
import org.thingsboard.server.common.data.device.profile.CustomTimeSchedule;
import org.thingsboard.server.common.data.device.profile.CustomTimeScheduleItem;
import org.thingsboard.server.common.data.id.CustomerId; import org.thingsboard.server.common.data.id.CustomerId;
import org.thingsboard.server.common.data.id.DeviceId; import org.thingsboard.server.common.data.id.DeviceId;
import org.thingsboard.server.common.data.id.DeviceProfileId; import org.thingsboard.server.common.data.id.DeviceProfileId;
@ -70,10 +70,10 @@ import org.thingsboard.server.dao.timeseries.TimeseriesService;
import java.math.BigDecimal; import java.math.BigDecimal;
import java.math.RoundingMode; import java.math.RoundingMode;
import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.ArrayList;
import java.util.Optional; import java.util.Optional;
import java.util.TreeMap; import java.util.TreeMap;
import java.util.UUID; import java.util.UUID;
@ -1157,7 +1157,7 @@ public class TbDeviceProfileNodeTest {
.thenReturn(listListenableFutureActiveSchedule); .thenReturn(listListenableFutureActiveSchedule);
TbMsg theMsg = TbMsg.newMsg("ALARM", deviceId, new TbMsgMetaData(), ""); TbMsg theMsg = TbMsg.newMsg("ALARM", deviceId, new TbMsgMetaData(), "");
Mockito.when(ctx.newMsg(Mockito.anyString(), Mockito.anyString(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.anyString())) Mockito.when(ctx.newMsg(Mockito.any(), Mockito.anyString(), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.anyString()))
.thenReturn(theMsg); .thenReturn(theMsg);
ObjectNode data = mapper.createObjectNode(); ObjectNode data = mapper.createObjectNode();