public void poll()
        {
            InitializeDocumentDescriptor command = null;

            _commandBus.When(c => c.Send(Arg.Any <InitializeDocumentDescriptor>(), Arg.Any <string>()))
            .Do(callInfo => command = (InitializeDocumentDescriptor)callInfo.Args()[0]);

            _queue.PollFileSystem();

            // asserts
            _blobstore.Received().Upload(Arg.Is(_originalFormat), Arg.Any <FileNameWithExtension>(), Arg.Any <Stream>());

            Assert.NotNull(command);
            Assert.AreEqual(_blobId, command.BlobId);
            Assert.NotNull(command.HandleInfo);
            Assert.AreEqual(_documentHandle, command.HandleInfo.Handle);
        }
        private void CreateDocument(
            DocumentDescriptorId documentDescriptorId,
            BlobId blobId,
            DocumentHandle handle,
            DocumentHandle fatherHandle,
            DocumentDescriptorId fatherDocumentDescriptorId,
            FileNameWithExtension fileName,
            DocumentCustomData customData
            )
        {
            var      descriptor = _blobStore.GetDescriptor(blobId);
            ICommand createDocument;
            var      handleInfo = new DocumentHandleInfo(handle, fileName, customData);

            if (fatherHandle == null)
            {
                if (Logger.IsDebugEnabled)
                {
                    Logger.DebugFormat("Initialize DocumentDescriptor {0} ", documentDescriptorId);
                }

                createDocument = new InitializeDocumentDescriptor(documentDescriptorId, blobId, handleInfo, descriptor.Hash, fileName);
            }
            else
            {
                if (Logger.IsDebugEnabled)
                {
                    Logger.DebugFormat("Initialize DocumentDescriptor as attach {0} ", documentDescriptorId);
                }

                createDocument = new InitializeDocumentDescriptorAsAttach(
                    documentDescriptorId,
                    blobId,
                    handleInfo,
                    fatherHandle,
                    fatherDocumentDescriptorId,
                    descriptor.Hash, fileName);
            }
            CommandBus.Send(createDocument, "api");
        }
        internal void UploadFile(String jobFile, DocumentImportTask task)
        {
            String fname = "";

            try
            {
                TenantContext.Enter(task.Tenant);

                if (!task.Uri.IsFile)
                {
                    LogAndThrow("Error importing task file {0}: Uri is not a file: {1}", jobFile, task.Uri);
                }

                fname = task.Uri.LocalPath;

                if (FileHasImportFailureMarker(fname, task.FileTimestamp))
                {
                    return;
                }

                if (!File.Exists(fname))
                {
                    LogAndThrow("Error importing task file {0}: File missing: {1}", jobFile, fname);
                }

                var blobStore         = GetBlobStoreForTenant();
                var identityGenerator = GetIdentityGeneratorForTenant();
                if (blobStore == null || identityGenerator == null)
                {
                    Logger.ErrorFormat("Tenant {1} not found or not configured for file: {1}", task.Tenant, fname);
                    return;
                }

                BlobId blobId;
                if (!String.IsNullOrEmpty(task.FileName))
                {
                    //use the real file name from the task not the name of the file
                    using (FileStream fs = File.Open(fname, FileMode.Open, FileAccess.Read))
                    {
                        blobId = blobStore.Upload(task.Format, new FileNameWithExtension(task.FileName), fs);
                    }
                }
                else
                {
                    //No filename given in task, use name of the blob
                    blobId = blobStore.Upload(task.Format, fname);
                }

                if (task.Format == OriginalFormat)
                {
                    var descriptor = blobStore.GetDescriptor(blobId);
                    var fileName   = new FileNameWithExtension(task.FileName);
                    var handleInfo = new DocumentHandleInfo(task.Handle, fileName, task.CustomData);
                    var documentId = identityGenerator.New <DocumentDescriptorId>();

                    var createDocument = new InitializeDocumentDescriptor(
                        documentId,
                        blobId,
                        handleInfo,
                        descriptor.Hash,
                        fileName
                        );
                    _commandBus.Send(createDocument, "import-from-file");
                }
                else
                {
                    var reader     = _tenantAccessor.Current.Container.Resolve <IDocumentWriter>();
                    var handle     = reader.FindOneById(task.Handle);
                    var documentId = handle.DocumentDescriptorId;

                    var command = new AddFormatToDocumentDescriptor(
                        documentId,
                        task.Format,
                        blobId,
                        new PipelineId("user-content")
                        );
                    _commandBus.Send(command, "import-from-file");
                }

                TaskExecuted(task);
                DeleteImportFailure(fname);
            }
            catch (Exception ex)
            {
                Logger.ErrorFormat(ex, "Job Import Queue - Error importing {0} - {1}", jobFile, ex.Message);
                ImportFailure failure = new ImportFailure()
                {
                    Error     = ex.ToString(),
                    FileName  = fname,
                    Timestamp = DateTime.Now,
                    ImportFileTimestampTicks = task.FileTimestamp.Ticks,
                };
                MarkImportFailure(failure);
            }
            finally
            {
                TenantContext.Exit();
            }
        }