Browse Source

Fix TenantIdLoaderTest

pull/14074/head
Andrii Landiak 1 year ago
parent
commit
74cd211a4e
  1. 1
      rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/util/TenantIdLoader.java
  2. 48
      rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/util/TenantIdLoaderTest.java

1
rule-engine/rule-engine-components/src/main/java/org/thingsboard/rule/engine/util/TenantIdLoader.java

@ -148,6 +148,7 @@ public class TenantIdLoader {
break; break;
case NOTIFICATION: case NOTIFICATION:
case ADMIN_SETTINGS: case ADMIN_SETTINGS:
case API_KEY:
return ctxTenantId; return ctxTenantId;
case NOTIFICATION_RULE: case NOTIFICATION_RULE:
tenantEntity = ctx.getNotificationRuleService().findNotificationRuleById(ctxTenantId, new NotificationRuleId(id)); tenantEntity = ctx.getNotificationRuleService().findNotificationRuleById(ctxTenantId, new NotificationRuleId(id));

48
rule-engine/rule-engine-components/src/test/java/org/thingsboard/rule/engine/util/TenantIdLoaderTest.java

@ -29,7 +29,6 @@ import org.thingsboard.rule.engine.api.RuleEngineAssetProfileCache;
import org.thingsboard.rule.engine.api.RuleEngineDeviceProfileCache; import org.thingsboard.rule.engine.api.RuleEngineDeviceProfileCache;
import org.thingsboard.rule.engine.api.RuleEngineRpcService; import org.thingsboard.rule.engine.api.RuleEngineRpcService;
import org.thingsboard.rule.engine.api.TbContext; import org.thingsboard.rule.engine.api.TbContext;
import org.thingsboard.server.common.data.AdminSettings;
import org.thingsboard.server.common.data.ApiUsageState; import org.thingsboard.server.common.data.ApiUsageState;
import org.thingsboard.server.common.data.Customer; import org.thingsboard.server.common.data.Customer;
import org.thingsboard.server.common.data.Dashboard; import org.thingsboard.server.common.data.Dashboard;
@ -53,7 +52,6 @@ import org.thingsboard.server.common.data.id.AssetProfileId;
import org.thingsboard.server.common.data.id.DeviceProfileId; import org.thingsboard.server.common.data.id.DeviceProfileId;
import org.thingsboard.server.common.data.id.EntityId; import org.thingsboard.server.common.data.id.EntityId;
import org.thingsboard.server.common.data.id.EntityIdFactory; import org.thingsboard.server.common.data.id.EntityIdFactory;
import org.thingsboard.server.common.data.id.NotificationId;
import org.thingsboard.server.common.data.id.TenantId; import org.thingsboard.server.common.data.id.TenantId;
import org.thingsboard.server.common.data.id.TenantProfileId; import org.thingsboard.server.common.data.id.TenantProfileId;
import org.thingsboard.server.common.data.job.Job; import org.thingsboard.server.common.data.job.Job;
@ -80,6 +78,7 @@ import org.thingsboard.server.dao.device.DeviceService;
import org.thingsboard.server.dao.domain.DomainService; import org.thingsboard.server.dao.domain.DomainService;
import org.thingsboard.server.dao.edge.EdgeService; import org.thingsboard.server.dao.edge.EdgeService;
import org.thingsboard.server.dao.entityview.EntityViewService; import org.thingsboard.server.dao.entityview.EntityViewService;
import org.thingsboard.server.dao.job.JobService;
import org.thingsboard.server.dao.mobile.MobileAppBundleService; import org.thingsboard.server.dao.mobile.MobileAppBundleService;
import org.thingsboard.server.dao.mobile.MobileAppService; import org.thingsboard.server.dao.mobile.MobileAppService;
import org.thingsboard.server.dao.notification.NotificationRequestService; import org.thingsboard.server.dao.notification.NotificationRequestService;
@ -88,11 +87,11 @@ import org.thingsboard.server.dao.notification.NotificationTargetService;
import org.thingsboard.server.dao.notification.NotificationTemplateService; import org.thingsboard.server.dao.notification.NotificationTemplateService;
import org.thingsboard.server.dao.oauth2.OAuth2ClientService; import org.thingsboard.server.dao.oauth2.OAuth2ClientService;
import org.thingsboard.server.dao.ota.OtaPackageService; import org.thingsboard.server.dao.ota.OtaPackageService;
import org.thingsboard.server.dao.pat.ApiKeyService;
import org.thingsboard.server.dao.queue.QueueService; import org.thingsboard.server.dao.queue.QueueService;
import org.thingsboard.server.dao.queue.QueueStatsService; import org.thingsboard.server.dao.queue.QueueStatsService;
import org.thingsboard.server.dao.resource.ResourceService; import org.thingsboard.server.dao.resource.ResourceService;
import org.thingsboard.server.dao.rule.RuleChainService; import org.thingsboard.server.dao.rule.RuleChainService;
import org.thingsboard.server.dao.job.JobService;
import org.thingsboard.server.dao.user.UserService; import org.thingsboard.server.dao.user.UserService;
import org.thingsboard.server.dao.widget.WidgetTypeService; import org.thingsboard.server.dao.widget.WidgetTypeService;
import org.thingsboard.server.dao.widget.WidgetsBundleService; import org.thingsboard.server.dao.widget.WidgetsBundleService;
@ -170,6 +169,8 @@ public class TenantIdLoaderTest {
private JobService jobService; private JobService jobService;
@Mock @Mock
private AiModelService aiModelService; private AiModelService aiModelService;
@Mock
private ApiKeyService apiKeyService;
private TenantId tenantId; private TenantId tenantId;
private TenantProfileId tenantProfileId; private TenantProfileId tenantProfileId;
@ -204,163 +205,124 @@ public class TenantIdLoaderTest {
case TENANT: case TENANT:
case NOTIFICATION: case NOTIFICATION:
case ADMIN_SETTINGS: case ADMIN_SETTINGS:
case API_KEY:
break; break;
case CUSTOMER: case CUSTOMER:
Customer customer = new Customer(); Customer customer = new Customer();
customer.setTenantId(tenantId); customer.setTenantId(tenantId);
when(ctx.getCustomerService()).thenReturn(customerService); when(ctx.getCustomerService()).thenReturn(customerService);
doReturn(customer).when(customerService).findCustomerById(eq(tenantId), any()); doReturn(customer).when(customerService).findCustomerById(eq(tenantId), any());
break; break;
case USER: case USER:
User user = new User(); User user = new User();
user.setTenantId(tenantId); user.setTenantId(tenantId);
when(ctx.getUserService()).thenReturn(userService); when(ctx.getUserService()).thenReturn(userService);
doReturn(user).when(userService).findUserById(eq(tenantId), any()); doReturn(user).when(userService).findUserById(eq(tenantId), any());
break; break;
case ASSET: case ASSET:
Asset asset = new Asset(); Asset asset = new Asset();
asset.setTenantId(tenantId); asset.setTenantId(tenantId);
when(ctx.getAssetService()).thenReturn(assetService); when(ctx.getAssetService()).thenReturn(assetService);
doReturn(asset).when(assetService).findAssetById(eq(tenantId), any()); doReturn(asset).when(assetService).findAssetById(eq(tenantId), any());
break; break;
case DEVICE: case DEVICE:
Device device = new Device(); Device device = new Device();
device.setTenantId(tenantId); device.setTenantId(tenantId);
when(ctx.getDeviceService()).thenReturn(deviceService); when(ctx.getDeviceService()).thenReturn(deviceService);
doReturn(device).when(deviceService).findDeviceById(eq(tenantId), any()); doReturn(device).when(deviceService).findDeviceById(eq(tenantId), any());
break; break;
case ALARM: case ALARM:
Alarm alarm = new Alarm(); Alarm alarm = new Alarm();
alarm.setTenantId(tenantId); alarm.setTenantId(tenantId);
when(ctx.getAlarmService()).thenReturn(alarmService); when(ctx.getAlarmService()).thenReturn(alarmService);
doReturn(alarm).when(alarmService).findAlarmById(eq(tenantId), any()); doReturn(alarm).when(alarmService).findAlarmById(eq(tenantId), any());
break; break;
case RULE_CHAIN: case RULE_CHAIN:
RuleChain ruleChain = new RuleChain(); RuleChain ruleChain = new RuleChain();
ruleChain.setTenantId(tenantId); ruleChain.setTenantId(tenantId);
when(ctx.getRuleChainService()).thenReturn(ruleChainService); when(ctx.getRuleChainService()).thenReturn(ruleChainService);
doReturn(ruleChain).when(ruleChainService).findRuleChainById(eq(tenantId), any()); doReturn(ruleChain).when(ruleChainService).findRuleChainById(eq(tenantId), any());
break; break;
case ENTITY_VIEW: case ENTITY_VIEW:
EntityView entityView = new EntityView(); EntityView entityView = new EntityView();
entityView.setTenantId(tenantId); entityView.setTenantId(tenantId);
when(ctx.getEntityViewService()).thenReturn(entityViewService); when(ctx.getEntityViewService()).thenReturn(entityViewService);
doReturn(entityView).when(entityViewService).findEntityViewById(eq(tenantId), any()); doReturn(entityView).when(entityViewService).findEntityViewById(eq(tenantId), any());
break; break;
case DASHBOARD: case DASHBOARD:
Dashboard dashboard = new Dashboard(); Dashboard dashboard = new Dashboard();
dashboard.setTenantId(tenantId); dashboard.setTenantId(tenantId);
when(ctx.getDashboardService()).thenReturn(dashboardService); when(ctx.getDashboardService()).thenReturn(dashboardService);
doReturn(dashboard).when(dashboardService).findDashboardById(eq(tenantId), any()); doReturn(dashboard).when(dashboardService).findDashboardById(eq(tenantId), any());
break; break;
case EDGE: case EDGE:
Edge edge = new Edge(); Edge edge = new Edge();
edge.setTenantId(tenantId); edge.setTenantId(tenantId);
when(ctx.getEdgeService()).thenReturn(edgeService); when(ctx.getEdgeService()).thenReturn(edgeService);
doReturn(edge).when(edgeService).findEdgeById(eq(tenantId), any()); doReturn(edge).when(edgeService).findEdgeById(eq(tenantId), any());
break; break;
case OTA_PACKAGE: case OTA_PACKAGE:
OtaPackage otaPackage = new OtaPackage(); OtaPackage otaPackage = new OtaPackage();
otaPackage.setTenantId(tenantId); otaPackage.setTenantId(tenantId);
when(ctx.getOtaPackageService()).thenReturn(otaPackageService); when(ctx.getOtaPackageService()).thenReturn(otaPackageService);
doReturn(otaPackage).when(otaPackageService).findOtaPackageInfoById(eq(tenantId), any()); doReturn(otaPackage).when(otaPackageService).findOtaPackageInfoById(eq(tenantId), any());
break; break;
case ASSET_PROFILE: case ASSET_PROFILE:
AssetProfile assetProfile = new AssetProfile(); AssetProfile assetProfile = new AssetProfile();
assetProfile.setTenantId(tenantId); assetProfile.setTenantId(tenantId);
when(ctx.getAssetProfileCache()).thenReturn(assetProfileCache); when(ctx.getAssetProfileCache()).thenReturn(assetProfileCache);
doReturn(assetProfile).when(assetProfileCache).get(eq(tenantId), any(AssetProfileId.class)); doReturn(assetProfile).when(assetProfileCache).get(eq(tenantId), any(AssetProfileId.class));
break; break;
case DEVICE_PROFILE: case DEVICE_PROFILE:
DeviceProfile deviceProfile = new DeviceProfile(); DeviceProfile deviceProfile = new DeviceProfile();
deviceProfile.setTenantId(tenantId); deviceProfile.setTenantId(tenantId);
when(ctx.getDeviceProfileCache()).thenReturn(deviceProfileCache); when(ctx.getDeviceProfileCache()).thenReturn(deviceProfileCache);
doReturn(deviceProfile).when(deviceProfileCache).get(eq(tenantId), any(DeviceProfileId.class)); doReturn(deviceProfile).when(deviceProfileCache).get(eq(tenantId), any(DeviceProfileId.class));
break; break;
case WIDGET_TYPE: case WIDGET_TYPE:
WidgetType widgetType = new WidgetType(); WidgetType widgetType = new WidgetType();
widgetType.setTenantId(tenantId); widgetType.setTenantId(tenantId);
when(ctx.getWidgetTypeService()).thenReturn(widgetTypeService); when(ctx.getWidgetTypeService()).thenReturn(widgetTypeService);
doReturn(widgetType).when(widgetTypeService).findWidgetTypeById(eq(tenantId), any()); doReturn(widgetType).when(widgetTypeService).findWidgetTypeById(eq(tenantId), any());
break; break;
case WIDGETS_BUNDLE: case WIDGETS_BUNDLE:
WidgetsBundle widgetsBundle = new WidgetsBundle(); WidgetsBundle widgetsBundle = new WidgetsBundle();
widgetsBundle.setTenantId(tenantId); widgetsBundle.setTenantId(tenantId);
when(ctx.getWidgetBundleService()).thenReturn(widgetsBundleService); when(ctx.getWidgetBundleService()).thenReturn(widgetsBundleService);
doReturn(widgetsBundle).when(widgetsBundleService).findWidgetsBundleById(eq(tenantId), any()); doReturn(widgetsBundle).when(widgetsBundleService).findWidgetsBundleById(eq(tenantId), any());
break; break;
case RPC: case RPC:
Rpc rpc = new Rpc(); Rpc rpc = new Rpc();
rpc.setTenantId(tenantId); rpc.setTenantId(tenantId);
when(ctx.getRpcService()).thenReturn(rpcService); when(ctx.getRpcService()).thenReturn(rpcService);
doReturn(rpc).when(rpcService).findRpcById(eq(tenantId), any()); doReturn(rpc).when(rpcService).findRpcById(eq(tenantId), any());
break; break;
case QUEUE: case QUEUE:
Queue queue = new Queue(); Queue queue = new Queue();
queue.setTenantId(tenantId); queue.setTenantId(tenantId);
when(ctx.getQueueService()).thenReturn(queueService); when(ctx.getQueueService()).thenReturn(queueService);
doReturn(queue).when(queueService).findQueueById(eq(tenantId), any()); doReturn(queue).when(queueService).findQueueById(eq(tenantId), any());
break; break;
case API_USAGE_STATE: case API_USAGE_STATE:
ApiUsageState apiUsageState = new ApiUsageState(); ApiUsageState apiUsageState = new ApiUsageState();
apiUsageState.setTenantId(tenantId); apiUsageState.setTenantId(tenantId);
when(ctx.getRuleEngineApiUsageStateService()).thenReturn(ruleEngineApiUsageStateService); when(ctx.getRuleEngineApiUsageStateService()).thenReturn(ruleEngineApiUsageStateService);
doReturn(apiUsageState).when(ruleEngineApiUsageStateService).findApiUsageStateById(eq(tenantId), any()); doReturn(apiUsageState).when(ruleEngineApiUsageStateService).findApiUsageStateById(eq(tenantId), any());
break; break;
case TB_RESOURCE: case TB_RESOURCE:
TbResource tbResource = new TbResource(); TbResource tbResource = new TbResource();
tbResource.setTenantId(tenantId); tbResource.setTenantId(tenantId);
when(ctx.getResourceService()).thenReturn(resourceService); when(ctx.getResourceService()).thenReturn(resourceService);
doReturn(tbResource).when(resourceService).findResourceInfoById(eq(tenantId), any()); doReturn(tbResource).when(resourceService).findResourceInfoById(eq(tenantId), any());
break; break;
case RULE_NODE: case RULE_NODE:
RuleNode ruleNode = new RuleNode(); RuleNode ruleNode = new RuleNode();
when(ctx.getRuleChainService()).thenReturn(ruleChainService); when(ctx.getRuleChainService()).thenReturn(ruleChainService);
doReturn(ruleNode).when(ruleChainService).findRuleNodeById(eq(tenantId), any()); doReturn(ruleNode).when(ruleChainService).findRuleNodeById(eq(tenantId), any());
break; break;
case TENANT_PROFILE: case TENANT_PROFILE:
TenantProfile tenantProfile = new TenantProfile(tenantProfileId); TenantProfile tenantProfile = new TenantProfile(tenantProfileId);
when(ctx.getTenantProfile()).thenReturn(tenantProfile); when(ctx.getTenantProfile()).thenReturn(tenantProfile);
break; break;
case NOTIFICATION_TARGET: case NOTIFICATION_TARGET:
NotificationTarget notificationTarget = new NotificationTarget(); NotificationTarget notificationTarget = new NotificationTarget();

Loading…
Cancel
Save