|
|
@ -11,6 +11,7 @@ import org.junit.function.ThrowingRunnable; |
|
|
import org.mockito.MockedStatic; |
|
|
import org.mockito.MockedStatic; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.beans.factory.annotation.Autowired; |
|
|
import org.springframework.data.util.Pair; |
|
|
import org.springframework.data.util.Pair; |
|
|
|
|
|
import org.springframework.test.util.ReflectionTestUtils; |
|
|
import org.springframework.test.context.TestPropertySource; |
|
|
import org.springframework.test.context.TestPropertySource; |
|
|
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean; |
|
|
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean; |
|
|
import org.thingsboard.common.util.JacksonUtil; |
|
|
import org.thingsboard.common.util.JacksonUtil; |
|
|
@ -53,6 +54,7 @@ import org.thingsboard.server.common.data.notification.NotificationRequestInfo; |
|
|
import org.thingsboard.server.common.data.notification.NotificationType; |
|
|
import org.thingsboard.server.common.data.notification.NotificationType; |
|
|
import org.thingsboard.server.common.data.notification.info.AlarmNotificationInfo; |
|
|
import org.thingsboard.server.common.data.notification.info.AlarmNotificationInfo; |
|
|
import org.thingsboard.server.common.data.notification.info.RateLimitsNotificationInfo; |
|
|
import org.thingsboard.server.common.data.notification.info.RateLimitsNotificationInfo; |
|
|
|
|
|
import org.thingsboard.server.common.data.notification.info.ResourcesShortageNotificationInfo; |
|
|
import org.thingsboard.server.common.data.notification.rule.DefaultNotificationRuleRecipientsConfig; |
|
|
import org.thingsboard.server.common.data.notification.rule.DefaultNotificationRuleRecipientsConfig; |
|
|
import org.thingsboard.server.common.data.notification.rule.EscalatedNotificationRuleRecipientsConfig; |
|
|
import org.thingsboard.server.common.data.notification.rule.EscalatedNotificationRuleRecipientsConfig; |
|
|
import org.thingsboard.server.common.data.notification.rule.NotificationRule; |
|
|
import org.thingsboard.server.common.data.notification.rule.NotificationRule; |
|
|
@ -88,12 +90,10 @@ import org.thingsboard.server.dao.notification.NotificationRequestService; |
|
|
import org.thingsboard.server.dao.rule.RuleChainService; |
|
|
import org.thingsboard.server.dao.rule.RuleChainService; |
|
|
import org.thingsboard.server.dao.service.DaoSqlTest; |
|
|
import org.thingsboard.server.dao.service.DaoSqlTest; |
|
|
import org.thingsboard.server.queue.notification.DefaultNotificationDeduplicationService; |
|
|
import org.thingsboard.server.queue.notification.DefaultNotificationDeduplicationService; |
|
|
import org.thingsboard.server.service.notification.rule.cache.DefaultNotificationRulesCache; |
|
|
|
|
|
import org.thingsboard.server.service.state.DeviceStateService; |
|
|
import org.thingsboard.server.service.state.DeviceStateService; |
|
|
import org.thingsboard.server.service.system.DefaultSystemInfoService; |
|
|
import org.thingsboard.server.service.system.DefaultSystemInfoService; |
|
|
import org.thingsboard.server.service.telemetry.AlarmSubscriptionService; |
|
|
import org.thingsboard.server.service.telemetry.AlarmSubscriptionService; |
|
|
|
|
|
|
|
|
import java.lang.reflect.Method; |
|
|
|
|
|
import java.util.HashMap; |
|
|
import java.util.HashMap; |
|
|
import java.util.List; |
|
|
import java.util.List; |
|
|
import java.util.Map; |
|
|
import java.util.Map; |
|
|
@ -121,7 +121,9 @@ import static org.thingsboard.server.common.data.notification.rule.trigger.confi |
|
|
@TestPropertySource(properties = { |
|
|
@TestPropertySource(properties = { |
|
|
"transport.http.enabled=true", |
|
|
"transport.http.enabled=true", |
|
|
"notification_system.rules.deduplication_durations=RATE_LIMITS:10000", |
|
|
"notification_system.rules.deduplication_durations=RATE_LIMITS:10000", |
|
|
"edges.enabled=true" |
|
|
"edges.enabled=true", |
|
|
|
|
|
// the real system info job reports resource shortages only once, at startup
|
|
|
|
|
|
"metrics.system_info.persist_frequency=3600" |
|
|
}) |
|
|
}) |
|
|
public class NotificationRuleApiTest extends AbstractNotificationApiTest { |
|
|
public class NotificationRuleApiTest extends AbstractNotificationApiTest { |
|
|
|
|
|
|
|
|
@ -138,8 +140,6 @@ public class NotificationRuleApiTest extends AbstractNotificationApiTest { |
|
|
@Autowired |
|
|
@Autowired |
|
|
private NotificationRuleProcessor notificationRuleProcessor; |
|
|
private NotificationRuleProcessor notificationRuleProcessor; |
|
|
@Autowired |
|
|
@Autowired |
|
|
private DefaultNotificationRulesCache notificationRulesCache; |
|
|
|
|
|
@Autowired |
|
|
|
|
|
private DeviceStateService deviceStateService; |
|
|
private DeviceStateService deviceStateService; |
|
|
|
|
|
|
|
|
@Before |
|
|
@Before |
|
|
@ -269,7 +269,7 @@ public class NotificationRuleApiTest extends AbstractNotificationApiTest { |
|
|
Device device = createDevice("Device with alarm", "233"); |
|
|
Device device = createDevice("Device with alarm", "233"); |
|
|
NotificationTarget target = createNotificationTarget(tenantAdminUserId); |
|
|
NotificationTarget target = createNotificationTarget(tenantAdminUserId); |
|
|
defaultNotifications.create(tenantId, DefaultNotifications.newAlarm, target.getId()); |
|
|
defaultNotifications.create(tenantId, DefaultNotifications.newAlarm, target.getId()); |
|
|
notificationRulesCache.evict(tenantId); |
|
|
awaitNotificationRulesCacheUpdate(tenantId, NotificationRuleTriggerType.ALARM); |
|
|
|
|
|
|
|
|
Alarm alarm = new Alarm(); |
|
|
Alarm alarm = new Alarm(); |
|
|
alarm.setSeverity(AlarmSeverity.CRITICAL); |
|
|
alarm.setSeverity(AlarmSeverity.CRITICAL); |
|
|
@ -444,7 +444,7 @@ public class NotificationRuleApiTest extends AbstractNotificationApiTest { |
|
|
defaultNotifications.create(TenantId.SYS_TENANT_ID, DefaultNotifications.exceededRateLimitsForSysadmin, sysadmins.getId()); |
|
|
defaultNotifications.create(TenantId.SYS_TENANT_ID, DefaultNotifications.exceededRateLimitsForSysadmin, sysadmins.getId()); |
|
|
defaultNotifications.create(TenantId.SYS_TENANT_ID, DefaultNotifications.exceededRateLimits, affectedTenantAdmins.getId()); |
|
|
defaultNotifications.create(TenantId.SYS_TENANT_ID, DefaultNotifications.exceededRateLimits, affectedTenantAdmins.getId()); |
|
|
defaultNotifications.create(TenantId.SYS_TENANT_ID, DefaultNotifications.exceededPerEntityRateLimits, affectedTenantAdmins.getId()); |
|
|
defaultNotifications.create(TenantId.SYS_TENANT_ID, DefaultNotifications.exceededPerEntityRateLimits, affectedTenantAdmins.getId()); |
|
|
notificationRulesCache.evict(TenantId.SYS_TENANT_ID); |
|
|
awaitNotificationRulesCacheUpdate(TenantId.SYS_TENANT_ID, NotificationRuleTriggerType.RATE_LIMITS); |
|
|
|
|
|
|
|
|
int n = 10; |
|
|
int n = 10; |
|
|
updateDefaultTenantProfileConfig(profileConfiguration -> { |
|
|
updateDefaultTenantProfileConfig(profileConfiguration -> { |
|
|
@ -783,14 +783,8 @@ public class NotificationRuleApiTest extends AbstractNotificationApiTest { |
|
|
createNotificationRule(triggerConfig, "Warning: ${resource} shortage", "${resource} shortage", createNotificationTarget(tenantAdminUserId).getId()); |
|
|
createNotificationRule(triggerConfig, "Warning: ${resource} shortage", "${resource} shortage", createNotificationTarget(tenantAdminUserId).getId()); |
|
|
loginTenantAdmin(); |
|
|
loginTenantAdmin(); |
|
|
|
|
|
|
|
|
// Mock SystemUtil to return 15% memory usage (exceeds 1% threshold)
|
|
|
// 15% RAM exceeds the 1% threshold, CPU and storage stay below 100%
|
|
|
try (MockedStatic<SystemUtil> mockedSystemUtil = mockStatic(SystemUtil.class)) { |
|
|
saveMonolithSystemInfo(15); |
|
|
mockedSystemUtil.when(SystemUtil::getMemoryUsage).thenReturn(Optional.of(15)); |
|
|
|
|
|
|
|
|
|
|
|
Method method = DefaultSystemInfoService.class.getDeclaredMethod("saveCurrentMonolithSystemInfo"); |
|
|
|
|
|
method.setAccessible(true); |
|
|
|
|
|
method.invoke(systemInfoService); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
await().atMost(TIMEOUT, TimeUnit.SECONDS).until(() -> getMyNotifications(false, 100).size() == 1); |
|
|
await().atMost(TIMEOUT, TimeUnit.SECONDS).until(() -> getMyNotifications(false, 100).size() == 1); |
|
|
Notification notification = getMyNotifications(false, 100).get(0); |
|
|
Notification notification = getMyNotifications(false, 100).get(0); |
|
|
@ -809,29 +803,32 @@ public class NotificationRuleApiTest extends AbstractNotificationApiTest { |
|
|
.build(); |
|
|
.build(); |
|
|
createNotificationRule(triggerConfig, "Warning: ${resource} shortage", "${resource} shortage", sysadmins.getId()); |
|
|
createNotificationRule(triggerConfig, "Warning: ${resource} shortage", "${resource} shortage", sysadmins.getId()); |
|
|
|
|
|
|
|
|
assertThat(getMyNotifications(false, 100)).size().isZero(); |
|
|
// the real system info job reports RAM under this node's service id, so count only this test's
|
|
|
|
|
|
String serviceId = "test-service-" + UUID.randomUUID(); |
|
|
for (int i = 0; i < 10; i++) { |
|
|
for (int i = 0; i < 10; i++) { |
|
|
notificationRuleProcessor.process(ResourcesShortageTrigger.builder() |
|
|
notificationRuleProcessor.process(ResourcesShortageTrigger.builder() |
|
|
.resource(Resource.RAM) |
|
|
.resource(Resource.RAM) |
|
|
.usage(15L) |
|
|
.usage(15L) |
|
|
.serviceType("serviceType") |
|
|
.serviceType("serviceType") |
|
|
.serviceId("serviceId") |
|
|
.serviceId(serviceId) |
|
|
.build()); |
|
|
.build()); |
|
|
TimeUnit.MILLISECONDS.sleep(300); |
|
|
TimeUnit.MILLISECONDS.sleep(300); |
|
|
} |
|
|
} |
|
|
await().atMost(TIMEOUT, TimeUnit.SECONDS).until(() -> getMyNotifications(false, 100).size() == 1); |
|
|
List<Notification> notifications = await().atMost(TIMEOUT, TimeUnit.SECONDS) |
|
|
Notification notification = getMyNotifications(false, 100).get(0); |
|
|
.until(() -> getResourcesShortageNotifications(serviceId), list -> list.size() == 1); |
|
|
|
|
|
Notification notification = notifications.get(0); |
|
|
assertThat(notification.getSubject()).isEqualTo("Warning: RAM shortage"); |
|
|
assertThat(notification.getSubject()).isEqualTo("Warning: RAM shortage"); |
|
|
assertThat(notification.getText()).isEqualTo("RAM shortage"); |
|
|
assertThat(notification.getText()).isEqualTo("RAM shortage"); |
|
|
|
|
|
|
|
|
// deduplication is 1 minute, no new message is exp
|
|
|
// deduplication is 1 hour, no new message is expected
|
|
|
notificationRuleProcessor.process(ResourcesShortageTrigger.builder() |
|
|
notificationRuleProcessor.process(ResourcesShortageTrigger.builder() |
|
|
.resource(Resource.RAM) |
|
|
.resource(Resource.RAM) |
|
|
.usage(5L) |
|
|
.usage(5L) |
|
|
.serviceType("serviceType") |
|
|
.serviceType("serviceType") |
|
|
.serviceId("serviceId") |
|
|
.serviceId(serviceId) |
|
|
.build()); |
|
|
.build()); |
|
|
await("").atMost(TIMEOUT, TimeUnit.SECONDS).untilAsserted(() -> assertThat(getMyNotifications(false, 100)).size().isOne()); |
|
|
await().during(3, TimeUnit.SECONDS).atMost(TIMEOUT, TimeUnit.SECONDS) |
|
|
|
|
|
.untilAsserted(() -> assertThat(getResourcesShortageNotifications(serviceId)).hasSize(1)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
@Test |
|
|
@Test |
|
|
@ -846,16 +843,7 @@ public class NotificationRuleApiTest extends AbstractNotificationApiTest { |
|
|
NotificationRule rule = createNotificationRule(triggerConfig, "Warning: ${resource} shortage", "${resource} shortage", sysadmins.getId()); |
|
|
NotificationRule rule = createNotificationRule(triggerConfig, "Warning: ${resource} shortage", "${resource} shortage", sysadmins.getId()); |
|
|
|
|
|
|
|
|
// Mock SystemUtil to return 15% usages (not exceeds 99% threshold)
|
|
|
// Mock SystemUtil to return 15% usages (not exceeds 99% threshold)
|
|
|
Method method; |
|
|
saveMonolithSystemInfo(15); |
|
|
try (MockedStatic<SystemUtil> mockedSystemUtil = mockStatic(SystemUtil.class)) { |
|
|
|
|
|
mockedSystemUtil.when(SystemUtil::getMemoryUsage).thenReturn(Optional.of(15)); |
|
|
|
|
|
mockedSystemUtil.when(SystemUtil::getCpuUsage).thenReturn(Optional.of(15)); |
|
|
|
|
|
mockedSystemUtil.when(SystemUtil::getDiscSpaceUsage).thenReturn(Optional.of(15)); |
|
|
|
|
|
|
|
|
|
|
|
method = DefaultSystemInfoService.class.getDeclaredMethod("saveCurrentMonolithSystemInfo"); |
|
|
|
|
|
method.setAccessible(true); |
|
|
|
|
|
method.invoke(systemInfoService); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
TimeUnit.SECONDS.sleep(5); |
|
|
TimeUnit.SECONDS.sleep(5); |
|
|
assertThat(getMyNotifications(false, 100)).size().isZero(); |
|
|
assertThat(getMyNotifications(false, 100)).size().isZero(); |
|
|
@ -868,7 +856,7 @@ public class NotificationRuleApiTest extends AbstractNotificationApiTest { |
|
|
rule.setTriggerConfig(triggerConfig); |
|
|
rule.setTriggerConfig(triggerConfig); |
|
|
saveNotificationRule(rule); |
|
|
saveNotificationRule(rule); |
|
|
|
|
|
|
|
|
method.invoke(systemInfoService); |
|
|
saveMonolithSystemInfo(15); |
|
|
|
|
|
|
|
|
await().atMost(TIMEOUT, TimeUnit.SECONDS).until(() -> getMyNotifications(false, 100).size() == 1); |
|
|
await().atMost(TIMEOUT, TimeUnit.SECONDS).until(() -> getMyNotifications(false, 100).size() == 1); |
|
|
Notification notification = getMyNotifications(false, 100).get(0); |
|
|
Notification notification = getMyNotifications(false, 100).get(0); |
|
|
@ -882,7 +870,6 @@ public class NotificationRuleApiTest extends AbstractNotificationApiTest { |
|
|
triggerConfig.setEntityTypes(Set.of(EntityType.DEVICE)); |
|
|
triggerConfig.setEntityTypes(Set.of(EntityType.DEVICE)); |
|
|
triggerConfig.setCreated(true); |
|
|
triggerConfig.setCreated(true); |
|
|
NotificationRule rule = createNotificationRule(triggerConfig, "Created", "Created", createNotificationTarget(tenantAdminUserId).getId()); |
|
|
NotificationRule rule = createNotificationRule(triggerConfig, "Created", "Created", createNotificationTarget(tenantAdminUserId).getId()); |
|
|
notificationRulesCache.evict(tenantId); |
|
|
|
|
|
|
|
|
|
|
|
assertThat(getMyNotifications(false, 100)).size().isZero(); |
|
|
assertThat(getMyNotifications(false, 100)).size().isZero(); |
|
|
createDevice("Device 1", "default", "111"); |
|
|
createDevice("Device 1", "default", "111"); |
|
|
@ -893,7 +880,6 @@ public class NotificationRuleApiTest extends AbstractNotificationApiTest { |
|
|
|
|
|
|
|
|
rule.setEnabled(false); |
|
|
rule.setEnabled(false); |
|
|
saveNotificationRule(rule); |
|
|
saveNotificationRule(rule); |
|
|
notificationRulesCache.evict(tenantId); |
|
|
|
|
|
|
|
|
|
|
|
createDevice("Device 2", "default", "222"); |
|
|
createDevice("Device 2", "default", "222"); |
|
|
TimeUnit.SECONDS.sleep(5); |
|
|
TimeUnit.SECONDS.sleep(5); |
|
|
@ -901,7 +887,6 @@ public class NotificationRuleApiTest extends AbstractNotificationApiTest { |
|
|
|
|
|
|
|
|
rule.setEnabled(true); |
|
|
rule.setEnabled(true); |
|
|
saveNotificationRule(rule); |
|
|
saveNotificationRule(rule); |
|
|
notificationRulesCache.evict(tenantId); |
|
|
|
|
|
|
|
|
|
|
|
createDevice("Device 3", "default", "333"); |
|
|
createDevice("Device 3", "default", "333"); |
|
|
await().atMost(TIMEOUT, TimeUnit.SECONDS) |
|
|
await().atMost(TIMEOUT, TimeUnit.SECONDS) |
|
|
@ -932,6 +917,22 @@ public class NotificationRuleApiTest extends AbstractNotificationApiTest { |
|
|
}, (notification, r) -> check.accept(notification)); |
|
|
}, (notification, r) -> check.accept(notification)); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private void saveMonolithSystemInfo(int usagePercent) { |
|
|
|
|
|
try (MockedStatic<SystemUtil> mockedSystemUtil = mockStatic(SystemUtil.class)) { |
|
|
|
|
|
mockedSystemUtil.when(SystemUtil::getMemoryUsage).thenReturn(Optional.of(usagePercent)); |
|
|
|
|
|
mockedSystemUtil.when(SystemUtil::getCpuUsage).thenReturn(Optional.of(usagePercent)); |
|
|
|
|
|
mockedSystemUtil.when(SystemUtil::getDiscSpaceUsage).thenReturn(Optional.of(usagePercent)); |
|
|
|
|
|
ReflectionTestUtils.invokeMethod(systemInfoService, "saveCurrentMonolithSystemInfo"); |
|
|
|
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
private List<Notification> getResourcesShortageNotifications(String serviceId) throws Exception { |
|
|
|
|
|
return getMyNotifications(false, 100).stream() |
|
|
|
|
|
.filter(notification -> notification.getInfo() instanceof ResourcesShortageNotificationInfo info |
|
|
|
|
|
&& serviceId.equals(info.getServiceId())) |
|
|
|
|
|
.toList(); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
private PageData<NotificationRequestInfo> findNotificationRequests(EntityType originatorType) { |
|
|
private PageData<NotificationRequestInfo> findNotificationRequests(EntityType originatorType) { |
|
|
return notificationRequestService.findNotificationRequestsInfosByTenantIdAndOriginatorType(tenantId, originatorType, new PageLink(100)); |
|
|
return notificationRequestService.findNotificationRequestsInfosByTenantIdAndOriginatorType(tenantId, originatorType, new PageLink(100)); |
|
|
} |
|
|
} |
|
|
|