HasRuleEngineProfile getRuleEngineProfileForEntityOrElseNull(TenantId tenantId, EntityId entityId, TbMsg tbMsg) {
        if (entityId.getEntityType().equals(EntityType.DEVICE)) {
            if (TbMsgType.ENTITY_DELETED.equals(tbMsg.getInternalType())) {
                try {
                    Device deletedDevice = JacksonUtil.fromString(tbMsg.getData(), Device.class);
                    if (deletedDevice == null) {
                        return null;
                    }
                    return deviceProfileCache.get(tenantId, deletedDevice.getDeviceProfileId());
                } catch (Exception e) {
                    log.warn("[{}][{}] Failed to deserialize device: {}", tenantId, entityId, tbMsg, e);
                    return null;
                }
            } else {
                return deviceProfileCache.get(tenantId, new DeviceId(entityId.getId()));
            }
        } else if (entityId.getEntityType().equals(EntityType.DEVICE_PROFILE)) {
            return deviceProfileCache.get(tenantId, new DeviceProfileId(entityId.getId()));
        } else if (entityId.getEntityType().equals(EntityType.ASSET)) {
            if (TbMsgType.ENTITY_DELETED.equals(tbMsg.getInternalType())) {
                try {
                    Asset deletedAsset = JacksonUtil.fromString(tbMsg.getData(), Asset.class);
                    if (deletedAsset == null) {
                        return null;
                    }
                    return assetProfileCache.get(tenantId, deletedAsset.getAssetProfileId());
                } catch (Exception e) {
                    log.warn("[{}][{}] Failed to deserialize asset: {}", tenantId, entityId, tbMsg, e);
                    return null;
                }
            } else {
                return assetProfileCache.get(tenantId, new AssetId(entityId.getId()));
            }
        } else if (entityId.getEntityType().equals(EntityType.ASSET_PROFILE)) {
            return assetProfileCache.get(tenantId, new AssetProfileId(entityId.getId()));
        }
        return null;
    }
@Test
    public void testGetRuleEngineProfileForUpdatedAndDeletedDevice() {
        DeviceId deviceId = new DeviceId(UUID.randomUUID());
        TenantId tenantId = new TenantId(UUID.randomUUID());
        DeviceProfileId deviceProfileId = new DeviceProfileId(UUID.randomUUID());

        Device device = new Device(deviceId);
        device.setDeviceProfileId(deviceProfileId);

        // device updated
        TbMsg tbMsg = TbMsg.builder().internalType(TbMsgType.ENTITY_UPDATED).build();
        ((DefaultTbClusterService) clusterService).getRuleEngineProfileForEntityOrElseNull(tenantId, deviceId, tbMsg);
        verify(deviceProfileCache, times(1)).get(tenantId, deviceId);

        // device deleted
        tbMsg = TbMsg.builder().internalType(TbMsgType.ENTITY_DELETED).data(JacksonUtil.toString(device)).build();
        ((DefaultTbClusterService) clusterService).getRuleEngineProfileForEntityOrElseNull(tenantId, deviceId, tbMsg);
        verify(deviceProfileCache, times(1)).get(tenantId, deviceProfileId);
    }