Browse Source
- AI credits limit increase request rolls back its dedupe key on any failure after it is stored, so a failed attempt no longer blocks retries (PR #16848) - DefaultAiChatServiceTest expects the credit check on createChat (PR #16848) - startNew uses the credit-checking process(action, user) overload and its test verifies it (PR #16848) - Unit tests for owner-scoped alarm subscriptions and TbEntityDataSubCtx.onSameEntities (PR #16848)pull/16848/head
6 changed files with 236 additions and 37 deletions
@ -0,0 +1,137 @@ |
|||||
|
// SPDX-FileCopyrightText: Copyright The ThingsBoard Authors
|
||||
|
// SPDX-License-Identifier: Apache-2.0
|
||||
|
package org.thingsboard.server.service.subscription; |
||||
|
|
||||
|
import org.junit.jupiter.api.BeforeEach; |
||||
|
import org.junit.jupiter.api.Test; |
||||
|
import org.junit.jupiter.api.extension.ExtendWith; |
||||
|
import org.mockito.ArgumentCaptor; |
||||
|
import org.mockito.Mock; |
||||
|
import org.mockito.junit.jupiter.MockitoExtension; |
||||
|
import org.thingsboard.server.common.data.alarm.Alarm; |
||||
|
import org.thingsboard.server.common.data.id.AlarmId; |
||||
|
import org.thingsboard.server.common.data.id.DeviceId; |
||||
|
import org.thingsboard.server.common.data.id.TenantId; |
||||
|
import org.thingsboard.server.common.data.page.PageData; |
||||
|
import org.thingsboard.server.common.data.query.AlarmData; |
||||
|
import org.thingsboard.server.common.data.query.AlarmDataPageLink; |
||||
|
import org.thingsboard.server.common.data.query.AlarmDataQuery; |
||||
|
import org.thingsboard.server.common.data.query.SingleEntityFilter; |
||||
|
import org.thingsboard.server.dao.alarm.AlarmService; |
||||
|
import org.thingsboard.server.service.security.model.SecurityUser; |
||||
|
import org.thingsboard.server.service.ws.WebSocketService; |
||||
|
import org.thingsboard.server.service.ws.WebSocketSessionRef; |
||||
|
import org.thingsboard.server.service.ws.telemetry.cmd.v2.AlarmDataUpdate; |
||||
|
import org.thingsboard.server.service.ws.telemetry.cmd.v2.CmdUpdate; |
||||
|
|
||||
|
import java.util.ArrayList; |
||||
|
import java.util.List; |
||||
|
import java.util.UUID; |
||||
|
|
||||
|
import static org.assertj.core.api.Assertions.assertThat; |
||||
|
import static org.mockito.ArgumentMatchers.any; |
||||
|
import static org.mockito.ArgumentMatchers.eq; |
||||
|
import static org.mockito.ArgumentMatchers.same; |
||||
|
import static org.mockito.BDDMockito.given; |
||||
|
import static org.mockito.BDDMockito.then; |
||||
|
import static org.mockito.Mockito.lenient; |
||||
|
import static org.mockito.Mockito.mock; |
||||
|
|
||||
|
@ExtendWith(MockitoExtension.class) |
||||
|
public class TbAlarmDataSubCtxTest { |
||||
|
|
||||
|
private final String sessionId = "session"; |
||||
|
private final int cmdId = 7; |
||||
|
private final int maxEntitiesPerAlarmSubscription = 100; |
||||
|
private final TenantId tenantId = TenantId.fromUUID(UUID.randomUUID()); |
||||
|
private final DeviceId deviceId = new DeviceId(UUID.randomUUID()); |
||||
|
|
||||
|
@Mock |
||||
|
private WebSocketService webSocketService; |
||||
|
@Mock |
||||
|
private AlarmService alarmService; |
||||
|
@Mock |
||||
|
private WebSocketSessionRef sessionRef; |
||||
|
@Mock |
||||
|
private SecurityUser securityUser; |
||||
|
|
||||
|
private TbAlarmDataSubCtx subCtx; |
||||
|
|
||||
|
@BeforeEach |
||||
|
public void setUp() { |
||||
|
lenient().when(sessionRef.getSessionId()).thenReturn(sessionId); |
||||
|
lenient().when(sessionRef.getSecurityCtx()).thenReturn(securityUser); |
||||
|
lenient().when(securityUser.getTenantId()).thenReturn(tenantId); |
||||
|
subCtx = new TbAlarmDataSubCtx("service", webSocketService, mock(), mock(), mock(), new SubscriptionServiceStatistics(), |
||||
|
alarmService, sessionRef, cmdId, maxEntitiesPerAlarmSubscription, 10); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void shouldBeOwnerScoped_onlyWhenQueryHasNoEntityFilter() { |
||||
|
assertThat(subCtx.isOwnerScoped()).isFalse(); |
||||
|
|
||||
|
subCtx.setQuery(query(null, 0)); |
||||
|
assertThat(subCtx.isOwnerScoped()).isTrue(); |
||||
|
|
||||
|
subCtx.setQuery(query(new SingleEntityFilter(), 0)); |
||||
|
assertThat(subCtx.isOwnerScoped()).isFalse(); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void shouldSendNothing_whenOwnerScopedRefreshReturnsTheSamePage() { |
||||
|
AlarmDataQuery query = query(null, 0); |
||||
|
subCtx.setQuery(query); |
||||
|
List<AlarmData> alarms = List.of(alarmData(), alarmData()); |
||||
|
subCtx.setAlarms(new PageData<>(alarms, 1, 2, false)); |
||||
|
given(alarmService.findAlarmDataByQuery(eq(tenantId), any(), any(), same(query))) |
||||
|
.willReturn(new PageData<>(new ArrayList<>(alarms), 1, 2, false)); |
||||
|
|
||||
|
subCtx.checkAndResetInvocationCounter(); |
||||
|
|
||||
|
then(alarmService).should().findAlarmDataByQuery(eq(tenantId), any(), any(), same(query)); |
||||
|
then(webSocketService).shouldHaveNoInteractions(); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void shouldSendTheFreshPage_whenOwnerScopedRefreshReturnsAChangedPage() { |
||||
|
AlarmDataQuery query = query(null, 0); |
||||
|
subCtx.setQuery(query); |
||||
|
subCtx.setAlarms(new PageData<>(List.of(alarmData()), 1, 1, false)); |
||||
|
PageData<AlarmData> fresh = new PageData<>(List.of(alarmData(), alarmData()), 1, 2, false); |
||||
|
given(alarmService.findAlarmDataByQuery(eq(tenantId), any(), any(), same(query))).willReturn(fresh); |
||||
|
|
||||
|
subCtx.checkAndResetInvocationCounter(); |
||||
|
|
||||
|
ArgumentCaptor<CmdUpdate> updateCaptor = ArgumentCaptor.forClass(CmdUpdate.class); |
||||
|
then(webSocketService).should().sendUpdate(eq(sessionId), updateCaptor.capture()); |
||||
|
AlarmDataUpdate update = (AlarmDataUpdate) updateCaptor.getValue(); |
||||
|
assertThat(update.getCmdId()).isEqualTo(cmdId); |
||||
|
assertThat(update.getData()).isSameAs(fresh); |
||||
|
assertThat(update.getUpdate()).isNull(); |
||||
|
assertThat(update.getAllowedEntities()).isEqualTo(maxEntitiesPerAlarmSubscription); |
||||
|
assertThat(subCtx.getAlarms()).isSameAs(fresh); |
||||
|
assertThat(subCtx.getAlarmsMap()).hasSize(2); |
||||
|
} |
||||
|
|
||||
|
@Test |
||||
|
public void shouldNotTouchAlarms_whenCleanupCalledWithoutTimeWindow() { |
||||
|
subCtx.setQuery(query(new SingleEntityFilter(), 0)); |
||||
|
|
||||
|
subCtx.cleanupOldAlarms(); |
||||
|
|
||||
|
then(alarmService).shouldHaveNoInteractions(); |
||||
|
then(webSocketService).shouldHaveNoInteractions(); |
||||
|
} |
||||
|
|
||||
|
private static AlarmDataQuery query(SingleEntityFilter entityFilter, long timeWindow) { |
||||
|
AlarmDataPageLink pageLink = new AlarmDataPageLink(10, 0, null, null, false, false, 0, 0, timeWindow, null, null, null, null); |
||||
|
return new AlarmDataQuery(entityFilter, pageLink, null, null, null, null); |
||||
|
} |
||||
|
|
||||
|
private AlarmData alarmData() { |
||||
|
Alarm alarm = new Alarm(new AlarmId(UUID.randomUUID())); |
||||
|
alarm.setOriginator(deviceId); |
||||
|
return new AlarmData(alarm, deviceId); |
||||
|
} |
||||
|
|
||||
|
} |
||||
Loading…
Reference in new issue