Merge pull request #7549 from thingsboard/feature/check_tenant_entity_for_nodes
[3.4.2] Feature/check tenant entity for nodes
This commit is contained in:
		
						commit
						cf73981081
					
				@ -76,6 +76,8 @@ import org.thingsboard.server.dao.tenant.TenantProfileService;
 | 
			
		||||
import org.thingsboard.server.dao.tenant.TenantService;
 | 
			
		||||
import org.thingsboard.server.dao.timeseries.TimeseriesService;
 | 
			
		||||
import org.thingsboard.server.dao.user.UserService;
 | 
			
		||||
import org.thingsboard.server.dao.widget.WidgetTypeService;
 | 
			
		||||
import org.thingsboard.server.dao.widget.WidgetsBundleService;
 | 
			
		||||
import org.thingsboard.server.queue.discovery.PartitionService;
 | 
			
		||||
import org.thingsboard.server.queue.discovery.TbServiceInfoProvider;
 | 
			
		||||
import org.thingsboard.server.queue.util.DataDecodingEncodingService;
 | 
			
		||||
@ -379,6 +381,16 @@ public class ActorSystemContext {
 | 
			
		||||
    @Getter
 | 
			
		||||
    private QueueService queueService;
 | 
			
		||||
 | 
			
		||||
    @Lazy
 | 
			
		||||
    @Autowired(required = false)
 | 
			
		||||
    @Getter
 | 
			
		||||
    private WidgetsBundleService widgetsBundleService;
 | 
			
		||||
 | 
			
		||||
    @Lazy
 | 
			
		||||
    @Autowired(required = false)
 | 
			
		||||
    @Getter
 | 
			
		||||
    private WidgetTypeService widgetTypeService;
 | 
			
		||||
 | 
			
		||||
    @Value("${actors.session.max_concurrent_sessions_per_device:1}")
 | 
			
		||||
    @Getter
 | 
			
		||||
    private long maxConcurrentSessionsPerDevice;
 | 
			
		||||
 | 
			
		||||
@ -26,6 +26,7 @@ import org.thingsboard.common.util.JacksonUtil;
 | 
			
		||||
import org.thingsboard.common.util.ListeningExecutor;
 | 
			
		||||
import org.thingsboard.rule.engine.api.MailService;
 | 
			
		||||
import org.thingsboard.rule.engine.api.RuleEngineAlarmService;
 | 
			
		||||
import org.thingsboard.rule.engine.api.RuleEngineApiUsageStateService;
 | 
			
		||||
import org.thingsboard.rule.engine.api.RuleEngineAssetProfileCache;
 | 
			
		||||
import org.thingsboard.rule.engine.api.RuleEngineDeviceProfileCache;
 | 
			
		||||
import org.thingsboard.rule.engine.api.RuleEngineRpcService;
 | 
			
		||||
@ -35,6 +36,7 @@ import org.thingsboard.rule.engine.api.SmsService;
 | 
			
		||||
import org.thingsboard.rule.engine.api.TbContext;
 | 
			
		||||
import org.thingsboard.rule.engine.api.TbRelationTypes;
 | 
			
		||||
import org.thingsboard.rule.engine.api.sms.SmsSenderFactory;
 | 
			
		||||
import org.thingsboard.rule.engine.util.TenantIdLoader;
 | 
			
		||||
import org.thingsboard.server.actors.ActorSystemContext;
 | 
			
		||||
import org.thingsboard.server.actors.TbActorRef;
 | 
			
		||||
import org.thingsboard.server.cluster.TbClusterService;
 | 
			
		||||
@ -89,6 +91,8 @@ import org.thingsboard.server.dao.rule.RuleChainService;
 | 
			
		||||
import org.thingsboard.server.dao.tenant.TenantService;
 | 
			
		||||
import org.thingsboard.server.dao.timeseries.TimeseriesService;
 | 
			
		||||
import org.thingsboard.server.dao.user.UserService;
 | 
			
		||||
import org.thingsboard.server.dao.widget.WidgetTypeService;
 | 
			
		||||
import org.thingsboard.server.dao.widget.WidgetsBundleService;
 | 
			
		||||
import org.thingsboard.server.gen.transport.TransportProtos;
 | 
			
		||||
import org.thingsboard.server.queue.TbQueueCallback;
 | 
			
		||||
import org.thingsboard.server.queue.TbQueueMsgMetadata;
 | 
			
		||||
@ -751,12 +755,34 @@ class DefaultTbContext implements TbContext {
 | 
			
		||||
        return mainCtx.getTenantProfileCache().get(getTenantId());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public WidgetsBundleService getWidgetBundleService() {
 | 
			
		||||
        return mainCtx.getWidgetsBundleService();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public WidgetTypeService getWidgetTypeService() {
 | 
			
		||||
        return mainCtx.getWidgetTypeService();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public RuleEngineApiUsageStateService getRuleEngineApiUsageStateService() {
 | 
			
		||||
        return mainCtx.getApiUsageStateService();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private TbMsgMetaData getActionMetaData(RuleNodeId ruleNodeId) {
 | 
			
		||||
        TbMsgMetaData metaData = new TbMsgMetaData();
 | 
			
		||||
        metaData.putValue("ruleNodeId", ruleNodeId.toString());
 | 
			
		||||
        return metaData;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void checkTenantEntity(EntityId entityId) {
 | 
			
		||||
        if (!this.getTenantId().equals(TenantIdLoader.findTenantId(this, entityId))) {
 | 
			
		||||
            throw new RuntimeException("Entity with id: '" + entityId + "' specified in the configuration doesn't belong to the current tenant.");
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private class SimpleTbQueueCallback implements TbQueueCallback {
 | 
			
		||||
        private final Runnable onSuccess;
 | 
			
		||||
        private final Consumer<Throwable> onFailure;
 | 
			
		||||
 | 
			
		||||
@ -395,6 +395,11 @@ public class DefaultTbApiUsageStateService extends AbstractPartitionBasedService
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public ApiUsageState findApiUsageStateById(TenantId tenantId, ApiUsageStateId id) {
 | 
			
		||||
        return apiUsageStateService.findApiUsageStateById(tenantId, id);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private interface StateChecker {
 | 
			
		||||
        boolean check(long threshold, long warnThreshold, long value);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
@ -16,7 +16,7 @@
 | 
			
		||||
package org.thingsboard.server.service.apiusage;
 | 
			
		||||
 | 
			
		||||
import org.springframework.context.ApplicationListener;
 | 
			
		||||
import org.thingsboard.server.common.data.ApiUsageState;
 | 
			
		||||
import org.thingsboard.rule.engine.api.RuleEngineApiUsageStateService;
 | 
			
		||||
import org.thingsboard.server.common.data.id.CustomerId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.TenantId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.TenantProfileId;
 | 
			
		||||
@ -26,7 +26,7 @@ import org.thingsboard.server.gen.transport.TransportProtos.ToUsageStatsServiceM
 | 
			
		||||
import org.thingsboard.server.queue.common.TbProtoQueueMsg;
 | 
			
		||||
import org.thingsboard.server.queue.discovery.event.PartitionChangeEvent;
 | 
			
		||||
 | 
			
		||||
public interface TbApiUsageStateService extends TbApiUsageStateClient, ApplicationListener<PartitionChangeEvent> {
 | 
			
		||||
public interface TbApiUsageStateService extends TbApiUsageStateClient, RuleEngineApiUsageStateService, ApplicationListener<PartitionChangeEvent> {
 | 
			
		||||
 | 
			
		||||
    void process(TbProtoQueueMsg<ToUsageStatsServiceMsg> msg, TbCallback callback);
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -19,6 +19,9 @@ import lombok.extern.slf4j.Slf4j;
 | 
			
		||||
import org.springframework.beans.factory.annotation.Autowired;
 | 
			
		||||
import org.springframework.stereotype.Service;
 | 
			
		||||
import org.thingsboard.common.util.ThingsBoardThreadFactory;
 | 
			
		||||
import org.thingsboard.server.common.data.id.RpcId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.TenantId;
 | 
			
		||||
import org.thingsboard.server.common.data.rpc.Rpc;
 | 
			
		||||
import org.thingsboard.server.common.data.rpc.RpcError;
 | 
			
		||||
import org.thingsboard.rule.engine.api.RuleEngineDeviceRpcRequest;
 | 
			
		||||
import org.thingsboard.rule.engine.api.RuleEngineDeviceRpcResponse;
 | 
			
		||||
@ -27,6 +30,7 @@ import org.thingsboard.server.common.msg.queue.ServiceType;
 | 
			
		||||
import org.thingsboard.server.common.msg.queue.TopicPartitionInfo;
 | 
			
		||||
import org.thingsboard.server.common.msg.rpc.FromDeviceRpcResponse;
 | 
			
		||||
import org.thingsboard.server.common.msg.rpc.ToDeviceRpcRequest;
 | 
			
		||||
import org.thingsboard.server.dao.rpc.RpcService;
 | 
			
		||||
import org.thingsboard.server.gen.transport.TransportProtos;
 | 
			
		||||
import org.thingsboard.server.queue.discovery.PartitionService;
 | 
			
		||||
import org.thingsboard.server.queue.discovery.TbServiceInfoProvider;
 | 
			
		||||
@ -52,6 +56,7 @@ public class DefaultTbRuleEngineRpcService implements TbRuleEngineDeviceRpcServi
 | 
			
		||||
    private final PartitionService partitionService;
 | 
			
		||||
    private final TbClusterService clusterService;
 | 
			
		||||
    private final TbServiceInfoProvider serviceInfoProvider;
 | 
			
		||||
    private final RpcService rpcService;
 | 
			
		||||
 | 
			
		||||
    private final ConcurrentMap<UUID, Consumer<FromDeviceRpcResponse>> toDeviceRpcRequests = new ConcurrentHashMap<>();
 | 
			
		||||
 | 
			
		||||
@ -61,10 +66,12 @@ public class DefaultTbRuleEngineRpcService implements TbRuleEngineDeviceRpcServi
 | 
			
		||||
 | 
			
		||||
    public DefaultTbRuleEngineRpcService(PartitionService partitionService,
 | 
			
		||||
                                         TbClusterService clusterService,
 | 
			
		||||
                                         TbServiceInfoProvider serviceInfoProvider) {
 | 
			
		||||
                                         TbServiceInfoProvider serviceInfoProvider,
 | 
			
		||||
                                         RpcService rpcService) {
 | 
			
		||||
        this.partitionService = partitionService;
 | 
			
		||||
        this.clusterService = clusterService;
 | 
			
		||||
        this.serviceInfoProvider = serviceInfoProvider;
 | 
			
		||||
        this.rpcService = rpcService;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Autowired(required = false)
 | 
			
		||||
@ -119,6 +126,11 @@ public class DefaultTbRuleEngineRpcService implements TbRuleEngineDeviceRpcServi
 | 
			
		||||
        });
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public Rpc findRpcById(TenantId tenantId, RpcId id) {
 | 
			
		||||
        return rpcService.findById(tenantId, id);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void processRpcResponseFromDevice(FromDeviceRpcResponse response) {
 | 
			
		||||
        log.trace("[{}] Received response to server-side RPC request from Core RPC Service", response.getId());
 | 
			
		||||
 | 
			
		||||
@ -129,6 +129,11 @@ public class DefaultAlarmSubscriptionService extends AbstractSubscriptionService
 | 
			
		||||
        return alarmService.findAlarmByIdAsync(tenantId, alarmId);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public Alarm findAlarmById(TenantId tenantId, AlarmId alarmId) {
 | 
			
		||||
        return alarmService.findAlarmById(tenantId, alarmId);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public ListenableFuture<AlarmInfo> findAlarmInfoByIdAsync(TenantId tenantId, AlarmId alarmId) {
 | 
			
		||||
        return alarmService.findAlarmInfoByIdAsync(tenantId, alarmId);
 | 
			
		||||
 | 
			
		||||
@ -48,6 +48,8 @@ public interface AlarmService {
 | 
			
		||||
 | 
			
		||||
    ListenableFuture<AlarmOperationResult> clearAlarm(TenantId tenantId, AlarmId alarmId, JsonNode details, long clearTs);
 | 
			
		||||
 | 
			
		||||
    Alarm findAlarmById(TenantId tenantId, AlarmId alarmId);
 | 
			
		||||
 | 
			
		||||
    ListenableFuture<Alarm> findAlarmByIdAsync(TenantId tenantId, AlarmId alarmId);
 | 
			
		||||
 | 
			
		||||
    ListenableFuture<AlarmInfo> findAlarmInfoByIdAsync(TenantId tenantId, AlarmId alarmId);
 | 
			
		||||
 | 
			
		||||
@ -262,9 +262,16 @@ public class BaseAlarmService extends AbstractEntityService implements AlarmServ
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public ListenableFuture<Alarm> findAlarmByIdAsync(TenantId tenantId, AlarmId alarmId) {
 | 
			
		||||
    public Alarm findAlarmById(TenantId tenantId, AlarmId alarmId) {
 | 
			
		||||
        log.trace("Executing findAlarmById [{}]", alarmId);
 | 
			
		||||
        validateId(alarmId, "Incorrect alarmId " + alarmId);
 | 
			
		||||
        return alarmDao.findAlarmById(tenantId, alarmId.getId());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public ListenableFuture<Alarm> findAlarmByIdAsync(TenantId tenantId, AlarmId alarmId) {
 | 
			
		||||
        log.trace("Executing findAlarmByIdAsync [{}]", alarmId);
 | 
			
		||||
        validateId(alarmId, "Incorrect alarmId " + alarmId);
 | 
			
		||||
        return alarmDao.findAlarmByIdAsync(tenantId, alarmId.getId());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -51,6 +51,8 @@ public interface RuleEngineAlarmService {
 | 
			
		||||
 | 
			
		||||
    ListenableFuture<Alarm> findAlarmByIdAsync(TenantId tenantId, AlarmId alarmId);
 | 
			
		||||
 | 
			
		||||
    Alarm findAlarmById(TenantId tenantId, AlarmId alarmId);
 | 
			
		||||
 | 
			
		||||
    ListenableFuture<Alarm> findLatestByOriginatorAndType(TenantId tenantId, EntityId originator, String type);
 | 
			
		||||
 | 
			
		||||
    ListenableFuture<AlarmInfo> findAlarmInfoByIdAsync(TenantId tenantId, AlarmId alarmId);
 | 
			
		||||
 | 
			
		||||
@ -0,0 +1,26 @@
 | 
			
		||||
/**
 | 
			
		||||
 * 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.rule.engine.api;
 | 
			
		||||
 | 
			
		||||
import org.thingsboard.server.common.data.ApiUsageState;
 | 
			
		||||
import org.thingsboard.server.common.data.id.ApiUsageStateId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.TenantId;
 | 
			
		||||
 | 
			
		||||
public interface RuleEngineApiUsageStateService {
 | 
			
		||||
 | 
			
		||||
    ApiUsageState findApiUsageStateById(TenantId tenantId, ApiUsageStateId id);
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -15,7 +15,9 @@
 | 
			
		||||
 */
 | 
			
		||||
package org.thingsboard.rule.engine.api;
 | 
			
		||||
 | 
			
		||||
import org.thingsboard.server.common.data.id.DeviceId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.RpcId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.TenantId;
 | 
			
		||||
import org.thingsboard.server.common.data.rpc.Rpc;
 | 
			
		||||
 | 
			
		||||
import java.util.UUID;
 | 
			
		||||
import java.util.function.Consumer;
 | 
			
		||||
@ -29,4 +31,5 @@ public interface RuleEngineRpcService {
 | 
			
		||||
 | 
			
		||||
    void sendRpcRequestToDevice(RuleEngineDeviceRpcRequest request, Consumer<RuleEngineDeviceRpcResponse> consumer);
 | 
			
		||||
 | 
			
		||||
    Rpc findRpcById(TenantId tenantId, RpcId id);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -62,6 +62,8 @@ import org.thingsboard.server.dao.rule.RuleChainService;
 | 
			
		||||
import org.thingsboard.server.dao.tenant.TenantService;
 | 
			
		||||
import org.thingsboard.server.dao.timeseries.TimeseriesService;
 | 
			
		||||
import org.thingsboard.server.dao.user.UserService;
 | 
			
		||||
import org.thingsboard.server.dao.widget.WidgetTypeService;
 | 
			
		||||
import org.thingsboard.server.dao.widget.WidgetsBundleService;
 | 
			
		||||
 | 
			
		||||
import java.util.List;
 | 
			
		||||
import java.util.Set;
 | 
			
		||||
@ -197,6 +199,8 @@ public interface TbContext {
 | 
			
		||||
     *
 | 
			
		||||
     */
 | 
			
		||||
 | 
			
		||||
    void checkTenantEntity(EntityId entityId);
 | 
			
		||||
 | 
			
		||||
    boolean isLocalEntity(EntityId entityId);
 | 
			
		||||
 | 
			
		||||
    RuleNodeId getSelfId();
 | 
			
		||||
@ -314,4 +318,9 @@ public interface TbContext {
 | 
			
		||||
 | 
			
		||||
    TenantProfile getTenantProfile();
 | 
			
		||||
 | 
			
		||||
    WidgetsBundleService getWidgetBundleService();
 | 
			
		||||
 | 
			
		||||
    WidgetTypeService getWidgetTypeService();
 | 
			
		||||
 | 
			
		||||
    RuleEngineApiUsageStateService getRuleEngineApiUsageStateService();
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -78,6 +78,7 @@ public class TbMsgGeneratorNode implements TbNode {
 | 
			
		||||
        this.currentMsgCount = 0;
 | 
			
		||||
        if (!StringUtils.isEmpty(config.getOriginatorId())) {
 | 
			
		||||
            originatorId = EntityIdFactory.getByTypeAndUuid(config.getOriginatorType(), config.getOriginatorId());
 | 
			
		||||
            ctx.checkTenantEntity(originatorId);
 | 
			
		||||
        } else {
 | 
			
		||||
            originatorId = ctx.getSelfId();
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
@ -55,10 +55,15 @@ import static org.thingsboard.common.util.DonAsynchron.withCallback;
 | 
			
		||||
public class TbCheckRelationNode implements TbNode {
 | 
			
		||||
 | 
			
		||||
    private TbCheckRelationNodeConfiguration config;
 | 
			
		||||
    private EntityId singleEntityId;
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
    public void init(TbContext ctx, TbNodeConfiguration configuration) throws TbNodeException {
 | 
			
		||||
        this.config = TbNodeUtils.convert(configuration, TbCheckRelationNodeConfiguration.class);
 | 
			
		||||
        if (config.isCheckForSingleEntity()) {
 | 
			
		||||
            this.singleEntityId = EntityIdFactory.getByTypeAndId(config.getEntityType(), config.getEntityId());
 | 
			
		||||
            ctx.checkTenantEntity(singleEntityId);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Override
 | 
			
		||||
@ -76,10 +81,10 @@ public class TbCheckRelationNode implements TbNode {
 | 
			
		||||
        EntityId from;
 | 
			
		||||
        EntityId to;
 | 
			
		||||
        if (EntitySearchDirection.FROM.name().equals(config.getDirection())) {
 | 
			
		||||
            from = EntityIdFactory.getByTypeAndId(config.getEntityType(), config.getEntityId());
 | 
			
		||||
            from = singleEntityId;
 | 
			
		||||
            to = msg.getOriginator();
 | 
			
		||||
        } else {
 | 
			
		||||
            to = EntityIdFactory.getByTypeAndId(config.getEntityType(), config.getEntityId());
 | 
			
		||||
            to = singleEntityId;
 | 
			
		||||
            from = msg.getOriginator();
 | 
			
		||||
        }
 | 
			
		||||
        return ctx.getRelationService().checkRelationAsync(ctx.getTenantId(), from, to, config.getRelationType(), RelationTypeGroup.COMMON);
 | 
			
		||||
 | 
			
		||||
@ -1,66 +0,0 @@
 | 
			
		||||
/**
 | 
			
		||||
 * 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.rule.engine.util;
 | 
			
		||||
 | 
			
		||||
import com.google.common.util.concurrent.Futures;
 | 
			
		||||
import com.google.common.util.concurrent.ListenableFuture;
 | 
			
		||||
import com.google.common.util.concurrent.MoreExecutors;
 | 
			
		||||
import org.thingsboard.rule.engine.api.TbContext;
 | 
			
		||||
import org.thingsboard.rule.engine.api.TbNodeException;
 | 
			
		||||
import org.thingsboard.server.common.data.HasTenantId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.AlarmId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.AssetId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.CustomerId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.DeviceId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.EntityId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.RuleChainId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.TenantId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.UserId;
 | 
			
		||||
 | 
			
		||||
public class EntitiesTenantIdAsyncLoader {
 | 
			
		||||
    /**
 | 
			
		||||
     * @deprecated consider to remove since tenantId is already defined in the TbContext.
 | 
			
		||||
     */
 | 
			
		||||
    @Deprecated
 | 
			
		||||
    public static ListenableFuture<TenantId> findEntityIdAsync(TbContext ctx, EntityId original) {
 | 
			
		||||
 | 
			
		||||
        switch (original.getEntityType()) {
 | 
			
		||||
            case TENANT:
 | 
			
		||||
                return Futures.immediateFuture((TenantId) original);
 | 
			
		||||
            case CUSTOMER:
 | 
			
		||||
                return getTenantAsync(ctx.getCustomerService().findCustomerByIdAsync(ctx.getTenantId(), (CustomerId) original));
 | 
			
		||||
            case USER:
 | 
			
		||||
                return getTenantAsync(ctx.getUserService().findUserByIdAsync(ctx.getTenantId(), (UserId) original));
 | 
			
		||||
            case ASSET:
 | 
			
		||||
                return getTenantAsync(ctx.getAssetService().findAssetByIdAsync(ctx.getTenantId(), (AssetId) original));
 | 
			
		||||
            case DEVICE:
 | 
			
		||||
                return getTenantAsync(ctx.getDeviceService().findDeviceByIdAsync(ctx.getTenantId(), (DeviceId) original));
 | 
			
		||||
            case ALARM:
 | 
			
		||||
                return getTenantAsync(ctx.getAlarmService().findAlarmByIdAsync(ctx.getTenantId(), (AlarmId) original));
 | 
			
		||||
            case RULE_CHAIN:
 | 
			
		||||
                return getTenantAsync(ctx.getRuleChainService().findRuleChainByIdAsync(ctx.getTenantId(), (RuleChainId) original));
 | 
			
		||||
            default:
 | 
			
		||||
                return Futures.immediateFailedFuture(new TbNodeException("Unexpected original EntityType " + original.getEntityType()));
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private static <T extends HasTenantId> ListenableFuture<TenantId> getTenantAsync(ListenableFuture<T> future) {
 | 
			
		||||
        return Futures.transformAsync(future, in -> {
 | 
			
		||||
            return in != null ? Futures.immediateFuture(in.getTenantId())
 | 
			
		||||
                    : Futures.immediateFuture(null);
 | 
			
		||||
        }, MoreExecutors.directExecutor());
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
@ -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.rule.engine.util;
 | 
			
		||||
 | 
			
		||||
import org.thingsboard.rule.engine.api.TbContext;
 | 
			
		||||
import org.thingsboard.server.common.data.EntityType;
 | 
			
		||||
import org.thingsboard.server.common.data.HasTenantId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.AlarmId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.ApiUsageStateId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.AssetId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.AssetProfileId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.CustomerId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.DashboardId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.DeviceId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.DeviceProfileId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.EdgeId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.EntityId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.EntityViewId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.OtaPackageId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.QueueId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.RpcId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.RuleChainId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.RuleNodeId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.TbResourceId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.TenantId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.UserId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.WidgetTypeId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.WidgetsBundleId;
 | 
			
		||||
import org.thingsboard.server.common.data.rule.RuleNode;
 | 
			
		||||
 | 
			
		||||
import java.util.UUID;
 | 
			
		||||
 | 
			
		||||
public class TenantIdLoader {
 | 
			
		||||
 | 
			
		||||
    public static TenantId findTenantId(TbContext ctx, EntityId entityId) {
 | 
			
		||||
        UUID id = entityId.getId();
 | 
			
		||||
        EntityType entityType = entityId.getEntityType();
 | 
			
		||||
        TenantId ctxTenantId = ctx.getTenantId();
 | 
			
		||||
 | 
			
		||||
        HasTenantId tenantEntity;
 | 
			
		||||
        switch (entityType) {
 | 
			
		||||
            case TENANT:
 | 
			
		||||
                return new TenantId(id);
 | 
			
		||||
            case CUSTOMER:
 | 
			
		||||
                tenantEntity = ctx.getCustomerService().findCustomerById(ctxTenantId, new CustomerId(id));
 | 
			
		||||
                break;
 | 
			
		||||
            case USER:
 | 
			
		||||
                tenantEntity = ctx.getUserService().findUserById(ctxTenantId, new UserId(id));
 | 
			
		||||
                break;
 | 
			
		||||
            case ASSET:
 | 
			
		||||
                tenantEntity = ctx.getAssetService().findAssetById(ctxTenantId, new AssetId(id));
 | 
			
		||||
                break;
 | 
			
		||||
            case DEVICE:
 | 
			
		||||
                tenantEntity = ctx.getDeviceService().findDeviceById(ctxTenantId, new DeviceId(id));
 | 
			
		||||
                break;
 | 
			
		||||
            case ALARM:
 | 
			
		||||
                tenantEntity = ctx.getAlarmService().findAlarmById(ctxTenantId, new AlarmId(id));
 | 
			
		||||
                break;
 | 
			
		||||
            case RULE_CHAIN:
 | 
			
		||||
                tenantEntity = ctx.getRuleChainService().findRuleChainById(ctxTenantId, new RuleChainId(id));
 | 
			
		||||
                break;
 | 
			
		||||
            case ENTITY_VIEW:
 | 
			
		||||
                tenantEntity = ctx.getEntityViewService().findEntityViewById(ctxTenantId, new EntityViewId(id));
 | 
			
		||||
                break;
 | 
			
		||||
            case DASHBOARD:
 | 
			
		||||
                tenantEntity = ctx.getDashboardService().findDashboardById(ctxTenantId, new DashboardId(id));
 | 
			
		||||
                break;
 | 
			
		||||
            case EDGE:
 | 
			
		||||
                tenantEntity = ctx.getEdgeService().findEdgeById(ctxTenantId, new EdgeId(id));
 | 
			
		||||
                break;
 | 
			
		||||
            case OTA_PACKAGE:
 | 
			
		||||
                tenantEntity = ctx.getOtaPackageService().findOtaPackageInfoById(ctxTenantId, new OtaPackageId(id));
 | 
			
		||||
                break;
 | 
			
		||||
            case ASSET_PROFILE:
 | 
			
		||||
                tenantEntity = ctx.getAssetProfileCache().get(ctxTenantId, new AssetProfileId(id));
 | 
			
		||||
                break;
 | 
			
		||||
            case DEVICE_PROFILE:
 | 
			
		||||
                tenantEntity = ctx.getDeviceProfileCache().get(ctxTenantId, new DeviceProfileId(id));
 | 
			
		||||
                break;
 | 
			
		||||
            case WIDGET_TYPE:
 | 
			
		||||
                tenantEntity = ctx.getWidgetTypeService().findWidgetTypeById(ctxTenantId, new WidgetTypeId(id));
 | 
			
		||||
                break;
 | 
			
		||||
            case WIDGETS_BUNDLE:
 | 
			
		||||
                tenantEntity = ctx.getWidgetBundleService().findWidgetsBundleById(ctxTenantId, new WidgetsBundleId(id));
 | 
			
		||||
                break;
 | 
			
		||||
            case RPC:
 | 
			
		||||
                tenantEntity = ctx.getRpcService().findRpcById(ctxTenantId, new RpcId(id));
 | 
			
		||||
                break;
 | 
			
		||||
            case QUEUE:
 | 
			
		||||
                tenantEntity = ctx.getQueueService().findQueueById(ctxTenantId, new QueueId(id));
 | 
			
		||||
                break;
 | 
			
		||||
            case API_USAGE_STATE:
 | 
			
		||||
                tenantEntity = ctx.getRuleEngineApiUsageStateService().findApiUsageStateById(ctxTenantId, new ApiUsageStateId(id));
 | 
			
		||||
                break;
 | 
			
		||||
            case TB_RESOURCE:
 | 
			
		||||
                tenantEntity = ctx.getResourceService().findResourceInfoById(ctxTenantId, new TbResourceId(id));
 | 
			
		||||
                break;
 | 
			
		||||
            case RULE_NODE:
 | 
			
		||||
                RuleNode ruleNode = ctx.getRuleChainService().findRuleNodeById(ctxTenantId, new RuleNodeId(id));
 | 
			
		||||
                if (ruleNode != null) {
 | 
			
		||||
                    tenantEntity = ctx.getRuleChainService().findRuleChainById(ctxTenantId, ruleNode.getRuleChainId());
 | 
			
		||||
                } else {
 | 
			
		||||
                    tenantEntity = null;
 | 
			
		||||
                }
 | 
			
		||||
                break;
 | 
			
		||||
            case TENANT_PROFILE:
 | 
			
		||||
                if (ctx.getTenantProfile().getId().equals(entityId)) {
 | 
			
		||||
                    return ctxTenantId;
 | 
			
		||||
                } else {
 | 
			
		||||
                    tenantEntity = null;
 | 
			
		||||
                }
 | 
			
		||||
                break;
 | 
			
		||||
            default:
 | 
			
		||||
                throw new RuntimeException("Unexpected entity type: " + entityId.getEntityType());
 | 
			
		||||
        }
 | 
			
		||||
        return tenantEntity != null ? tenantEntity.getTenantId() : null;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
@ -0,0 +1,351 @@
 | 
			
		||||
/**
 | 
			
		||||
 * 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.rule.engine.util;
 | 
			
		||||
 | 
			
		||||
import org.junit.After;
 | 
			
		||||
import org.junit.Assert;
 | 
			
		||||
import org.junit.Before;
 | 
			
		||||
import org.junit.Test;
 | 
			
		||||
import org.junit.runner.RunWith;
 | 
			
		||||
import org.mockito.Mock;
 | 
			
		||||
import org.mockito.junit.MockitoJUnitRunner;
 | 
			
		||||
import org.thingsboard.common.util.AbstractListeningExecutor;
 | 
			
		||||
import org.thingsboard.rule.engine.api.RuleEngineAlarmService;
 | 
			
		||||
import org.thingsboard.rule.engine.api.RuleEngineApiUsageStateService;
 | 
			
		||||
import org.thingsboard.rule.engine.api.RuleEngineAssetProfileCache;
 | 
			
		||||
import org.thingsboard.rule.engine.api.RuleEngineDeviceProfileCache;
 | 
			
		||||
import org.thingsboard.rule.engine.api.RuleEngineRpcService;
 | 
			
		||||
import org.thingsboard.rule.engine.api.TbContext;
 | 
			
		||||
import org.thingsboard.server.common.data.ApiUsageState;
 | 
			
		||||
import org.thingsboard.server.common.data.Customer;
 | 
			
		||||
import org.thingsboard.server.common.data.Dashboard;
 | 
			
		||||
import org.thingsboard.server.common.data.Device;
 | 
			
		||||
import org.thingsboard.server.common.data.DeviceProfile;
 | 
			
		||||
import org.thingsboard.server.common.data.EntityType;
 | 
			
		||||
import org.thingsboard.server.common.data.EntityView;
 | 
			
		||||
import org.thingsboard.server.common.data.OtaPackage;
 | 
			
		||||
import org.thingsboard.server.common.data.TbResource;
 | 
			
		||||
import org.thingsboard.server.common.data.TenantProfile;
 | 
			
		||||
import org.thingsboard.server.common.data.User;
 | 
			
		||||
import org.thingsboard.server.common.data.alarm.Alarm;
 | 
			
		||||
import org.thingsboard.server.common.data.asset.Asset;
 | 
			
		||||
import org.thingsboard.server.common.data.asset.AssetProfile;
 | 
			
		||||
import org.thingsboard.server.common.data.edge.Edge;
 | 
			
		||||
import org.thingsboard.server.common.data.id.AssetProfileId;
 | 
			
		||||
import org.thingsboard.server.common.data.id.DeviceProfileId;
 | 
			
		||||
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.TenantProfileId;
 | 
			
		||||
import org.thingsboard.server.common.data.queue.Queue;
 | 
			
		||||
import org.thingsboard.server.common.data.rpc.Rpc;
 | 
			
		||||
import org.thingsboard.server.common.data.rule.RuleChain;
 | 
			
		||||
import org.thingsboard.server.common.data.rule.RuleNode;
 | 
			
		||||
import org.thingsboard.server.common.data.widget.WidgetType;
 | 
			
		||||
import org.thingsboard.server.common.data.widget.WidgetsBundle;
 | 
			
		||||
import org.thingsboard.server.dao.asset.AssetService;
 | 
			
		||||
import org.thingsboard.server.dao.customer.CustomerService;
 | 
			
		||||
import org.thingsboard.server.dao.dashboard.DashboardService;
 | 
			
		||||
import org.thingsboard.server.dao.device.DeviceService;
 | 
			
		||||
import org.thingsboard.server.dao.edge.EdgeService;
 | 
			
		||||
import org.thingsboard.server.dao.entityview.EntityViewService;
 | 
			
		||||
import org.thingsboard.server.dao.ota.OtaPackageService;
 | 
			
		||||
import org.thingsboard.server.dao.queue.QueueService;
 | 
			
		||||
import org.thingsboard.server.dao.resource.ResourceService;
 | 
			
		||||
import org.thingsboard.server.dao.rule.RuleChainService;
 | 
			
		||||
import org.thingsboard.server.dao.user.UserService;
 | 
			
		||||
import org.thingsboard.server.dao.widget.WidgetTypeService;
 | 
			
		||||
import org.thingsboard.server.dao.widget.WidgetsBundleService;
 | 
			
		||||
 | 
			
		||||
import java.util.UUID;
 | 
			
		||||
 | 
			
		||||
import static org.mockito.ArgumentMatchers.any;
 | 
			
		||||
import static org.mockito.ArgumentMatchers.eq;
 | 
			
		||||
import static org.mockito.Mockito.doReturn;
 | 
			
		||||
import static org.mockito.Mockito.when;
 | 
			
		||||
 | 
			
		||||
@RunWith(MockitoJUnitRunner.class)
 | 
			
		||||
public class TenantIdLoaderTest {
 | 
			
		||||
 | 
			
		||||
    @Mock
 | 
			
		||||
    private TbContext ctx;
 | 
			
		||||
    @Mock
 | 
			
		||||
    private CustomerService customerService;
 | 
			
		||||
    @Mock
 | 
			
		||||
    private UserService userService;
 | 
			
		||||
    @Mock
 | 
			
		||||
    private AssetService assetService;
 | 
			
		||||
    @Mock
 | 
			
		||||
    private DeviceService deviceService;
 | 
			
		||||
    @Mock
 | 
			
		||||
    private RuleEngineAlarmService alarmService;
 | 
			
		||||
    @Mock
 | 
			
		||||
    private RuleChainService ruleChainService;
 | 
			
		||||
    @Mock
 | 
			
		||||
    private EntityViewService entityViewService;
 | 
			
		||||
    @Mock
 | 
			
		||||
    private DashboardService dashboardService;
 | 
			
		||||
    @Mock
 | 
			
		||||
    private EdgeService edgeService;
 | 
			
		||||
    @Mock
 | 
			
		||||
    private OtaPackageService otaPackageService;
 | 
			
		||||
    @Mock
 | 
			
		||||
    private RuleEngineAssetProfileCache assetProfileCache;
 | 
			
		||||
    @Mock
 | 
			
		||||
    private RuleEngineDeviceProfileCache deviceProfileCache;
 | 
			
		||||
    @Mock
 | 
			
		||||
    private WidgetTypeService widgetTypeService;
 | 
			
		||||
    @Mock
 | 
			
		||||
    private WidgetsBundleService widgetsBundleService;
 | 
			
		||||
    @Mock
 | 
			
		||||
    private QueueService queueService;
 | 
			
		||||
    @Mock
 | 
			
		||||
    private ResourceService resourceService;
 | 
			
		||||
    @Mock
 | 
			
		||||
    private RuleEngineRpcService rpcService;
 | 
			
		||||
    @Mock
 | 
			
		||||
    private RuleEngineApiUsageStateService ruleEngineApiUsageStateService;
 | 
			
		||||
 | 
			
		||||
    private TenantId tenantId;
 | 
			
		||||
    private TenantProfileId tenantProfileId;
 | 
			
		||||
    private AbstractListeningExecutor dbExecutor;
 | 
			
		||||
 | 
			
		||||
    @Before
 | 
			
		||||
    public void before() {
 | 
			
		||||
        dbExecutor = new AbstractListeningExecutor() {
 | 
			
		||||
            @Override
 | 
			
		||||
            protected int getThreadPollSize() {
 | 
			
		||||
                return 3;
 | 
			
		||||
            }
 | 
			
		||||
        };
 | 
			
		||||
        dbExecutor.init();
 | 
			
		||||
        this.tenantId = new TenantId(UUID.randomUUID());
 | 
			
		||||
        this.tenantProfileId = new TenantProfileId(UUID.randomUUID());
 | 
			
		||||
 | 
			
		||||
        when(ctx.getTenantId()).thenReturn(tenantId);
 | 
			
		||||
 | 
			
		||||
        for (EntityType entityType : EntityType.values()) {
 | 
			
		||||
            initMocks(entityType, tenantId);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @After
 | 
			
		||||
    public void after() {
 | 
			
		||||
        dbExecutor.destroy();
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void initMocks(EntityType entityType, TenantId tenantId) {
 | 
			
		||||
        switch (entityType) {
 | 
			
		||||
            case TENANT:
 | 
			
		||||
                break;
 | 
			
		||||
            case CUSTOMER:
 | 
			
		||||
                Customer customer = new Customer();
 | 
			
		||||
                customer.setTenantId(tenantId);
 | 
			
		||||
 | 
			
		||||
                when(ctx.getCustomerService()).thenReturn(customerService);
 | 
			
		||||
                doReturn(customer).when(customerService).findCustomerById(eq(tenantId), any());
 | 
			
		||||
 | 
			
		||||
                break;
 | 
			
		||||
            case USER:
 | 
			
		||||
                User user = new User();
 | 
			
		||||
                user.setTenantId(tenantId);
 | 
			
		||||
 | 
			
		||||
                when(ctx.getUserService()).thenReturn(userService);
 | 
			
		||||
                doReturn(user).when(userService).findUserById(eq(tenantId), any());
 | 
			
		||||
 | 
			
		||||
                break;
 | 
			
		||||
            case ASSET:
 | 
			
		||||
                Asset asset = new Asset();
 | 
			
		||||
                asset.setTenantId(tenantId);
 | 
			
		||||
 | 
			
		||||
                when(ctx.getAssetService()).thenReturn(assetService);
 | 
			
		||||
                doReturn(asset).when(assetService).findAssetById(eq(tenantId), any());
 | 
			
		||||
 | 
			
		||||
                break;
 | 
			
		||||
            case DEVICE:
 | 
			
		||||
                Device device = new Device();
 | 
			
		||||
                device.setTenantId(tenantId);
 | 
			
		||||
 | 
			
		||||
                when(ctx.getDeviceService()).thenReturn(deviceService);
 | 
			
		||||
                doReturn(device).when(deviceService).findDeviceById(eq(tenantId), any());
 | 
			
		||||
 | 
			
		||||
                break;
 | 
			
		||||
            case ALARM:
 | 
			
		||||
                Alarm alarm = new Alarm();
 | 
			
		||||
                alarm.setTenantId(tenantId);
 | 
			
		||||
 | 
			
		||||
                when(ctx.getAlarmService()).thenReturn(alarmService);
 | 
			
		||||
                doReturn(alarm).when(alarmService).findAlarmById(eq(tenantId), any());
 | 
			
		||||
 | 
			
		||||
                break;
 | 
			
		||||
            case RULE_CHAIN:
 | 
			
		||||
                RuleChain ruleChain = new RuleChain();
 | 
			
		||||
                ruleChain.setTenantId(tenantId);
 | 
			
		||||
 | 
			
		||||
                when(ctx.getRuleChainService()).thenReturn(ruleChainService);
 | 
			
		||||
                doReturn(ruleChain).when(ruleChainService).findRuleChainById(eq(tenantId), any());
 | 
			
		||||
 | 
			
		||||
                break;
 | 
			
		||||
            case ENTITY_VIEW:
 | 
			
		||||
                EntityView entityView = new EntityView();
 | 
			
		||||
                entityView.setTenantId(tenantId);
 | 
			
		||||
 | 
			
		||||
                when(ctx.getEntityViewService()).thenReturn(entityViewService);
 | 
			
		||||
                doReturn(entityView).when(entityViewService).findEntityViewById(eq(tenantId), any());
 | 
			
		||||
 | 
			
		||||
                break;
 | 
			
		||||
            case DASHBOARD:
 | 
			
		||||
                Dashboard dashboard = new Dashboard();
 | 
			
		||||
                dashboard.setTenantId(tenantId);
 | 
			
		||||
 | 
			
		||||
                when(ctx.getDashboardService()).thenReturn(dashboardService);
 | 
			
		||||
                doReturn(dashboard).when(dashboardService).findDashboardById(eq(tenantId), any());
 | 
			
		||||
 | 
			
		||||
                break;
 | 
			
		||||
            case EDGE:
 | 
			
		||||
                Edge edge = new Edge();
 | 
			
		||||
                edge.setTenantId(tenantId);
 | 
			
		||||
 | 
			
		||||
                when(ctx.getEdgeService()).thenReturn(edgeService);
 | 
			
		||||
                doReturn(edge).when(edgeService).findEdgeById(eq(tenantId), any());
 | 
			
		||||
 | 
			
		||||
                break;
 | 
			
		||||
            case OTA_PACKAGE:
 | 
			
		||||
                OtaPackage otaPackage = new OtaPackage();
 | 
			
		||||
                otaPackage.setTenantId(tenantId);
 | 
			
		||||
 | 
			
		||||
                when(ctx.getOtaPackageService()).thenReturn(otaPackageService);
 | 
			
		||||
                doReturn(otaPackage).when(otaPackageService).findOtaPackageInfoById(eq(tenantId), any());
 | 
			
		||||
 | 
			
		||||
                break;
 | 
			
		||||
            case ASSET_PROFILE:
 | 
			
		||||
                AssetProfile assetProfile = new AssetProfile();
 | 
			
		||||
                assetProfile.setTenantId(tenantId);
 | 
			
		||||
 | 
			
		||||
                when(ctx.getAssetProfileCache()).thenReturn(assetProfileCache);
 | 
			
		||||
                doReturn(assetProfile).when(assetProfileCache).get(eq(tenantId), any(AssetProfileId.class));
 | 
			
		||||
 | 
			
		||||
                break;
 | 
			
		||||
            case DEVICE_PROFILE:
 | 
			
		||||
                DeviceProfile deviceProfile = new DeviceProfile();
 | 
			
		||||
                deviceProfile.setTenantId(tenantId);
 | 
			
		||||
 | 
			
		||||
                when(ctx.getDeviceProfileCache()).thenReturn(deviceProfileCache);
 | 
			
		||||
                doReturn(deviceProfile).when(deviceProfileCache).get(eq(tenantId), any(DeviceProfileId.class));
 | 
			
		||||
 | 
			
		||||
                break;
 | 
			
		||||
            case WIDGET_TYPE:
 | 
			
		||||
                WidgetType widgetType = new WidgetType();
 | 
			
		||||
                widgetType.setTenantId(tenantId);
 | 
			
		||||
 | 
			
		||||
                when(ctx.getWidgetTypeService()).thenReturn(widgetTypeService);
 | 
			
		||||
                doReturn(widgetType).when(widgetTypeService).findWidgetTypeById(eq(tenantId), any());
 | 
			
		||||
 | 
			
		||||
                break;
 | 
			
		||||
            case WIDGETS_BUNDLE:
 | 
			
		||||
                WidgetsBundle widgetsBundle = new WidgetsBundle();
 | 
			
		||||
                widgetsBundle.setTenantId(tenantId);
 | 
			
		||||
 | 
			
		||||
                when(ctx.getWidgetBundleService()).thenReturn(widgetsBundleService);
 | 
			
		||||
                doReturn(widgetsBundle).when(widgetsBundleService).findWidgetsBundleById(eq(tenantId), any());
 | 
			
		||||
 | 
			
		||||
                break;
 | 
			
		||||
            case RPC:
 | 
			
		||||
                Rpc rpc = new Rpc();
 | 
			
		||||
                rpc.setTenantId(tenantId);
 | 
			
		||||
 | 
			
		||||
                when(ctx.getRpcService()).thenReturn(rpcService);
 | 
			
		||||
                doReturn(rpc).when(rpcService).findRpcById(eq(tenantId), any());
 | 
			
		||||
 | 
			
		||||
                break;
 | 
			
		||||
            case QUEUE:
 | 
			
		||||
                Queue queue = new Queue();
 | 
			
		||||
                queue.setTenantId(tenantId);
 | 
			
		||||
 | 
			
		||||
                when(ctx.getQueueService()).thenReturn(queueService);
 | 
			
		||||
                doReturn(queue).when(queueService).findQueueById(eq(tenantId), any());
 | 
			
		||||
 | 
			
		||||
                break;
 | 
			
		||||
            case API_USAGE_STATE:
 | 
			
		||||
                ApiUsageState apiUsageState = new ApiUsageState();
 | 
			
		||||
                apiUsageState.setTenantId(tenantId);
 | 
			
		||||
 | 
			
		||||
                when(ctx.getRuleEngineApiUsageStateService()).thenReturn(ruleEngineApiUsageStateService);
 | 
			
		||||
                doReturn(apiUsageState).when(ruleEngineApiUsageStateService).findApiUsageStateById(eq(tenantId), any());
 | 
			
		||||
 | 
			
		||||
                break;
 | 
			
		||||
            case TB_RESOURCE:
 | 
			
		||||
                TbResource tbResource = new TbResource();
 | 
			
		||||
                tbResource.setTenantId(tenantId);
 | 
			
		||||
 | 
			
		||||
                when(ctx.getResourceService()).thenReturn(resourceService);
 | 
			
		||||
                doReturn(tbResource).when(resourceService).findResourceInfoById(eq(tenantId), any());
 | 
			
		||||
 | 
			
		||||
                break;
 | 
			
		||||
            case RULE_NODE:
 | 
			
		||||
                RuleNode ruleNode = new RuleNode();
 | 
			
		||||
 | 
			
		||||
                when(ctx.getRuleChainService()).thenReturn(ruleChainService);
 | 
			
		||||
                doReturn(ruleNode).when(ruleChainService).findRuleNodeById(eq(tenantId), any());
 | 
			
		||||
 | 
			
		||||
                break;
 | 
			
		||||
            case TENANT_PROFILE:
 | 
			
		||||
                TenantProfile tenantProfile = new TenantProfile(tenantProfileId);
 | 
			
		||||
 | 
			
		||||
                when(ctx.getTenantProfile()).thenReturn(tenantProfile);
 | 
			
		||||
 | 
			
		||||
                break;
 | 
			
		||||
            default:
 | 
			
		||||
                throw new RuntimeException("Unexpected original EntityType " + entityType);
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private EntityId getEntityId(EntityType entityType) {
 | 
			
		||||
        return EntityIdFactory.getByTypeAndUuid(entityType, UUID.randomUUID());
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    private void checkTenant(TenantId checkTenantId, boolean equals) {
 | 
			
		||||
        for (EntityType entityType : EntityType.values()) {
 | 
			
		||||
            EntityId entityId;
 | 
			
		||||
            if (EntityType.TENANT.equals(entityType)) {
 | 
			
		||||
                entityId = tenantId;
 | 
			
		||||
            } else if (EntityType.TENANT_PROFILE.equals(entityType)) {
 | 
			
		||||
                entityId = tenantProfileId;
 | 
			
		||||
            } else {
 | 
			
		||||
                entityId = getEntityId(entityType);
 | 
			
		||||
            }
 | 
			
		||||
            TenantId targetTenantId = TenantIdLoader.findTenantId(ctx, entityId);
 | 
			
		||||
            String msg = "Check entity type <" + entityType.name() + ">:";
 | 
			
		||||
            if (equals) {
 | 
			
		||||
                Assert.assertEquals(msg, targetTenantId, checkTenantId);
 | 
			
		||||
            } else {
 | 
			
		||||
                Assert.assertNotEquals(msg, targetTenantId, checkTenantId);
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void test_findEntityIdAsync_current_tenant() {
 | 
			
		||||
        checkTenant(tenantId, true);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    @Test
 | 
			
		||||
    public void test_findEntityIdAsync_other_tenant() {
 | 
			
		||||
        checkTenant(new TenantId(UUID.randomUUID()), false);
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
}
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user